Skip to content

Commit f29dbe4

Browse files
committed
[CodingStyle] Skip NameImporter change when the name is already written as is
1 parent 1d0224b commit f29dbe4

4 files changed

Lines changed: 53 additions & 0 deletions

File tree

‎rules/CodingStyle/Node/NameImporter.php‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ private function importNameAndCollectNewUseStatement(
8686
// make use of existing use import
8787
$nameInUse = $this->resolveNameInUse($fullyQualified, $currentUses);
8888
if ($nameInUse instanceof Name) {
89+
// already written with this name, nothing to change
90+
if ($this->isWrittenAs($file, $fullyQualified, $nameInUse)) {
91+
return null;
92+
}
93+
8994
$nameInUse->setAttribute(AttributeKey::NAMESPACED_NAME, $fullyQualified->toString());
9095
return $nameInUse;
9196
}
@@ -139,6 +144,14 @@ private function importNameAndCollectNewUseStatement(
139144
return null;
140145
}
141146

147+
private function isWrittenAs(File $file, FullyQualified $fullyQualified, Name $name): bool
148+
{
149+
$oldTokens = $file->getOldTokens();
150+
$startTokenPos = $fullyQualified->getStartTokenPos();
151+
152+
return isset($oldTokens[$startTokenPos]) && $oldTokens[$startTokenPos]->text === $name->toString();
153+
}
154+
142155
private function addUseImport(
143156
FileNode $fileNode,
144157
FullyQualified $fullyQualified,

‎tests/Application/ApplicationFileProcessor/ApplicationFileProcessorTest.php‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,21 @@ public function testCleanFileIsCachedAsUnchanged(): void
4343
$this->assertFalse($this->changedFilesDetector->hasFileChanged($filePath));
4444
}
4545

46+
public function testShortNameNextToSameShortNameAliasIsCachedAsUnchanged(): void
47+
{
48+
self::$rectorConfig = null;
49+
$this->bootFromConfigFiles([__DIR__ . '/config-import-names.php']);
50+
$applicationFileProcessor = $this->make(ApplicationFileProcessor::class);
51+
$changedFilesDetector = $this->make(ChangedFilesDetector::class);
52+
$changedFilesDetector->clear();
53+
54+
$filePath = __DIR__ . '/Source/ImportedNextToSameShortNameAlias.php';
55+
56+
$applicationFileProcessor->processFiles([$filePath], new Configuration(isDryRun: true));
57+
58+
$this->assertFalse($changedFilesDetector->hasFileChanged($filePath));
59+
}
60+
4661
public function testOnlyRuleRunCachesUnderOwnScopeWithoutPoisoningFullRun(): void
4762
{
4863
$filePath = __DIR__ . '/Source/CleanFile.php';
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\Application\ApplicationFileProcessor\Source;
6+
7+
use PhpParser\Node\Expr\BinaryOp\Plus;
8+
use PhpParser\Node\Expr\AssignOp\Plus as AssignPlus;
9+
10+
final class ImportedNextToSameShortNameAlias
11+
{
12+
public function run(Plus $plus, AssignPlus $assignPlus): void
13+
{
14+
}
15+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
7+
return static function (RectorConfig $rectorConfig): void {
8+
$rectorConfig->cacheDirectory(sys_get_temp_dir() . '/_rector_import_names_test');
9+
$rectorConfig->importNames();
10+
};

0 commit comments

Comments
 (0)