-
Notifications
You must be signed in to change notification settings - Fork 7
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
Annotations are ignored when scanning for used/unused imports #7
Comments
Annotations are framework-specific (in this case, Symfony), and the goal of this project is to analyse PHP code, not framework specific features. The feature you are asking for should take the form of a PHPCS plugin/extension specifically tailored for Symfony. |
We could create an option to have this sniff scan comments for symbols; it would also help in other places where symbols are used in comments (like phpunit). However, this gets tricky fast because comments contain all kinds of words which are not going to be recognized, so how do you determine which ones should be treated as symbols? Maybe just capitalized words? That's still tricky for sentences which start with a capital letter. I suppose another option would be to add an option like |
Thank you both for your comments, they're fair =) Indeed, my request might be out of scope of the project. I'm trying to add solid linting/formatting to our Symfony project, and this is definitely going to be a problem if we have false positives/negatives when dealing with annotations (the same thing happens with Doctrine, obviously). I have no idea how to contribute, but would be willing to try! Any pointers on where I should look for examples on adding parser logic? Cheers! |
Just a note: it also gives false "unused" warnings when using an import only within a block of PHPDoc, like this: use AppBundle\CustomException;
/**
* @return string
*
* @throws CustomException
*/
public function testFunction() { ... } |
That's expected. PHPDoc are really just comments. There will be no problems with running a script if the classes listed in PHPDoc annotations are not imported, so there's no need to scan them or report them. If an IDE integration shows a warning, that's an issue specific to that IDE and not to PHP itself.
Thanks for your enthusiasm! Options in PHPCS are pretty simple; you can see how this plugin uses the The first thing I'd do is to add a fixture and a test. For example, take the code snippet you pasted above and put it in a file, then create a test to make sure there are no errors. Basically something like this test except that We'd then need to include comments in the register function (beware, there are different kinds of comment token types) to include them in the scan. Next, the real challenge, we'd need to add a new function inside
In general, you can see my article on writing phpcs sniffs if you've never worked with one before. |
Thanks, will look at it as soon as I have the time and will report back =) |
I don't know how commonly PHPDoc is used, but if it's pretty common, it could be worth allowing an option to disable the warning if present on PHPDoc declarations. I'm using Psalm and declaring types through PHPDoc blocks. :) Anyway, just a suggestion. |
I agree with @zanona, having the sniff detect PHPDoc would be beneficial to everyone using static analysis tools (Psalm, PHPStan, etc.). |
In the following code,
Route
is imported and used, but is being reported as unused. Conversely,Method
is used without being imported, but PHPCS does not mind.The text was updated successfully, but these errors were encountered: