We currently have a list of disallowed classes on the GlobalClass rule. These classes should not be called statically and currently include some entity classes. This list is currently limited to 'File', 'Node', 'NodeType', 'Role', 'Term' and 'User'. It would be nice to increase this list to include all core entities, as none of these should be called statically.

As some of the entity classes may contain generic names we'll most likely trigger false positives if we would just increase the list to all entities currently available within core, so it would be best to take the full namespace of referenced classes into consideration.

Comments

Arkener created an issue. See original summary.

arkener’s picture

PR: https://github.com/pfrenssen/coder/pull/108

Running this change against the latest version of core returns the following:

FILE: /usr/share/nginx/html/project/drupal9/core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatter.php
-----------------------------------------------------------------------------------------------------------------
FOUND 0 ERRORS AND 3 WARNINGS AFFECTING 3 LINES
-----------------------------------------------------------------------------------------------------------------
 245 | WARNING | ImageStyle::load calls should be avoided in classes, use dependency injection instead
 260 | WARNING | ImageStyle::load calls should be avoided in classes, use dependency injection instead
 266 | WARNING | ImageStyle::load calls should be avoided in classes, use dependency injection instead
-----------------------------------------------------------------------------------------------------------------


FILE: /usr/share/nginx/html/project/drupal9/core/modules/taxonomy/src/TaxonomyPermissions.php
--------------------------------------------------------------------------------------------------------------
FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
--------------------------------------------------------------------------------------------------------------
 52 | WARNING | Vocabulary::loadMultiple calls should be avoided in classes, use dependency injection instead
--------------------------------------------------------------------------------------------------------------


FILE: /usr/share/nginx/html/project/drupal9/core/modules/editor/src/Element.php
-----------------------------------------------------------------------------------------------------------
FOUND 0 ERRORS AND 2 WARNINGS AFFECTING 2 LINES
-----------------------------------------------------------------------------------------------------------
  59 | WARNING | Editor::loadMultiple calls should be avoided in classes, use dependency injection instead
 106 | WARNING | FilterFormat::load calls should be avoided in classes, use dependency injection instead
-----------------------------------------------------------------------------------------------------------
arkener’s picture

Status: Active » Needs review
klausi’s picture

Status: Needs review » Reviewed & tested by the community

Thank you, looks good!

  • Arkener authored e8aa87a on 8.x-3.x
    feat(GlobalClass): Add FQN based class check and increase list of...
arkener’s picture

Status: Reviewed & tested by the community » Fixed

Merged, thanks for the review!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.