Skip to content

[CodeQuality] Wrap assign on right side of binary op in LogicalToBooleanRector#8151

Merged
TomasVotruba merged 1 commit into
mainfrom
fix-logical-to-boolean-assign-right
Jul 6, 2026
Merged

[CodeQuality] Wrap assign on right side of binary op in LogicalToBooleanRector#8151
TomasVotruba merged 1 commit into
mainfrom
fix-logical-to-boolean-assign-right

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

Fixes rectorphp/rector#9797

LogicalToBooleanRector missed parentheses around an assignment sitting on the right-hand side of a boolean operator, producing code that is not equivalent to the input.

Before

true and $x = 42 and false;

was rewritten to:

true && $x = 42 && false;

which PHP parses as true && ($x = (42 && false)) — so $x is assigned false instead of 42.

After

true && ($x = 42) && false;

Cause

BetterStandardPrinter::wrapBinaryOpWithBrackets() already forced a reprint (adding parentheses) when the left operand of a binary op was an Assign, but had no symmetric handling for the right operand. Added the mirror check.

The left-hand case (already covered) stays correct:

-$f = false and true;
+($f = false) && true;

@TomasVotruba TomasVotruba merged commit 269af16 into main Jul 6, 2026
65 checks passed
@TomasVotruba TomasVotruba deleted the fix-logical-to-boolean-assign-right branch July 6, 2026 19:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect behavior of LogicalToBooleanRector: parentheses are still missing around some assignments

1 participant