|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Rules\Api; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use PhpParser\Node\Expr\MethodCall; |
| 7 | +use PhpParser\NodeVisitor\NodeConnectingVisitor; |
| 8 | +use PHPStan\Analyser\Scope; |
| 9 | +use PHPStan\DependencyInjection\Container; |
| 10 | +use PHPStan\Parser\RichParser; |
| 11 | +use PHPStan\Rules\Rule; |
| 12 | +use PHPStan\Rules\RuleErrorBuilder; |
| 13 | +use PHPStan\Type\Constant\ConstantStringType; |
| 14 | +use PHPStan\Type\ObjectType; |
| 15 | +use function array_keys; |
| 16 | +use function in_array; |
| 17 | +use function sprintf; |
| 18 | +use function strpos; |
| 19 | + |
| 20 | +/** |
| 21 | + * @implements Rule<MethodCall> |
| 22 | + */ |
| 23 | +class NodeConnectingVisitorAttributesRule implements Rule |
| 24 | +{ |
| 25 | + |
| 26 | + public function __construct(private Container $container) |
| 27 | + { |
| 28 | + } |
| 29 | + |
| 30 | + public function getNodeType(): string |
| 31 | + { |
| 32 | + return MethodCall::class; |
| 33 | + } |
| 34 | + |
| 35 | + public function processNode(Node $node, Scope $scope): array |
| 36 | + { |
| 37 | + if (!$node->name instanceof Node\Identifier) { |
| 38 | + return []; |
| 39 | + } |
| 40 | + if ($node->name->toLowerString() !== 'getattribute') { |
| 41 | + return []; |
| 42 | + } |
| 43 | + $calledOnType = $scope->getType($node->var); |
| 44 | + if (!(new ObjectType(Node::class))->isSuperTypeOf($calledOnType)->yes()) { |
| 45 | + return []; |
| 46 | + } |
| 47 | + $args = $node->getArgs(); |
| 48 | + if (!isset($args[0])) { |
| 49 | + return []; |
| 50 | + } |
| 51 | + $argType = $scope->getType($args[0]->value); |
| 52 | + if (!$argType instanceof ConstantStringType) { |
| 53 | + return []; |
| 54 | + } |
| 55 | + if (!in_array($argType->getValue(), ['parent', 'previous', 'next'], true)) { |
| 56 | + return []; |
| 57 | + } |
| 58 | + if (!$scope->isInClass()) { |
| 59 | + return []; |
| 60 | + } |
| 61 | + |
| 62 | + $classReflection = $scope->getClassReflection(); |
| 63 | + $hasPhpStanInterface = false; |
| 64 | + foreach (array_keys($classReflection->getInterfaces()) as $interfaceName) { |
| 65 | + if (strpos($interfaceName, 'PHPStan\\') !== 0) { |
| 66 | + continue; |
| 67 | + } |
| 68 | + |
| 69 | + $hasPhpStanInterface = true; |
| 70 | + } |
| 71 | + |
| 72 | + if (!$hasPhpStanInterface) { |
| 73 | + return []; |
| 74 | + } |
| 75 | + |
| 76 | + $isVisitorRegistered = false; |
| 77 | + foreach ($this->container->getServicesByTag(RichParser::VISITOR_SERVICE_TAG) as $service) { |
| 78 | + if ($service::class !== NodeConnectingVisitor::class) { |
| 79 | + continue; |
| 80 | + } |
| 81 | + |
| 82 | + $isVisitorRegistered = true; |
| 83 | + break; |
| 84 | + } |
| 85 | + |
| 86 | + if ($isVisitorRegistered) { |
| 87 | + return []; |
| 88 | + } |
| 89 | + |
| 90 | + return [ |
| 91 | + RuleErrorBuilder::message(sprintf('Node attribute \'%s\' is no longer available.', $argType->getValue())) |
| 92 | + ->tip('See: https://phpstan.org/blog/preprocessing-ast-for-custom-rules') |
| 93 | + ->build(), |
| 94 | + ]; |
| 95 | + } |
| 96 | + |
| 97 | +} |
0 commit comments