Skip to content
Open
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\ClassMethod\ReplaceArgumentDefaultValueRector\Fixture;

use Rector\Tests\Arguments\Rector\ClassMethod\ReplaceArgumentDefaultValueRector\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\ClassMethod\ReplaceArgumentDefaultValueRector\Fixture;

use Rector\Tests\Arguments\Rector\ClassMethod\ReplaceArgumentDefaultValueRector\Source\SomeMultiArg;

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

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

namespace Rector\Tests\Arguments\Rector\ClassMethod\ReplaceArgumentDefaultValueRector\Fixture;

use Rector\Tests\Arguments\Rector\ClassMethod\ReplaceArgumentDefaultValueRector\Source\SomeMultiArg;

use Symfony\Component\Yaml\Yaml;

$object = new SomeMultiArg();
$object->firstArgument(b: 1);
$object->secondArgument(a: 1);
$object->secondArgument(0, c: 2);
Yaml::parse('...', flags: false, other: false, another: true);
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Arguments\Rector\ClassMethod\ReplaceArgumentDefaultValueRector\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 @@ -63,5 +63,19 @@
null,
[]
),
new ReplaceArgumentDefaultValue(
'Rector\Tests\Arguments\Rector\ClassMethod\ReplaceArgumentDefaultValueRector\Source\SomeMultiArg',
'firstArgument',
0,
0,
3
),
new ReplaceArgumentDefaultValue(
'Rector\Tests\Arguments\Rector\ClassMethod\ReplaceArgumentDefaultValueRector\Source\SomeMultiArg',
'secondArgument',
1,
1,
4
),
]);
};
56 changes: 49 additions & 7 deletions rules/Arguments/ArgumentDefaultValueReplacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,22 @@
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\Arguments\Contract\ReplaceArgumentDefaultValueInterface;
use Rector\Arguments\ValueObject\ReplaceArgumentDefaultValue;
use Rector\NodeAnalyzer\ArgsAnalyzer;
use Rector\PhpParser\AstResolver;
use Rector\PhpParser\Node\NodeFactory;
use Rector\PhpParser\Node\Value\ValueResolver;

final readonly class ArgumentDefaultValueReplacer
{
public function __construct(
private NodeFactory $nodeFactory,
private ValueResolver $valueResolver
private ValueResolver $valueResolver,
private ArgsAnalyzer $argsAnalyzer,
private AstResolver $astResolver,
) {
}

Expand All @@ -44,10 +49,6 @@ public function processReplaces(
return $this->processParams($node, $replaceArgumentDefaultValue);
}

if (! isset($node->args[$replaceArgumentDefaultValue->getPosition()])) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to do this check later because the position may change if we have named arguments

return null;
}

return $this->processArgs($node, $replaceArgumentDefaultValue);
}

Expand Down Expand Up @@ -102,7 +103,48 @@ private function processArgs(
}

$position = $replaceArgumentDefaultValue->getPosition();
$particularArg = $expr->getArgs()[$position] ?? null;
$arguments = $expr->getArgs();
$firstNamedArgPosition = $this->argsAnalyzer->resolveFirstNamedArgPosition($arguments);
// if the call has named argyments and we want to replace an array of values
// we cannot replace it as we cannot really match this array of values to
// the existing arguments, it would be too complex
if ($firstNamedArgPosition !== null && is_array($replaceArgumentDefaultValue->getValueBefore())) {
return null;
}

// if the call has named arguments and the argument that we want to replace 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 argyment as the position to replace
// if it is not, we cannot replace it
if ($firstNamedArgPosition !== null && $position >= $firstNamedArgPosition) {
$call = $this->astResolver->resolveClassMethodOrFunctionFromCall($expr);
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($arguments, $paramName, $newPosition);
}

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

$position = $newPosition;
}

if (! isset($arguments[$position])) {
return null;
}

$particularArg = $arguments[$position] ?? null;
if (! $particularArg instanceof Arg) {
return null;
}
Expand All @@ -111,7 +153,7 @@ private function processArgs(
if (is_scalar(
$replaceArgumentDefaultValue->getValueBefore()
) && $argValue === $replaceArgumentDefaultValue->getValueBefore()) {
$expr->args[$position] = $this->normalizeValueToArgument($replaceArgumentDefaultValue->getValueAfter());
$particularArg->value = $this->normalizeValue($replaceArgumentDefaultValue->getValueAfter());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't want to create a new argument, instead we just set the value of the existing argument. This allows us to keep named arguments

return $expr;
}

Expand Down