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

[Downgrade 7.0] Strip unnecessary parentheses around expressions #1617

Merged
merged 1 commit into from
Jan 24, 2022

Conversation

jtojnar
Copy link
Contributor

@jtojnar jtojnar commented Jan 3, 2022

The Uniform variable syntax RFC implemented in PHP 7.0 allows wrapping arbitrary expressions in parentheses, which is not supported by PHP 5. Let’s re-create those AST nodes so that the parentheses are stripped where not strictly necessary.

This does not deal with syntax like ($foo->bar)(), where parentheses are mandatory, which is used to disambiguate method calls from calling callable properties.

@jtojnar jtojnar marked this pull request as ready for review January 4, 2022 23:39
@samsonasik
Copy link
Member

The class name seems too long:

https://github.com/rectorphp/rector-src/runs/4708654341?check_suite_focus=true#step:5:9

Could you try rename to the shorter name? Thank you

@jtojnar
Copy link
Contributor Author

jtojnar commented Jan 9, 2022

It looks to me the rule thinks more than five lines of comments must be code even when it is documentation:

https://github.com/symplify/symplify/blob/b13e0f3357b723635d8a556ea8ecfad6b419be09/packages/easy-ci/src/Command/CheckCommentedCodeCommand.php#L44

@samsonasik
Copy link
Member

I see, How about move comment to the method?

@jtojnar
Copy link
Contributor Author

jtojnar commented Jan 9, 2022

The comment already has a method for itself (sharing it with two statements). I guess I could make the lines slightly longer so it fits on five lines.

@jtojnar
Copy link
Contributor Author

jtojnar commented Jan 9, 2022

Or remove the rule since it does not actually check for commented out code but comment length.

@jtojnar jtojnar force-pushed the remove-extra-parens branch 4 times, most recently from be68c84 to e1246ab Compare January 24, 2022 06:10
@jtojnar jtojnar force-pushed the remove-extra-parens branch 3 times, most recently from 0f0010c to bfd678a Compare January 24, 2022 06:37
The Uniform variable syntax RFC implemented in PHP 7.0 allows wrapping
arbitrary expressions in parentheses, which is not supported by PHP 5.
Since PHP 5 cannot parse that we need to strip them.

And because parenthesization is not part of the AST, we need to force
the code to be re-generated. Rector will re-generate code for AST nodes
that changed. Or we can remove the original node reference to the same effect.
The code generator will only put parentheses where strictly necessary,
like to disambiguate method call from calling a callable property
in `($foo->bar)()`. That should be handled by a different rule.
@TomasVotruba
Copy link
Member

Thank you

@TomasVotruba TomasVotruba merged commit 85719ff into rectorphp:main Jan 24, 2022
@jtojnar jtojnar deleted the remove-extra-parens branch January 24, 2022 08:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants