Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Rector\Tests\Arguments\Rector\MethodCall\RemoveMethodCallParamRector\Fixture;

use Rector\Tests\Arguments\Rector\MethodCall\RemoveMethodCallParamRector\Source\SomeMultiArg;

$object = new SomeMultiArg();
$object->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);

?>

-----
<?php

namespace Rector\Tests\Arguments\Rector\MethodCall\RemoveMethodCallParamRector\Fixture;

use Rector\Tests\Arguments\Rector\MethodCall\RemoveMethodCallParamRector\Source\SomeMultiArg;

$object = new SomeMultiArg();
$object->firstArgument(b: 1);
$object->firstArgument();
$object->firstArgument(b: 1);
$object->secondArgument(0);
$object->secondArgument();
$object->secondArgument(a: 0);

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Rector\Tests\Arguments\Rector\MethodCall\RemoveMethodCallParamRector\Fixture;

use Rector\Tests\Arguments\Rector\MethodCall\RemoveMethodCallParamRector\Source\SomeMultiArg;

$object = new SomeMultiArg();
$object->firstArgument(b: 1);
$object->secondArgument(a: 1);
$object->secondArgument(0, c: 2);
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Arguments\Rector\MethodCall\RemoveMethodCallParamRector\Source;

class SomeMultiArg
{
public function firstArgument($a = 1, $b = 2)
{
}

public function secondArgument($a = 1, $b = 2, $c = 3)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
use Rector\Arguments\ValueObject\RemoveMethodCallParam;
use Rector\Config\RectorConfig;
use Rector\Tests\Arguments\Rector\MethodCall\RemoveMethodCallParamRector\Source\MethodCaller;
use Rector\Tests\Arguments\Rector\MethodCall\RemoveMethodCallParamRector\Source\SomeMultiArg;
use Rector\Tests\Arguments\Rector\MethodCall\RemoveMethodCallParamRector\Source\StaticCaller;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig
->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),
]);
};
44 changes: 42 additions & 2 deletions rules/Arguments/Rector/MethodCall/RemoveMethodCallParamRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use Rector\Arguments\ValueObject\RemoveMethodCallParam;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\NodeAnalyzer\ArgsAnalyzer;
use Rector\PhpParser\AstResolver;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand All @@ -25,6 +28,12 @@ final class RemoveMethodCallParamRector extends AbstractRector implements Config
*/
private array $removeMethodCallParams = [];

public function __construct(
private readonly AstResolver $astResolver,
private readonly ArgsAnalyzer $argsAnalyzer,
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Remove parameter of method call', [
Expand Down Expand Up @@ -83,11 +92,42 @@ public function refactor(Node $node): ?Node
}

$args = $node->getArgs();
if (! isset($args[$removeMethodCallParam->getParamPosition()])) {
$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) {
$call = $this->astResolver->resolveClassMethodOrFunctionFromCall($node);
if ($call === null) {
return null;
}

$paramName = null;
$variable = $call->params[$position]->var;
if ($variable instanceof Variable) {
$paramName = $variable->name;
}

$newPosition = -1;
if (is_string($paramName)) {
$newPosition = $this->argsAnalyzer->resolveArgPosition($args, $paramName, $newPosition);
}

if ($newPosition === -1) {
return null;
}

$position = $newPosition;
}

if (! isset($args[$position])) {
continue;
}

unset($node->args[$removeMethodCallParam->getParamPosition()]);
unset($node->args[$position]);
$hasChanged = true;
}

Expand Down