From 057ea7a6875e1e0a2c1c336899e986c32a7edf58 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Wed, 25 Nov 2026 00:18:41 +0100 Subject: [PATCH] [CodeQuality] Remove NarrowPreviousExceptionCodeArgRector, as it changes getCode() of new exception to 0 --- config/set/named-args.php | 2 - .../Fixture/named.php.inc | 53 ------- .../Fixture/positional.php.inc | 35 ---- .../skip_custom_parameter_order.php.inc | 23 --- .../Fixture/skip_different_code.php.inc | 33 ---- ...rrowPreviousExceptionCodeArgRectorTest.php | 28 ---- .../config/configured_rule.php | 9 -- .../NarrowPreviousExceptionCodeArgRector.php | 149 ------------------ src/Config/Level/CodeQualityLevel.php | 2 - 9 files changed, 334 deletions(-) delete mode 100644 rules-tests/CodeQuality/Rector/New_/NarrowPreviousExceptionCodeArgRector/Fixture/named.php.inc delete mode 100644 rules-tests/CodeQuality/Rector/New_/NarrowPreviousExceptionCodeArgRector/Fixture/positional.php.inc delete mode 100644 rules-tests/CodeQuality/Rector/New_/NarrowPreviousExceptionCodeArgRector/Fixture/skip_custom_parameter_order.php.inc delete mode 100644 rules-tests/CodeQuality/Rector/New_/NarrowPreviousExceptionCodeArgRector/Fixture/skip_different_code.php.inc delete mode 100644 rules-tests/CodeQuality/Rector/New_/NarrowPreviousExceptionCodeArgRector/NarrowPreviousExceptionCodeArgRectorTest.php delete mode 100644 rules-tests/CodeQuality/Rector/New_/NarrowPreviousExceptionCodeArgRector/config/configured_rule.php delete mode 100644 rules/CodeQuality/Rector/New_/NarrowPreviousExceptionCodeArgRector.php diff --git a/config/set/named-args.php b/config/set/named-args.php index 44a1c6313dc..ae54d0d150a 100644 --- a/config/set/named-args.php +++ b/config/set/named-args.php @@ -7,7 +7,6 @@ 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; @@ -19,6 +18,5 @@ SortCallLikeNamedArgsRector::class, SortAttributeNamedArgsRector::class, ExplicitAttributeNamedArgsRector::class, - NarrowPreviousExceptionCodeArgRector::class, ]); }; diff --git a/rules-tests/CodeQuality/Rector/New_/NarrowPreviousExceptionCodeArgRector/Fixture/named.php.inc b/rules-tests/CodeQuality/Rector/New_/NarrowPreviousExceptionCodeArgRector/Fixture/named.php.inc deleted file mode 100644 index 2040d54d4cb..00000000000 --- a/rules-tests/CodeQuality/Rector/New_/NarrowPreviousExceptionCodeArgRector/Fixture/named.php.inc +++ /dev/null @@ -1,53 +0,0 @@ -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 deleted file mode 100644 index bdcbd8fbf1d..00000000000 --- a/rules-tests/CodeQuality/Rector/New_/NarrowPreviousExceptionCodeArgRector/Fixture/positional.php.inc +++ /dev/null @@ -1,35 +0,0 @@ -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 deleted file mode 100644 index 3c5264403c2..00000000000 --- a/rules-tests/CodeQuality/Rector/New_/NarrowPreviousExceptionCodeArgRector/Fixture/skip_custom_parameter_order.php.inc +++ /dev/null @@ -1,23 +0,0 @@ -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 deleted file mode 100644 index 6fbf0ada31f..00000000000 --- a/rules-tests/CodeQuality/Rector/New_/NarrowPreviousExceptionCodeArgRector/Fixture/skip_different_code.php.inc +++ /dev/null @@ -1,33 +0,0 @@ -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 deleted file mode 100644 index 7700cbac9b2..00000000000 --- a/rules-tests/CodeQuality/Rector/New_/NarrowPreviousExceptionCodeArgRector/NarrowPreviousExceptionCodeArgRectorTest.php +++ /dev/null @@ -1,28 +0,0 @@ -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 deleted file mode 100644 index 0cf38515d05..00000000000 --- a/rules-tests/CodeQuality/Rector/New_/NarrowPreviousExceptionCodeArgRector/config/configured_rule.php +++ /dev/null @@ -1,9 +0,0 @@ -withRules([NarrowPreviousExceptionCodeArgRector::class]); diff --git a/rules/CodeQuality/Rector/New_/NarrowPreviousExceptionCodeArgRector.php b/rules/CodeQuality/Rector/New_/NarrowPreviousExceptionCodeArgRector.php deleted file mode 100644 index d1d81c3069a..00000000000 --- a/rules/CodeQuality/Rector/New_/NarrowPreviousExceptionCodeArgRector.php +++ /dev/null @@ -1,149 +0,0 @@ -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 1a0808a4ccc..e6368998a57 100644 --- a/src/Config/Level/CodeQualityLevel.php +++ b/src/Config/Level/CodeQualityLevel.php @@ -52,7 +52,6 @@ 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; @@ -129,7 +128,6 @@ final class CodeQualityLevel ArrayExplicitBoolCompareRector::class, ExplicitNullableObjectInstanceRector::class, ThrowWithPreviousExceptionRector::class, - NarrowPreviousExceptionCodeArgRector::class, RemoveSoleValueSprintfRector::class, ExplicitReturnNullRector::class, ArrayMergeOfNonArraysToSimpleArrayRector::class,