diff --git a/rules-tests/CodingStyle/ClassNameImport/ShortNameResolver/ShortNameResolverTest.php b/rules-tests/CodingStyle/ClassNameImport/ShortNameResolver/ShortNameResolverTest.php index 504f5b6679a..ab528224255 100644 --- a/rules-tests/CodingStyle/ClassNameImport/ShortNameResolver/ShortNameResolverTest.php +++ b/rules-tests/CodingStyle/ClassNameImport/ShortNameResolver/ShortNameResolverTest.php @@ -39,6 +39,34 @@ public function test(string $filePath, array $expectedShortNames): void $this->assertSame($expectedShortNames, $shortNames); } + public function testResolveFromFileAgainAfterStmtsChange(): void + { + $file = $this->testingParser->parseFilePathToFile(__DIR__ . '/Fixture/various_imports.php.inc'); + $this->assertNotSame([], $this->shortNameResolver->resolveFromFile($file)); + + $file->changeNewStmts([]); + + $this->assertSame([], $this->shortNameResolver->resolveFromFile($file)); + } + + public function testResolveFromFilePerFile(): void + { + $firstFile = $this->testingParser->parseFilePathToFile(__DIR__ . '/Fixture/various_imports.php.inc'); + $secondFile = $this->testingParser->parseFilePathToFile(__DIR__ . '/Fixture/partial_names.php.inc'); + + $firstExpected = [ + 'VariousImports' => 'Rector\Tests\CodingStyle\ClassNameImport\ShortNameResolver\Fixture\VariousImports', + 'SomeFile' => 'Rector\Tests\CodingStyle\ClassNameImport\ShortNameResolver\Source\SomeFile', + ]; + $secondExpected = [ + 'PartialNames' => 'Rector\Tests\CodingStyle\ClassNameImport\ShortNameResolver\Fixture\PartialNames', + ]; + + $this->assertSame($firstExpected, $this->shortNameResolver->resolveFromFile($firstFile)); + $this->assertSame($secondExpected, $this->shortNameResolver->resolveFromFile($secondFile)); + $this->assertSame($firstExpected, $this->shortNameResolver->resolveFromFile($firstFile)); + } + public function testResolveShortClassLikeNamesAgainAfterStmtsChange(): void { $file = $this->testingParser->parseFilePathToFile(__DIR__ . '/Fixture/various_imports.php.inc'); diff --git a/rules/CodingStyle/ClassNameImport/ShortNameResolver.php b/rules/CodingStyle/ClassNameImport/ShortNameResolver.php index 871833c6aff..b78363ce4ff 100644 --- a/rules/CodingStyle/ClassNameImport/ShortNameResolver.php +++ b/rules/CodingStyle/ClassNameImport/ShortNameResolver.php @@ -26,10 +26,14 @@ */ final class ShortNameResolver { + private ?File $shortNamesFile = null; + + private int $shortNamesVersion = 0; + /** - * @var array + * @var array */ - private array $shortNamesByFilePath = []; + private array $shortNamesToFullyQualifiedNames = []; private ?File $shortClassLikeNamesFile = null; @@ -54,16 +58,16 @@ public function __construct( */ public function resolveFromFile(File $file): array { - $filePath = $file->getFilePath(); - - if (isset($this->shortNamesByFilePath[$filePath])) { - return $this->shortNamesByFilePath[$filePath]; + // asked for every imported name, while the short names only change with a new version of the file stmts + if ($file === $this->shortNamesFile && $file->getNewStmtsVersion() === $this->shortNamesVersion) { + return $this->shortNamesToFullyQualifiedNames; } - $shortNamesToFullyQualifiedNames = $this->resolveForStmts($file->getNewStmts()); - $this->shortNamesByFilePath[$filePath] = $shortNamesToFullyQualifiedNames; + $this->shortNamesFile = $file; + $this->shortNamesVersion = $file->getNewStmtsVersion(); + $this->shortNamesToFullyQualifiedNames = $this->resolveForStmts($file->getNewStmts()); - return $shortNamesToFullyQualifiedNames; + return $this->shortNamesToFullyQualifiedNames; } /**