Skip to content

Commit d5322a3

Browse files
committed
Stop relying on parent in throw flagging visitor
1 parent cf26290 commit d5322a3

File tree

1 file changed

+6
-27
lines changed

1 file changed

+6
-27
lines changed

src/FeatureCounter/NodeVisitor/ThrowExpressionFlaggingNodeVisitor.php

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,18 @@
55
use PhpParser\Node;
66
use PhpParser\Node\Expr\Throw_;
77
use PhpParser\Node\Stmt\Expression;
8+
use PhpParser\NodeVisitor;
89
use PhpParser\NodeVisitorAbstract;
910

10-
use function array_pop;
11-
use function count;
12-
1311
final class ThrowExpressionFlaggingNodeVisitor extends NodeVisitorAbstract {
14-
/**
15-
* @var Node[]
16-
*/
17-
private array $stack = [];
18-
19-
public function beforeTraverse(array $nodes) {
20-
$this->stack = [];
21-
22-
return null;
23-
}
24-
2512
public function enterNode(Node $node) {
26-
if ($node instanceof Throw_ && ! empty($this->stack)) {
27-
$parent = $this->stack[count($this->stack) - 1];
28-
29-
if (! $parent instanceof Expression) {
30-
$node->setAttribute('is_throw_expression', true);
31-
}
13+
if ($node instanceof Expression && $node->expr instanceof Throw_) {
14+
return NodeVisitor::DONT_TRAVERSE_CHILDREN;
3215
}
3316

34-
$this->stack[] = $node;
35-
36-
return null;
37-
}
38-
39-
public function leaveNode(Node $node) {
40-
array_pop($this->stack);
17+
if ($node instanceof Throw_) {
18+
$node->setAttribute('is_throw_expression', true);
19+
}
4120

4221
return null;
4322
}

0 commit comments

Comments
 (0)