2121use PhpParser \Node \Expr \PropertyFetch ;
2222use PhpParser \Node \Expr \StaticPropertyFetch ;
2323use PhpParser \Node \Expr \Variable ;
24+ use PhpParser \Node \FunctionLike ;
2425use PhpParser \Node \Identifier ;
2526use PhpParser \Node \Name \FullyQualified ;
2627use PhpParser \Node \Param ;
2728use PhpParser \Node \Scalar \String_ ;
2829use PhpParser \Node \Stmt \Break_ ;
30+ use PhpParser \Node \Stmt \Catch_ ;
2931use PhpParser \Node \Stmt \Class_ ;
3032use PhpParser \Node \Stmt \Do_ ;
3133use PhpParser \Node \Stmt \Else_ ;
3234use PhpParser \Node \Stmt \ElseIf_ ;
35+ use PhpParser \Node \Stmt \Finally_ ;
3336use PhpParser \Node \Stmt \For_ ;
3437use PhpParser \Node \Stmt \Foreach_ ;
35- use PhpParser \Node \Stmt \Function_ ;
3638use PhpParser \Node \Stmt \If_ ;
3739use PhpParser \Node \Stmt \Switch_ ;
3840use PhpParser \Node \Stmt \TryCatch ;
3941use PhpParser \Node \Stmt \Unset_ ;
4042use PhpParser \Node \Stmt \While_ ;
41- use PhpParser \NodeVisitor ;
4243use PhpParser \NodeVisitorAbstract ;
4344use Rector \Contract \PhpParser \DecoratingNodeVisitorInterface ;
4445use Rector \NodeTypeResolver \Node \AttributeKey ;
45- use Rector \PhpDocParser \NodeTraverser \SimpleCallableNodeTraverser ;
46- use Rector \PhpParser \NodeTraverser \SimpleNodeTraverser ;
4746
4847final class ContextNodeVisitor extends NodeVisitorAbstract implements DecoratingNodeVisitorInterface
4948{
50- public function __construct (
51- private readonly SimpleCallableNodeTraverser $ simpleCallableNodeTraverser
52- ) {
49+ /**
50+ * Context is tracked by depth during the main traversal, instead of traversing
51+ * the loop, try and attribute children again on every enter
52+ */
53+ private int $ loopDepth = 0 ;
54+
55+ private int $ tryDepth = 0 ;
56+
57+ private int $ attributeDepth = 0 ;
58+
59+ /**
60+ * @var array<array{int, int}>
61+ */
62+ private array $ outerLoopAndTryDepths = [];
63+
64+ /**
65+ * @param Node[] $nodes
66+ */
67+ public function beforeTraverse (array $ nodes ): null
68+ {
69+ $ this ->loopDepth = 0 ;
70+ $ this ->tryDepth = 0 ;
71+ $ this ->attributeDepth = 0 ;
72+ $ this ->outerLoopAndTryDepths = [];
73+
74+ return null ;
5375 }
5476
5577 public function enterNode (Node $ node ): ?Node
5678 {
79+ $ this ->decorateEnclosingContext ($ node );
80+
5781 if ($ node instanceof For_ || $ node instanceof Foreach_ || $ node instanceof While_ || $ node instanceof Do_ || $ node instanceof Switch_) {
5882 $ this ->processContextInLoop ($ node );
83+ ++$ this ->loopDepth ;
5984 return null ;
6085 }
6186
@@ -73,8 +98,7 @@ public function enterNode(Node $node): ?Node
7398 }
7499
75100 if ($ node instanceof TryCatch) {
76- SimpleNodeTraverser ::decorateWithAttributeValue ($ node ->stmts , AttributeKey::IS_IN_TRY_BLOCK , true );
77-
101+ ++$ this ->tryDepth ;
78102 return null ;
79103 }
80104
@@ -87,7 +111,7 @@ public function enterNode(Node $node): ?Node
87111 }
88112
89113 if ($ node instanceof Attribute) {
90- $ this ->processContextInAttribute ( $ node ) ;
114+ ++ $ this ->attributeDepth ;
91115 return null ;
92116 }
93117
@@ -134,6 +158,67 @@ public function enterNode(Node $node): ?Node
134158 return null ;
135159 }
136160
161+ public function leaveNode (Node $ node ): null
162+ {
163+ if ($ node instanceof For_ || $ node instanceof Foreach_ || $ node instanceof While_ || $ node instanceof Do_ || $ node instanceof Switch_) {
164+ --$ this ->loopDepth ;
165+ return null ;
166+ }
167+
168+ if ($ node instanceof TryCatch) {
169+ --$ this ->tryDepth ;
170+ return null ;
171+ }
172+
173+ // back to the try stmts of the parent TryCatch, see decorateEnclosingContext()
174+ if ($ node instanceof Catch_ || $ node instanceof Finally_) {
175+ ++$ this ->tryDepth ;
176+ return null ;
177+ }
178+
179+ if ($ node instanceof Attribute) {
180+ --$ this ->attributeDepth ;
181+ return null ;
182+ }
183+
184+ if ($ node instanceof Class_ || $ node instanceof FunctionLike) {
185+ [$ this ->loopDepth , $ this ->tryDepth ] = array_pop ($ this ->outerLoopAndTryDepths ) ?? [0 , 0 ];
186+ }
187+
188+ return null ;
189+ }
190+
191+ private function decorateEnclosingContext (Node $ node ): void
192+ {
193+ // catch and finally are not part of the try block of their parent TryCatch
194+ if ($ node instanceof Catch_ || $ node instanceof Finally_) {
195+ --$ this ->tryDepth ;
196+ }
197+
198+ // nested class or function-like does not belong to outer loop or try block
199+ if ($ node instanceof Class_ || $ node instanceof FunctionLike) {
200+ $ this ->outerLoopAndTryDepths [] = [$ this ->loopDepth , $ this ->tryDepth ];
201+ $ this ->loopDepth = 0 ;
202+ $ this ->tryDepth = 0 ;
203+ } elseif ($ this ->tryDepth > 0 ) {
204+ $ node ->setAttribute (AttributeKey::IS_IN_TRY_BLOCK , true );
205+ }
206+
207+ if ($ this ->loopDepth > 0 && ($ node instanceof If_ || $ node instanceof Break_)) {
208+ $ node ->setAttribute (AttributeKey::IS_IN_LOOP_OR_SWITCH , true );
209+ }
210+
211+ if ($ this ->attributeDepth > 0 ) {
212+ if ($ node instanceof Array_) {
213+ $ node ->setAttribute (AttributeKey::IS_ARRAY_IN_ATTRIBUTE , true );
214+ }
215+
216+ if ($ node instanceof Closure) {
217+ $ node ->setAttribute (AttributeKey::IS_CLOSURE_IN_ATTRIBUTE , true );
218+ }
219+ }
220+ }
221+
137222 private function processInsideArrayDimFetch (ArrayDimFetch $ arrayDimFetch ): void
138223 {
139224 if ($ arrayDimFetch ->var instanceof PropertyFetch || $ arrayDimFetch ->var instanceof StaticPropertyFetch) {
@@ -203,24 +288,6 @@ private function processSetFactoryArgument(MethodCall $methodCall): void
203288 }
204289 }
205290
206- private function processContextInAttribute (Attribute $ attribute ): void
207- {
208- $ this ->simpleCallableNodeTraverser ->traverseNodesWithCallable (
209- $ attribute ->args ,
210- static function (Node $ subNode ): null {
211- if ($ subNode instanceof Array_) {
212- $ subNode ->setAttribute (AttributeKey::IS_ARRAY_IN_ATTRIBUTE , true );
213- }
214-
215- if ($ subNode instanceof Closure) {
216- $ subNode ->setAttribute (AttributeKey::IS_CLOSURE_IN_ATTRIBUTE , true );
217- }
218-
219- return null ;
220- }
221- );
222- }
223-
224291 private function processContextInIf (If_ |Else_ |ElseIf_ $ node ): void
225292 {
226293 foreach ($ node ->stmts as $ stmt ) {
@@ -239,22 +306,5 @@ private function processContextInLoop(For_|Foreach_|While_|Do_|Switch_ $node): v
239306
240307 $ node ->valueVar ->setAttribute (AttributeKey::IS_VARIABLE_LOOP , true );
241308 }
242-
243- $ stmts = $ node instanceof Switch_ ? $ node ->cases : $ node ->stmts ;
244-
245- $ this ->simpleCallableNodeTraverser ->traverseNodesWithCallable (
246- $ stmts ,
247- static function (Node $ subNode ): ?int {
248- if ($ subNode instanceof Class_ || $ subNode instanceof Function_ || $ subNode instanceof Closure) {
249- return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN ;
250- }
251-
252- if ($ subNode instanceof If_ || $ subNode instanceof Break_) {
253- $ subNode ->setAttribute (AttributeKey::IS_IN_LOOP_OR_SWITCH , true );
254- }
255-
256- return null ;
257- }
258- );
259309 }
260310}
0 commit comments