From b0b65de833f01ae740ad8964a8d5c70d3582f6af Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Thu, 22 Oct 2026 11:50:29 +0200 Subject: [PATCH] [Arguments] Deprecate RemoveMethodCallParamRector as too niche and generic --- .../Fixture/include_static_call.php.inc | 19 --- .../Fixture/remove_named_arguments.php.inc | 32 ----- .../Fixture/skip_already_removed.php.inc | 13 -- .../Fixture/skip_named_arguments.php.inc | 10 -- .../Fixture/some_class.php.inc | 31 ----- .../RemoveMethodCallParamRectorTest.php | 28 ---- .../Source/MethodCaller.php | 7 - .../Source/SomeMultiArg.php | 16 --- .../Source/StaticCaller.php | 9 -- .../config/configured_rule.php | 20 --- .../RemoveMethodCallParamRector.php | 120 ++---------------- .../ValueObject/RemoveMethodCallParam.php | 35 ----- 12 files changed, 10 insertions(+), 330 deletions(-) delete mode 100644 rules-tests/Arguments/Rector/MethodCall/RemoveMethodCallParamRector/Fixture/include_static_call.php.inc delete mode 100644 rules-tests/Arguments/Rector/MethodCall/RemoveMethodCallParamRector/Fixture/remove_named_arguments.php.inc delete mode 100644 rules-tests/Arguments/Rector/MethodCall/RemoveMethodCallParamRector/Fixture/skip_already_removed.php.inc delete mode 100644 rules-tests/Arguments/Rector/MethodCall/RemoveMethodCallParamRector/Fixture/skip_named_arguments.php.inc delete mode 100644 rules-tests/Arguments/Rector/MethodCall/RemoveMethodCallParamRector/Fixture/some_class.php.inc delete mode 100644 rules-tests/Arguments/Rector/MethodCall/RemoveMethodCallParamRector/RemoveMethodCallParamRectorTest.php delete mode 100644 rules-tests/Arguments/Rector/MethodCall/RemoveMethodCallParamRector/Source/MethodCaller.php delete mode 100644 rules-tests/Arguments/Rector/MethodCall/RemoveMethodCallParamRector/Source/SomeMultiArg.php delete mode 100644 rules-tests/Arguments/Rector/MethodCall/RemoveMethodCallParamRector/Source/StaticCaller.php delete mode 100644 rules-tests/Arguments/Rector/MethodCall/RemoveMethodCallParamRector/config/configured_rule.php delete mode 100644 rules/Arguments/ValueObject/RemoveMethodCallParam.php diff --git a/rules-tests/Arguments/Rector/MethodCall/RemoveMethodCallParamRector/Fixture/include_static_call.php.inc b/rules-tests/Arguments/Rector/MethodCall/RemoveMethodCallParamRector/Fixture/include_static_call.php.inc deleted file mode 100644 index 711aa2ac9dd..00000000000 --- a/rules-tests/Arguments/Rector/MethodCall/RemoveMethodCallParamRector/Fixture/include_static_call.php.inc +++ /dev/null @@ -1,19 +0,0 @@ - ------ - diff --git a/rules-tests/Arguments/Rector/MethodCall/RemoveMethodCallParamRector/Fixture/remove_named_arguments.php.inc b/rules-tests/Arguments/Rector/MethodCall/RemoveMethodCallParamRector/Fixture/remove_named_arguments.php.inc deleted file mode 100644 index 682f5bcc919..00000000000 --- a/rules-tests/Arguments/Rector/MethodCall/RemoveMethodCallParamRector/Fixture/remove_named_arguments.php.inc +++ /dev/null @@ -1,32 +0,0 @@ -firstArgument(0, b: 1); -$object->firstArgument(a: 0); -$object->firstArgument(b: 1, a: 0); -$object->secondArgument(0, b: 1); -$object->secondArgument(b: 1); -$object->secondArgument(b: 1, a: 0); - -?> - ------ -firstArgument(b: 1); -$object->firstArgument(); -$object->firstArgument(b: 1); -$object->secondArgument(0); -$object->secondArgument(); -$object->secondArgument(a: 0); - -?> diff --git a/rules-tests/Arguments/Rector/MethodCall/RemoveMethodCallParamRector/Fixture/skip_already_removed.php.inc b/rules-tests/Arguments/Rector/MethodCall/RemoveMethodCallParamRector/Fixture/skip_already_removed.php.inc deleted file mode 100644 index b7fb422d773..00000000000 --- a/rules-tests/Arguments/Rector/MethodCall/RemoveMethodCallParamRector/Fixture/skip_already_removed.php.inc +++ /dev/null @@ -1,13 +0,0 @@ -process(1); - } -} diff --git a/rules-tests/Arguments/Rector/MethodCall/RemoveMethodCallParamRector/Fixture/skip_named_arguments.php.inc b/rules-tests/Arguments/Rector/MethodCall/RemoveMethodCallParamRector/Fixture/skip_named_arguments.php.inc deleted file mode 100644 index bf33f0f5840..00000000000 --- a/rules-tests/Arguments/Rector/MethodCall/RemoveMethodCallParamRector/Fixture/skip_named_arguments.php.inc +++ /dev/null @@ -1,10 +0,0 @@ -firstArgument(b: 1); -$object->secondArgument(a: 1); -$object->secondArgument(0, c: 2); diff --git a/rules-tests/Arguments/Rector/MethodCall/RemoveMethodCallParamRector/Fixture/some_class.php.inc b/rules-tests/Arguments/Rector/MethodCall/RemoveMethodCallParamRector/Fixture/some_class.php.inc deleted file mode 100644 index 52e2e8d7e11..00000000000 --- a/rules-tests/Arguments/Rector/MethodCall/RemoveMethodCallParamRector/Fixture/some_class.php.inc +++ /dev/null @@ -1,31 +0,0 @@ -process(1, 2); - } -} - -?> ------ -process(1); - } -} - -?> diff --git a/rules-tests/Arguments/Rector/MethodCall/RemoveMethodCallParamRector/RemoveMethodCallParamRectorTest.php b/rules-tests/Arguments/Rector/MethodCall/RemoveMethodCallParamRector/RemoveMethodCallParamRectorTest.php deleted file mode 100644 index e76a4b4c481..00000000000 --- a/rules-tests/Arguments/Rector/MethodCall/RemoveMethodCallParamRector/RemoveMethodCallParamRectorTest.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/Arguments/Rector/MethodCall/RemoveMethodCallParamRector/Source/MethodCaller.php b/rules-tests/Arguments/Rector/MethodCall/RemoveMethodCallParamRector/Source/MethodCaller.php deleted file mode 100644 index 5632b010946..00000000000 --- a/rules-tests/Arguments/Rector/MethodCall/RemoveMethodCallParamRector/Source/MethodCaller.php +++ /dev/null @@ -1,7 +0,0 @@ -ruleWithConfiguration(RemoveMethodCallParamRector::class, [ - new RemoveMethodCallParam(MethodCaller::class, 'process', 1), - new RemoveMethodCallParam(StaticCaller::class, 'remove', 3), - new RemoveMethodCallParam(SomeMultiArg::class, 'firstArgument', 0), - new RemoveMethodCallParam(SomeMultiArg::class, 'secondArgument', 1), - ]); -}; diff --git a/rules/Arguments/Rector/MethodCall/RemoveMethodCallParamRector.php b/rules/Arguments/Rector/MethodCall/RemoveMethodCallParamRector.php index ae6bbe80ea6..a13bae490e5 100644 --- a/rules/Arguments/Rector/MethodCall/RemoveMethodCallParamRector.php +++ b/rules/Arguments/Rector/MethodCall/RemoveMethodCallParamRector.php @@ -7,39 +7,21 @@ use PhpParser\Node; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\StaticCall; -use PHPStan\Reflection\FunctionReflection; -use PHPStan\Reflection\MethodReflection; -use PHPStan\Reflection\ParametersAcceptorSelector; -use Rector\Arguments\ValueObject\RemoveMethodCallParam; -use Rector\Contract\Rector\ConfigurableRectorInterface; -use Rector\NodeAnalyzer\ArgsAnalyzer; +use Rector\Configuration\Deprecation\Contract\DeprecatedInterface; +use Rector\Exception\ShouldNotHappenException; use Rector\Rector\AbstractRector; -use Rector\Reflection\ReflectionResolver; -use Rector\RuleDoc\CodeSample\ConfiguredCodeSample; +use Rector\RuleDoc\CodeSample\CodeSample; use Rector\RuleDoc\RuleDefinition; -use Webmozart\Assert\Assert; /** - * @note used extensively https://github.com/search?q=RemoveMethodCallParamRector%3A%3Aclass+language%3APHP&type=code&l=PHP - * @see \Rector\Tests\Arguments\Rector\MethodCall\RemoveMethodCallParamRector\RemoveMethodCallParamRectorTest + * @deprecated This rule is deprecated, as too niche and generic. It can create invalid calls on unrelated objects. Use a specific rule instead. */ -final class RemoveMethodCallParamRector extends AbstractRector implements ConfigurableRectorInterface +final class RemoveMethodCallParamRector extends AbstractRector implements DeprecatedInterface { - /** - * @var array - */ - private array $removeMethodCallParamsByMethod = []; - - public function __construct( - private readonly ReflectionResolver $reflectionResolver, - private readonly ArgsAnalyzer $argsAnalyzer, - ) { - } - public function getRuleDefinition(): RuleDefinition { return new RuleDefinition('Remove parameter of method call', [ - new ConfiguredCodeSample( + new CodeSample( <<<'CODE_SAMPLE' final class SomeClass { @@ -59,8 +41,6 @@ public function run(Caller $caller) } } CODE_SAMPLE - , - [new RemoveMethodCallParam('Caller', 'process', 1)] ), ]); } @@ -78,89 +58,9 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - $hasChanged = false; - - if ($node->isFirstClassCallable()) { - return null; - } - - $methodName = $this->getName($node->name); - if ($methodName === null) { - return null; - } - - foreach ($this->removeMethodCallParamsByMethod[strtolower($methodName)] ?? [] as $removeMethodCallParam) { - if (! $this->isCallerObjectType($node, $removeMethodCallParam)) { - continue; - } - - $args = $node->getArgs(); - $position = $removeMethodCallParam->getParamPosition(); - $firstNamedArgPosition = $this->argsAnalyzer->resolveFirstNamedArgPosition($args); - - // if the call has named arguments and the argument that we want to remove is not - // before any named argument, we need to check if it is in the list of named arguments - // if it is, we use the position of the named argument as the position to remove - // if it is not, we cannot remove it - if ($firstNamedArgPosition !== null && $position >= $firstNamedArgPosition) { - $reflection = $this->reflectionResolver->resolveFunctionLikeReflectionFromCall($node); - if (! $reflection instanceof MethodReflection && ! $reflection instanceof FunctionReflection) { - return null; - } - - $parametersAcceptor = ParametersAcceptorSelector::combineAcceptors($reflection->getVariants()); - $parameterReflection = $parametersAcceptor->getParameters()[$position] ?? null; - - $newPosition = -1; - if ($parameterReflection !== null) { - $newPosition = $this->argsAnalyzer->resolveArgPosition( - $args, - $parameterReflection->getName(), - $newPosition - ); - } - - if ($newPosition === -1) { - return null; - } - - $position = $newPosition; - } - - if (! isset($args[$position])) { - continue; - } - - unset($node->args[$position]); - $hasChanged = true; - } - - if (! $hasChanged) { - return null; - } - - return $node; - } - - /** - * @param mixed[] $configuration - */ - public function configure(array $configuration): void - { - Assert::allIsInstanceOf($configuration, RemoveMethodCallParam::class); - - $this->removeMethodCallParamsByMethod = []; - foreach ($configuration as $removeMethodCallParam) { - $this->removeMethodCallParamsByMethod[strtolower($removeMethodCallParam->getMethodName())][] = $removeMethodCallParam; - } - } - - private function isCallerObjectType(StaticCall|MethodCall $call, RemoveMethodCallParam $removeMethodCallParam): bool - { - if ($call instanceof MethodCall) { - return $this->isObjectType($call->var, $removeMethodCallParam->getObjectType()); - } - - return $this->isObjectType($call->class, $removeMethodCallParam->getObjectType()); + throw new ShouldNotHappenException(sprintf( + '"%s" rule is deprecated, as too niche and generic. Use a specific rule instead', + self::class + )); } } diff --git a/rules/Arguments/ValueObject/RemoveMethodCallParam.php b/rules/Arguments/ValueObject/RemoveMethodCallParam.php deleted file mode 100644 index 53c7e016111..00000000000 --- a/rules/Arguments/ValueObject/RemoveMethodCallParam.php +++ /dev/null @@ -1,35 +0,0 @@ -class); - } - - public function getMethodName(): string - { - return $this->methodName; - } - - public function getParamPosition(): int - { - return $this->paramPosition; - } -}