Skip to content

Commit

Permalink
fix bug where updated files were incorrectly removed from the classmap
Browse files Browse the repository at this point in the history
  • Loading branch information
frederikbosch committed Aug 23, 2024
1 parent f4c18b1 commit dfbd447
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 21 deletions.
38 changes: 22 additions & 16 deletions src/ClassScanner/ComposerMapGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,30 +64,31 @@ 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;
}
}

if ($shouldFullGenerate) {
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;
}
}

Expand All @@ -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) {
Expand All @@ -112,4 +118,4 @@ public function update(ClassMap $classMap, array $updatedFiles): ClassMap

return $classMap;
}
}
}
22 changes: 17 additions & 5 deletions tests/ClassScanner/ComposerMapGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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];
Expand Down

0 comments on commit dfbd447

Please sign in to comment.