NodeTraverser::addVisitor()   A
last analyzed

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
dl 0
loc 4
c 0
b 0
f 0
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace tomzx\AbstractParser;
4
5
class NodeTraverser implements NodeTraverserInterface
6
{
7
    /**
8
     * @var \tomzx\AbstractParser\NodeVisitorInterface[]
9
     */
10
    protected $visitors = [];
11
12
    /**
13
     * @param \tomzx\AbstractParser\NodeVisitorInterface $visitor
14
     * @return void
15
     */
16
    public function addVisitor(NodeVisitorInterface $visitor)
17
    {
18
        $this->visitors[] = $visitor;
19
    }
20
21
    /**
22
     * @param \tomzx\AbstractParser\NodeVisitorInterface $visitor
23
     * @return void
24
     */
25
    public function removeVisitor(NodeVisitorInterface $visitor)
26
    {
27
        foreach ($this->visitors as $index => $storedVisitor) {
28
            if ($storedVisitor === $visitor) {
29
                unset($this->visitors[$index]);
30
                return;
31
            }
32
        }
33
    }
34
35
    /**
36
     * @param array $nodes
37
     * @return array
38
     */
39
    public function traverse(array $nodes)
40
    {
41 View Code Duplication
        foreach ($this->visitors as $visitor) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42
            if (($return = $visitor->beforeTraverse($nodes)) !== null) {
43
                $nodes = $return;
44
            }
45
        }
46
47
        $nodes = $this->traverseArray($nodes);
48
49 View Code Duplication
        foreach ($this->visitors as $visitor) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
            if (($return = $visitor->afterTraverse($nodes)) !== null) {
51
                $nodes = $return;
52
            }
53
        }
54
55
        return $nodes;
56
    }
57
58
    /**
59
     * @param array $nodes
60
     * @return array
61
     */
62
    protected function traverseArray(array &$nodes)
63
    {
64
        $replaceNodes = [];
65
        foreach ($nodes as $index => &$node) {
66
            if (is_array($node)) {
67
                $node = $this->traverseArray($node);
68
            } elseif ($node instanceof NodeInterface) {
69
                $traverseChildren = true;
70
                foreach ($this->visitors as $visitor) {
71
                    $return = $visitor->enterNode($node);
72
                    if (self::DONT_TRAVERSE_CHILDREN === $return) {
73
                        $traverseChildren = false;
74
                    } elseif ($return !== null) {
75
                        $node = $return;
76
                    }
77
                }
78
79
                if ($traverseChildren) {
80
                    $this->traverseArray($node->getChildren());
0 ignored issues
show
Bug introduced by
$node->getChildren() cannot be passed to traversearray() as the parameter $nodes expects a reference.
Loading history...
81
                }
82
83
                foreach ($this->visitors as $visitor) {
84
                    $return = $visitor->exitNode($node);
85
86
                    if (self::REMOVE_NODE === $return) {
87
                        $replaceNodes[] = [$index, []];
88
                        break;
89
                    } elseif (is_array($return)) {
90
                        $replaceNodes[] = [$index, $return];
91
                    } elseif ($return !== null) {
92
                        $node = $return;
93
                    }
94
                }
95
            }
96
        }
97
98
        if ( ! empty($replaceNodes)) {
99
            while (list($index, $replace) = array_pop($replaceNodes)) {
100
                array_splice($nodes, $index, 1, $replace);
101
            }
102
        }
103
104
        return $nodes;
105
    }
106
}
107