diff --git a/config/set/named-args.php b/config/set/named-args.php index ae54d0d150a..44a1c6313dc 100644 --- a/config/set/named-args.php +++ b/config/set/named-args.php @@ -7,6 +7,7 @@ use Rector\CodeQuality\Rector\CallLike\AddNameToBooleanArgumentRector; use Rector\CodeQuality\Rector\CallLike\AddNameToNullArgumentRector; use Rector\CodeQuality\Rector\FuncCall\SortCallLikeNamedArgsRector; +use Rector\CodeQuality\Rector\New_\NarrowPreviousExceptionCodeArgRector; use Rector\Config\RectorConfig; use Rector\DeadCode\Rector\MethodCall\RemoveNullNamedArgOnNullDefaultParamRector; @@ -18,5 +19,6 @@ SortCallLikeNamedArgsRector::class, SortAttributeNamedArgsRector::class, ExplicitAttributeNamedArgsRector::class, + NarrowPreviousExceptionCodeArgRector::class, ]); }; 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 deleted file mode 100644 index 792b460802d..00000000000 --- a/rules-tests/CodeQuality/Rector/Catch_/ThrowWithPreviousExceptionRector/Fixture/named_argument.php.inc +++ /dev/null @@ -1,35 +0,0 @@ -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 deleted file mode 100644 index 288f9996267..00000000000 --- a/rules-tests/CodeQuality/Rector/Catch_/ThrowWithPreviousExceptionRector/Fixture/named_argument_for_message_and_previous.php.inc +++ /dev/null @@ -1,39 +0,0 @@ -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 new file mode 100644 index 00000000000..1e3f425dde8 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/Catch_/ThrowWithPreviousExceptionRector/Fixture/skip_named_argument.php.inc @@ -0,0 +1,15 @@ +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 new file mode 100644 index 00000000000..deb1e100b69 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/Catch_/ThrowWithPreviousExceptionRector/Fixture/skip_named_argument_for_message_and_previous.php.inc @@ -0,0 +1,17 @@ +run(); + }catch(\Throwable $throwable) { + throw new LogicException(message: 'Some exception', previous: $throwable); + } + } +} diff --git a/rules-tests/CodeQuality/Rector/New_/NarrowPreviousExceptionCodeArgRector/Fixture/named.php.inc b/rules-tests/CodeQuality/Rector/New_/NarrowPreviousExceptionCodeArgRector/Fixture/named.php.inc new file mode 100644 index 00000000000..2040d54d4cb --- /dev/null +++ b/rules-tests/CodeQuality/Rector/New_/NarrowPreviousExceptionCodeArgRector/Fixture/named.php.inc @@ -0,0 +1,53 @@ +run(); + } catch (\Throwable $throwable) { + throw new \RuntimeException(message: 'Failed', code: $throwable->getCode(), previous: $throwable); + } + } + + public function mixed() + { + try { + $this->run(); + } catch (\Throwable $throwable) { + throw new \RuntimeException('Failed', $throwable->getCode(), previous: $throwable); + } + } +} + +?> +----- +run(); + } catch (\Throwable $throwable) { + throw new \RuntimeException(message: 'Failed', previous: $throwable); + } + } + + public function mixed() + { + try { + $this->run(); + } catch (\Throwable $throwable) { + throw new \RuntimeException('Failed', previous: $throwable); + } + } +} + +?> diff --git a/rules-tests/CodeQuality/Rector/New_/NarrowPreviousExceptionCodeArgRector/Fixture/positional.php.inc b/rules-tests/CodeQuality/Rector/New_/NarrowPreviousExceptionCodeArgRector/Fixture/positional.php.inc new file mode 100644 index 00000000000..bdcbd8fbf1d --- /dev/null +++ b/rules-tests/CodeQuality/Rector/New_/NarrowPreviousExceptionCodeArgRector/Fixture/positional.php.inc @@ -0,0 +1,35 @@ +run(); + } catch (\Throwable $throwable) { + throw new \RuntimeException('Failed', $throwable->getCode(), $throwable); + } + } +} + +?> +----- +run(); + } catch (\Throwable $throwable) { + throw new \RuntimeException('Failed', previous: $throwable); + } + } +} + +?> diff --git a/rules-tests/CodeQuality/Rector/New_/NarrowPreviousExceptionCodeArgRector/Fixture/skip_custom_parameter_order.php.inc b/rules-tests/CodeQuality/Rector/New_/NarrowPreviousExceptionCodeArgRector/Fixture/skip_custom_parameter_order.php.inc new file mode 100644 index 00000000000..3c5264403c2 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/New_/NarrowPreviousExceptionCodeArgRector/Fixture/skip_custom_parameter_order.php.inc @@ -0,0 +1,23 @@ +run(); + } catch (\Throwable $throwable) { + throw new CustomOrderException('Failed', $throwable->getCode(), $throwable); + } + } +} diff --git a/rules-tests/CodeQuality/Rector/New_/NarrowPreviousExceptionCodeArgRector/Fixture/skip_different_code.php.inc b/rules-tests/CodeQuality/Rector/New_/NarrowPreviousExceptionCodeArgRector/Fixture/skip_different_code.php.inc new file mode 100644 index 00000000000..6fbf0ada31f --- /dev/null +++ b/rules-tests/CodeQuality/Rector/New_/NarrowPreviousExceptionCodeArgRector/Fixture/skip_different_code.php.inc @@ -0,0 +1,33 @@ +run($other); + } catch (\Throwable $throwable) { + throw new \RuntimeException('Failed', 404, $throwable); + } + } + + public function otherVariable(\Throwable $other) + { + try { + $this->run($other); + } catch (\Throwable $throwable) { + throw new \RuntimeException('Failed', $other->getCode(), $throwable); + } + } + + public function missingPrevious(\Throwable $other) + { + try { + $this->run($other); + } catch (\Throwable $throwable) { + throw new \RuntimeException('Failed', $throwable->getCode()); + } + } +} diff --git a/rules-tests/CodeQuality/Rector/New_/NarrowPreviousExceptionCodeArgRector/NarrowPreviousExceptionCodeArgRectorTest.php b/rules-tests/CodeQuality/Rector/New_/NarrowPreviousExceptionCodeArgRector/NarrowPreviousExceptionCodeArgRectorTest.php new file mode 100644 index 00000000000..7700cbac9b2 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/New_/NarrowPreviousExceptionCodeArgRector/NarrowPreviousExceptionCodeArgRectorTest.php @@ -0,0 +1,28 @@ +doTestFile($filePath); + } + + public static function provideData(): Iterator + { + return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/configured_rule.php'; + } +} diff --git a/rules-tests/CodeQuality/Rector/New_/NarrowPreviousExceptionCodeArgRector/config/configured_rule.php b/rules-tests/CodeQuality/Rector/New_/NarrowPreviousExceptionCodeArgRector/config/configured_rule.php new file mode 100644 index 00000000000..0cf38515d05 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/New_/NarrowPreviousExceptionCodeArgRector/config/configured_rule.php @@ -0,0 +1,9 @@ +withRules([NarrowPreviousExceptionCodeArgRector::class]); diff --git a/rules/CodeQuality/Rector/Catch_/ThrowWithPreviousExceptionRector.php b/rules/CodeQuality/Rector/Catch_/ThrowWithPreviousExceptionRector.php index 73b177e3038..90647407aad 100644 --- a/rules/CodeQuality/Rector/Catch_/ThrowWithPreviousExceptionRector.php +++ b/rules/CodeQuality/Rector/Catch_/ThrowWithPreviousExceptionRector.php @@ -185,14 +185,8 @@ private function refactorThrow(Throw_ $throw, Variable $caughtThrowableVariable) /** @var Arg $arg1 */ $arg1 = $new->args[1]; if ($arg1->name instanceof Identifier && $arg1->name->toString() === 'previous') { - 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) { + // previous is already passed, code is omitted on purpose + if (! $hasChanged) { return null; } } elseif ($this->hasParameter($new, 'previous') && ! $this->hasArgument($new, 'previous')) { diff --git a/rules/CodeQuality/Rector/New_/NarrowPreviousExceptionCodeArgRector.php b/rules/CodeQuality/Rector/New_/NarrowPreviousExceptionCodeArgRector.php new file mode 100644 index 00000000000..d1d81c3069a --- /dev/null +++ b/rules/CodeQuality/Rector/New_/NarrowPreviousExceptionCodeArgRector.php @@ -0,0 +1,149 @@ +getCode(), $throwable); +} +CODE_SAMPLE + , + <<<'CODE_SAMPLE' +try { + $someCode = 1; +} catch (Throwable $throwable) { + throw new \RuntimeException('ups', previous: $throwable); +} +CODE_SAMPLE + ), + ] + ); + } + + /** + * @return array> + */ + public function getNodeTypes(): array + { + return [New_::class]; + } + + /** + * @param New_ $node + */ + public function refactor(Node $node): ?Node + { + if ($node->isFirstClassCallable()) { + return null; + } + + if (! $node->class instanceof Name) { + return null; + } + + $args = $node->getArgs(); + if (count($args) !== 3) { + return null; + } + + $codeArg = $node->getArg('code', self::CODE_POSITION); + $previousArg = $node->getArg('previous', self::PREVIOUS_POSITION); + if (! $codeArg instanceof Arg || ! $previousArg instanceof Arg) { + return null; + } + + if (! $previousArg->value instanceof Variable) { + return null; + } + + if (! $codeArg->value instanceof MethodCall || ! $this->isName($codeArg->value->name, 'getCode')) { + return null; + } + + if (! $this->nodeComparator->areNodesEqual($codeArg->value->var, $previousArg->value)) { + return null; + } + + if (! $this->isObjectType($node->class, new ObjectType('Throwable'))) { + return null; + } + + if (! $this->hasCodeAndPreviousParameterPositions($node->class)) { + return null; + } + + $node->args = array_values(array_filter($node->args, fn (Arg|VariadicPlaceholder|ArgPlaceholder $arg): bool => $arg !== $codeArg)); + $previousArg->name = new Identifier('previous'); + + return $node; + } + + private function hasCodeAndPreviousParameterPositions(Name $name): bool + { + $className = $this->getName($name); + if (! $this->reflectionProvider->hasClass($className)) { + return false; + } + + $classReflection = $this->reflectionProvider->getClass($className); + if (! $classReflection->hasConstructor()) { + return false; + } + + $extendedParametersAcceptor = ParametersAcceptorSelector::combineAcceptors( + $classReflection->getConstructor() + ->getVariants() + ); + $parameters = $extendedParametersAcceptor->getParameters(); + + if (! isset($parameters[self::CODE_POSITION], $parameters[self::PREVIOUS_POSITION])) { + return false; + } + + if ($parameters[self::CODE_POSITION]->getName() !== 'code') { + return false; + } + + return $parameters[self::PREVIOUS_POSITION]->getName() === 'previous'; + } +} diff --git a/src/Config/Level/CodeQualityLevel.php b/src/Config/Level/CodeQualityLevel.php index e6368998a57..1a0808a4ccc 100644 --- a/src/Config/Level/CodeQualityLevel.php +++ b/src/Config/Level/CodeQualityLevel.php @@ -52,6 +52,7 @@ use Rector\CodeQuality\Rector\Isset_\IssetOnPropertyObjectToPropertyExistsRector; use Rector\CodeQuality\Rector\LogicalAnd\AndAssignsToSeparateLinesRector; use Rector\CodeQuality\Rector\LogicalAnd\LogicalToBooleanRector; +use Rector\CodeQuality\Rector\New_\NarrowPreviousExceptionCodeArgRector; use Rector\CodeQuality\Rector\New_\NewStaticToNewSelfRector; use Rector\CodeQuality\Rector\NotEqual\CommonNotEqualRector; use Rector\CodeQuality\Rector\NullsafeMethodCall\CleanupUnneededNullsafeOperatorRector; @@ -128,6 +129,7 @@ final class CodeQualityLevel ArrayExplicitBoolCompareRector::class, ExplicitNullableObjectInstanceRector::class, ThrowWithPreviousExceptionRector::class, + NarrowPreviousExceptionCodeArgRector::class, RemoveSoleValueSprintfRector::class, ExplicitReturnNullRector::class, ArrayMergeOfNonArraysToSimpleArrayRector::class,