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

ParentNodeVisitor   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 0
cts 11
cp 0
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A beforeTraverse() 0 4 1
A enterNode() 0 7 2
A leaveNode() 0 4 1
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