Add SwapMethodCallArgumentsRector#3726
Conversation
|
Hi, thanks for the feature. |
|
The use case is a change in order of parameters in an external package. In my case a child class calls a parent's method which is imported from a package. For example: abstract class ParentClass
{
public function call(int $param1, string $param2): void
{
}
}becomes abstract class ParentClass
{
public function call(string $param2, int $param1): void
{
}
}So the child class can be refactored automatically from class ChildClass extends ParentClass
{
public function callParent(): void
{
$this->call(123, 'some string');
}
}to class ChildClass extends ParentClass
{
public function callParent(): void
{
$this->call('some string', 123);
}
}But I generalised it to the method call, not only in the parent-child case. |
|
General rules should be useful in an open-source packages, so we can add it in publicly used sets. Otherwise it's better to use simply IDE. What os package has this change? |
|
Actually it's not an open-source package, it's internal to my company. I thought it would be useful elsewhere. But I didn't know the rules are primarily made for the public sets. Sorry for the miscomprehension! |
| ): array { | ||
| $newArguments = []; | ||
| foreach ($swapMethodCallArguments->getOrder() as $oldPosition => $newPosition) { | ||
| if (! isset($call->getArgs()[$oldPosition])) { |
There was a problem hiding this comment.
call->getArgs() in loop can be moved to before loop to a variable
There was a problem hiding this comment.
@samsonasik Could you handle the update and then merge it?
rules/Arguments/Rector/MethodCall/SwapMethodCallArgumentsRector.php
Outdated
Show resolved
Hide resolved
Well said, let's give it a try if this will catch up 👍 Thank you |
…r.php Co-authored-by: Abdul Malik Ikhsan <samsonasik@gmail.com>
01c5f05 to
30e2381
Compare
* ✨ add SwapMethodCallArgumentsRector * Update rules/Arguments/Rector/MethodCall/SwapMethodCallArgumentsRector.php Co-authored-by: Abdul Malik Ikhsan <samsonasik@gmail.com> * Clean up --------- Co-authored-by: Tomas Votruba <tomas.vot@gmail.com> Co-authored-by: Abdul Malik Ikhsan <samsonasik@gmail.com>
|
Hi @gaydamakha, I've noticed this rule could lead to infinite argument swaps on each run. Which would condition to run Rector just once and make usage confusing. Instead, it's better to create a custom rule for these swaps and check e.g. argument type and variable name to match their new positions. Only that way the rule can be idempotent and usable in CI 👍 |
|
Ref #4766 |
I was missing this feature during the development. Tell me if i forgot something or if I need to open an issue.
I was inspired by
SwapFuncCallArgumentsRectorto write this rector, so there is a bit of code duplication. I was hesitating to get out theSwapMethodCallArgumentsRector::resolveNewArgumentsmethod but didn't find a suited name neither namespace for a class. If you have any idea I'll be glad to refactor this.