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

Breadcrumb

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

class CallToDeprecatedStaticMethodRule

@implements Rule<StaticCall>

Hierarchy

  • class \PHPStan\Rules\Deprecations\CallToDeprecatedStaticMethodRule implements \PHPStan\Rules\Rule

Expanded class hierarchy of CallToDeprecatedStaticMethodRule

File

vendor/phpstan/phpstan-deprecation-rules/src/Rules/Deprecations/CallToDeprecatedStaticMethodRule.php, line 24

Namespace

PHPStan\Rules\Deprecations
View source
class CallToDeprecatedStaticMethodRule implements Rule {
    
    /** @var ReflectionProvider */
    private $reflectionProvider;
    
    /** @var RuleLevelHelper */
    private $ruleLevelHelper;
    
    /** @var DeprecatedScopeHelper */
    private $deprecatedScopeHelper;
    public function __construct(ReflectionProvider $reflectionProvider, RuleLevelHelper $ruleLevelHelper, DeprecatedScopeHelper $deprecatedScopeHelper) {
        $this->reflectionProvider = $reflectionProvider;
        $this->ruleLevelHelper = $ruleLevelHelper;
        $this->deprecatedScopeHelper = $deprecatedScopeHelper;
    }
    public function getNodeType() : string {
        return StaticCall::class;
    }
    public function processNode(Node $node, Scope $scope) : array {
        if ($this->deprecatedScopeHelper
            ->isScopeDeprecated($scope)) {
            return [];
        }
        if (!$node->name instanceof Identifier) {
            return [];
        }
        $methodName = $node->name->name;
        $referencedClasses = [];
        if ($node->class instanceof Name) {
            $referencedClasses[] = $scope->resolveName($node->class);
        }
        else {
            $classTypeResult = $this->ruleLevelHelper
                ->findTypeToCheck($scope, $node->class, '', static function (Type $type) use ($methodName) : bool {
                return $type->canCallMethods()
                    ->yes() && $type->hasMethod($methodName)
                    ->yes();
            });
            if ($classTypeResult->getType() instanceof ErrorType) {
                return [];
            }
            $referencedClasses = $classTypeResult->getReferencedClasses();
        }
        $errors = [];
        foreach ($referencedClasses as $referencedClass) {
            try {
                $class = $this->reflectionProvider
                    ->getClass($referencedClass);
                $methodReflection = $class->getMethod($methodName, $scope);
            } catch (ClassNotFoundException $e) {
                continue;
            } catch (MissingMethodFromReflectionException $e) {
                continue;
            }
            if ($class->isDeprecated()) {
                $classDescription = $class->getDeprecatedDescription();
                if ($classDescription === null) {
                    $errors[] = RuleErrorBuilder::message(sprintf('Call to method %s() of deprecated %s %s.', $methodReflection->getName(), strtolower($methodReflection->getDeclaringClass()
                        ->getClassTypeDescription()), $methodReflection->getDeclaringClass()
                        ->getName()))
                        ->identifier(sprintf('staticMethod.deprecated%s', $methodReflection->getDeclaringClass()
                        ->getClassTypeDescription()))
                        ->build();
                }
                else {
                    $errors[] = RuleErrorBuilder::message(sprintf("Call to method %s() of deprecated %s %s:\n%s", $methodReflection->getName(), strtolower($methodReflection->getDeclaringClass()
                        ->getClassTypeDescription()), $methodReflection->getDeclaringClass()
                        ->getName(), $classDescription))
                        ->identifier(sprintf('staticMethod.deprecated%s', $methodReflection->getDeclaringClass()
                        ->getClassTypeDescription()))
                        ->build();
                }
            }
            if (!$methodReflection->isDeprecated()
                ->yes()) {
                continue;
            }
            $description = $methodReflection->getDeprecatedDescription();
            if ($description === null) {
                $errors[] = RuleErrorBuilder::message(sprintf('Call to deprecated method %s() of %s %s.', $methodReflection->getName(), strtolower($methodReflection->getDeclaringClass()
                    ->getClassTypeDescription()), $methodReflection->getDeclaringClass()
                    ->getName()))
                    ->identifier('staticMethod.deprecated')
                    ->build();
            }
            else {
                $errors[] = RuleErrorBuilder::message(sprintf("Call to deprecated method %s() of %s %s:\n%s", $methodReflection->getName(), strtolower($methodReflection->getDeclaringClass()
                    ->getClassTypeDescription()), $methodReflection->getDeclaringClass()
                    ->getName(), $description))
                    ->identifier('staticMethod.deprecated')
                    ->build();
            }
        }
        return $errors;
    }

}

Members

Title Sort descending Modifiers Object type Summary
CallToDeprecatedStaticMethodRule::$deprecatedScopeHelper private property @var DeprecatedScopeHelper
CallToDeprecatedStaticMethodRule::$reflectionProvider private property @var ReflectionProvider
CallToDeprecatedStaticMethodRule::$ruleLevelHelper private property @var RuleLevelHelper
CallToDeprecatedStaticMethodRule::getNodeType public function
CallToDeprecatedStaticMethodRule::processNode public function
CallToDeprecatedStaticMethodRule::__construct public function
RSS feed
Powered by Drupal