From 2a595545d099500a9feb8ffeb7fbd0ae225184b7 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Mon, 6 Jul 2026 21:20:51 +0200 Subject: [PATCH] [CodeQuality] Wrap assign on right side of binary op in parentheses in LogicalToBooleanRector --- .../Fixture/assign_on_right.php.inc | 15 +++++++++++++++ src/PhpParser/Printer/BetterStandardPrinter.php | 6 ++++++ 2 files changed, 21 insertions(+) create mode 100644 rules-tests/CodeQuality/Rector/LogicalAnd/LogicalToBooleanRector/Fixture/assign_on_right.php.inc diff --git a/rules-tests/CodeQuality/Rector/LogicalAnd/LogicalToBooleanRector/Fixture/assign_on_right.php.inc b/rules-tests/CodeQuality/Rector/LogicalAnd/LogicalToBooleanRector/Fixture/assign_on_right.php.inc new file mode 100644 index 00000000000..400dfa49381 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/LogicalAnd/LogicalToBooleanRector/Fixture/assign_on_right.php.inc @@ -0,0 +1,15 @@ + +----- + diff --git a/src/PhpParser/Printer/BetterStandardPrinter.php b/src/PhpParser/Printer/BetterStandardPrinter.php index 96067ef515f..f2f18342f9c 100644 --- a/src/PhpParser/Printer/BetterStandardPrinter.php +++ b/src/PhpParser/Printer/BetterStandardPrinter.php @@ -478,6 +478,12 @@ 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())) { + $node->right->setAttribute(AttributeKey::ORIGINAL_NODE, null); + } + if ($node->left instanceof BinaryOp && $node->left->getAttribute(AttributeKey::ORIGINAL_NODE) instanceof Node) { $node->left->setAttribute(AttributeKey::ORIGINAL_NODE, null);