From 979f483c1c9b40514b6f60cce20d0daba4a671c2 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Thu, 11 Jun 2026 23:15:22 +0700 Subject: [PATCH 1/2] [CodeQuality] Handle crash on custom exception not autoloaded on ThrowWithPreviousExceptionRector --- ...ip_custom_exception_not_autoloaded.php.inc | 23 +++++++++++++++++++ .../ThrowWithPreviousExceptionRector.php | 19 +++++++++++---- 2 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 rules-tests/CodeQuality/Rector/Catch_/ThrowWithPreviousExceptionRector/Fixture/skip_custom_exception_not_autoloaded.php.inc diff --git a/rules-tests/CodeQuality/Rector/Catch_/ThrowWithPreviousExceptionRector/Fixture/skip_custom_exception_not_autoloaded.php.inc b/rules-tests/CodeQuality/Rector/Catch_/ThrowWithPreviousExceptionRector/Fixture/skip_custom_exception_not_autoloaded.php.inc new file mode 100644 index 00000000000..f47a23b6aca --- /dev/null +++ b/rules-tests/CodeQuality/Rector/Catch_/ThrowWithPreviousExceptionRector/Fixture/skip_custom_exception_not_autoloaded.php.inc @@ -0,0 +1,23 @@ + diff --git a/rules/CodeQuality/Rector/Catch_/ThrowWithPreviousExceptionRector.php b/rules/CodeQuality/Rector/Catch_/ThrowWithPreviousExceptionRector.php index ba36f610026..fea0438410d 100644 --- a/rules/CodeQuality/Rector/Catch_/ThrowWithPreviousExceptionRector.php +++ b/rules/CodeQuality/Rector/Catch_/ThrowWithPreviousExceptionRector.php @@ -102,8 +102,13 @@ public function refactor(Node $node): ?Node return null; } - $isChanged = $this->refactorThrow($node, $caughtThrowableVariable); - return $isChanged; + $result = $this->refactorThrow($node, $caughtThrowableVariable); + if ($result === null) { + return null; + } + + $isChanged = true; + return $result; }); if (! (bool) $isChanged) { @@ -142,6 +147,12 @@ private function refactorThrow(Throw_ $throw, Variable $caughtThrowableVariable) $messageArgument = $new->args[0] ?? null; $shouldUseNamedArguments = $messageArgument instanceof Arg && $messageArgument->name instanceof Identifier; + $hasCodeParameter = $this->hasParameter($new, 'code'); + $hasCodeArgument = $this->hasArgument($new, 'code'); + if (! isset($new->getArgs()[1]) && (! $hasCodeParameter || $hasCodeArgument)) { + return null; + } + $hasChanged = false; if (! isset($new->args[0])) { // get previous message @@ -158,7 +169,7 @@ private function refactorThrow(Throw_ $throw, Variable $caughtThrowableVariable) } if (! isset($new->getArgs()[1])) { - if ($this->hasParameter($new, 'code') && ! $this->hasArgument($new, 'code')) { + if ($hasCodeParameter && ! $hasCodeArgument) { // get previous code $new->args[1] = new Arg( new MethodCall($caughtThrowableVariable, 'getCode'), @@ -173,7 +184,7 @@ private function refactorThrow(Throw_ $throw, Variable $caughtThrowableVariable) /** @var Arg $arg1 */ $arg1 = $new->args[1]; if ($arg1->name instanceof Identifier && $arg1->name->toString() === 'previous') { - if ($this->hasParameter($new, 'code') && ! $this->hasArgument($new, 'code')) { + if ($hasCodeParameter && ! $hasCodeArgument) { $new->args[1] = new Arg( new MethodCall($caughtThrowableVariable, 'getCode'), name: $shouldUseNamedArguments ? new Identifier('code') : null From 6f68295f42f4148dd0029843899c6a3a54706163 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Thu, 11 Jun 2026 23:19:48 +0700 Subject: [PATCH 2/2] rectify --- .../Rector/Catch_/ThrowWithPreviousExceptionRector.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rules/CodeQuality/Rector/Catch_/ThrowWithPreviousExceptionRector.php b/rules/CodeQuality/Rector/Catch_/ThrowWithPreviousExceptionRector.php index fea0438410d..8dd09b84655 100644 --- a/rules/CodeQuality/Rector/Catch_/ThrowWithPreviousExceptionRector.php +++ b/rules/CodeQuality/Rector/Catch_/ThrowWithPreviousExceptionRector.php @@ -111,7 +111,7 @@ public function refactor(Node $node): ?Node return $result; }); - if (! (bool) $isChanged) { + if (! $isChanged) { return null; }