Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
22 changes: 13 additions & 9 deletions rules/CodingStyle/ClassNameImport/ShortNameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@
*/
final class ShortNameResolver
{
private ?File $shortNamesFile = null;

private int $shortNamesVersion = 0;

/**
* @var array<string, string[]>
* @var array<string, string>
*/
private array $shortNamesByFilePath = [];
private array $shortNamesToFullyQualifiedNames = [];

private ?File $shortClassLikeNamesFile = null;

Expand All @@ -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;
}

/**
Expand Down
Loading