diff --git a/src/ClassScanner/ComposerMapGenerator.php b/src/ClassScanner/ComposerMapGenerator.php index 2e2f832..95398c5 100644 --- a/src/ClassScanner/ComposerMapGenerator.php +++ b/src/ClassScanner/ComposerMapGenerator.php @@ -64,11 +64,21 @@ private function convertToClassMap(ClassMap $classMap, array $composerMap): Clas public function update(ClassMap $classMap, array $updatedFiles): ClassMap { + $deleted = []; + $skip = []; + $shouldFullGenerate = false; foreach ($updatedFiles as $index => $updatedFile) { if ($this->basePath !== '' && \str_starts_with($updatedFile, $this->basePath)) { - $updatedFiles[$index] = \substr($updatedFile, \strlen($this->basePath)); - $shouldFullGenerate = $shouldFullGenerate || $classMap->isAttributeClassFile($updatedFiles[$index]); + $checkFile = \substr($updatedFile, \strlen($this->basePath)); + + if (!\is_file($updatedFile)) { + $deleted[] = $checkFile; + } else { + $shouldFullGenerate = $shouldFullGenerate || $classMap->isAttributeClassFile($checkFile); + } + + $updatedFiles[$index] = $checkFile; } } @@ -76,18 +86,9 @@ public function update(ClassMap $classMap, array $updatedFiles): ClassMap return $this->generate(); } - $deleted = []; - $skip = []; - foreach ($classMap->getFiles() as $file) { if (!\in_array($file, $updatedFiles, true)) { - $skip[$file] = true; - } - } - - foreach ($updatedFiles as $file) { - if (!\is_file($file)) { - $deleted[] = $file; + $skip[$this->basePath . $file] = true; } } @@ -101,9 +102,14 @@ public function update(ClassMap $classMap, array $updatedFiles): ClassMap $generator->scanPaths($path, $this->excluded); } - $classMap = $this->convertToClassMap( - new ClassMap($this->paths, $this->basePath), - $generator->getClassMap()->getMap() + $classMap = $classMap->merge( + $this->convertToClassMap( + new ClassMap( + $this->paths, + $this->basePath + ), + $generator->getClassMap()->getMap() + ) ); foreach ($deleted as $filename) { @@ -112,4 +118,4 @@ public function update(ClassMap $classMap, array $updatedFiles): ClassMap return $classMap; } -} \ No newline at end of file +} diff --git a/tests/ClassScanner/ComposerMapGeneratorTest.php b/tests/ClassScanner/ComposerMapGeneratorTest.php index a26d1b1..c23f11f 100644 --- a/tests/ClassScanner/ComposerMapGeneratorTest.php +++ b/tests/ClassScanner/ComposerMapGeneratorTest.php @@ -31,7 +31,7 @@ protected function tearDown(): void public function testAddingClass() { - $generator = new ComposerMapGenerator([__DIR__ . '/../Fake', $this->dir]); + $generator = new ComposerMapGenerator([__DIR__ . '/../Fake', $this->dir], $this->dir); $classSuffix = \bin2hex(\random_bytes(4)); $newClassName = 'CacheTest\\NewFile' . $classSuffix; @@ -48,10 +48,7 @@ public function testAddingClass() public function testRemovingClass() { - $generator = new ComposerMapGenerator([ - __DIR__ . '/../Fake', - $this->dir - ]); + $generator = new ComposerMapGenerator([__DIR__ . '/../Fake', $this->dir], $this->dir); $classSuffix = \bin2hex(\random_bytes(4)); $newClassName = 'CacheTest\\NewFile' . $classSuffix; @@ -66,6 +63,21 @@ public function testRemovingClass() $this->assertNotContains($newClassName, $classMap2->getClasses()); } + public function testUpdateClass() + { + $generator = new ComposerMapGenerator([__DIR__ . '/../Fake', $this->dir], $this->dir); + + $classSuffix = \bin2hex(\random_bytes(4)); + $newClassName = 'CacheTest\\NewFile' . $classSuffix; + $this->createRandomClassFile($newClassName); + + $classMap = $generator->generate(); + $this->assertContains($newClassName, $classMap->getClasses()); + + $classMap2 = $generator->update($classMap, [$this->dir . '/NewFile' . $classSuffix . '.php']); + $this->assertContains($newClassName, $classMap2->getClasses()); + } + private function createRandomClassFile(string $className, int $value = 0): string { $bareName = \array_reverse(\explode('\\', $className))[0];