Skip to content

[CodingStyle] Skip NameImporter change when the name is already written as is - #8512

Open
GErpeldinger wants to merge 1 commit into
rectorphp:mainfrom
GErpeldinger:fix-name-importing-aliased-same-short-name
Open

GErpeldinger wants to merge 1 commit into
rectorphp:mainfrom
GErpeldinger:fix-name-importing-aliased-same-short-name

Conversation

@GErpeldinger

@GErpeldinger GErpeldinger commented Sep 22, 2026

Copy link
Copy Markdown
Contributor

Fixes rectorphp/rector#9911

resolveNameInUse() returns the short name when another import with the same short name is aliased. When the code already uses that short name, the returned name is identical, yet it is reported as a change, so a --dry-run never caches the file.

importNameAndCollectNewUseStatement() now returns null in that case, as it already does at its end when the original token matches.

The new ApplicationFileProcessorTest case fails without the fix: the file is not cached after a dry run.

Second commit, for the CI: ReturnNullableTypeRectorPhp82Test read Fixture/ with the default config, so FixturePhp82/ never ran and both test classes wrote the same temporary files, which made the PHP 8.4 tests fail randomly here. It now uses FixturePhp82/ and configured_rule_php82.php.

🤖 Drafted with AI assistance.

Comment thread rules/CodingStyle/Node/NameImporter.php Outdated
@GErpeldinger

GErpeldinger commented Sep 22, 2026

Copy link
Copy Markdown
Contributor Author

The PHP 8.4 tests failed on an unrelated flaky test: since #6110, ReturnNullableTypeRectorPhp82Test read the same Fixture/ directory as ReturnNullableTypeRectorTest, so both classes wrote the same temporary files in parallel. The second commit fixes it, and FixturePhp82/ now runs for the first time.

This race is rare: both classes must be in different fast-unit chunks and on the same fixture at the same time. In the 70 failed test runs since fast-unit was added (2026-08-14), it happened 4 times: this run, and 3 times on StrStartsWithRector/Fixture/, which PolyfillRectorTest also reads (#5388). That second case can still happen on main.

The same logs have many other Failed to open stream warnings, mostly on AddFunctionVoidReturnTypeWhereNoReturnRector/Fixture/some_function.php. These are a different problem, and they never fail a run: DynamicSourceLocatorProvider keeps the temporary file of the last Rector test, and a later MultilineTest or PhpDocInfoPrinterTest in the same process reads it after tearDown() deleted it. To reproduce:

vendor/bin/phpunit rules-tests/TypeDeclaration/Rector/Function_/AddFunctionVoidReturnTypeWhereNoReturnRector/AddFunctionVoidReturnTypeWhereNoReturnRectorTest.php tests/BetterPhpDocParser/PhpDocInfo/PhpDocInfoPrinter/MultilineTest.php

🤖 Drafted with AI assistance.

@TomasVotruba

Copy link
Copy Markdown
Member

Thanks for the numbers 👌

@samsonasik

Copy link
Copy Markdown
Member

imo, it is not worth the effort, checking the token is only when we need some quirk that not easy to catch by ast, eg: detecting parentheses start/end.

please create separate PR for unrelated change on fixture directory rename fix.

@GErpeldinger

GErpeldinger commented Sep 23, 2026

Copy link
Copy Markdown
Contributor Author

@samsonasik i opened #8517 for the bug in the tests, i check if i find a better solution for this branch

@GErpeldinger
GErpeldinger force-pushed the fix-name-importing-aliased-same-short-name branch from ca0dcd5 to e8ed9eb Compare September 23, 2026 14:41
@GErpeldinger

Copy link
Copy Markdown
Contributor Author

@samsonasik Done: the check now compares the originalName attribute that NameResolver sets, no tokens. The fixture fix is merged in #8517, so it is gone from this PR.

🤖 Drafted with AI assistance.

Comment on lines +46 to +59
public function testShortNameNextToSameShortNameAliasIsCachedAsUnchanged(): void
{
self::$rectorConfig = null;
$this->bootFromConfigFiles([__DIR__ . '/config-import-names.php']);
$applicationFileProcessor = $this->make(ApplicationFileProcessor::class);
$changedFilesDetector = $this->make(ChangedFilesDetector::class);
$changedFilesDetector->clear();

$filePath = __DIR__ . '/Source/ImportedNextToSameShortNameAlias.php';

$applicationFileProcessor->processFiles([$filePath], new Configuration(isDryRun: true));

$this->assertFalse($changedFilesDetector->hasFileChanged($filePath));
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is pretty hard to read. Instead, better use a Rector-like before/after tests. There are already import names rules in place, inspire there.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed: the test now has its own small class, ImportNamesCacheTest, with the setup in setUp(), so the test itself is three lines.

I tried a before/after fixture in ImportFullyQualifiedNamesRector first, but it passes on main: the output is identical, and fixture tests don't see that the file is recorded as changed. That only shows in the cache (and the dry run's changed_files), so the test checks the cache.

🤖 Drafted with AI assistance.

@GErpeldinger GErpeldinger Sep 23, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly, i'm not sure if we can do better in this case, but i found already better the readability, wdyt ?

@GErpeldinger
GErpeldinger force-pushed the fix-name-importing-aliased-same-short-name branch from e8ed9eb to b9af79d Compare September 23, 2026 20:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

withImportNames() reports a change on a short name already imported next to a same-short-name alias

3 participants