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

deviantintegral created an issue. See original summary.

dawehner’s picture

Well, wouldn't require this to basically revert #2090353: Don't require to put the use statement into plugin classes ?

drunken monkey’s picture

Well, wouldn't require this to basically revert #2090353: Don't require to put the use statement into plugin classes ?

It 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.

sierkje’s picture

SensioLabs Insight also throws errors, for example: "Missing use statement should be avoided".

But:

Well, wouldn't require this to basically revert #2090353: Don't require to put the use statement into plugin classes?

It would, and I'm very much against that.

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?

tim.plunkett’s picture

Importing those classes does nothing for runtime. It might make your IDE happier.

slootjes’s picture

+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!

dawehner’s picture

@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.

markhalliwell’s picture

FWIW, 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.

slootjes’s picture

dawehner, 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.

deviantintegral’s picture

Yes, 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.

jhodgdon’s picture

A 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?).

mpp’s picture

Ran 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

tim.plunkett’s picture

phpstorm requires an import

My 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.

mpp’s picture

Indeed.

I found some more explanation here https://drupal.stackexchange.com/questions/243486/annotations-without-cl...

Drupal's annotation system is based on the Doctrine PHP library, and not on Symfony like the rest of Drupal.

Doctrine uses a global annotation registry to map classes to files (rather than an autoloader). This page contains the list of classes in that registry.

We should update the change record on https://www.drupal.org/node/2096117 to reflect this.

dww’s picture

Status: Active » Closed (outdated)

We reviewed this in the coding standards meeting this week.

  • Seems that the "problem" has mostly solved itself.
  • Meanwhile, core is moving to replace all annotations with attributes.
  • So there was broad agreement to close this one as outdated.

Thanks,
-Derek