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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Ability to import FQCNs found during analysis #7597

Merged
merged 10 commits into from Dec 20, 2023

Conversation

Wirone
Copy link
Member

@Wirone Wirone commented Dec 19, 2023

Fixes #6582 🎉 .

@Wirone Wirone added kind/enhancement kind/feature topic/types FQCNs and types used in signatures (class-like, function, property) topic/fqcn Fully Qualified Class Name usage and conversions labels Dec 19, 2023
@Wirone Wirone self-assigned this Dec 19, 2023
@coveralls
Copy link

coveralls commented Dec 19, 2023

Coverage Status

coverage: 94.795% (-0.007%) from 94.802%
when pulling 3472b55 on Wirone:codito/fqcn-import
into b737791 on PHP-CS-Fixer:master.

Comment on lines +301 to +310
// 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], '{'] : ['{'])) {
Copy link
Member Author

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.
@Wirone Wirone enabled auto-merge (squash) December 20, 2023 18:09
@Wirone Wirone merged commit 27765a6 into PHP-CS-Fixer:master Dec 20, 2023
26 checks passed
@kubawerlos
Copy link
Contributor

Thank you @Wirone

@Wirone Wirone deleted the codito/fqcn-import branch December 20, 2023 23:36
mvorisek added a commit to mvorisek/PHP-CS-Fixer that referenced this pull request Dec 26, 2023
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/enhancement kind/feature topic/fqcn Fully Qualified Class Name usage and conversions topic/types FQCNs and types used in signatures (class-like, function, property)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Missing a FQCN to Import (use) conversion
3 participants