Skip to content

Commit 415f196

Browse files
committed
Use more efficient AttributeParentConnectingVisitor
1 parent 8a68c67 commit 415f196

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php declare(strict_types=1);
2+
3+
/*
4+
* This file is part of phpunit/php-code-coverage.
5+
*
6+
* (c) Sebastian Bergmann <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
namespace SebastianBergmann\CodeCoverage\StaticAnalysis;
12+
13+
use PhpParser\Node;
14+
use PhpParser\NodeVisitorAbstract;
15+
16+
use function array_pop;
17+
use function count;
18+
19+
/**
20+
* Visitor that connects a child node to its parent node.
21+
*
22+
* On the child node, the parent node can be accessed through
23+
* <code>$node->getAttribute('parent')</code>.
24+
*/
25+
final class AttributeParentConnectingVisitor extends NodeVisitorAbstract {
26+
/**
27+
* @var Node[]
28+
*/
29+
private array $stack = [];
30+
31+
public function beforeTraverse(array $nodes) {
32+
$this->stack = [];
33+
}
34+
35+
public function enterNode(Node $node) {
36+
if (
37+
!empty($this->stack)
38+
&& ($node instanceof Node\Attribute || $node instanceof Node\AttributeGroup)
39+
) {
40+
$node->setAttribute('parent', $this->stack[count($this->stack) - 1]);
41+
}
42+
43+
$this->stack[] = $node;
44+
}
45+
46+
public function leaveNode(Node $node) {
47+
array_pop($this->stack);
48+
}
49+
}

src/StaticAnalysis/ParsingFileAnalyser.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ private function analyse(string $filename): void
180180
$executableLinesFindingVisitor = new ExecutableLinesFindingVisitor($source);
181181

182182
$traverser->addVisitor(new NameResolver);
183-
$traverser->addVisitor(new ParentConnectingVisitor);
183+
$traverser->addVisitor(new AttributeParentConnectingVisitor());
184184
$traverser->addVisitor($codeUnitFindingVisitor);
185185
$traverser->addVisitor($lineCountingVisitor);
186186
$traverser->addVisitor($ignoredLinesFindingVisitor);

0 commit comments

Comments
 (0)