Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. GlobalDrupalDependencyInjectionRule.php

function GlobalDrupalDependencyInjectionRule::processNode

File

vendor/mglaman/phpstan-drupal/src/Rules/Drupal/GlobalDrupalDependencyInjectionRule.php, line 20

Class

GlobalDrupalDependencyInjectionRule
@implements Rule<Node\Expr\StaticCall>

Namespace

mglaman\PHPStanDrupal\Rules\Drupal

Code

public function processNode(Node $node, Scope $scope) : array {
    // Only check static calls to \Drupal
    if (!$node->class instanceof Node\Name\FullyQualified || (string) $node->class !== 'Drupal') {
        return [];
    }
    // Do not raise if called inside a trait.
    if (!$scope->isInClass() || $scope->isInTrait()) {
        return [];
    }
    $scopeClassReflection = $scope->getClassReflection();
    // Enums cannot have dependency injection.
    if ($scopeClassReflection->isEnum()) {
        return [];
    }
    $allowed_list = [
        // Ignore tests.
'PHPUnit\\Framework\\Test',
        // Typed data objects cannot use dependency injection.
'Drupal\\Core\\TypedData\\TypedDataInterface',
        // Render elements cannot use dependency injection.
'Drupal\\Core\\Render\\Element\\ElementInterface',
        'Drupal\\Core\\Render\\Element\\FormElementInterface',
        'Drupal\\config_translation\\FormElement\\ElementInterface',
        // Entities don't use services for now
        // @see https://www.drupal.org/project/drupal/issues/2913224
'Drupal\\Core\\Entity\\EntityInterface',
        // Stream wrappers are only registered as a service for their tags
        // and cannot use dependency injection. Function calls like
        // file_exists, stat, etc. will construct the class directly.
'Drupal\\Core\\StreamWrapper\\StreamWrapperInterface',
        // Ignore Nightwatch test setup classes.
'Drupal\\TestSite\\TestSetupInterface',
    ];
    foreach ($allowed_list as $item) {
        if ($scopeClassReflection->implementsInterface($item)) {
            return [];
        }
    }
    $scopeFunction = $scope->getFunction();
    if ($scopeFunction === null) {
        return [];
    }
    if (!$scopeFunction instanceof ExtendedMethodReflection) {
        return [];
    }
    if ($scopeFunction->isStatic()) {
        return [];
    }
    return [
        '\\Drupal calls should be avoided in classes, use dependency injection instead',
    ];
}
RSS feed
Powered by Drupal