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

NoUnusedImportsFixer - Fix undetected unused imports when type mismatch #5720

Merged
merged 1 commit into from Aug 29, 2021

Conversation

julienfalque
Copy link
Member

@julienfalque julienfalque commented May 16, 2021

Fixes #3020.

@coveralls
Copy link

coveralls commented May 17, 2021

Coverage Status

Coverage increased (+0.01%) to 92.293% when pulling 1fc2bda on julienfalque:unused-import-types into 810c3c0 on FriendsOfPHP:3.0.

Copy link
Contributor

@GrahamCampbell GrahamCampbell left a comment

Choose a reason for hiding this comment

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

Nice. 👍

Copy link
Contributor

@GrahamCampbell GrahamCampbell left a comment

Choose a reason for hiding this comment

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

This incorrectly removes on of the imports for a union catch:

// ...
} catch (InvalidFormatException | ParseErrorException) {

@julienfalque
Copy link
Member Author

@GrahamCampbell I don't reproduce that, could you provide the full snippet please?

@GrahamCampbell
Copy link
Contributor

Oh, I think the issue only occurs when combined with #5685.

@julienfalque
Copy link
Member Author

@GrahamCampbell Actually I didn't notice your snippet uses a non-capturing catch 😐. I do reproduce the issue now, trying to fix it.

@julienfalque
Copy link
Member Author

Fixed in #5685, which this PR now depends on. Thanks @GrahamCampbell!

@GrahamCampbell
Copy link
Contributor

I think this also has an issue with PHP 8 attributes.

<?php

use Symfony\Component\Routing\Annotation\Route;

class Controller
{
    #[Route('/foo', name: 'foo')]
    public function foo(Request $request): Response
    {
    }
}

@julienfalque julienfalque force-pushed the unused-import-types branch 2 times, most recently from 209767f to f1fde51 Compare May 23, 2021 16:22
@julienfalque
Copy link
Member Author

@GrahamCampbell Fixed too, thanks 👍

@julienfalque julienfalque force-pushed the unused-import-types branch 2 times, most recently from a148d2f to 2770ab7 Compare May 25, 2021 21:04
@GrahamCampbell
Copy link
Contributor

Where there other changes there, or just a rebase?

@julienfalque
Copy link
Member Author

Only a rebase (onto #5741 instead of #5685).

@SpacePossum
Copy link
Contributor

Fixed another issue for goto labels and in detecting goto labels. Please rebase and squash my commits out.
There is a thing I'm not 100% about:

                if ($analyzer->isConstantInvocation($index)) {
                    $type = NamespaceUseAnalysis::TYPE_CONSTANT;
                } elseif ($nextMeaningfulToken->equals('(') && !$prevMeaningfulToken->isGivenKind($tokensNotBeforeFunctionCall)) {
                    $type = NamespaceUseAnalysis::TYPE_FUNCTION;
                } else {
                    $type = NamespaceUseAnalysis::TYPE_CLASS;
                }

First case is 100% constant invocation. The second, function and 3rd (default) class, are not checked on the same detail level. If T_STRING would be used in the future for a new PHP feature we would default here to class, while previous to this PR the default was to assume it was used. Could we improve the testing without adding to much code?

@julienfalque
Copy link
Member Author

If T_STRING would be used in the future for a new PHP feature we would default here to class, while previous to this PR the default was to assume it was used. Could we improve the testing without adding to much code?

I think the current code is fine. If we know for sure that the tested token does not match a function or const import, then the token can only match a class/interface/trait import, which is what NamespaceUseAnalysis::TYPE_CLASS means. This would only fail if PHP introduces a new type of import but then supporting it would be part of the updates we do for each new PHP version.

@keradus keradus changed the base branch from 2.19 to 3.0 August 2, 2021 18:08
@SpacePossum
Copy link
Contributor

The goto case was also considered a "class" usage by the code because it is a T_STRING and is not a constant and not a function. I think when PHP would use the T_STRING token in the future for something different or new it would happen again.
For now I cannot think of such a case so we should be good though :)

@GrahamCampbell
Copy link
Contributor

Merge conflicts. 😭

@kubawerlos kubawerlos added the RTM Ready To Merge label Aug 28, 2021
@kubawerlos kubawerlos added this to the 3.0.3 milestone Aug 28, 2021
@keradus keradus changed the title Fix undetected unused imports when type mismatch NoUnusedImportsFixer - Fix undetected unused imports when type mismatch Aug 29, 2021
@keradus
Copy link
Member

keradus commented Aug 29, 2021

Thank you @julienfalque.

@keradus keradus merged commit ef34976 into PHP-CS-Fixer:3.0 Aug 29, 2021
@keradus
Copy link
Member

keradus commented Aug 29, 2021

thanks @SpacePossum

@keradus keradus removed the RTM Ready To Merge label Aug 29, 2021
@julienfalque julienfalque deleted the unused-import-types branch August 29, 2021 19:57
@andyrooster
Copy link

@julienfalque I think this changed has caused an issue with not detecting PHP 8 attributes usage when there is more than one in a block, for example with the below code the IsGranted and JsonSchema use statements get removed using CS Fixer v3.1.0. This doesn't happen in CS Fixer v3.0.2

use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Security;

...
#[
    Route('/basket/{uuid}/item', name: 'addBasketItem', requirements: ['uuid' => '%regex.uuid%'], methods: ['POST']),
    IsGranted('ROLE_USER'),
    JsonSchema('Public/Basket/addItem.json'),
]

@GrahamCampbell
Copy link
Contributor

@andyrooster can you create a bug ticket for this following the template, so this can be properly tracked?

@andyrooster
Copy link

@GrahamCampbell #5940

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Not removing unused import if has same name function
7 participants