NodeTraverser.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php declare(strict_types=1);
  2. namespace PhpParser;
  3. class NodeTraverser implements NodeTraverserInterface {
  4. /**
  5. * @deprecated Use NodeVisitor::DONT_TRAVERSE_CHILDREN instead.
  6. */
  7. public const DONT_TRAVERSE_CHILDREN = NodeVisitor::DONT_TRAVERSE_CHILDREN;
  8. /**
  9. * @deprecated Use NodeVisitor::STOP_TRAVERSAL instead.
  10. */
  11. public const STOP_TRAVERSAL = NodeVisitor::STOP_TRAVERSAL;
  12. /**
  13. * @deprecated Use NodeVisitor::REMOVE_NODE instead.
  14. */
  15. public const REMOVE_NODE = NodeVisitor::REMOVE_NODE;
  16. /**
  17. * @deprecated Use NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN instead.
  18. */
  19. public const DONT_TRAVERSE_CURRENT_AND_CHILDREN = NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
  20. /** @var list<NodeVisitor> Visitors */
  21. protected array $visitors = [];
  22. /** @var bool Whether traversal should be stopped */
  23. protected bool $stopTraversal;
  24. /**
  25. * Create a traverser with the given visitors.
  26. *
  27. * @param NodeVisitor ...$visitors Node visitors
  28. */
  29. public function __construct(NodeVisitor ...$visitors) {
  30. $this->visitors = $visitors;
  31. }
  32. /**
  33. * Adds a visitor.
  34. *
  35. * @param NodeVisitor $visitor Visitor to add
  36. */
  37. public function addVisitor(NodeVisitor $visitor): void {
  38. $this->visitors[] = $visitor;
  39. }
  40. /**
  41. * Removes an added visitor.
  42. */
  43. public function removeVisitor(NodeVisitor $visitor): void {
  44. $index = array_search($visitor, $this->visitors);
  45. if ($index !== false) {
  46. array_splice($this->visitors, $index, 1, []);
  47. }
  48. }
  49. /**
  50. * Traverses an array of nodes using the registered visitors.
  51. *
  52. * @param Node[] $nodes Array of nodes
  53. *
  54. * @return Node[] Traversed array of nodes
  55. */
  56. public function traverse(array $nodes): array {
  57. $this->stopTraversal = false;
  58. foreach ($this->visitors as $visitor) {
  59. if (null !== $return = $visitor->beforeTraverse($nodes)) {
  60. $nodes = $return;
  61. }
  62. }
  63. $nodes = $this->traverseArray($nodes);
  64. for ($i = \count($this->visitors) - 1; $i >= 0; --$i) {
  65. $visitor = $this->visitors[$i];
  66. if (null !== $return = $visitor->afterTraverse($nodes)) {
  67. $nodes = $return;
  68. }
  69. }
  70. return $nodes;
  71. }
  72. /**
  73. * Recursively traverse a node.
  74. *
  75. * @param Node $node Node to traverse.
  76. */
  77. protected function traverseNode(Node $node): void {
  78. foreach ($node->getSubNodeNames() as $name) {
  79. $subNode = $node->$name;
  80. if (\is_array($subNode)) {
  81. $node->$name = $this->traverseArray($subNode);
  82. if ($this->stopTraversal) {
  83. break;
  84. }
  85. } elseif ($subNode instanceof Node) {
  86. $traverseChildren = true;
  87. $visitorIndex = -1;
  88. foreach ($this->visitors as $visitorIndex => $visitor) {
  89. $return = $visitor->enterNode($subNode);
  90. if (null !== $return) {
  91. if ($return instanceof Node) {
  92. $this->ensureReplacementReasonable($subNode, $return);
  93. $subNode = $node->$name = $return;
  94. } elseif (NodeVisitor::DONT_TRAVERSE_CHILDREN === $return) {
  95. $traverseChildren = false;
  96. } elseif (NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN === $return) {
  97. $traverseChildren = false;
  98. break;
  99. } elseif (NodeVisitor::STOP_TRAVERSAL === $return) {
  100. $this->stopTraversal = true;
  101. break 2;
  102. } elseif (NodeVisitor::REPLACE_WITH_NULL === $return) {
  103. $node->$name = null;
  104. continue 2;
  105. } else {
  106. throw new \LogicException(
  107. 'enterNode() returned invalid value of type ' . gettype($return)
  108. );
  109. }
  110. }
  111. }
  112. if ($traverseChildren) {
  113. $this->traverseNode($subNode);
  114. if ($this->stopTraversal) {
  115. break;
  116. }
  117. }
  118. for (; $visitorIndex >= 0; --$visitorIndex) {
  119. $visitor = $this->visitors[$visitorIndex];
  120. $return = $visitor->leaveNode($subNode);
  121. if (null !== $return) {
  122. if ($return instanceof Node) {
  123. $this->ensureReplacementReasonable($subNode, $return);
  124. $subNode = $node->$name = $return;
  125. } elseif (NodeVisitor::STOP_TRAVERSAL === $return) {
  126. $this->stopTraversal = true;
  127. break 2;
  128. } elseif (NodeVisitor::REPLACE_WITH_NULL === $return) {
  129. $node->$name = null;
  130. break;
  131. } elseif (\is_array($return)) {
  132. throw new \LogicException(
  133. 'leaveNode() may only return an array ' .
  134. 'if the parent structure is an array'
  135. );
  136. } else {
  137. throw new \LogicException(
  138. 'leaveNode() returned invalid value of type ' . gettype($return)
  139. );
  140. }
  141. }
  142. }
  143. }
  144. }
  145. }
  146. /**
  147. * Recursively traverse array (usually of nodes).
  148. *
  149. * @param array $nodes Array to traverse
  150. *
  151. * @return array Result of traversal (may be original array or changed one)
  152. */
  153. protected function traverseArray(array $nodes): array {
  154. $doNodes = [];
  155. foreach ($nodes as $i => $node) {
  156. if ($node instanceof Node) {
  157. $traverseChildren = true;
  158. $visitorIndex = -1;
  159. foreach ($this->visitors as $visitorIndex => $visitor) {
  160. $return = $visitor->enterNode($node);
  161. if (null !== $return) {
  162. if ($return instanceof Node) {
  163. $this->ensureReplacementReasonable($node, $return);
  164. $nodes[$i] = $node = $return;
  165. } elseif (\is_array($return)) {
  166. $doNodes[] = [$i, $return];
  167. continue 2;
  168. } elseif (NodeVisitor::REMOVE_NODE === $return) {
  169. $doNodes[] = [$i, []];
  170. continue 2;
  171. } elseif (NodeVisitor::DONT_TRAVERSE_CHILDREN === $return) {
  172. $traverseChildren = false;
  173. } elseif (NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN === $return) {
  174. $traverseChildren = false;
  175. break;
  176. } elseif (NodeVisitor::STOP_TRAVERSAL === $return) {
  177. $this->stopTraversal = true;
  178. break 2;
  179. } elseif (NodeVisitor::REPLACE_WITH_NULL === $return) {
  180. throw new \LogicException(
  181. 'REPLACE_WITH_NULL can not be used if the parent structure is an array');
  182. } else {
  183. throw new \LogicException(
  184. 'enterNode() returned invalid value of type ' . gettype($return)
  185. );
  186. }
  187. }
  188. }
  189. if ($traverseChildren) {
  190. $this->traverseNode($node);
  191. if ($this->stopTraversal) {
  192. break;
  193. }
  194. }
  195. for (; $visitorIndex >= 0; --$visitorIndex) {
  196. $visitor = $this->visitors[$visitorIndex];
  197. $return = $visitor->leaveNode($node);
  198. if (null !== $return) {
  199. if ($return instanceof Node) {
  200. $this->ensureReplacementReasonable($node, $return);
  201. $nodes[$i] = $node = $return;
  202. } elseif (\is_array($return)) {
  203. $doNodes[] = [$i, $return];
  204. break;
  205. } elseif (NodeVisitor::REMOVE_NODE === $return) {
  206. $doNodes[] = [$i, []];
  207. break;
  208. } elseif (NodeVisitor::STOP_TRAVERSAL === $return) {
  209. $this->stopTraversal = true;
  210. break 2;
  211. } elseif (NodeVisitor::REPLACE_WITH_NULL === $return) {
  212. throw new \LogicException(
  213. 'REPLACE_WITH_NULL can not be used if the parent structure is an array');
  214. } else {
  215. throw new \LogicException(
  216. 'leaveNode() returned invalid value of type ' . gettype($return)
  217. );
  218. }
  219. }
  220. }
  221. } elseif (\is_array($node)) {
  222. throw new \LogicException('Invalid node structure: Contains nested arrays');
  223. }
  224. }
  225. if (!empty($doNodes)) {
  226. while (list($i, $replace) = array_pop($doNodes)) {
  227. array_splice($nodes, $i, 1, $replace);
  228. }
  229. }
  230. return $nodes;
  231. }
  232. private function ensureReplacementReasonable(Node $old, Node $new): void {
  233. if ($old instanceof Node\Stmt && $new instanceof Node\Expr) {
  234. throw new \LogicException(
  235. "Trying to replace statement ({$old->getType()}) " .
  236. "with expression ({$new->getType()}). Are you missing a " .
  237. "Stmt_Expression wrapper?"
  238. );
  239. }
  240. if ($old instanceof Node\Expr && $new instanceof Node\Stmt) {
  241. throw new \LogicException(
  242. "Trying to replace expression ({$old->getType()}) " .
  243. "with statement ({$new->getType()})"
  244. );
  245. }
  246. }
  247. }