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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Ability to import FQCNs found during analysis #7597
Conversation
// We handle `extends` and `implements` with similar logic, but we need to exit the loop under different conditions. | ||
$isExtends = $tokens[$index]->equals([T_EXTENDS]); | ||
$index = $tokens->getNextMeaningfulToken($index); | ||
$extend = ['content' => '', 'tokens' => []]; | ||
|
||
while (true) { | ||
if ($tokens[$index]->equalsAny([',', '{', [T_IMPLEMENTS]])) { | ||
$this->shortenClassIfPossible($tokens, $extend, $uses, $namespaceName); | ||
|
||
if ($tokens[$index]->equals('{')) { | ||
if ($tokens[$index]->equalsAny($isExtends ? [[T_IMPLEMENTS], '{'] : ['{'])) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI: basically it is a fix for redundant analysis that is currently executed when extends
and implements
are both used in a class. This method is invoked 2 times in applyFix()
because both T_EXTENDS
and T_IMPLEMENTS
trigger it, but currently both are exiting on {
, which means implements
part is processed 2 times.
In the context of this particular PR, it resulted in symbols being imported twice, because e.g. implements \Other\Foo
was shortened to implements Foo
in the first run (for extends
), and when processing implements
it was registering Foo
for importing. I've fixed the issue of importing non-FQNs (symbol must start with \
to be registered for importing), but this scenario allowed me to find the redundant loop.
Seems like leftover from previous versions of array shape in `ImportProcessor::insertImports()`.
If symbol does not start with `\` it means that it's already imported (as a full import or partial like `use Foo\Bar;` + `Bar\Baz` usage), or it's a global symbol in a global namespace. Either way, it's tests' and static analysis' responsibility to catch invalid symbols, we just treat them as valid ones and handle only FQCNs.
There is `GlobalNamespaceImportFixer` which handle this. Maybe in the future it could be unified, but at this point let's have single fixer for that.
c943f82
to
3472b55
Compare
Thank you @Wirone |
…ixer#7597)" This reverts commit 27765a6.
Fixes #6582 🎉 .