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

Breadcrumb

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

function PluginManagerInspectionRule::inspectYamlPluginManager

1 call to PluginManagerInspectionRule::inspectYamlPluginManager()
PluginManagerInspectionRule::processNode in vendor/mglaman/phpstan-drupal/src/Rules/Classes/PluginManagerInspectionRule.php

File

vendor/mglaman/phpstan-drupal/src/Rules/Classes/PluginManagerInspectionRule.php, line 111

Class

PluginManagerInspectionRule
@implements \PHPStan\Rules\Rule<\PhpParser\Node\Stmt\Class_>

Namespace

mglaman\PHPStanDrupal\Rules\Classes

Code

private function inspectYamlPluginManager(Node\Stmt\Class_ $class) : array {
    $errors = [];
    $fqn = (string) $class->namespacedName;
    $reflection = $this->reflectionProvider
        ->getClass($fqn);
    $constructor = $reflection->getConstructor();
    if ($constructor->getDeclaringClass()
        ->getName() !== $fqn) {
        $errors[] = sprintf('%s must override __construct if using YAML plugins.', $fqn);
    }
    else {
        foreach ($class->stmts as $stmt) {
            if ($stmt instanceof Node\Stmt\ClassMethod && $stmt->name
                ->toString() === '__construct') {
                foreach ($stmt->stmts ?? [] as $constructorStmt) {
                    if ($constructorStmt instanceof Node\Stmt\Expression) {
                        $constructorStmt = $constructorStmt->expr;
                    }
                    if ($constructorStmt instanceof Node\Expr\StaticCall && $constructorStmt->class instanceof Node\Name && (string) $constructorStmt->class === 'parent' && $constructorStmt->name instanceof Node\Identifier && $constructorStmt->name->name === '__construct') {
                        $errors[] = sprintf('YAML plugin managers should not invoke its parent constructor.');
                    }
                }
            }
        }
    }
    return $errors;
}
RSS feed
Powered by Drupal