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

Breadcrumb

  1. Drupal Core 11.1.x

CallToDeprecatedFunctionRule.php

Namespace

PHPStan\Rules\Deprecations

File

vendor/phpstan/phpstan-deprecation-rules/src/Rules/Deprecations/CallToDeprecatedFunctionRule.php

View source
<?php

declare (strict_types=1);
namespace PHPStan\Rules\Deprecations;

use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use PHPStan\Analyser\Scope;
use PHPStan\Broker\FunctionNotFoundException;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use function sprintf;

/**
 * @implements Rule<FuncCall>
 */
class CallToDeprecatedFunctionRule 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 FuncCall::class;
    }
    public function processNode(Node $node, Scope $scope) : array {
        if ($this->deprecatedScopeHelper
            ->isScopeDeprecated($scope)) {
            return [];
        }
        if (!$node->name instanceof Name) {
            return [];
        }
        try {
            $function = $this->reflectionProvider
                ->getFunction($node->name, $scope);
        } catch (FunctionNotFoundException $e) {
            // Other rules will notify if the function is not found
            return [];
        }
        if ($function->isDeprecated()
            ->yes()) {
            $description = $function->getDeprecatedDescription();
            if ($description === null) {
                return [
                    RuleErrorBuilder::message(sprintf('Call to deprecated function %s().', $function->getName()))
                        ->identifier('function.deprecated')
                        ->build(),
                ];
            }
            return [
                RuleErrorBuilder::message(sprintf("Call to deprecated function %s():\n%s", $function->getName(), $description))
                    ->identifier('function.deprecated')
                    ->build(),
            ];
        }
        return [];
    }

}

Classes

Title Deprecated Summary
CallToDeprecatedFunctionRule @implements Rule<FuncCall>
RSS feed
Powered by Drupal