@@ -130,6 +130,7 @@ public function refactor(Node $node): ?Node
130130 }
131131
132132 $ catch ->var = new Variable ($ newVariableName );
133+
133134 $ this ->renameVariableInStmts (
134135 $ catch ,
135136 $ oldVariableName ,
@@ -149,6 +150,23 @@ public function refactor(Node $node): ?Node
149150 return null ;
150151 }
151152
153+ private function shouldSkipFollowingCatch (Catch_ $ catch , string $ newVariableName , string $ oldVariableName ): bool
154+ {
155+ if ($ catch ->var instanceof Variable) {
156+ $ nextCatchVariableName = $ this ->getName ($ catch ->var );
157+
158+ return in_array ($ nextCatchVariableName , [$ newVariableName , $ oldVariableName ], true );
159+ }
160+
161+ if (count ($ catch ->types ) === 1 ) {
162+ $ soleType = $ catch ->types [0 ]->toString ();
163+
164+ return lcfirst ($ soleType ) === $ newVariableName ;
165+ }
166+
167+ return false ;
168+ }
169+
152170 private function resolveNewVariableName (string $ typeShortName ): string
153171 {
154172 return Strings::replace (
@@ -229,12 +247,17 @@ private function replaceNextUsageVariable(
229247
230248 $ this ->traverseNodesWithCallable ($ nextNode , function (Node $ node ) use (
231249 $ oldVariableName ,
250+ $ newVariableName ,
232251 &$ nonAssignedVariables
233252 ): ?int {
234253 if ($ node instanceof Assign && $ node ->var instanceof Variable) {
235254 return NodeVisitor::STOP_TRAVERSAL ;
236255 }
237256
257+ if ($ node instanceof Catch_ && $ this ->shouldSkipFollowingCatch ($ node , $ newVariableName , $ oldVariableName )) {
258+ return NodeVisitor::STOP_TRAVERSAL ;
259+ }
260+
238261 if (! $ node instanceof Variable) {
239262 return null ;
240263 }
0 commit comments