Skip to content

Commit d638ea7

Browse files
committed
Added support for ConstantString unions in NodeConnectingVisitorAttributesRule
1 parent 59e068b commit d638ea7

File tree

3 files changed

+36
-25
lines changed

3 files changed

+36
-25
lines changed

src/Rules/Api/NodeConnectingVisitorAttributesRule.php

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use PHPStan\Rules\RuleErrorBuilder;
1010
use PHPStan\Type\ObjectType;
1111
use function array_keys;
12-
use function count;
1312
use function in_array;
1413
use function sprintf;
1514
use function str_starts_with;
@@ -41,38 +40,40 @@ public function processNode(Node $node, Scope $scope): array
4140
if (!isset($args[0])) {
4241
return [];
4342
}
43+
44+
$messages = [];
4445
$argType = $scope->getType($args[0]->value);
45-
if (count($argType->getConstantStrings()) !== 1) {
46-
return [];
47-
}
48-
$argValue = $argType->getConstantScalarValues()[0];
49-
if (!in_array($argValue, ['parent', 'previous', 'next'], true)) {
50-
return [];
51-
}
52-
if (!$scope->isInClass()) {
53-
return [];
54-
}
46+
foreach ($argType->getConstantStrings() as $constantString) {
47+
$argValue = $constantString->getValue();
48+
if (!in_array($argValue, ['parent', 'previous', 'next'], true)) {
49+
continue;
50+
}
5551

56-
$classReflection = $scope->getClassReflection();
57-
$hasPhpStanInterface = false;
58-
foreach (array_keys($classReflection->getInterfaces()) as $interfaceName) {
59-
if (!str_starts_with($interfaceName, 'PHPStan\\')) {
52+
if (!$scope->isInClass()) {
6053
continue;
6154
}
6255

63-
$hasPhpStanInterface = true;
64-
}
56+
$classReflection = $scope->getClassReflection();
57+
$hasPhpStanInterface = false;
58+
foreach (array_keys($classReflection->getInterfaces()) as $interfaceName) {
59+
if (!str_starts_with($interfaceName, 'PHPStan\\')) {
60+
continue;
61+
}
6562

66-
if (!$hasPhpStanInterface) {
67-
return [];
68-
}
63+
$hasPhpStanInterface = true;
64+
}
6965

70-
return [
71-
RuleErrorBuilder::message(sprintf('Node attribute \'%s\' is no longer available.', $argValue))
66+
if (!$hasPhpStanInterface) {
67+
continue;
68+
}
69+
70+
$messages[] = RuleErrorBuilder::message(sprintf('Node attribute \'%s\' is no longer available.', $argValue))
7271
->identifier('phpParser.nodeConnectingAttribute')
7372
->tip('See: https://phpstan.org/blog/preprocessing-ast-for-custom-rules')
74-
->build(),
75-
];
73+
->build();
74+
}
75+
76+
return $messages;
7677
}
7778

7879
}

tests/PHPStan/Rules/Api/NodeConnectingVisitorAttributesRuleTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@ public function testRule(): void
2121
$this->analyse([__DIR__ . '/data/node-connecting-visitor.php'], [
2222
[
2323
'Node attribute \'parent\' is no longer available.',
24-
18,
24+
22,
25+
'See: https://phpstan.org/blog/preprocessing-ast-for-custom-rules',
26+
],
27+
[
28+
'Node attribute \'parent\' is no longer available.',
29+
24,
2530
'See: https://phpstan.org/blog/preprocessing-ast-for-custom-rules',
2631
],
2732
]);

tests/PHPStan/Rules/Api/data/node-connecting-visitor.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
class MyRule implements Rule
1010
{
11+
12+
/** @var 'parent'|'myCustomAttribute' */
13+
public string $attrName;
14+
1115
public function getNodeType(): string
1216
{
1317
return Node::class;
@@ -17,6 +21,7 @@ public function processNode(Node $node, Scope $scope): array
1721
{
1822
$parent = $node->getAttribute("parent");
1923
$custom = $node->getAttribute("myCustomAttribute");
24+
$parent = $node->getAttribute($this->attrName);
2025

2126
return [];
2227
}

0 commit comments

Comments
 (0)