From 569777552f29ef7acf3144f7e18ef8d7a20ed86b Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Wed, 25 Nov 2026 00:28:10 +0100 Subject: [PATCH] [CodeQuality] Restore adding code with named previous arg in ThrowWithPreviousExceptionRector --- .../Fixture/named_argument.php.inc | 35 +++++++++++++++++ ..._argument_for_message_and_previous.php.inc | 39 +++++++++++++++++++ .../Fixture/skip_named_argument.php.inc | 15 ------- ..._argument_for_message_and_previous.php.inc | 17 -------- .../ThrowWithPreviousExceptionRector.php | 10 ++++- 5 files changed, 82 insertions(+), 34 deletions(-) create mode 100644 rules-tests/CodeQuality/Rector/Catch_/ThrowWithPreviousExceptionRector/Fixture/named_argument.php.inc create mode 100644 rules-tests/CodeQuality/Rector/Catch_/ThrowWithPreviousExceptionRector/Fixture/named_argument_for_message_and_previous.php.inc delete mode 100644 rules-tests/CodeQuality/Rector/Catch_/ThrowWithPreviousExceptionRector/Fixture/skip_named_argument.php.inc delete mode 100644 rules-tests/CodeQuality/Rector/Catch_/ThrowWithPreviousExceptionRector/Fixture/skip_named_argument_for_message_and_previous.php.inc 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')) {