Skip to content

Commit 74a3618

Browse files
staabmnikic
authored andcommitted
Simplify NodeTraverser->traverseArray()
1 parent 6478c5a commit 74a3618

File tree

1 file changed

+65
-62
lines changed

1 file changed

+65
-62
lines changed

lib/PhpParser/NodeTraverser.php

+65-62
Original file line numberDiff line numberDiff line change
@@ -176,77 +176,80 @@ protected function traverseArray(array $nodes): array {
176176
$doNodes = [];
177177

178178
foreach ($nodes as $i => $node) {
179-
if ($node instanceof Node) {
180-
$traverseChildren = true;
181-
$visitorIndex = -1;
182-
183-
foreach ($this->visitors as $visitorIndex => $visitor) {
184-
$return = $visitor->enterNode($node);
185-
if (null !== $return) {
186-
if ($return instanceof Node) {
187-
$this->ensureReplacementReasonable($node, $return);
188-
$nodes[$i] = $node = $return;
189-
} elseif (\is_array($return)) {
190-
$doNodes[] = [$i, $return];
191-
continue 2;
192-
} elseif (NodeVisitor::REMOVE_NODE === $return) {
193-
$doNodes[] = [$i, []];
194-
continue 2;
195-
} elseif (NodeVisitor::DONT_TRAVERSE_CHILDREN === $return) {
196-
$traverseChildren = false;
197-
} elseif (NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN === $return) {
198-
$traverseChildren = false;
199-
break;
200-
} elseif (NodeVisitor::STOP_TRAVERSAL === $return) {
201-
$this->stopTraversal = true;
202-
break 2;
203-
} elseif (NodeVisitor::REPLACE_WITH_NULL === $return) {
204-
throw new \LogicException(
205-
'REPLACE_WITH_NULL can not be used if the parent structure is an array');
206-
} else {
207-
throw new \LogicException(
208-
'enterNode() returned invalid value of type ' . gettype($return)
209-
);
210-
}
211-
}
179+
if (!$node instanceof Node) {
180+
if (\is_array($node)) {
181+
throw new \LogicException('Invalid node structure: Contains nested arrays');
212182
}
183+
continue;
184+
}
213185

214-
if ($traverseChildren) {
215-
$this->traverseNode($node);
216-
if ($this->stopTraversal) {
186+
$traverseChildren = true;
187+
$visitorIndex = -1;
188+
189+
foreach ($this->visitors as $visitorIndex => $visitor) {
190+
$return = $visitor->enterNode($node);
191+
if (null !== $return) {
192+
if ($return instanceof Node) {
193+
$this->ensureReplacementReasonable($node, $return);
194+
$nodes[$i] = $node = $return;
195+
} elseif (\is_array($return)) {
196+
$doNodes[] = [$i, $return];
197+
continue 2;
198+
} elseif (NodeVisitor::REMOVE_NODE === $return) {
199+
$doNodes[] = [$i, []];
200+
continue 2;
201+
} elseif (NodeVisitor::DONT_TRAVERSE_CHILDREN === $return) {
202+
$traverseChildren = false;
203+
} elseif (NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN === $return) {
204+
$traverseChildren = false;
217205
break;
206+
} elseif (NodeVisitor::STOP_TRAVERSAL === $return) {
207+
$this->stopTraversal = true;
208+
break 2;
209+
} elseif (NodeVisitor::REPLACE_WITH_NULL === $return) {
210+
throw new \LogicException(
211+
'REPLACE_WITH_NULL can not be used if the parent structure is an array');
212+
} else {
213+
throw new \LogicException(
214+
'enterNode() returned invalid value of type ' . gettype($return)
215+
);
218216
}
219217
}
218+
}
220219

221-
for (; $visitorIndex >= 0; --$visitorIndex) {
222-
$visitor = $this->visitors[$visitorIndex];
223-
$return = $visitor->leaveNode($node);
220+
if ($traverseChildren) {
221+
$this->traverseNode($node);
222+
if ($this->stopTraversal) {
223+
break;
224+
}
225+
}
224226

225-
if (null !== $return) {
226-
if ($return instanceof Node) {
227-
$this->ensureReplacementReasonable($node, $return);
228-
$nodes[$i] = $node = $return;
229-
} elseif (\is_array($return)) {
230-
$doNodes[] = [$i, $return];
231-
break;
232-
} elseif (NodeVisitor::REMOVE_NODE === $return) {
233-
$doNodes[] = [$i, []];
234-
break;
235-
} elseif (NodeVisitor::STOP_TRAVERSAL === $return) {
236-
$this->stopTraversal = true;
237-
break 2;
238-
} elseif (NodeVisitor::REPLACE_WITH_NULL === $return) {
239-
throw new \LogicException(
240-
'REPLACE_WITH_NULL can not be used if the parent structure is an array');
241-
} else {
242-
throw new \LogicException(
243-
'leaveNode() returned invalid value of type ' . gettype($return)
244-
);
245-
}
227+
for (; $visitorIndex >= 0; --$visitorIndex) {
228+
$visitor = $this->visitors[$visitorIndex];
229+
$return = $visitor->leaveNode($node);
230+
231+
if (null !== $return) {
232+
if ($return instanceof Node) {
233+
$this->ensureReplacementReasonable($node, $return);
234+
$nodes[$i] = $node = $return;
235+
} elseif (\is_array($return)) {
236+
$doNodes[] = [$i, $return];
237+
break;
238+
} elseif (NodeVisitor::REMOVE_NODE === $return) {
239+
$doNodes[] = [$i, []];
240+
break;
241+
} elseif (NodeVisitor::STOP_TRAVERSAL === $return) {
242+
$this->stopTraversal = true;
243+
break 2;
244+
} elseif (NodeVisitor::REPLACE_WITH_NULL === $return) {
245+
throw new \LogicException(
246+
'REPLACE_WITH_NULL can not be used if the parent structure is an array');
247+
} else {
248+
throw new \LogicException(
249+
'leaveNode() returned invalid value of type ' . gettype($return)
250+
);
246251
}
247252
}
248-
} elseif (\is_array($node)) {
249-
throw new \LogicException('Invalid node structure: Contains nested arrays');
250253
}
251254
}
252255

0 commit comments

Comments
 (0)