This came out of a bug report against code sniffer: #2625312: Drupal.Classes.UnusedUseStatement doesn't recognize annotations
When you use annotations in a class, some IDEs provide autocomplete against the annotation itself. PHPStorm will even detect the property type (string, array, etc) and fill in quotes or braces. However, PHPStorm needs the annotation class to be imported. This in turn throws a standards error for an unused import.
It looks like Symfony 2 imports classes that are used in annotations, but Doctrine doesn't. I don't see any downside to importing them, given that the annotation classes are probably loaded on a given request anyways.
Could we either:
- Flip the standard around, so that all annotations should be imported to a class
- Remove the inspection for unused imports for any class that has 'Annotation' in it's namespace?
Comments
Comment #2
dawehnerWell, wouldn't require this to basically revert #2090353: Don't require to put the use statement into plugin classes ?
Comment #3
drunken monkeyIt would, and I'm very much against that.
Not having to import those classes makes things a lot simpler.
PhpStorm complaining about it annoys me, too, but there's already an issue for that. You could try to work on that, if you'd like to see this problem fixed.
Comment #4
sierkje CreditAttribution: sierkje commentedSensioLabs Insight also throws errors, for example: "Missing use statement should be avoided".
But:
I filed a feature request/bug report over there, off the island, but I still have a (not very urgent) question. I understand that it is not necessary to explicitly import these annotation classes, but is it actually recommended to never import these classes (in contrib code), or is explicit import optional and allowed?
Comment #5
tim.plunkettImporting those classes does nothing for runtime. It might make your IDE happier.
Comment #6
slootjes CreditAttribution: slootjes commented+1 on importing the namespaces, makes it much more IDE friendly. In Doctrine ORM this is also used and it allows IDEs to autocomplete options, very useful!
Comment #7
dawehner@slootjes Nothing blocks you from doing that in your code, I hope. I think requiring people to add them would be a major pain, especially as people are so used to it.
Comment #8
markhalliwellFWIW, it appears the PHPStorm no longer has issues about this. Not sure if it was the annotation plugin that got updated or phpstorm... or both, but it can recognize annotations without requiring a use statement.
Comment #9
slootjes CreditAttribution: slootjes commenteddawehner, I'd like to see this becoming required as it's much like magic now. Looking at other code standards in Drupal where everything is overly explicit I'm surprised this was decided on in the first place.
Comment #10
deviantintegral CreditAttribution: deviantintegral at Lullabot commentedYes, I'm seeing improved behaviour as well. However, if you have two annotation classes with the same name (but different namespaces), you still have to import the class to tell it which annotation you are using. That's a pretty rare edge case (I've yet to see this in real code) so perhaps we can just close this now.
Comment #11
jhodgdonA few thoughts:
a) I don't think we have any cases in Core where we've defined two annotation classes with the same name. I sure hope not, because obviously since we don't generally put a "use" statement in, they would be ambiguous for annotations.
b) As things are now, if you did put a "use" statement in for an annotation class, Coder would complain about an unused use statement.
c) This is assuming that by "import" in the issue summary and issue title, you mean "put in a use statement". If that is what you mean, I think it would be clearer to change the title and issue summary to say "put in a use statement". If you mean something else, then I think the issue summary should clarify what you mean by "import a class", because ... I can't think of what it would mean? Thanks!
d) It's also very helpful in coding standards issues to suggest specific changes (which could be to the Coder module and/or to coding standards pages) that would need to be made if the standard would be adopted, and to point out whether Core is generally in compliance with the proposed standard, or if a large patch would need to be made (like in this case, I guess it would mean that all annotated classes would need a 1-line patch to put a use statement in?).
Comment #12
mpp CreditAttribution: mpp at AmeXio for District09 commentedRan into this aswell:
- phpstorm requires an import
- phpcs complains about an unused statement
Historically speaking I can understand why we removed the need for import statements on annotations:
- editors didn't support autocompletion (yet);
- new oo pattern in D8 for a lot of developers that resulted in some funky errors if the annotation class wasn't imported;
But are these (still) valid reasons or are there other reasons?
imho, at the least we should consider making phpcs clever enough not to complain about it:
- having two code quality tools contradict each other is never pretty;
- IF (theoretically) someone would ever want to use two annotations with the same name in one class it wouldn't be possible.
- PSR-12 states that for traits an import statement is a must; I couldn't find anything in particular wrt annotations
https://www.php-fig.org/psr/psr-12/#42-using-traits
Comment #13
tim.plunkettMy PHPStorm config happily recognizes annotations without imports.
I believe it's via the PHP Annotations plugin, which is a dependency of the Drupal Symfony Bridge plugin.
Comment #14
mpp CreditAttribution: mpp at AmeXio for District09 commentedIndeed.
I found some more explanation here https://drupal.stackexchange.com/questions/243486/annotations-without-cl...
We should update the change record on https://www.drupal.org/node/2096117 to reflect this.
Comment #15
dwwWe reviewed this in the coding standards meeting this week.
Thanks,
-Derek