-
-
Notifications
You must be signed in to change notification settings - Fork 424
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
base: main
Are you sure you want to change the base?
Skip named arguments when adding, modifying or removing parameters #7757
Conversation
|
Thanks for PR. What is the exact use case or a set this broke for you? |
|
It happened on a private project, unfortunately I cannot share the code. What I saw is that we had a subproject with a method with many parameters, similar to And we wanted to change all places where the function was called so that the $mediaType parameter would always be 'PNG'. The problem is that we had some places where this function was called like this:
When we tried to replace the value for argument 2 with 'PNG' using the (these are made up examples, just to give you the idea, as I said I cannot share the exact code) See https://getrector.com/demo/14aed97f-da7d-4e27-aeb5-9c77dd3938d5 I don't think these rules can be safely applied if the calls use named parameters, that's why I added some code to disable them for this case, but maybe you can suggest something to keep them working for this usage? |
|
I see. Can you add The rule should be smart enough to pick up the right position regardless int or arg name. |
|
This is the updated example https://getrector.com/demo/a6398562-3904-4caa-be42-c881cd451692 Notice that the rule here also changes the default value in the MediaManager class. In our use case this does not happen because the code is actually in a different subproject so Rector won't touch it What do you want to do here? I am not sure how these rules can really be changed so that they handle named arguments correctly |
|
Thanks for the update! The rule should work as follows: - $manager->addMediaFolder($folderName, mediaType: '', color:'');
+ $manager->addMediaFolder($folderName, mediaType: 'PNG', color: ''); |
|
Can you update it to handle this change? It would benefit all other use cases we already use this rules for. |
|
I'll try to take a look and see if I can think of a way to handle this case (and similar ones for the other rules) |
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.
Does this rector has the same problem for splat operator like
->method($a, ...$b);
?
(Splat can also contain named args)
…rguments - Check if parameter is already present as named argument before adding - When named arguments exist, add new parameter as named argument at end - Add comprehensive test fixtures for named argument scenarios
|
@TomasVotruba I have modified the
Please let me know if you think this approach is correct. If you do, I'll work to implement something similar in the rules which replace or remove arguments |
|
|
||
| if (isset($node->args[$position])) { | ||
| return true; | ||
| // If named arguments exist |
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 argumentName from:
$position = $this->argsAnalyzer->resolveArgPosition($args, $argumentName, $position);see
rector-src/src/NodeAnalyzer/ArgsAnalyzer.php
Lines 35 to 50 in 2e1e9cc
| public function resolveArgPosition(array $args, string $name, int $defaultPosition): int | |
| { | |
| foreach ($args as $position => $arg) { | |
| if (! $arg->name instanceof Identifier) { | |
| continue; | |
| } | |
| if (! $this->nodeNameResolver->isName($arg->name, $name)) { | |
| continue; | |
| } | |
| return $position; | |
| } | |
| return $defaultPosition; | |
| } |
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
|
@TomasVotruba were you able to take a look at the update I added for the |
|
Thanks for the ping. Not yet, I usually review once @samsonasik approves to streamline merging if its ok for both of us |
|
@TomasVotruba this won't be ready for merging yet as I have just updated one of the rules I originally modified and just wanted to have your opinion on whether what I had implemented made sense before I worked on the other two cases. Should I go ahead and implement the other updates? |
|
I have no time for deeper now. Let's do first rule, merge and then take it from there. |
The
ArgumentAdderRector,ReplaceArgumentDefaultValueRector,FunctionArgumentDefaultValueReplacerRectorandRemoveMethodCallParamRectorrules add, modify or remove parameters using the position of the parameter. This can be problematic if the function/method call uses named arguments as we may end up modifying the wrong parameter and producing invalid code. This PR modifies these rules so that if there are any named parameters in the calls, the rules are not applied.Includes new fixtures which confirm the new functionality