diff --git a/rules/CodingStyle/Node/NameImporter.php b/rules/CodingStyle/Node/NameImporter.php index c2e90dd3d19..c228d97e2f5 100644 --- a/rules/CodingStyle/Node/NameImporter.php +++ b/rules/CodingStyle/Node/NameImporter.php @@ -86,6 +86,12 @@ private function importNameAndCollectNewUseStatement( // make use of existing use import $nameInUse = $this->resolveNameInUse($fullyQualified, $currentUses); if ($nameInUse instanceof Name) { + // already written with this name, nothing to change + $originalName = $fullyQualified->getAttribute(AttributeKey::ORIGINAL_NAME); + if ($originalName instanceof Name && $originalName->toString() === $nameInUse->toString()) { + return null; + } + $nameInUse->setAttribute(AttributeKey::NAMESPACED_NAME, $fullyQualified->toString()); return $nameInUse; } diff --git a/tests/Application/ApplicationFileProcessor/ImportNamesCacheTest.php b/tests/Application/ApplicationFileProcessor/ImportNamesCacheTest.php new file mode 100644 index 00000000000..cbf45a95425 --- /dev/null +++ b/tests/Application/ApplicationFileProcessor/ImportNamesCacheTest.php @@ -0,0 +1,38 @@ +bootFromConfigFiles([__DIR__ . '/config-import-names.php']); + + $this->applicationFileProcessor = $this->make(ApplicationFileProcessor::class); + $this->changedFilesDetector = $this->make(ChangedFilesDetector::class); + $this->changedFilesDetector->clear(); + } + + public function testShortNameNextToSameShortNameAliasIsCachedAsUnchanged(): void + { + $filePath = __DIR__ . '/Source/ImportedNextToSameShortNameAlias.php'; + + $this->applicationFileProcessor->processFiles([$filePath], new Configuration(isDryRun: true)); + + $this->assertFalse($this->changedFilesDetector->hasFileChanged($filePath)); + } +} diff --git a/tests/Application/ApplicationFileProcessor/Source/ImportedNextToSameShortNameAlias.php b/tests/Application/ApplicationFileProcessor/Source/ImportedNextToSameShortNameAlias.php new file mode 100644 index 00000000000..99c96c9c685 --- /dev/null +++ b/tests/Application/ApplicationFileProcessor/Source/ImportedNextToSameShortNameAlias.php @@ -0,0 +1,15 @@ +cacheDirectory(sys_get_temp_dir() . '/_rector_import_names_test'); + $rectorConfig->importNames(); +};