From b9af79dacdf552e50d67136cdd9b729e03aed57b Mon Sep 17 00:00:00 2001 From: Guillaume Erpeldinger Date: Wed, 23 Sep 2026 00:59:16 +0200 Subject: [PATCH] [CodingStyle] Skip NameImporter change when the name is already written as is --- rules/CodingStyle/Node/NameImporter.php | 6 +++ .../ImportNamesCacheTest.php | 38 +++++++++++++++++++ .../ImportedNextToSameShortNameAlias.php | 15 ++++++++ .../config-import-names.php | 10 +++++ 4 files changed, 69 insertions(+) create mode 100644 tests/Application/ApplicationFileProcessor/ImportNamesCacheTest.php create mode 100644 tests/Application/ApplicationFileProcessor/Source/ImportedNextToSameShortNameAlias.php create mode 100644 tests/Application/ApplicationFileProcessor/config-import-names.php 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(); +};