Skip to content

Commit 4b33bd9

Browse files
committed
StatementOrderVisitor
1 parent 379941f commit 4b33bd9

2 files changed

Lines changed: 69 additions & 0 deletions

File tree

conf/config.neon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ services:
275275
setup:
276276
- addVisitor(@PhpParser\NodeVisitor\NameResolver)
277277
- addVisitor(@PhpParser\NodeVisitor\NodeConnectingVisitor)
278+
- addVisitor(@PHPStan\NodeVisitor\StatementOrderVisitor)
278279

279280
-
280281
class: PhpParser\NodeVisitor\NameResolver
@@ -471,6 +472,9 @@ services:
471472
arguments:
472473
fileExtensions: %fileExtensions%
473474

475+
-
476+
class: PHPStan\NodeVisitor\StatementOrderVisitor
477+
474478
-
475479
class: PHPStan\Parallel\ParallelAnalyser
476480
arguments:
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\NodeVisitor;
4+
5+
use PhpParser\Node;
6+
use PhpParser\NodeVisitorAbstract;
7+
8+
class StatementOrderVisitor extends NodeVisitorAbstract
9+
{
10+
11+
/** @var int[] */
12+
private array $orderStack = [];
13+
14+
private int $depth = 0;
15+
16+
/**
17+
* @param Node[] $nodes $nodes
18+
* @return null
19+
*/
20+
public function beforeTraverse(array $nodes)
21+
{
22+
$this->orderStack = [0];
23+
$this->depth = 0;
24+
25+
return null;
26+
}
27+
28+
/**
29+
* @param Node $node
30+
* @return null
31+
*/
32+
public function enterNode(Node $node)
33+
{
34+
if (!$node instanceof Node\Stmt) {
35+
return null;
36+
}
37+
38+
$order = $this->orderStack[count($this->orderStack) - 1];
39+
$node->setAttribute('statementOrder', $order);
40+
$node->setAttribute('statementDepth', $this->depth);
41+
42+
$this->orderStack[count($this->orderStack) - 1] = $order + 1;
43+
$this->orderStack[] = 0;
44+
$this->depth++;
45+
46+
return null;
47+
}
48+
49+
/**
50+
* @param Node $node
51+
* @return null
52+
*/
53+
public function leaveNode(Node $node)
54+
{
55+
if (!$node instanceof Node\Stmt) {
56+
return null;
57+
}
58+
59+
array_pop($this->orderStack);
60+
$this->depth--;
61+
62+
return null;
63+
}
64+
65+
}

0 commit comments

Comments
 (0)