Skip to content

Commit 6aa872e

Browse files
authored
Merge pull request #9 from moufmouf/filemap
Adding a getClassMap method
2 parents 743016b + 7036efc commit 6aa872e

File tree

4 files changed

+31
-10
lines changed

4 files changed

+31
-10
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
language: php
22
matrix:
33
include:
4-
- php: 7.2
4+
- php: 7.4
55
env: PREFER_LOWEST=""
66
- php: 7.1
77
env: PREFER_LOWEST=""

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
"php-coveralls/php-coveralls": "^2.1",
2222
"phpunit/phpunit": "^7.2.7",
2323
"squizlabs/php_codesniffer": "^3.2.3",
24-
"phpstan/phpstan": "^0.10.3",
24+
"phpstan/phpstan": "^0.12",
2525
"maglnet/composer-require-checker": "^1.0",
26-
"thecodingmachine/phpstan-strict-rules": "^0.11.0",
26+
"thecodingmachine/phpstan-strict-rules": "^0.12",
2727
"symfony/cache": "^4.1.4"
2828
},
2929
"autoload": {
@@ -45,7 +45,7 @@
4545
"scripts": {
4646
"cs-check": "phpcs",
4747
"cs-fix": "phpcbf",
48-
"phpstan": "phpstan analyse src -c phpstan.neon --level=7 --no-progress -vvv"
48+
"phpstan": "phpstan analyse src -c phpstan.neon --level=8 --no-progress -vvv"
4949
},
5050
"minimum-stability": "alpha",
5151
"prefer-stable": true

src/Glob/GlobClassExplorer.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
namespace TheCodingMachine\ClassExplorer\Glob;
55

6+
use SplFileInfo;
7+
use function array_keys;
68
use function chdir;
79
use DirectoryIterator;
810
use GlobIterator;
@@ -66,14 +68,24 @@ public function __construct(string $namespace, CacheInterface $cache, ?int $cach
6668
/**
6769
* Returns an array of fully qualified class names.
6870
*
69-
* @return string[]
71+
* @return array<int,string>
7072
*/
7173
public function getClasses(): array
74+
{
75+
return array_keys($this->getClassMap());
76+
}
77+
78+
/**
79+
* Returns an array mapping the fully qualified class name to the file path.
80+
*
81+
* @return array<string,SplFileInfo>
82+
*/
83+
public function getClassMap(): array
7284
{
7385
$key = 'globClassExplorer_'.str_replace('\\', '_', $this->namespace);
7486
$classes = $this->cache->get($key);
7587
if ($classes === null) {
76-
$classes = $this->doGetClasses();
88+
$classes = $this->doGetClassMap();
7789
$this->cache->set($key, $classes, $this->cacheTtl);
7890
}
7991
return $classes;
@@ -82,9 +94,9 @@ public function getClasses(): array
8294
/**
8395
* Returns an array of fully qualified class names, without the cache.
8496
*
85-
* @return string[]
97+
* @return array<string,SplFileInfo>
8698
*/
87-
private function doGetClasses(): array
99+
private function doGetClassMap(): array
88100
{
89101
$namespace = trim($this->namespace, '\\').'\\';
90102
if ($this->classNameMapper === null) {
@@ -103,7 +115,7 @@ private function doGetClasses(): array
103115
foreach ($filesForDir as $file) {
104116
// Trim the root directory name and the PHP extension
105117
$fileTrimPrefixSuffix = \substr($file, $dirLen, -4);
106-
$classes[] = $namespace.\str_replace('/', '\\', $fileTrimPrefixSuffix);
118+
$classes[$namespace.\str_replace('/', '\\', $fileTrimPrefixSuffix)] = $file;
107119
}
108120
}
109121
chdir($oldCwd);
@@ -112,7 +124,7 @@ private function doGetClasses(): array
112124

113125
/**
114126
* @param string $directory
115-
* @return \Iterator
127+
* @return \Iterator<SplFileInfo>
116128
*/
117129
private function getPhpFilesForDir(string $directory): \Iterator
118130
{

tests/Glob/GlobClassExplorerTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,13 @@ public function testGetNotExistingClasses()
4040

4141
$this->assertSame([], $classes);
4242
}
43+
44+
public function testGetClassMap()
45+
{
46+
$explorer = new GlobClassExplorer('\\TheCodingMachine\\ClassExplorer\\', new NullCache(), null, null, true, __DIR__.'/../..');
47+
$classMap = $explorer->getClassMap();
48+
49+
$this->assertArrayHasKey(GlobClassExplorer::class, $classMap);
50+
$this->assertSame('src/Glob/GlobClassExplorer.php', (string) $classMap[GlobClassExplorer::class]);
51+
}
4352
}

0 commit comments

Comments
 (0)