Conditions | 19 |
Paths | 26 |
Total Lines | 89 |
Code Lines | 54 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
71 | public function traverse(array $nodes, VisitorContext $context = null): array |
||
72 | { |
||
73 | $context = $context ?? new VisitorContext(); |
||
74 | |||
75 | $ctx = clone $context; |
||
76 | foreach ($nodes as $index => $node) { |
||
77 | if ($this->stopTraversal) { |
||
78 | break; |
||
79 | } |
||
80 | |||
81 | $traverseChildren = true; |
||
82 | $breakVisitorID = null; |
||
83 | |||
84 | if ($node instanceof NodeInterface) { |
||
85 | $ctx = $context->withNode($node); |
||
86 | } |
||
87 | |||
88 | foreach ($this->visitors as $visitorID => $visitor) { |
||
89 | $result = $visitor->enterNode($node, $ctx); |
||
90 | |||
91 | switch (true) { |
||
92 | case $result === null: |
||
93 | break; |
||
94 | |||
95 | case $result instanceof NodeInterface: |
||
96 | $node = $result; |
||
97 | break; |
||
98 | |||
99 | case $result === VisitorInterface::DONT_TRAVERSE_CHILDREN: |
||
100 | $traverseChildren = false; |
||
101 | break; |
||
102 | |||
103 | case $result === VisitorInterface::DONT_TRAVERSE_CURRENT_AND_CHILDREN: |
||
104 | $traverseChildren = false; |
||
105 | $breakVisitorID = $visitorID; |
||
106 | break 2; |
||
107 | |||
108 | case $result === VisitorInterface::STOP_TRAVERSAL: |
||
109 | $this->stopTraversal = true; |
||
110 | |||
111 | break 3; |
||
112 | |||
113 | default: |
||
114 | throw new \LogicException( |
||
115 | 'enterNode() returned invalid value of type ' . gettype($result) |
||
116 | ); |
||
117 | } |
||
118 | } |
||
119 | |||
120 | // sub nodes |
||
121 | if ($traverseChildren && $node instanceof NodeInterface) { |
||
122 | $nodes[$index] = $this->traverseNode($node, $ctx); |
||
123 | if ($this->stopTraversal) { |
||
124 | break; |
||
125 | } |
||
126 | } |
||
127 | |||
128 | foreach ($this->visitors as $visitorID => $visitor) { |
||
129 | $result = $visitor->leaveNode($node, $ctx); |
||
130 | |||
131 | switch (true) { |
||
132 | case $result === null: |
||
133 | break; |
||
134 | |||
135 | case $result instanceof NodeInterface: |
||
136 | $nodes[$index] = $result; |
||
137 | break; |
||
138 | |||
139 | case $result === VisitorInterface::REMOVE_NODE: |
||
140 | unset($nodes[$index]); |
||
141 | break; |
||
142 | |||
143 | case $result === VisitorInterface::STOP_TRAVERSAL: |
||
144 | $this->stopTraversal = true; |
||
145 | break 3; |
||
146 | |||
147 | default: |
||
148 | throw new \LogicException( |
||
149 | 'leaveNode() returned invalid value of type ' . gettype($result) |
||
150 | ); |
||
151 | } |
||
152 | |||
153 | if ($breakVisitorID === $visitorID) { |
||
154 | break; |
||
155 | } |
||
156 | } |
||
157 | } |
||
158 | |||
159 | return array_values($nodes); |
||
160 | } |
||
259 |