Skip to content

Don't require parentheses for short-ternaries#7604

Merged
samsonasik merged 1 commit into
rectorphp:mainfrom
martinstuecklschwaiger:short-ternary-without-parentheses
Oct 29, 2025
Merged

Don't require parentheses for short-ternaries#7604
samsonasik merged 1 commit into
rectorphp:mainfrom
martinstuecklschwaiger:short-ternary-without-parentheses

Conversation

@martinstuecklschwaiger
Copy link
Copy Markdown
Contributor

Chaining of short-ternaries (?: also called elvis operator) is stable and behaves reasonably.

This is described in the PHP Manual right below the warning about using nested ternary expressions.

https://www.php.net/manual/en/language.operators.comparison.php#:~:text=Chaining%20of%20short%2Dternaries%20(%3F%3A)%2C%20however%2C%20is%20stable%20and%20behaves%20reasonably

The test code is taken right from the PHP Manual.

This fixes rectorphp/rector#8552.

Chaining of short-ternaries (`?:` also called elvis operator) is stable
and behaves reasonably

This is described in the PHP Manual right below the warning about using
nested ternary expressions.

The example code is taken from the PHP Manual.

https://www.php.net/manual/en/language.operators.comparison.php#:~:text=Chaining%20of%20short%2Dternaries%20(%3F%3A)%2C%20however%2C%20is%20stable%20and%20behaves%20reasonably
@samsonasik
Copy link
Copy Markdown
Member

Okay, let's give it a try, thank you @martinstuecklschwaiger

@samsonasik samsonasik merged commit 2ed3aa8 into rectorphp:main Oct 29, 2025
53 checks passed
@martinstuecklschwaiger martinstuecklschwaiger deleted the short-ternary-without-parentheses branch October 29, 2025 14:55
TomasVotruba added a commit that referenced this pull request Oct 29, 2025
TomasVotruba added a commit that referenced this pull request Oct 29, 2025
@github-actions
Copy link
Copy Markdown
Contributor

This pull request has been automatically locked because it has been closed for 150 days. Please open a new PR if you want to continue the work.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Mar 29, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect behavior of ParenthesizeNestedTernaryRector: elvis operator is interpreted as a ternary

2 participants