Skip to content

Commit 832f338

Browse files
committed
[CodeQuality] Preserve parentheses around right-side assign in LogicalToBooleanRector
Fixes rectorphp/rector#9799 When an assignment sits on the right/nested side of a converted boolean operator, the printer reused the Assign node verbatim and dropped the parentheses the pretty-printer would add in context: true and ($x = 42) and false; // became: true && $x = 42 && false; -> $x = (42 && false) = false The right-side reprint was guarded by !haveParens, but source parens live in the parent node token range (not the Assign node), so the guard skipped the reprint and the parens were lost anyway. Reprint unconditionally; the pretty-printer only adds parens when semantically needed.
1 parent ffa770e commit 832f338

3 files changed

Lines changed: 34 additions & 0 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
true and $x = 42 and false;
4+
5+
var_dump($x);
6+
7+
?>
8+
-----
9+
<?php
10+
11+
true && ($x = 42) && false;
12+
13+
var_dump($x);
14+
15+
?>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
true and ($x = 42) and false;
4+
5+
var_dump($x);
6+
7+
?>
8+
-----
9+
<?php
10+
11+
true && ($x = 42) && false;
12+
13+
var_dump($x);
14+
15+
?>

‎src/PhpParser/Printer/BetterStandardPrinter.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,10 @@ private function wrapBinaryOpWithBrackets(Node $node): void
478478
$node->left->setAttribute(AttributeKey::ORIGINAL_NODE, null);
479479
}
480480

481+
if ($node->right instanceof Assign && $this->origTokens instanceof TokenStream) {
482+
$node->right->setAttribute(AttributeKey::ORIGINAL_NODE, null);
483+
}
484+
481485
if ($node->left instanceof BinaryOp &&
482486
$node->left->getAttribute(AttributeKey::ORIGINAL_NODE) instanceof Node) {
483487
$node->left->setAttribute(AttributeKey::ORIGINAL_NODE, null);

0 commit comments

Comments
 (0)