-
-
Notifications
You must be signed in to change notification settings - Fork 426
Manage named arguments when replacing a value #7784
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
carlos-granados
wants to merge
1
commit into
rectorphp:main
Choose a base branch
from
carlos-granados:manage-named-arguments-when-replacing-values
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
...tor/ClassMethod/ReplaceArgumentDefaultValueRector/Fixture/replace_named_arguments.php.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
|
|
||
| ?> |
13 changes: 13 additions & 0 deletions
13
...Rector/ClassMethod/ReplaceArgumentDefaultValueRector/Fixture/skip_named_arguments.php.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); |
16 changes: 16 additions & 0 deletions
16
...ts/Arguments/Rector/ClassMethod/ReplaceArgumentDefaultValueRector/Source/SomeMultiArg.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| { | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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, | ||
| ) { | ||
| } | ||
|
|
||
|
|
@@ -44,10 +49,6 @@ public function processReplaces( | |
| return $this->processParams($node, $replaceArgumentDefaultValue); | ||
| } | ||
|
|
||
| if (! isset($node->args[$replaceArgumentDefaultValue->getPosition()])) { | ||
| return null; | ||
| } | ||
|
|
||
| return $this->processArgs($node, $replaceArgumentDefaultValue); | ||
| } | ||
|
|
||
|
|
@@ -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; | ||
| } | ||
|
|
@@ -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()); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| } | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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