NodeTraverser::traverseArray()   C
last analyzed

Complexity

Conditions 14
Paths 70

Size

Total Lines 44
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 210

Importance

Changes 0
Metric Value
dl 0
loc 44
c 0
b 0
f 0
ccs 0
cts 39
cp 0
rs 5.0864
cc 14
eloc 28
nc 70
nop 1
crap 210

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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