1 <?php declare(strict_types=1);
5 class NodeTraverser implements NodeTraverserInterface
8 * If NodeVisitor::enterNode() returns DONT_TRAVERSE_CHILDREN, child nodes
9 * of the current node will not be traversed for any visitors.
11 * For subsequent visitors enterNode() will still be called on the current
12 * node and leaveNode() will also be invoked for the current node.
14 const DONT_TRAVERSE_CHILDREN = 1;
17 * If NodeVisitor::enterNode() or NodeVisitor::leaveNode() returns
18 * STOP_TRAVERSAL, traversal is aborted.
20 * The afterTraverse() method will still be invoked.
22 const STOP_TRAVERSAL = 2;
25 * If NodeVisitor::leaveNode() returns REMOVE_NODE for a node that occurs
26 * in an array, it will be removed from the array.
28 * For subsequent visitors leaveNode() will still be invoked for the
31 const REMOVE_NODE = 3;
33 /** @var NodeVisitor[] Visitors */
36 /** @var bool Whether traversal should be stopped */
37 protected $stopTraversal;
40 * Constructs a node traverser.
42 public function __construct() {
49 * @param NodeVisitor $visitor Visitor to add
51 public function addVisitor(NodeVisitor $visitor) {
52 $this->visitors[] = $visitor;
56 * Removes an added visitor.
58 * @param NodeVisitor $visitor
60 public function removeVisitor(NodeVisitor $visitor) {
61 foreach ($this->visitors as $index => $storedVisitor) {
62 if ($storedVisitor === $visitor) {
63 unset($this->visitors[$index]);
70 * Traverses an array of nodes using the registered visitors.
72 * @param Node[] $nodes Array of nodes
74 * @return Node[] Traversed array of nodes
76 public function traverse(array $nodes) : array {
77 $this->stopTraversal = false;
79 foreach ($this->visitors as $visitor) {
80 if (null !== $return = $visitor->beforeTraverse($nodes)) {
85 $nodes = $this->traverseArray($nodes);
87 foreach ($this->visitors as $visitor) {
88 if (null !== $return = $visitor->afterTraverse($nodes)) {
97 * Recursively traverse a node.
99 * @param Node $node Node to traverse.
101 * @return Node Result of traversal (may be original node or new one)
103 protected function traverseNode(Node $node) : Node {
104 foreach ($node->getSubNodeNames() as $name) {
105 $subNode =& $node->$name;
107 if (\is_array($subNode)) {
108 $subNode = $this->traverseArray($subNode);
109 if ($this->stopTraversal) {
112 } elseif ($subNode instanceof Node) {
113 $traverseChildren = true;
114 foreach ($this->visitors as $visitor) {
115 $return = $visitor->enterNode($subNode);
116 if (null !== $return) {
117 if ($return instanceof Node) {
118 $this->ensureReplacementReasonable($subNode, $return);
120 } elseif (self::DONT_TRAVERSE_CHILDREN === $return) {
121 $traverseChildren = false;
122 } elseif (self::STOP_TRAVERSAL === $return) {
123 $this->stopTraversal = true;
126 throw new \LogicException(
127 'enterNode() returned invalid value of type ' . gettype($return)
133 if ($traverseChildren) {
134 $subNode = $this->traverseNode($subNode);
135 if ($this->stopTraversal) {
140 foreach ($this->visitors as $visitor) {
141 $return = $visitor->leaveNode($subNode);
142 if (null !== $return) {
143 if ($return instanceof Node) {
144 $this->ensureReplacementReasonable($subNode, $return);
146 } elseif (self::STOP_TRAVERSAL === $return) {
147 $this->stopTraversal = true;
149 } elseif (\is_array($return)) {
150 throw new \LogicException(
151 'leaveNode() may only return an array ' .
152 'if the parent structure is an array'
155 throw new \LogicException(
156 'leaveNode() returned invalid value of type ' . gettype($return)
168 * Recursively traverse array (usually of nodes).
170 * @param array $nodes Array to traverse
172 * @return array Result of traversal (may be original array or changed one)
174 protected function traverseArray(array $nodes) : array {
177 foreach ($nodes as $i => &$node) {
178 if ($node instanceof Node) {
179 $traverseChildren = true;
180 foreach ($this->visitors as $visitor) {
181 $return = $visitor->enterNode($node);
182 if (null !== $return) {
183 if ($return instanceof Node) {
184 $this->ensureReplacementReasonable($node, $return);
186 } elseif (self::DONT_TRAVERSE_CHILDREN === $return) {
187 $traverseChildren = false;
188 } elseif (self::STOP_TRAVERSAL === $return) {
189 $this->stopTraversal = true;
192 throw new \LogicException(
193 'enterNode() returned invalid value of type ' . gettype($return)
199 if ($traverseChildren) {
200 $node = $this->traverseNode($node);
201 if ($this->stopTraversal) {
206 foreach ($this->visitors as $visitor) {
207 $return = $visitor->leaveNode($node);
208 if (null !== $return) {
209 if ($return instanceof Node) {
210 $this->ensureReplacementReasonable($node, $return);
212 } elseif (\is_array($return)) {
213 $doNodes[] = [$i, $return];
215 } elseif (self::REMOVE_NODE === $return) {
216 $doNodes[] = [$i, []];
218 } elseif (self::STOP_TRAVERSAL === $return) {
219 $this->stopTraversal = true;
221 } elseif (false === $return) {
222 throw new \LogicException(
223 'bool(false) return from leaveNode() no longer supported. ' .
224 'Return NodeTraverser::REMOVE_NODE instead'
227 throw new \LogicException(
228 'leaveNode() returned invalid value of type ' . gettype($return)
233 } elseif (\is_array($node)) {
234 throw new \LogicException('Invalid node structure: Contains nested arrays');
238 if (!empty($doNodes)) {
239 while (list($i, $replace) = array_pop($doNodes)) {
240 array_splice($nodes, $i, 1, $replace);
247 private function ensureReplacementReasonable($old, $new) {
248 if ($old instanceof Node\Stmt && $new instanceof Node\Expr) {
249 throw new \LogicException(
250 "Trying to replace statement ({$old->getType()}) " .
251 "with expression ({$new->getType()}). Are you missing a " .
252 "Stmt_Expression wrapper?"
256 if ($old instanceof Node\Expr && $new instanceof Node\Stmt) {
257 throw new \LogicException(
258 "Trying to replace expression ({$old->getType()}) " .
259 "with statement ({$new->getType()})"