NodeTraverser::traverseArray()   C
last analyzed

Complexity

Conditions 14
Paths 70

Size

Total Lines 45
Code Lines 29

Duplication

Lines 8
Ratio 17.78 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 8
loc 45
rs 5.0864
cc 14
eloc 29
nc 70
nop 1

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 PhpParser;
4
5
class NodeTraverser implements NodeTraverserInterface
6
{
7
    /**
8
     * @var NodeVisitor[] Visitors
9
     */
10
    protected $visitors;
11
12
    /**
13
     * @var bool
14
     */
15
    private $cloneNodes;
16
17
    /**
18
     * Constructs a node traverser.
19
     *
20
     * @param bool $cloneNodes Should the traverser clone the nodes when traversing the AST
21
     */
22
    public function __construct($cloneNodes = false) {
23
        $this->visitors = array();
24
        $this->cloneNodes = $cloneNodes;
25
    }
26
27
    /**
28
     * Adds a visitor.
29
     *
30
     * @param NodeVisitor $visitor Visitor to add
31
     */
32
    public function addVisitor(NodeVisitor $visitor) {
33
        $this->visitors[] = $visitor;
34
    }
35
36
    /**
37
     * Removes an added visitor.
38
     *
39
     * @param NodeVisitor $visitor
40
     */
41
    public function removeVisitor(NodeVisitor $visitor) {
42
        foreach ($this->visitors as $index => $storedVisitor) {
43
            if ($storedVisitor === $visitor) {
44
                unset($this->visitors[$index]);
45
                break;
46
            }
47
        }
48
    }
49
50
    /**
51
     * Traverses an array of nodes using the registered visitors.
52
     *
53
     * @param Node[] $nodes Array of nodes
54
     *
55
     * @return Node[] Traversed array of nodes
56
     */
57
    public function traverse(array $nodes) {
58 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...
59
            if (null !== $return = $visitor->beforeTraverse($nodes)) {
60
                $nodes = $return;
61
            }
62
        }
63
64
        $nodes = $this->traverseArray($nodes);
65
66 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...
67
            if (null !== $return = $visitor->afterTraverse($nodes)) {
68
                $nodes = $return;
69
            }
70
        }
71
72
        return $nodes;
73
    }
74
75
    protected function traverseNode(Node $node) {
76
        if ($this->cloneNodes) {
77
            $node = clone $node;
78
        }
79
80
        foreach ($node->getSubNodeNames() as $name) {
81
            $subNode =& $node->$name;
82
83
            if (is_array($subNode)) {
84
                $subNode = $this->traverseArray($subNode);
85
            } elseif ($subNode instanceof Node) {
86
                $traverseChildren = true;
87 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...
88
                    $return = $visitor->enterNode($subNode);
89
                    if (self::DONT_TRAVERSE_CHILDREN === $return) {
90
                        $traverseChildren = false;
91
                    } else if (null !== $return) {
92
                        $subNode = $return;
93
                    }
94
                }
95
96
                if ($traverseChildren) {
97
                    $subNode = $this->traverseNode($subNode);
98
                }
99
100
                foreach ($this->visitors as $visitor) {
101
                    if (null !== $return = $visitor->leaveNode($subNode)) {
102
                        $subNode = $return;
103
                    }
104
                }
105
            }
106
        }
107
108
        return $node;
109
    }
110
111
    protected function traverseArray(array $nodes) {
112
        $doNodes = array();
113
114
        foreach ($nodes as $i => &$node) {
115
            if (is_array($node)) {
116
                $node = $this->traverseArray($node);
117
            } elseif ($node instanceof Node) {
118
                $traverseChildren = true;
119 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...
120
                    $return = $visitor->enterNode($node);
121
                    if (self::DONT_TRAVERSE_CHILDREN === $return) {
122
                        $traverseChildren = false;
123
                    } else if (null !== $return) {
124
                        $node = $return;
125
                    }
126
                }
127
128
                if ($traverseChildren) {
129
                    $node = $this->traverseNode($node);
130
                }
131
132
                foreach ($this->visitors as $visitor) {
133
                    $return = $visitor->leaveNode($node);
134
135
                    if (self::REMOVE_NODE === $return) {
136
                        $doNodes[] = array($i, array());
137
                        break;
138
                    } elseif (is_array($return)) {
139
                        $doNodes[] = array($i, $return);
140
                        break;
141
                    } elseif (null !== $return) {
142
                        $node = $return;
143
                    }
144
                }
145
            }
146
        }
147
148
        if (!empty($doNodes)) {
149
            while (list($i, $replace) = array_pop($doNodes)) {
150
                array_splice($nodes, $i, 1, $replace);
151
            }
152
        }
153
154
        return $nodes;
155
    }
156
}
157