diff --git a/rules-tests/CodeQuality/Rector/Catch_/ThrowWithPreviousExceptionRector/Fixture/named_argument.php.inc b/rules-tests/CodeQuality/Rector/Catch_/ThrowWithPreviousExceptionRector/Fixture/named_argument.php.inc new file mode 100644 index 00000000000..792b460802d --- /dev/null +++ b/rules-tests/CodeQuality/Rector/Catch_/ThrowWithPreviousExceptionRector/Fixture/named_argument.php.inc @@ -0,0 +1,35 @@ +run(); + }catch(\Throwable $throwable) { + throw new \LogicException('Some exception', previous: $throwable); + } + } +} + +?> +----- +run(); + }catch(\Throwable $throwable) { + throw new \LogicException('Some exception', $throwable->getCode(), previous: $throwable); + } + } +} + +?> diff --git a/rules-tests/CodeQuality/Rector/Catch_/ThrowWithPreviousExceptionRector/Fixture/named_argument_for_message_and_previous.php.inc b/rules-tests/CodeQuality/Rector/Catch_/ThrowWithPreviousExceptionRector/Fixture/named_argument_for_message_and_previous.php.inc new file mode 100644 index 00000000000..288f9996267 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/Catch_/ThrowWithPreviousExceptionRector/Fixture/named_argument_for_message_and_previous.php.inc @@ -0,0 +1,39 @@ +run(); + }catch(\Throwable $throwable) { + throw new LogicException(message: 'Some exception', previous: $throwable); + } + } +} + +?> +----- +run(); + }catch(\Throwable $throwable) { + throw new LogicException(message: 'Some exception', code: $throwable->getCode(), previous: $throwable); + } + } +} + +?> diff --git a/rules-tests/CodeQuality/Rector/Catch_/ThrowWithPreviousExceptionRector/Fixture/skip_named_argument.php.inc b/rules-tests/CodeQuality/Rector/Catch_/ThrowWithPreviousExceptionRector/Fixture/skip_named_argument.php.inc deleted file mode 100644 index 1e3f425dde8..00000000000 --- a/rules-tests/CodeQuality/Rector/Catch_/ThrowWithPreviousExceptionRector/Fixture/skip_named_argument.php.inc +++ /dev/null @@ -1,15 +0,0 @@ -run(); - }catch(\Throwable $throwable) { - throw new \LogicException('Some exception', previous: $throwable); - } - } -} diff --git a/rules-tests/CodeQuality/Rector/Catch_/ThrowWithPreviousExceptionRector/Fixture/skip_named_argument_for_message_and_previous.php.inc b/rules-tests/CodeQuality/Rector/Catch_/ThrowWithPreviousExceptionRector/Fixture/skip_named_argument_for_message_and_previous.php.inc deleted file mode 100644 index deb1e100b69..00000000000 --- a/rules-tests/CodeQuality/Rector/Catch_/ThrowWithPreviousExceptionRector/Fixture/skip_named_argument_for_message_and_previous.php.inc +++ /dev/null @@ -1,17 +0,0 @@ -run(); - }catch(\Throwable $throwable) { - throw new LogicException(message: 'Some exception', previous: $throwable); - } - } -} diff --git a/rules/CodeQuality/Rector/Catch_/ThrowWithPreviousExceptionRector.php b/rules/CodeQuality/Rector/Catch_/ThrowWithPreviousExceptionRector.php index 90647407aad..73b177e3038 100644 --- a/rules/CodeQuality/Rector/Catch_/ThrowWithPreviousExceptionRector.php +++ b/rules/CodeQuality/Rector/Catch_/ThrowWithPreviousExceptionRector.php @@ -185,8 +185,14 @@ private function refactorThrow(Throw_ $throw, Variable $caughtThrowableVariable) /** @var Arg $arg1 */ $arg1 = $new->args[1]; if ($arg1->name instanceof Identifier && $arg1->name->toString() === 'previous') { - // previous is already passed, code is omitted on purpose - if (! $hasChanged) { + if ($hasCodeParameter && ! $hasCodeArgument) { + $new->args[1] = new Arg( + new MethodCall($caughtThrowableVariable, 'getCode'), + name: $shouldUseNamedArguments ? new Identifier('code') : null + ); + $new->args[$exceptionArgumentPosition] = $arg1; + $hasChanged = true; + } elseif (! $hasChanged) { return null; } } elseif ($this->hasParameter($new, 'previous') && ! $this->hasArgument($new, 'previous')) {