@@ -11,8 +11,13 @@ class StatementOrderVisitor extends NodeVisitorAbstract
1111 /** @var int[] */
1212 private array $ orderStack = [];
1313
14+ /** @var int[] */
15+ private array $ expressionOrderStack = [];
16+
1417 private int $ depth = 0 ;
1518
19+ private int $ expressionDepth = 0 ;
20+
1621 /**
1722 * @param Node[] $nodes $nodes
1823 * @return null
@@ -31,18 +36,30 @@ public function beforeTraverse(array $nodes)
3136 */
3237 public function enterNode (Node $ node )
3338 {
34- if (!$ node instanceof Node \Stmt) {
35- return null ;
36- }
37-
3839 $ order = $ this ->orderStack [count ($ this ->orderStack ) - 1 ];
3940 $ node ->setAttribute ('statementOrder ' , $ order );
4041 $ node ->setAttribute ('statementDepth ' , $ this ->depth );
4142
43+ if ($ node instanceof Node \Expr && count ($ this ->expressionOrderStack ) > 0 ) {
44+ $ expressionOrder = $ this ->expressionOrderStack [count ($ this ->expressionOrderStack ) - 1 ];
45+ $ node ->setAttribute ('expressionOrder ' , $ expressionOrder );
46+ $ node ->setAttribute ('expressionDepth ' , $ this ->expressionDepth );
47+ $ this ->expressionOrderStack [count ($ this ->expressionOrderStack ) - 1 ] = $ expressionOrder + 1 ;
48+ $ this ->expressionOrderStack [] = 0 ;
49+ $ this ->expressionDepth ++;
50+ }
51+
52+ if (!$ node instanceof Node \Stmt) {
53+ return null ;
54+ }
55+
4256 $ this ->orderStack [count ($ this ->orderStack ) - 1 ] = $ order + 1 ;
4357 $ this ->orderStack [] = 0 ;
4458 $ this ->depth ++;
4559
60+ $ this ->expressionOrderStack = [0 ];
61+ $ this ->expressionDepth = 0 ;
62+
4663 return null ;
4764 }
4865
@@ -52,6 +69,10 @@ public function enterNode(Node $node)
5269 */
5370 public function leaveNode (Node $ node )
5471 {
72+ if ($ node instanceof Node \Expr) {
73+ array_pop ($ this ->expressionOrderStack );
74+ $ this ->expressionDepth --;
75+ }
5576 if (!$ node instanceof Node \Stmt) {
5677 return null ;
5778 }
0 commit comments