NodeTraverser   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 102
Duplicated Lines 9.8 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 10
loc 102
c 0
b 0
f 0
wmc 23
lcom 1
cbo 1
ccs 0
cts 67
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A addVisitor() 0 4 1
A removeVisitor() 0 9 3
B traverse() 10 18 5
C traverseArray() 0 44 14

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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