From 63fd5ecb4410aa96d28209df591a0200ff61d25b Mon Sep 17 00:00:00 2001 From: Olivier Dolbeau Date: Thu, 19 Nov 2026 14:55:31 +0100 Subject: [PATCH] [Printer] Wrap compound assign on the left of instanceof and binary ops wrapAssign() only wrapped Assign, so a rule building an Instanceof_ around an AssignOp lost its parentheses: ExplicitNullableObjectInstanceRector turned `! ($param ??= $fallback)` into `!$param ??= $fallback instanceof Foo`, which PHP parses as `$param ??= ($fallback instanceof Foo)`. Co-Authored-By: Claude Opus 5.5 --- .../Fixture/on_coalesce_assign.php.inc | 35 +++++++++++++++++++ .../Printer/BetterStandardPrinter.php | 2 +- 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 rules-tests/CodeQuality/Rector/If_/ExplicitNullableObjectInstanceRector/Fixture/on_coalesce_assign.php.inc diff --git a/rules-tests/CodeQuality/Rector/If_/ExplicitNullableObjectInstanceRector/Fixture/on_coalesce_assign.php.inc b/rules-tests/CodeQuality/Rector/If_/ExplicitNullableObjectInstanceRector/Fixture/on_coalesce_assign.php.inc new file mode 100644 index 00000000000..721446fc276 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/If_/ExplicitNullableObjectInstanceRector/Fixture/on_coalesce_assign.php.inc @@ -0,0 +1,35 @@ + +----- + diff --git a/src/PhpParser/Printer/BetterStandardPrinter.php b/src/PhpParser/Printer/BetterStandardPrinter.php index ec7ef38461f..71d36f93364 100644 --- a/src/PhpParser/Printer/BetterStandardPrinter.php +++ b/src/PhpParser/Printer/BetterStandardPrinter.php @@ -617,7 +617,7 @@ private function reprintLowerPrecedenceOperand(Node $node): void */ private function wrapAssign(Node $leftNode, Node $rightNode): void { - if ($leftNode instanceof Assign && $leftNode->getStartTokenPos() > 0 && $rightNode->getStartTokenPos() < 0) { + if (($leftNode instanceof Assign || $leftNode instanceof AssignOp) && $leftNode->getStartTokenPos() > 0 && $rightNode->getStartTokenPos() < 0) { $leftNode->setAttribute(AttributeKey::WRAPPED_IN_PARENTHESES, true); } }