Skip to content

Commit 017857c

Browse files
committed
Fix next catch variable override in CatchExceptionNameMatchingTypeRector
1 parent 00addcd commit 017857c

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

rules/CodingStyle/Rector/Catch_/CatchExceptionNameMatchingTypeRector.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
public 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,19 @@ 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_) {
258+
if ($this->shouldSkipFollowingCatch($node, $newVariableName, $oldVariableName)) {
259+
return NodeVisitor::STOP_TRAVERSAL;
260+
}
261+
}
262+
238263
if (! $node instanceof Variable) {
239264
return null;
240265
}

0 commit comments

Comments
 (0)