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

Breadcrumb

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

class CallToDeprecatedMethodRule

@implements Rule<MethodCall>

Hierarchy

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

Expanded class hierarchy of CallToDeprecatedMethodRule

File

vendor/phpstan/phpstan-deprecation-rules/src/Rules/Deprecations/CallToDeprecatedMethodRule.php, line 20

Namespace

PHPStan\Rules\Deprecations
View source
class CallToDeprecatedMethodRule implements Rule {
    
    /** @var ReflectionProvider */
    private $reflectionProvider;
    
    /** @var DeprecatedScopeHelper */
    private $deprecatedScopeHelper;
    public function __construct(ReflectionProvider $reflectionProvider, DeprecatedScopeHelper $deprecatedScopeHelper) {
        $this->reflectionProvider = $reflectionProvider;
        $this->deprecatedScopeHelper = $deprecatedScopeHelper;
    }
    public function getNodeType() : string {
        return MethodCall::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;
        $methodCalledOnType = $scope->getType($node->var);
        $referencedClasses = $methodCalledOnType->getObjectClassNames();
        foreach ($referencedClasses as $referencedClass) {
            try {
                $classReflection = $this->reflectionProvider
                    ->getClass($referencedClass);
                $methodReflection = $classReflection->getMethod($methodName, $scope);
                if (!$methodReflection->isDeprecated()
                    ->yes()) {
                    continue;
                }
                $description = $methodReflection->getDeprecatedDescription();
                if ($description === null) {
                    return [
                        RuleErrorBuilder::message(sprintf('Call to deprecated method %s() of %s %s.', $methodReflection->getName(), strtolower($methodReflection->getDeclaringClass()
                            ->getClassTypeDescription()), $methodReflection->getDeclaringClass()
                            ->getName()))
                            ->identifier('method.deprecated')
                            ->build(),
                    ];
                }
                return [
                    RuleErrorBuilder::message(sprintf("Call to deprecated method %s() of %s %s:\n%s", $methodReflection->getName(), strtolower($methodReflection->getDeclaringClass()
                        ->getClassTypeDescription()), $methodReflection->getDeclaringClass()
                        ->getName(), $description))
                        ->identifier('method.deprecated')
                        ->build(),
                ];
            } catch (ClassNotFoundException $e) {
                // Other rules will notify if the class is not found
            } catch (MissingMethodFromReflectionException $e) {
                // Other rules will notify if the the method is not found
            }
        }
        return [];
    }

}

Members

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