Skip to content

Commit 173ee46

Browse files
author
Florian Krämer
committed
Merge branch 'master' of github.com:Phauthentic/phpstan-rules into dependency-constraints
2 parents 64a048a + 5b70051 commit 173ee46

File tree

2 files changed

+62
-22
lines changed

2 files changed

+62
-22
lines changed

src/CleanCode/ControlStructureNestingRule.php

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44

55
namespace Phauthentic\PHPStanRules\CleanCode;
66

7+
use Phauthentic\PHPStanRules\PhpParser\ParentNodeAttributeVisitor;
78
use PhpParser\Node;
89
use PhpParser\Node\Stmt\Catch_;
910
use PhpParser\Node\Stmt\Else_;
1011
use PhpParser\Node\Stmt\ElseIf_;
1112
use PhpParser\Node\Stmt\If_;
1213
use PhpParser\Node\Stmt\TryCatch;
1314
use PhpParser\NodeTraverser;
14-
use PhpParser\NodeVisitorAbstract;
1515
use PHPStan\Analyser\Scope;
1616
use PHPStan\Rules\Rule;
1717
use PHPStan\Rules\RuleErrorBuilder;
@@ -114,27 +114,8 @@ public function addError(int $nestingLevel, Else_|If_|Catch_|ElseIf_|TryCatch $n
114114
return $errors;
115115
}
116116

117-
public function createNodeVisitor(): object
117+
public function createNodeVisitor(): ParentNodeAttributeVisitor
118118
{
119-
return new class extends NodeVisitorAbstract {
120-
public function enterNode(Node $node)
121-
{
122-
foreach ($node->getSubNodeNames() as $subNodeName) {
123-
$subNode = $node->$subNodeName;
124-
if (is_array($subNode)) {
125-
foreach ($subNode as $childNode) {
126-
if ($childNode instanceof Node) {
127-
$childNode->setAttribute('parent', $node);
128-
}
129-
}
130-
continue;
131-
}
132-
133-
if ($subNode instanceof Node) {
134-
$subNode->setAttribute('parent', $node);
135-
}
136-
}
137-
}
138-
};
119+
return new ParentNodeAttributeVisitor();
139120
}
140121
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Phauthentic\PHPStanRules\PhpParser;
6+
7+
use PhpParser\Node;
8+
use PhpParser\NodeVisitorAbstract;
9+
10+
/**
11+
* Node visitor that sets parent node attributes on child nodes.
12+
*
13+
* This enables traversing up the AST tree to determine nesting levels.
14+
*/
15+
class ParentNodeAttributeVisitor extends NodeVisitorAbstract
16+
{
17+
/**
18+
* @return int|Node|null
19+
*/
20+
public function enterNode(Node $node)
21+
{
22+
foreach ($node->getSubNodeNames() as $subNodeName) {
23+
$subNode = $node->$subNodeName;
24+
$this->setParentAttributeForSubNode($subNode, $node);
25+
}
26+
27+
return null;
28+
}
29+
30+
/**
31+
* @param mixed $subNode
32+
* @param Node $parentNode
33+
*/
34+
private function setParentAttributeForSubNode($subNode, Node $parentNode): void
35+
{
36+
if (is_array($subNode)) {
37+
$this->setParentAttributeForArrayOfNodes($subNode, $parentNode);
38+
return;
39+
}
40+
41+
if ($subNode instanceof Node) {
42+
$subNode->setAttribute('parent', $parentNode);
43+
}
44+
}
45+
46+
/**
47+
* @param array<mixed> $nodes
48+
* @param Node $parentNode
49+
*/
50+
private function setParentAttributeForArrayOfNodes(array $nodes, Node $parentNode): void
51+
{
52+
foreach ($nodes as $childNode) {
53+
if ($childNode instanceof Node) {
54+
$childNode->setAttribute('parent', $parentNode);
55+
}
56+
}
57+
}
58+
}
59+

0 commit comments

Comments
 (0)