Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Import only unique symbols' short names #7635

Merged

Conversation

Wirone
Copy link
Member

@Wirone Wirone commented Dec 29, 2023

Fixes #7632 (hopefully, but at least it will prevent changes like these done in symfony/symfony#53244)

This is quick fix for obvious bug, but I am wondering if we could provide feature for auto-aliasing imports with colliding short names (e.g. when \Foo\A and \Bar\A are found we could add use Foo\A as FooA; and use Bar\A as BarA) 馃. Anyway, this is something for separate PR.

Because of missing reference `$uses` array was not updated after each import.
@Wirone Wirone added kind/bug topic/fqcn Fully Qualified Class Name usage and conversions labels Dec 29, 2023
@Wirone Wirone requested a review from keradus December 29, 2023 01:41
@Wirone Wirone self-assigned this Dec 29, 2023
@coveralls
Copy link

coveralls commented Dec 29, 2023

Coverage Status

coverage: 94.778%. remained the same
when pulling e63cdf6 on Wirone:codito/7632-fqcn-import-symbol-name-clashes
into 826207a on PHP-CS-Fixer:master.

@@ -264,7 +264,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
/**
* @param array<string, string> $uses
*/
private function fixFunction(FunctionsAnalyzer $functionsAnalyzer, Tokens $tokens, int $index, array $uses, string $namespaceName): void
private function fixFunction(FunctionsAnalyzer $functionsAnalyzer, Tokens $tokens, int $index, array &$uses, string $namespaceName): void
Copy link
Member

Choose a reason for hiding this comment

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

non-blocking

I took a look how this uses is used and passed around most of the methods.
maybe it could be state property and avoid pass-by-reference in all the methods 馃槄

Copy link
Member Author

Choose a reason for hiding this comment

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

If anything, I would prefer creating some object representing a list of imports, so it's always passed by reference. Explicit arguments are better than quasi-global state.

@keradus keradus merged commit 77a5d43 into PHP-CS-Fixer:master Dec 29, 2023
25 checks passed
@Wirone Wirone deleted the codito/7632-fqcn-import-symbol-name-clashes branch December 29, 2023 09:44
@mvorisek
Copy link
Contributor

mvorisek commented Dec 30, 2023

This is quick fix for obvious bug, but I am wondering if we could provide feature for auto-aliasing imports with colliding short names (e.g. when \Foo\A and \Bar\A are found we could add use Foo\A as FooA; and use Bar\A as BarA) 馃. Anyway, this is something for separate PR.

until then, no importing should be done, as:

  • later, I do not expect the fixer to rename A to FooA
  • "Bar\A" could be must more sensual to be imported as A later than "Foo\A"

@Wirone
Copy link
Member Author

Wirone commented Dec 30, 2023

until then, no importing should be done (...)

I don't agree. All possible FQCNs should be imported (in this case only first conflicting name). Nothing is going to be renamed later, because for multiple A symbols only first is imported and will remain untouched later. Only subsequent A symbols are ignored right now, but later we can provide ability to imported them too, but as aliased imports.

@mvorisek
Copy link
Contributor

please reread my post, my point is the the 2nd symbol might be preffered to be imported as A but with the current idea - import only the first unique name, this is impossible to catch in a review process

@Wirone
Copy link
Member Author

Wirone commented Dec 30, 2023

Fixer does not have ability to determine "sensuality" of the imports, so even if we provide aliasing feature, it would still require manual work to select which import should be non-aliased. So even if first symbol is imported now, and the other will be later, then you could catch it on the second review (because you'll see the imports are aliased, so you can compare it against non-aliased one). Delaying imports for conflicting names, as you suggested, won't fix this because you just can't choose which symbol should be imported first, it's just the first one found and processed. We would need to have an config option for choosing which parts of code should be processed (signatures, extends, implements etc), but even with such granularity it wouldn't be possible to ensure proper importing order for all cases. So, if manual work is required anyway, I would suggest you just should run check first, see potential changes, manually do "more sensual" import, and then apply fixes 馃檪.

@mvorisek
Copy link
Contributor

Delaying imports for conflicting names, as you suggested, won't fix this because you just can't choose which symbol should be imported first, it's just the first one found and processed

it would help, because you will see 2 aliased imports instead of 1 unaliased and one 1 aliased (especially, later only the 1 aliased, as users will fix/commit the 1st unaliased sooner because of the current behaviour)

@Wirone
Copy link
Member Author

Wirone commented Dec 30, 2023

it would help, because you will see 2 aliased imports instead of 1 unaliased and one 1 aliased

No, you would see 1 regular and 1 aliased import, because imports are registered in order when they're found and if short name is not already imported, then it's imported without alias (because it's not needed at this point). Yes, actual imports are created after analysis, when all symbols are collected, so we can determine name collisions and do something about it, but I don't see the point in importing all of them as aliased because it would require the exact same manual work to un-alias one of them, like you would need to do when symbol that was imported first turn out to be "less sensual" (and you need to alias it to be able to use non-aliased import for other symbol). I think you're overcomplicating something that in any scenario requires similar, manual work.

danog pushed a commit to zoonru/PHP-CS-Fixer that referenced this pull request Feb 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug topic/fqcn Fully Qualified Class Name usage and conversions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

fully_qualified_strict_types problem with same-name, different-namespace symbols
4 participants