Skip to content

Add SwapMethodCallArgumentsRector#3726

Merged
samsonasik merged 4 commits intorectorphp:mainfrom
gaydamakha:feature/swap_method_call_arguments_rector
May 6, 2023
Merged

Add SwapMethodCallArgumentsRector#3726
samsonasik merged 4 commits intorectorphp:mainfrom
gaydamakha:feature/swap_method_call_arguments_rector

Conversation

@gaydamakha
Copy link
Copy Markdown
Contributor

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 SwapFuncCallArgumentsRector to write this rector, so there is a bit of code duplication. I was hesitating to get out the SwapMethodCallArgumentsRector::resolveNewArguments method but didn't find a suited name neither namespace for a class. If you have any idea I'll be glad to refactor this.

@gaydamakha gaydamakha requested a review from TomasVotruba as a code owner May 2, 2023 07:58
@gaydamakha gaydamakha changed the title ✨ add SwapMethodCallArgumentsRector Add SwapMethodCallArgumentsRector May 2, 2023
@TomasVotruba
Copy link
Copy Markdown
Member

Hi, thanks for the feature.
What is the use case here?

@gaydamakha
Copy link
Copy Markdown
Contributor Author

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.

@TomasVotruba
Copy link
Copy Markdown
Member

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?

@gaydamakha
Copy link
Copy Markdown
Contributor Author

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])) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

call->getArgs() in loop can be moved to before loop to a variable

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@samsonasik Could you handle the update and then merge it?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes, updated 01c5f05

@TomasVotruba
Copy link
Copy Markdown
Member

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!

Well said, let's give it a try if this will catch up 👍 Thank you

@samsonasik samsonasik enabled auto-merge (squash) May 6, 2023 18:08
@samsonasik samsonasik force-pushed the feature/swap_method_call_arguments_rector branch from 01c5f05 to 30e2381 Compare May 6, 2023 18:08
@samsonasik samsonasik merged commit 575e799 into rectorphp:main May 6, 2023
samsonasik added a commit that referenced this pull request May 8, 2023
* ✨ 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>
@TomasVotruba
Copy link
Copy Markdown
Member

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 👍

@TomasVotruba
Copy link
Copy Markdown
Member

Ref #4766

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants