From 64759044756a62788fd0358320dc861f675d3d3e Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Tue, 7 Jul 2026 18:50:37 +0200 Subject: [PATCH] [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. --- .../assign_with_existing_parentheses.php.inc | 15 +++++++++++++++ src/PhpParser/Printer/BetterStandardPrinter.php | 4 +--- 2 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 rules-tests/CodeQuality/Rector/LogicalAnd/LogicalToBooleanRector/Fixture/assign_with_existing_parentheses.php.inc diff --git a/rules-tests/CodeQuality/Rector/LogicalAnd/LogicalToBooleanRector/Fixture/assign_with_existing_parentheses.php.inc b/rules-tests/CodeQuality/Rector/LogicalAnd/LogicalToBooleanRector/Fixture/assign_with_existing_parentheses.php.inc new file mode 100644 index 00000000000..030587ae5f9 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/LogicalAnd/LogicalToBooleanRector/Fixture/assign_with_existing_parentheses.php.inc @@ -0,0 +1,15 @@ + +----- + diff --git a/src/PhpParser/Printer/BetterStandardPrinter.php b/src/PhpParser/Printer/BetterStandardPrinter.php index f2f18342f9c..5dc11f7e3ed 100644 --- a/src/PhpParser/Printer/BetterStandardPrinter.php +++ b/src/PhpParser/Printer/BetterStandardPrinter.php @@ -478,9 +478,7 @@ private function wrapBinaryOpWithBrackets(Node $node): void $node->left->setAttribute(AttributeKey::ORIGINAL_NODE, null); } - if ($node->right instanceof Assign - && $this->origTokens instanceof TokenStream - && ! $this->origTokens->haveParens($node->right->getStartTokenPos(), $node->right->getEndTokenPos())) { + if ($node->right instanceof Assign && $this->origTokens instanceof TokenStream) { $node->right->setAttribute(AttributeKey::ORIGINAL_NODE, null); }