Unused (or duplicate) imports in php file should be detected by a code sniff
see https://gerrit.wikimedia.org/r/#/c/358340/ for test cases
Unused (or duplicate) imports in php file should be detected by a code sniff
see https://gerrit.wikimedia.org/r/#/c/358340/ for test cases
Subject | Repo | Branch | Lines +/- | |
---|---|---|---|---|
Add sniff to find unused "use" statements | mediawiki/tools/codesniffer | master | +330 -0 |
This sounds like something that Phan would check, rather than phpcs (not that it currently does, I think; there's a mention here though).
Probably more like something phan does, but it should also be doable in phpcs as well.
The slevomat version is PHP 7 only, and appears to be a little entangled with the rest of their code to just copy. Drupal also has one, which they've tried to upstream at https://github.com/squizlabs/PHP_CodeSniffer/pull/1106
Change 375547 had a related patch set uploaded (by Legoktm; owner: Legoktm):
[mediawiki/tools/codesniffer@master] Add sniff to find unused "use" statements
I tested out both the Drupal one and the modified version that's being attempted in upstream (ExplodingCabbage). The ExplodingCabbage had false positives, and generally seemed extremely over complicated to support a Doctrine use case or something. The Drupal one worked for the most part, though they actually turn document comments in @return/@param into explict namespaces. I undid that part, and added code that marks uses in doc blocks as used.
Change 375547 merged by jenkins-bot:
[mediawiki/tools/codesniffer@master] Add sniff to find unused "use" statements
Just for information
Found another one: https://github.com/slevomat/coding-standard/blob/master/SlevomatCodingStandard/Sniffs/Namespaces/UnusedUsesSniff.php