@@ -37,7 +37,7 @@ public function processNode(Node $node, Scope $scope): array
37
37
{
38
38
$ this ->setParentAttributesToEnableCorrectNestingDetection ($ node );
39
39
40
- if (!$ this ->nodeIsAControllerStructure ($ node )) {
40
+ if (!$ this ->nodeIsAControlStructure ($ node )) {
41
41
return [];
42
42
}
43
43
@@ -50,7 +50,7 @@ public function processNode(Node $node, Scope $scope): array
50
50
return $ errors ;
51
51
}
52
52
53
- private function nodeIsAControllerStructure (Node $ node ): bool
53
+ private function nodeIsAControlStructure (Node $ node ): bool
54
54
{
55
55
return $ node instanceof If_ ||
56
56
$ node instanceof Else_ ||
@@ -62,7 +62,7 @@ private function nodeIsAControllerStructure(Node $node): bool
62
62
private function getNestingLevel (Node $ node , int $ currentLevel = 1 ): int
63
63
{
64
64
$ parent = $ node ->getAttribute ('parent ' );
65
- if ($ this ->nodeIsAControllerStructure ($ parent )) {
65
+ if ($ this ->nodeIsAControlStructure ($ parent )) {
66
66
return $ this ->getNestingLevel ($ parent , $ currentLevel + 1 );
67
67
}
68
68
@@ -76,26 +76,7 @@ private function getNestingLevel(Node $node, int $currentLevel = 1): int
76
76
public function setParentAttributesToEnableCorrectNestingDetection (Node $ node ): void
77
77
{
78
78
$ traverser = new NodeTraverser ();
79
- $ traverser ->addVisitor (
80
- new class extends NodeVisitorAbstract {
81
- public function enterNode (Node $ node )
82
- {
83
- foreach ($ node ->getSubNodeNames () as $ subNodeName ) {
84
- $ subNode = $ node ->$ subNodeName ;
85
- if (is_array ($ subNode )) {
86
- foreach ($ subNode as $ childNode ) {
87
- if ($ childNode instanceof Node) {
88
- $ childNode ->setAttribute ('parent ' , $ node );
89
- }
90
- }
91
- } elseif ($ subNode instanceof Node) {
92
- $ subNode ->setAttribute ('parent ' , $ node );
93
- }
94
- }
95
- }
96
- }
97
- );
98
-
79
+ $ traverser ->addVisitor ($ this ->createNodeVisitor ());
99
80
$ traverser ->traverse ([$ node ]);
100
81
}
101
82
@@ -107,13 +88,38 @@ public function enterNode(Node $node)
107
88
*/
108
89
public function addError (int $ nestingLevel , Else_ |If_ |Catch_ |ElseIf_ |TryCatch $ node , array $ errors ): array
109
90
{
110
- $ errors [] = RuleErrorBuilder::message (
111
- sprintf (
112
- 'Nesting level of %d exceeded. Maximum allowed is %d. ' ,
113
- $ nestingLevel ,
114
- $ this ->maxNestingLevel
115
- )
116
- )->line ($ node ->getLine ())->build ();
91
+ $ errorMessage = sprintf (
92
+ 'Nesting level of %d exceeded. Maximum allowed is %d. ' ,
93
+ $ nestingLevel ,
94
+ $ this ->maxNestingLevel
95
+ );
96
+
97
+ $ errors [] = RuleErrorBuilder::message ($ errorMessage )->line ($ node ->getLine ())->build ();
98
+
117
99
return $ errors ;
118
100
}
101
+
102
+ public function createNodeVisitor (): object
103
+ {
104
+ return new class extends NodeVisitorAbstract {
105
+ public function enterNode (Node $ node )
106
+ {
107
+ foreach ($ node ->getSubNodeNames () as $ subNodeName ) {
108
+ $ subNode = $ node ->$ subNodeName ;
109
+ if (is_array ($ subNode )) {
110
+ foreach ($ subNode as $ childNode ) {
111
+ if ($ childNode instanceof Node) {
112
+ $ childNode ->setAttribute ('parent ' , $ node );
113
+ }
114
+ }
115
+ continue ;
116
+ }
117
+
118
+ if ($ subNode instanceof Node) {
119
+ $ subNode ->setAttribute ('parent ' , $ node );
120
+ }
121
+ }
122
+ }
123
+ };
124
+ }
119
125
}
0 commit comments