Using \Drupal::anything in OO code is not a good idea, mostly because it means you aren't properly injecting your dependencies.
There should be at least a warning for this.
Using \Drupal::anything in OO code is not a good idea, mostly because it means you aren't properly injecting your dependencies.
There should be at least a warning for this.
Comments
Comment #3
klausiPushed a first version of this to throw warnings in classes that extend FormBase or ControllerBase. Further suggestions welcome!
Comment #4
dawehnerIt is interesting, because for me, the codesmell is mainly when its part of generic services, rather the controllers / forms, which are integration level code anyway.
Comment #5
martin107 commentedRegrettably I think \Drupal::anything is going to be with us for a while.
Maybe a movement/attitude like the the one I am linking to.
Slowly we can beat the practice out of core.
Anyway just a suggestion.
Comment #7
klausiOpened sister issue #2720343: Add a warning for global function use like t() in classes.
Comment #9
mile23+1 on this idea. I doubt it will be in core's phpcs.xml.dist file any time soon, however. :-)
Comment #10
dawehnerNice!
Yeah I'm truely convinced that everytime you have to use
\Drupalyou have some sort of design flaw.Random example: Entity classes should not contain logic, so often this logic should be totally separate and just change the entity object for example.
Comment #11
mile23:-)
Comment #12
klausiI think we can call this fixed now. The sniff only reports on a subset of classes, but it is good enough as a start.