-
-
Notifications
You must be signed in to change notification settings - Fork 426
Skip named arguments when adding, modifying or removing parameters #7757
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
Closed
carlos-granados
wants to merge
3
commits into
rectorphp:main
from
carlos-granados:skip-named-arguments
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
34bd36d
Skip named arguments when adding, modifying or removing parameters
carlos-granados 6cb5d82
refactor: improve ArgumentAdderRector to intelligently handle named a…
carlos-granados 07b1784
refactor: refine ArgumentAdderRector named argument handling
carlos-granados 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
35 changes: 35 additions & 0 deletions
35
...-tests/Arguments/Rector/ClassMethod/ArgumentAdderRector/Fixture/named_arg_add_new.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,35 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\Arguments\Rector\ClassMethod\ArgumentAdderRector\Fixture; | ||
|
|
||
| use Rector\Tests\Arguments\Rector\ClassMethod\ArgumentAdderRector\Source\SomeMultiArg; | ||
|
|
||
| class NamedArgAddNew | ||
| { | ||
| public function run() | ||
| { | ||
| $obj = new SomeMultiArg(); | ||
| // Named arguments exist, but not the one we're adding (c) | ||
| $obj->run(b: 5); | ||
| } | ||
| } | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\Arguments\Rector\ClassMethod\ArgumentAdderRector\Fixture; | ||
|
|
||
| use Rector\Tests\Arguments\Rector\ClassMethod\ArgumentAdderRector\Source\SomeMultiArg; | ||
|
|
||
| class NamedArgAddNew | ||
| { | ||
| public function run() | ||
| { | ||
| $obj = new SomeMultiArg(); | ||
| // Named arguments exist, but not the one we're adding (c) | ||
| $obj->run(b: 5, c: 4); | ||
| } | ||
| } | ||
|
|
||
| ?> |
37 changes: 37 additions & 0 deletions
37
...s/Rector/ClassMethod/ArgumentAdderRector/Fixture/named_arg_mixed_positional_named.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,37 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\Arguments\Rector\ClassMethod\ArgumentAdderRector\Fixture; | ||
|
|
||
| use Rector\Tests\Arguments\Rector\ClassMethod\ArgumentAdderRector\Source\SomeMultiArg; | ||
|
|
||
| class NamedArgMixedPositionalNamed | ||
| { | ||
| public function run() | ||
| { | ||
| $obj = new SomeMultiArg(); | ||
| // Mix of positional and named arguments | ||
| $obj->run(0, c: 6); | ||
| $obj->run(0, b: 6); | ||
| } | ||
| } | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\Arguments\Rector\ClassMethod\ArgumentAdderRector\Fixture; | ||
|
|
||
| use Rector\Tests\Arguments\Rector\ClassMethod\ArgumentAdderRector\Source\SomeMultiArg; | ||
|
|
||
| class NamedArgMixedPositionalNamed | ||
| { | ||
| public function run() | ||
| { | ||
| $obj = new SomeMultiArg(); | ||
| // Mix of positional and named arguments | ||
| $obj->run(0, c: 6); | ||
| $obj->run(0, b: 6, c: 4); | ||
| } | ||
| } | ||
|
|
||
| ?> |
16 changes: 16 additions & 0 deletions
16
...nts/Rector/ClassMethod/ArgumentAdderRector/Fixture/skip_named_arg_already_present.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,16 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\Arguments\Rector\ClassMethod\ArgumentAdderRector\Fixture; | ||
|
|
||
| use Rector\Tests\Arguments\Rector\ClassMethod\ArgumentAdderRector\Source\SomeMultiArg; | ||
|
|
||
| class SkipNamedArgAlreadyPresent | ||
| { | ||
| public function run() | ||
| { | ||
| $obj = new SomeMultiArg(); | ||
| // Parameter 'c' is already provided as named argument | ||
| $obj->run(b: 5, c: 999); | ||
| $obj->run(c: 999, b: 5); | ||
| } | ||
| } |
14 changes: 14 additions & 0 deletions
14
...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,14 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\Arguments\Rector\ClassMethod\ReplaceArgumentDefaultValueRector\Fixture; | ||
|
|
||
| use Rector\Tests\Arguments\Rector\ClassMethod\ReplaceArgumentDefaultValueRector\Fixture\ReplaceMethodArgumentWithConstant; | ||
|
|
||
| class SkipNamedArguments | ||
| { | ||
| public function run() | ||
| { | ||
| $obj = new ReplaceMethodArgumentWithConstant(); | ||
| $obj->setSomeMethod(someValue: 'some value'); | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
.../FuncCall/FunctionArgumentDefaultValueReplacerRector/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,12 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\Arguments\Rector\FuncCall\FunctionArgumentDefaultValueReplacerRector\Fixture; | ||
|
|
||
| class SkipNamedArguments | ||
| { | ||
| public function run() | ||
| { | ||
| version_compare(version1: '5.6', version2: PHP_VERSION, operator: 'lte'); | ||
| version_compare('5.6', PHP_VERSION, operator: 'lte'); | ||
| } | ||
| } |
14 changes: 14 additions & 0 deletions
14
...uments/Rector/MethodCall/RemoveMethodCallParamRector/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,14 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\Arguments\Rector\MethodCall\RemoveMethodCallParamRector\Fixture; | ||
|
|
||
| use Rector\Tests\Arguments\Rector\MethodCall\RemoveMethodCallParamRector\Source\MethodCaller; | ||
|
|
||
| final class SkipNamedArguments | ||
| { | ||
| public function run(MethodCaller $caller) | ||
| { | ||
| $caller->process(first: 1, second: 2); | ||
| $caller->process(1, second: 2); | ||
| } | ||
| } |
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
I think to get correct position, you can combine it with
argumentNamefrom:see
rector-src/src/NodeAnalyzer/ArgsAnalyzer.php
Lines 35 to 50 in 2e1e9cc
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.
Ok, but not sure how that would be useful here
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.
The logic is to compare parameter name vs arg name, if equal, use its position whenever they match, otherwise, use default position originally provided.
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.
That can be useful in the rule that tries to replace an argument, but I don't think we need this in this rule which just tries to add a new argument, if the argument already exists we need to skip the rule and if it doesn't, trying to add it positionally can be problematic, so better just add it as a new named argument