File tree 2 files changed +64
-2
lines changed
2 files changed +64
-2
lines changed Original file line number Diff line number Diff line change
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 function array_pop ;
14
+ use function count ;
15
+ use PhpParser \Node ;
16
+ use PhpParser \NodeVisitor ;
17
+
18
+ /**
19
+ * Visitor that connects a child node to its parent node optimzed for Attribute nodes.
20
+ *
21
+ * On the child node, the parent node can be accessed through
22
+ * <code>$node->getAttribute('parent')</code>.
23
+ */
24
+ final class AttributeParentConnectingVisitor implements NodeVisitor
25
+ {
26
+ /**
27
+ * @var Node[]
28
+ */
29
+ private array $ stack = [];
30
+
31
+ public function beforeTraverse (array $ nodes ): null
32
+ {
33
+ $ this ->stack = [];
34
+
35
+ return null ;
36
+ }
37
+
38
+ public function enterNode (Node $ node ): null
39
+ {
40
+ if (
41
+ $ this ->stack !== [] &&
42
+ ($ node instanceof Node \Attribute || $ node instanceof Node \AttributeGroup)
43
+ ) {
44
+ $ node ->setAttribute ('parent ' , $ this ->stack [count ($ this ->stack ) - 1 ]);
45
+ }
46
+
47
+ $ this ->stack [] = $ node ;
48
+
49
+ return null ;
50
+ }
51
+
52
+ public function leaveNode (Node $ node ): null
53
+ {
54
+ array_pop ($ this ->stack );
55
+
56
+ return null ;
57
+ }
58
+
59
+ public function afterTraverse (array $ nodes ): null
60
+ {
61
+ return null ;
62
+ }
63
+ }
Original file line number Diff line number Diff line change 26
26
use PhpParser \Error ;
27
27
use PhpParser \NodeTraverser ;
28
28
use PhpParser \NodeVisitor \NameResolver ;
29
- use PhpParser \NodeVisitor \ParentConnectingVisitor ;
30
29
use PhpParser \ParserFactory ;
31
30
use SebastianBergmann \CodeCoverage \ParserException ;
32
31
use SebastianBergmann \LinesOfCode \LineCountingVisitor ;
@@ -180,7 +179,7 @@ private function analyse(string $filename): void
180
179
$ executableLinesFindingVisitor = new ExecutableLinesFindingVisitor ($ source );
181
180
182
181
$ traverser ->addVisitor (new NameResolver );
183
- $ traverser ->addVisitor (new ParentConnectingVisitor );
182
+ $ traverser ->addVisitor (new AttributeParentConnectingVisitor );
184
183
$ traverser ->addVisitor ($ codeUnitFindingVisitor );
185
184
$ traverser ->addVisitor ($ lineCountingVisitor );
186
185
$ traverser ->addVisitor ($ ignoredLinesFindingVisitor );
You can’t perform that action at this time.
0 commit comments