Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

$x = 0;
($x += 42) and true;

?>
-----
<?php

$x = 0;
($x += 42) && true;

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

$z = 0;
($z ?: 7) and true;

?>
-----
<?php

$z = 0;
($z ?: 7) && true;

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

(print "foo") and (print "bar");

?>
-----
<?php

(print "foo") && print "bar";

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

(true ? 42 : 0) and true;

?>
-----
<?php

(true ? 42 : 0) && true;

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

function run()
{
(yield 1) and true;
(yield from [1, 2]) and true;
}

?>
-----
<?php

function run()
{
(yield 1) && true;
(yield from [1, 2]) && true;
}

?>
37 changes: 37 additions & 0 deletions src/PhpParser/Printer/BetterStandardPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,19 @@
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\AssignOp;
use PhpParser\Node\Expr\AssignRef;
use PhpParser\Node\Expr\BinaryOp;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
use PhpParser\Node\Expr\BinaryOp\Pipe;
use PhpParser\Node\Expr\CallLike;
use PhpParser\Node\Expr\Instanceof_;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Print_;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Expr\Yield_;
use PhpParser\Node\Expr\YieldFrom;
use PhpParser\Node\InterpolatedStringPart;
use PhpParser\Node\Scalar\InterpolatedString;
use PhpParser\Node\Scalar\String_;
Expand Down Expand Up @@ -482,6 +488,14 @@ private function wrapBinaryOpWithBrackets(Node $node): void
$node->right->setAttribute(AttributeKey::ORIGINAL_NODE, null);
}

// boolean operators (&&, ||) bind tighter than the logical ones (and, or); when
// LogicalToBooleanRector swaps them, operands with lower precedence must be reprinted
// so the pretty-printer re-adds the parentheses that keep the original semantics
if ($node instanceof BooleanAnd || $node instanceof BooleanOr) {
$this->reprintLowerPrecedenceOperand($node->left);
$this->reprintLowerPrecedenceOperand($node->right);
}

if ($node->left instanceof BinaryOp &&
$node->left->getAttribute(AttributeKey::ORIGINAL_NODE) instanceof Node) {
$node->left->setAttribute(AttributeKey::ORIGINAL_NODE, null);
Expand All @@ -493,6 +507,29 @@ private function wrapBinaryOpWithBrackets(Node $node): void
}
}

/**
* @see https://github.com/rectorphp/rector/issues/9800
*/
private function reprintLowerPrecedenceOperand(Node $node): void
{
if (! $node instanceof AssignRef
&& ! $node instanceof AssignOp
&& ! $node instanceof Ternary
&& ! $node instanceof Print_
&& ! $node instanceof Yield_
&& ! $node instanceof YieldFrom) {
return;
}

$node->setAttribute(AttributeKey::ORIGINAL_NODE, null);

// the format-preserving printer keeps yield's precedence parentheses only for the
// "yield from" and empty "yield" forms; "yield <value>" needs them added explicitly
if ($node instanceof Yield_ && $node->value instanceof Expr) {
$node->setAttribute(AttributeKey::WRAPPED_IN_PARENTHESES, true);
}
}

/**
* ensure left side is assign and right side is just created
*
Expand Down
Loading