Passed
Push — master ( 2a3624...3abe14 )
by Théo
03:10
created

ParentNodeVisitor::beforeTraverse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Humbug\PhpScoper\NodeVisitor;
6
7
use PhpParser\Node;
8
use PhpParser\NodeVisitorAbstract;
9
10
final class ParentNodeVisitor extends NodeVisitorAbstract
11
{
12
    private $stack;
13
14
    /**
15
     * @inheritdoc
16
     */
17
    public function beforeTraverse(array $nodes)
18
    {
19
        $this->stack = [];
20
    }
21
22
    /**
23
     * @inheritdoc
24
     */
25
    public function enterNode(Node $node)
26
    {
27
        if (!empty($this->stack)) {
28
            $node->setAttribute('parent', $this->stack[count($this->stack) - 1]);
29
        }
30
        $this->stack[] = $node;
31
    }
32
33
    /**
34
     * @inheritdoc
35
     */
36
    public function leaveNode(Node $node)
37
    {
38
        array_pop($this->stack);
39
    }
40
}
41