Completed
Pull Request — master (#38)
by Pádraic
02:51
created

ParentConnectorVisitor::beforeTraverse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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