Skip to content

Commit 49e52e7

Browse files
committed
Added support for ConstantString unions in NodeConnectingVisitorAttributesRule
1 parent fd676ed commit 49e52e7

File tree

3 files changed

+36
-24
lines changed

3 files changed

+36
-24
lines changed

src/Rules/Api/NodeConnectingVisitorAttributesRule.php

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,38 +41,40 @@ public function processNode(Node $node, Scope $scope): array
4141
if (!isset($args[0])) {
4242
return [];
4343
}
44+
45+
$messages = [];
4446
$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-
}
47+
foreach ($argType->getConstantStrings() as $constantString) {
48+
$argValue = $constantString->getValue();
49+
if (!in_array($argValue, ['parent', 'previous', 'next'], true)) {
50+
continue;
51+
}
5552

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

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

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

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

7880
}

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)