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] Add RemoveUselessReturnExprInConstructRector #5158

Merged
merged 10 commits into from
Oct 12, 2023

Conversation

samsonasik
Copy link
Member

@samsonasik samsonasik commented Oct 12, 2023

@TomasVotruba @staabm here new rule to remove useless Return Expr in construct:

 class SomeClass
 {
     public function __construct()
     {
         if (rand(0, 1)) {
             $this->init();
-            return true;
+            return;
         }

         if (rand(2, 3)) {
-            return parent::construct();
+            parent::construct();
+            return;
         }

         $this->execute();
     }
 }

@samsonasik samsonasik changed the title [DeadCode] Add RemoveUselessReturnExprInConstruct [DeadCode] Add RemoveUselessReturnExprInConstructRector Oct 12, 2023
@samsonasik
Copy link
Member Author

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

Copy link
Contributor

@staabm staabm left a comment

Choose a reason for hiding this comment

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

awesome

@samsonasik
Copy link
Member Author

samsonasik commented Oct 12, 2023

It seems it can cause a bug if it call a method call, eg:

return parent::__construct();

which should be :

-return parent::__construct();
+parent::__construct();
+return;

so use its Expr to Expression for that case

@samsonasik samsonasik marked this pull request as draft October 12, 2023 07:59
@TomasVotruba
Copy link
Member

Looks good 👍 Feel free to merge when ready

@samsonasik
Copy link
Member Author

I added fixture for return dynamic Expr 1e79d54

@samsonasik samsonasik marked this pull request as ready for review October 12, 2023 10:17
@samsonasik
Copy link
Member Author

samsonasik commented Oct 12, 2023

Implemented, I added ability to CallableNodeVisitor to allow return array of Node (it Stmt[] obviously, but mark as Node[] to ensure same signature with what in AbstractRector), to allow replace single node to multi nodes on usage:

-return parent::__construct();
+parent::__construct();
+return;

with SimpleCallableNodeTraverser c1f0717

@samsonasik
Copy link
Member Author

All checks have passed 🎉 @TomasVotruba I am merging it to have faster feedback to test ;)

@samsonasik samsonasik merged commit b7a5fef into main Oct 12, 2023
@samsonasik samsonasik deleted the remove-useless-return-expr-in-construct branch October 12, 2023 10:22
@TomasVotruba
Copy link
Member

👏

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.

4 participants