Skip to content

Commit 45f70ed

Browse files
staabmnikic
authored andcommitted
Simplify NodeTraverser->traverseNode()
1 parent 74a3618 commit 45f70ed

File tree

1 file changed

+61
-55
lines changed

1 file changed

+61
-55
lines changed

lib/PhpParser/NodeTraverser.php

+61-55
Original file line numberDiff line numberDiff line change
@@ -99,66 +99,72 @@ protected function traverseNode(Node $node): void {
9999
if ($this->stopTraversal) {
100100
break;
101101
}
102-
} elseif ($subNode instanceof Node) {
103-
$traverseChildren = true;
104-
$visitorIndex = -1;
105-
106-
foreach ($this->visitors as $visitorIndex => $visitor) {
107-
$return = $visitor->enterNode($subNode);
108-
if (null !== $return) {
109-
if ($return instanceof Node) {
110-
$this->ensureReplacementReasonable($subNode, $return);
111-
$subNode = $node->$name = $return;
112-
} elseif (NodeVisitor::DONT_TRAVERSE_CHILDREN === $return) {
113-
$traverseChildren = false;
114-
} elseif (NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN === $return) {
115-
$traverseChildren = false;
116-
break;
117-
} elseif (NodeVisitor::STOP_TRAVERSAL === $return) {
118-
$this->stopTraversal = true;
119-
break 2;
120-
} elseif (NodeVisitor::REPLACE_WITH_NULL === $return) {
121-
$node->$name = null;
122-
continue 2;
123-
} else {
124-
throw new \LogicException(
125-
'enterNode() returned invalid value of type ' . gettype($return)
126-
);
127-
}
128-
}
129-
}
130102

131-
if ($traverseChildren) {
132-
$this->traverseNode($subNode);
133-
if ($this->stopTraversal) {
103+
continue;
104+
}
105+
106+
if (!$subNode instanceof Node) {
107+
continue;
108+
}
109+
110+
$traverseChildren = true;
111+
$visitorIndex = -1;
112+
113+
foreach ($this->visitors as $visitorIndex => $visitor) {
114+
$return = $visitor->enterNode($subNode);
115+
if (null !== $return) {
116+
if ($return instanceof Node) {
117+
$this->ensureReplacementReasonable($subNode, $return);
118+
$subNode = $node->$name = $return;
119+
} elseif (NodeVisitor::DONT_TRAVERSE_CHILDREN === $return) {
120+
$traverseChildren = false;
121+
} elseif (NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN === $return) {
122+
$traverseChildren = false;
134123
break;
124+
} elseif (NodeVisitor::STOP_TRAVERSAL === $return) {
125+
$this->stopTraversal = true;
126+
break 2;
127+
} elseif (NodeVisitor::REPLACE_WITH_NULL === $return) {
128+
$node->$name = null;
129+
continue 2;
130+
} else {
131+
throw new \LogicException(
132+
'enterNode() returned invalid value of type ' . gettype($return)
133+
);
135134
}
136135
}
136+
}
137137

138-
for (; $visitorIndex >= 0; --$visitorIndex) {
139-
$visitor = $this->visitors[$visitorIndex];
140-
$return = $visitor->leaveNode($subNode);
141-
142-
if (null !== $return) {
143-
if ($return instanceof Node) {
144-
$this->ensureReplacementReasonable($subNode, $return);
145-
$subNode = $node->$name = $return;
146-
} elseif (NodeVisitor::STOP_TRAVERSAL === $return) {
147-
$this->stopTraversal = true;
148-
break 2;
149-
} elseif (NodeVisitor::REPLACE_WITH_NULL === $return) {
150-
$node->$name = null;
151-
break;
152-
} elseif (\is_array($return)) {
153-
throw new \LogicException(
154-
'leaveNode() may only return an array ' .
155-
'if the parent structure is an array'
156-
);
157-
} else {
158-
throw new \LogicException(
159-
'leaveNode() returned invalid value of type ' . gettype($return)
160-
);
161-
}
138+
if ($traverseChildren) {
139+
$this->traverseNode($subNode);
140+
if ($this->stopTraversal) {
141+
break;
142+
}
143+
}
144+
145+
for (; $visitorIndex >= 0; --$visitorIndex) {
146+
$visitor = $this->visitors[$visitorIndex];
147+
$return = $visitor->leaveNode($subNode);
148+
149+
if (null !== $return) {
150+
if ($return instanceof Node) {
151+
$this->ensureReplacementReasonable($subNode, $return);
152+
$subNode = $node->$name = $return;
153+
} elseif (NodeVisitor::STOP_TRAVERSAL === $return) {
154+
$this->stopTraversal = true;
155+
break 2;
156+
} elseif (NodeVisitor::REPLACE_WITH_NULL === $return) {
157+
$node->$name = null;
158+
break;
159+
} elseif (\is_array($return)) {
160+
throw new \LogicException(
161+
'leaveNode() may only return an array ' .
162+
'if the parent structure is an array'
163+
);
164+
} else {
165+
throw new \LogicException(
166+
'leaveNode() returned invalid value of type ' . gettype($return)
167+
);
162168
}
163169
}
164170
}

0 commit comments

Comments
 (0)