Conditions | 18 |
Paths | 16 |
Total Lines | 88 |
Code Lines | 55 |
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 |
||
169 | private function traverseNode(NodeInterface $node, VisitorContext $context): NodeInterface |
||
170 | { |
||
171 | $ctx = clone $context; |
||
172 | foreach ($node as $name => $_) { |
||
173 | $child = &$node->$name; |
||
174 | if (is_array($child)) { |
||
175 | $child = $this->traverse($child, $ctx); |
||
176 | if ($this->stopTraversal) { |
||
177 | break; |
||
178 | } |
||
179 | |||
180 | continue; |
||
181 | } |
||
182 | |||
183 | if (!$child instanceof NodeInterface) { |
||
184 | continue; |
||
185 | } |
||
186 | |||
187 | $ctx = $context->withNode($child); |
||
188 | |||
189 | $traverseChildren = true; |
||
190 | $breakVisitorID = null; |
||
191 | |||
192 | foreach ($this->visitors as $visitorID => $visitor) { |
||
193 | $result = $visitor->enterNode($child, $ctx); |
||
194 | switch (true) { |
||
195 | case $result === null: |
||
196 | break; |
||
197 | |||
198 | case $result instanceof NodeInterface: |
||
199 | $child = $result; |
||
200 | break; |
||
201 | |||
202 | case VisitorInterface::DONT_TRAVERSE_CHILDREN: |
||
203 | $traverseChildren = false; |
||
204 | break; |
||
205 | |||
206 | case VisitorInterface::DONT_TRAVERSE_CURRENT_AND_CHILDREN: |
||
207 | $traverseChildren = false; |
||
208 | $breakVisitorID = $visitorID; |
||
209 | break 2; |
||
210 | |||
211 | case VisitorInterface::STOP_TRAVERSAL: |
||
212 | $this->stopTraversal = true; |
||
213 | break 3; |
||
214 | |||
215 | default: |
||
216 | throw new \LogicException( |
||
217 | 'enterNode() returned invalid value of type ' . gettype($result) |
||
218 | ); |
||
219 | } |
||
220 | } |
||
221 | |||
222 | if ($traverseChildren) { |
||
223 | $child = $this->traverseNode($child, $ctx); |
||
224 | if ($this->stopTraversal) { |
||
225 | break; |
||
226 | } |
||
227 | } |
||
228 | |||
229 | foreach ($this->visitors as $visitorID => $visitor) { |
||
230 | $result = $visitor->leaveNode($child, $ctx); |
||
231 | |||
232 | switch (true) { |
||
233 | case $result === null: |
||
234 | break; |
||
235 | |||
236 | case $result instanceof NodeInterface: |
||
237 | $child = $result; |
||
238 | break; |
||
239 | |||
240 | case $result === VisitorInterface::STOP_TRAVERSAL: |
||
241 | $this->stopTraversal = true; |
||
242 | break 3; |
||
243 | |||
244 | default: |
||
245 | throw new \LogicException( |
||
246 | 'leaveNode() returned invalid value of type ' . gettype($result) |
||
247 | ); |
||
248 | } |
||
249 | |||
250 | if ($breakVisitorID === $visitorID) { |
||
251 | break; |
||
252 | } |
||
253 | } |
||
254 | } |
||
255 | |||
256 | return $node; |
||
257 | } |
||
259 |