Skip to content

NoUnusedImportsFixer - Ignore references in comments #2814

@julienfalque

Description

@julienfalque

no_unused_imports fixer searches for references to the imported elements almost everywhere in the code, including comments. This sometimes leads to not removing use statements that are unused because the same word was found in a comment, despite the comment being unrelated. See symfony/symfony#22962 (comment) for an example.

This check is done using a regular expression on the PHP code, which is the reason why the fixer can find false positives quite easily. IMO this is a bug and we should improve the regular expression.

But if we consider this a behavior change, then it would be nice to introduce a strict mode via a configuration option. When enabled, the fixer would only check usage of the imported elements in actual code and ignore comments completely (or look for PHPDocs/custom annotations only, e.g. @param Foo or @Annotation()).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions