Skip to content

Commit b61523c

Browse files
committed
Fix: The classmap could point to a file
1 parent 14e1d53 commit b61523c

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/MemoizeClassMapGenerator.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use function array_filter;
1111
use function array_merge;
1212
use function filemtime;
13+
use function is_dir;
1314
use function is_int;
1415
use function time;
1516

@@ -119,6 +120,11 @@ private function should_update(int $timestamp, string $path): bool
119120
return true;
120121
}
121122

123+
// Could be a file referenced as class map, we don't want to iterate over that.
124+
if (!is_dir($path)) {
125+
return false;
126+
}
127+
122128
foreach (new DirectoryIterator($path) as $di) {
123129
if ($di->isDir() && !$di->isDot()) {
124130
if ($this->should_update($timestamp, $di->getPathname())) {

tests/MemoizeClassMapGeneratorTest.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ protected function setUp(): void
3535

3636
public function testMemoize(): void
3737
{
38-
$map = $this->map();
38+
$map = $this->map(self::DIR);
3939
$this->assertEmpty($map);
4040

4141
// check changes in the directory are detected
@@ -52,7 +52,13 @@ class A {
5252
PHP
5353
);
5454

55-
$map = $this->map();
55+
$map = $this->map(self::DIR . 'a.php');
56+
$this->assertEquals([
57+
'App\A' => self::DIR . 'a.php',
58+
], $map);
59+
60+
// map again to test the code is not trying to iterate over the file like it's a directory
61+
$map = $this->map(self::DIR . 'a.php');
5662
$this->assertEquals([
5763
'App\A' => self::DIR . 'a.php',
5864
], $map);
@@ -71,7 +77,7 @@ class B {
7177
PHP
7278
);
7379

74-
$map = $this->map();
80+
$map = $this->map(self::DIR);
7581
$this->assertEquals([
7682
'App\A' => self::DIR . 'a.php',
7783
'App\B' => self::DIR . 'a/b/c/b.php',
@@ -90,14 +96,14 @@ private static function write(string $name, string $data): void
9096
/**
9197
* @return array<class-string, string>
9298
*/
93-
private static function map(): array
99+
private static function map(string $path): array
94100
{
95101
$generator = new MemoizeClassMapGenerator(
96102
new FileDatastore(get_cache_dir()),
97103
new NullIO(),
98104
);
99105

100-
$generator->scanPaths(self::DIR);
106+
$generator->scanPaths($path);
101107

102108
return $generator->getMap();
103109
}

0 commit comments

Comments
 (0)