-
Notifications
You must be signed in to change notification settings - Fork 506
/
Copy pathNodeConnectingVisitorAttributesRule.php
79 lines (66 loc) · 1.83 KB
/
NodeConnectingVisitorAttributesRule.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Api;
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\ObjectType;
use function array_keys;
use function in_array;
use function sprintf;
use function str_starts_with;
/**
* @implements Rule<MethodCall>
*/
final class NodeConnectingVisitorAttributesRule implements Rule
{
public function getNodeType(): string
{
return MethodCall::class;
}
public function processNode(Node $node, Scope $scope): array
{
if (!$node->name instanceof Node\Identifier) {
return [];
}
if ($node->name->toLowerString() !== 'getattribute') {
return [];
}
$calledOnType = $scope->getType($node->var);
if (!(new ObjectType(Node::class))->isSuperTypeOf($calledOnType)->yes()) {
return [];
}
$args = $node->getArgs();
if (!isset($args[0])) {
return [];
}
$messages = [];
$argType = $scope->getType($args[0]->value);
foreach ($argType->getConstantStrings() as $constantString) {
$argValue = $constantString->getValue();
if (!in_array($argValue, ['parent', 'previous', 'next'], true)) {
continue;
}
if (!$scope->isInClass()) {
continue;
}
$classReflection = $scope->getClassReflection();
$hasPhpStanInterface = false;
foreach (array_keys($classReflection->getInterfaces()) as $interfaceName) {
if (!str_starts_with($interfaceName, 'PHPStan\\')) {
continue;
}
$hasPhpStanInterface = true;
}
if (!$hasPhpStanInterface) {
continue;
}
$messages[] = RuleErrorBuilder::message(sprintf('Node attribute \'%s\' is no longer available.', $argValue))
->identifier('phpParser.nodeConnectingAttribute')
->tip('See: https://phpstan.org/blog/preprocessing-ast-for-custom-rules')
->build();
}
return $messages;
}
}