44
55namespace Rector \PhpParser \NodeTraverser ;
66
7+ use LogicException ;
8+ use PhpParser \Node \Stmt ;
9+ use PhpParser \Node \Expr ;
710use PhpParser \Node ;
811use PhpParser \NodeTraverserInterface ;
912use PhpParser \NodeVisitor ;
@@ -65,7 +68,7 @@ public function addVisitor(NodeVisitor $visitor): void
6568 */
6669 public function removeVisitor (NodeVisitor $ visitor ): void
6770 {
68- $ index = array_search ($ visitor , $ this ->visitors );
71+ $ index = array_search ($ visitor , $ this ->visitors , true );
6972 if ($ index !== false ) {
7073 array_splice ($ this ->visitors , $ index , 1 , []);
7174 }
@@ -86,20 +89,22 @@ public function traverse(array $nodes): array
8689 $ nodes = $ return ;
8790 }
8891 }
92+
8993 $ nodes = $ this ->traverseArray ($ nodes );
9094 for ($ i = \count ($ this ->visitors ) - 1 ; $ i >= 0 ; --$ i ) {
9195 $ visitor = $ this ->visitors [$ i ];
9296 if (null !== $ return = $ visitor ->afterTraverse ($ nodes )) {
9397 $ nodes = $ return ;
9498 }
9599 }
100+
96101 return $ nodes ;
97102 }
98103
99104 /**
100105 * @return NodeVisitor[]
101106 */
102- abstract public function getVisitorsForNode (\ PhpParser \ Node $ node ): array ;
107+ abstract public function getVisitorsForNode (Node $ node ): array ;
103108
104109 /**
105110 * Recursively traverse a node.
@@ -115,11 +120,14 @@ protected function traverseNode(Node $node): void
115120 if ($ this ->stopTraversal ) {
116121 break ;
117122 }
123+
118124 continue ;
119125 }
126+
120127 if (! $ subNode instanceof Node) {
121128 continue ;
122129 }
130+
123131 $ traverseChildren = true ;
124132 $ visitorIndex = -1 ;
125133 $ currentNodeVisitors = $ this ->getVisitorsForNode ($ subNode );
@@ -128,7 +136,8 @@ protected function traverseNode(Node $node): void
128136 if ($ return !== null ) {
129137 if ($ return instanceof Node) {
130138 $ this ->ensureReplacementReasonable ($ subNode , $ return );
131- $ subNode = $ node ->{$ name } = $ return ;
139+ $ subNode = $ return ;
140+ $ node ->{$ name } = $ return ;
132141 } elseif ($ return === NodeVisitor::DONT_TRAVERSE_CHILDREN ) {
133142 $ traverseChildren = false ;
134143 } elseif ($ return === NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN ) {
@@ -141,35 +150,38 @@ protected function traverseNode(Node $node): void
141150 $ node ->{$ name } = null ;
142151 continue 2 ;
143152 } else {
144- throw new \ LogicException ('enterNode() returned invalid value of type ' . gettype ($ return ));
153+ throw new LogicException ('enterNode() returned invalid value of type ' . gettype ($ return ));
145154 }
146155 }
147156 }
157+
148158 if ($ traverseChildren ) {
149159 $ this ->traverseNode ($ subNode );
150160 if ($ this ->stopTraversal ) {
151161 break ;
152162 }
153163 }
164+
154165 for (; $ visitorIndex >= 0 ; --$ visitorIndex ) {
155166 $ visitor = $ currentNodeVisitors [$ visitorIndex ];
156167 $ return = $ visitor ->leaveNode ($ subNode );
157168 if ($ return !== null ) {
158169 if ($ return instanceof Node) {
159170 $ this ->ensureReplacementReasonable ($ subNode , $ return );
160- $ subNode = $ node ->{$ name } = $ return ;
171+ $ subNode = $ return ;
172+ $ node ->{$ name } = $ return ;
161173 } elseif ($ return === NodeVisitor::STOP_TRAVERSAL ) {
162174 $ this ->stopTraversal = true ;
163175 break 2 ;
164176 } elseif ($ return === NodeVisitor::REPLACE_WITH_NULL ) {
165177 $ node ->{$ name } = null ;
166178 break ;
167179 } elseif (\is_array ($ return )) {
168- throw new \ LogicException (
169- 'leaveNode() may only return an array ' . ' if the parent structure is an array '
180+ throw new LogicException (
181+ 'leaveNode() may only return an array if the parent structure is an array '
170182 );
171183 } else {
172- throw new \ LogicException ('leaveNode() returned invalid value of type ' . gettype ($ return ));
184+ throw new LogicException ('leaveNode() returned invalid value of type ' . gettype ($ return ));
173185 }
174186 }
175187 }
@@ -179,7 +191,7 @@ protected function traverseNode(Node $node): void
179191 /**
180192 * Recursively traverse array (usually of nodes).
181193 *
182- * @param array $nodes Array to traverse
194+ * @param Node[] $nodes Array to traverse
183195 *
184196 * @return array Result of traversal (may be original array or changed one)
185197 */
@@ -189,10 +201,12 @@ protected function traverseArray(array $nodes): array
189201 foreach ($ nodes as $ i => $ node ) {
190202 if (! $ node instanceof Node) {
191203 if (\is_array ($ node )) {
192- throw new \ LogicException ('Invalid node structure: Contains nested arrays ' );
204+ throw new LogicException ('Invalid node structure: Contains nested arrays ' );
193205 }
206+
194207 continue ;
195208 }
209+
196210 $ traverseChildren = true ;
197211 $ visitorIndex = -1 ;
198212 $ currentNodeVisitors = $ this ->getVisitorsForNode ($ node );
@@ -217,20 +231,22 @@ protected function traverseArray(array $nodes): array
217231 $ this ->stopTraversal = true ;
218232 break 2 ;
219233 } elseif ($ return === NodeVisitor::REPLACE_WITH_NULL ) {
220- throw new \ LogicException (
234+ throw new LogicException (
221235 'REPLACE_WITH_NULL can not be used if the parent structure is an array '
222236 );
223237 } else {
224- throw new \ LogicException ('enterNode() returned invalid value of type ' . gettype ($ return ));
238+ throw new LogicException ('enterNode() returned invalid value of type ' . gettype ($ return ));
225239 }
226240 }
227241 }
242+
228243 if ($ traverseChildren ) {
229244 $ this ->traverseNode ($ node );
230245 if ($ this ->stopTraversal ) {
231246 break ;
232247 }
233248 }
249+
234250 for (; $ visitorIndex >= 0 ; --$ visitorIndex ) {
235251 $ visitor = $ currentNodeVisitors [$ visitorIndex ];
236252 $ return = $ visitor ->leaveNode ($ node );
@@ -248,33 +264,36 @@ protected function traverseArray(array $nodes): array
248264 $ this ->stopTraversal = true ;
249265 break 2 ;
250266 } elseif ($ return === NodeVisitor::REPLACE_WITH_NULL ) {
251- throw new \ LogicException (
267+ throw new LogicException (
252268 'REPLACE_WITH_NULL can not be used if the parent structure is an array '
253269 );
254270 } else {
255- throw new \ LogicException ('leaveNode() returned invalid value of type ' . gettype ($ return ));
271+ throw new LogicException ('leaveNode() returned invalid value of type ' . gettype ($ return ));
256272 }
257273 }
258274 }
259275 }
260- if (! empty ($ doNodes )) {
261- while (list ($ i , $ replace ) = array_pop ($ doNodes )) {
276+
277+ if ($ doNodes !== []) {
278+ while ([$ i , $ replace ] = array_pop ($ doNodes )) {
262279 array_splice ($ nodes , $ i , 1 , $ replace );
263280 }
264281 }
282+
265283 return $ nodes ;
266284 }
267285
268286 private function ensureReplacementReasonable (Node $ old , Node $ new ): void
269287 {
270- if ($ old instanceof Node \ Stmt && $ new instanceof Node \ Expr) {
271- throw new \ LogicException (
272- " Trying to replace statement ( { $ old ->getType ()} ) " . " with expression ( { $ new -> getType ()} ) . Are you missing a " . 'Stmt_Expression wrapper? '
288+ if ($ old instanceof Stmt && $ new instanceof Expr) {
289+ throw new LogicException (
290+ sprintf ( ' Trying to replace statement (%s) ' , $ old ->getType ()) . sprintf ( ' with expression (%s) . Are you missing a ' , $ new -> getType ()) . 'Stmt_Expression wrapper? '
273291 );
274292 }
275- if ($ old instanceof Node \Expr && $ new instanceof Node \Stmt) {
276- throw new \LogicException (
277- "Trying to replace expression ( {$ old ->getType ()}) " . "with statement ( {$ new ->getType ()}) "
293+
294+ if ($ old instanceof Expr && $ new instanceof Stmt) {
295+ throw new LogicException (
296+ sprintf ('Trying to replace expression (%s) ' , $ old ->getType ()) . sprintf ('with statement (%s) ' , $ new ->getType ())
278297 );
279298 }
280299 }
0 commit comments