From 00addcd0d87c9b9fe480c7a69e75e41d05ea6c2f Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Thu, 23 Oct 2025 13:55:11 +0200 Subject: [PATCH 1/2] add fixture for missed exception rename --- .../identical_runs_after_each_other.php.inc | 53 +++++++++++++++++++ .../Source/Notification.php | 7 +++ .../Source/Notification.php | 10 ++++ 3 files changed, 70 insertions(+) create mode 100644 rules-tests/CodingStyle/Rector/Catch_/CatchExceptionNameMatchingTypeRector/Fixture/identical_runs_after_each_other.php.inc create mode 100644 rules-tests/CodingStyle/Rector/Catch_/CatchExceptionNameMatchingTypeRector/Source/Notification.php create mode 100644 rules-tests/Rector/Tests/CodingStyle/Rector/Catch_/CatchExceptionNameMatchingTypeRector/Source/Notification.php diff --git a/rules-tests/CodingStyle/Rector/Catch_/CatchExceptionNameMatchingTypeRector/Fixture/identical_runs_after_each_other.php.inc b/rules-tests/CodingStyle/Rector/Catch_/CatchExceptionNameMatchingTypeRector/Fixture/identical_runs_after_each_other.php.inc new file mode 100644 index 00000000000..8b9db4ef986 --- /dev/null +++ b/rules-tests/CodingStyle/Rector/Catch_/CatchExceptionNameMatchingTypeRector/Fixture/identical_runs_after_each_other.php.inc @@ -0,0 +1,53 @@ +exception($e); + } + + try { + } catch (Throwable $e) { + $notification = new Notification(); + $notification->exception($e); + } + } +} + +?> +----- +exception($throwable); + } + + try { + } catch (Throwable $throwable) { + $notification = new Notification(); + $notification->exception($throwable); + } + } +} + +?> diff --git a/rules-tests/CodingStyle/Rector/Catch_/CatchExceptionNameMatchingTypeRector/Source/Notification.php b/rules-tests/CodingStyle/Rector/Catch_/CatchExceptionNameMatchingTypeRector/Source/Notification.php new file mode 100644 index 00000000000..32449cf442d --- /dev/null +++ b/rules-tests/CodingStyle/Rector/Catch_/CatchExceptionNameMatchingTypeRector/Source/Notification.php @@ -0,0 +1,7 @@ + Date: Thu, 23 Oct 2025 14:17:08 +0200 Subject: [PATCH 2/2] Fix next catch variable override in CatchExceptionNameMatchingTypeRector --- .../CatchExceptionNameMatchingTypeRector.php | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/rules/CodingStyle/Rector/Catch_/CatchExceptionNameMatchingTypeRector.php b/rules/CodingStyle/Rector/Catch_/CatchExceptionNameMatchingTypeRector.php index 7bce0c68f87..2eeb231a608 100644 --- a/rules/CodingStyle/Rector/Catch_/CatchExceptionNameMatchingTypeRector.php +++ b/rules/CodingStyle/Rector/Catch_/CatchExceptionNameMatchingTypeRector.php @@ -130,6 +130,7 @@ public function refactor(Node $node): ?Node } $catch->var = new Variable($newVariableName); + $this->renameVariableInStmts( $catch, $oldVariableName, @@ -149,6 +150,23 @@ public function refactor(Node $node): ?Node return null; } + private function shouldSkipFollowingCatch(Catch_ $catch, string $newVariableName, string $oldVariableName): bool + { + if ($catch->var instanceof Variable) { + $nextCatchVariableName = $this->getName($catch->var); + + return in_array($nextCatchVariableName, [$newVariableName, $oldVariableName], true); + } + + if (count($catch->types) === 1) { + $soleType = $catch->types[0]->toString(); + + return lcfirst($soleType) === $newVariableName; + } + + return false; + } + private function resolveNewVariableName(string $typeShortName): string { return Strings::replace( @@ -229,12 +247,17 @@ private function replaceNextUsageVariable( $this->traverseNodesWithCallable($nextNode, function (Node $node) use ( $oldVariableName, + $newVariableName, &$nonAssignedVariables ): ?int { if ($node instanceof Assign && $node->var instanceof Variable) { return NodeVisitor::STOP_TRAVERSAL; } + if ($node instanceof Catch_ && $this->shouldSkipFollowingCatch($node, $newVariableName, $oldVariableName)) { + return NodeVisitor::STOP_TRAVERSAL; + } + if (! $node instanceof Variable) { return null; }