Skip to content
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

[DeadCode] Handle RemoveUnusedPrivateMethodRector+RemoveDuplicatedIfReturnRector when private method used in FuncCall with ArrowFunction #3007

Merged
merged 9 commits into from
Oct 22, 2022

Conversation

samsonasik
Copy link
Member

@samsonasik samsonasik commented Oct 21, 2022

Given the following code:

final class SkipUsedInArrayMap
{
    public function run(): array
    {
        return array_map(
            static fn (string $bar): string => self::returnFoo(),
            ['bar']
        );
    }

    private static function returnFoo(): string
    {
        return 'foo';
    }
}

It currently remove the private method, which should not, as it used. Applied rules:

Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodRector;
Rector\DeadCode\Rector\FunctionLike\RemoveDuplicatedIfReturnRector;

Ref https://getrector.org/demo/66d03737-04b4-4345-89e5-074046ba496c

This PR try to fix it. Fixes rectorphp/rector#7547

…eturnRector when private method used in array_map
@samsonasik
Copy link
Member Author

Fixed 🎉 /cc @alcohol

@samsonasik
Copy link
Member Author

All checks have passed 🎉 @TomasVotruba it is ready for review.

@samsonasik samsonasik marked this pull request as draft October 21, 2022 16:58
@samsonasik
Copy link
Member Author

The issue seems not about array_map(), change to other FuncCall seems still removed.

@samsonasik samsonasik changed the title [DeadCode] Handle RemoveUnusedPrivateMethodRector+RemoveDuplicatedIfReturnRector when private method used in array_map [DeadCode] Handle RemoveUnusedPrivateMethodRector+RemoveDuplicatedIfReturnRector when private method used in FuncCall Oct 21, 2022
@samsonasik samsonasik force-pushed the handle-used-in-array-map- branch from a073b84 to ba39cbe Compare October 21, 2022 17:47
@samsonasik samsonasik marked this pull request as ready for review October 21, 2022 17:47
@samsonasik
Copy link
Member Author

It seems can be resolved by verify node has parent for ParentConnectingVisitor on SimpleCallableNodeTraverser

@samsonasik
Copy link
Member Author

All checks have passed 🎉 @TomasVotruba I think it is ready.

@samsonasik
Copy link
Member Author

Ok, I got it, the ArrowFunction when call ->getStmts(), it returns :

return [new Node\Stmt\Return_($this->expr)]

while the Node\Stmt\Return_ itself doesn't has parent Node yet.

It can be correctly handled by check Return_ statement has no parent, while Return_ expr has parent ArrowFunction, so mirror Return_ parent to the ArrowFunction f7125f3

@samsonasik samsonasik changed the title [DeadCode] Handle RemoveUnusedPrivateMethodRector+RemoveDuplicatedIfReturnRector when private method used in FuncCall [DeadCode] Handle RemoveUnusedPrivateMethodRector+RemoveDuplicatedIfReturnRector when private method used in FuncCall with ArrowFunction Oct 22, 2022
@samsonasik
Copy link
Member Author

All checks have passed 🎉 @TomasVotruba I think it is ready.

@TomasVotruba TomasVotruba merged commit 81e326c into main Oct 22, 2022
@TomasVotruba TomasVotruba deleted the handle-used-in-array-map- branch October 22, 2022 09:50
@TomasVotruba
Copy link
Member

Thank you 😊

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.

Incorrect behavior of RemoveUnusedPrivateMethodRector
3 participants