diff --git a/lib/PhpParser/PrettyPrinter/Standard.php b/lib/PhpParser/PrettyPrinter/Standard.php index c44bda9833..b4a7e8e693 100644 --- a/lib/PhpParser/PrettyPrinter/Standard.php +++ b/lib/PhpParser/PrettyPrinter/Standard.php @@ -659,6 +659,12 @@ protected function pExpr_Clone(Expr\Clone_ $node) { } protected function pExpr_Ternary(Expr\Ternary $node) { + if ($node->cond instanceof Expr\Ternary) { + return '(' . $this->pInfixOp(Expr\Ternary::class, + $node->cond, ') ?' . (null !== $node->if ? ' ' . $this->p($node->if) . ' ' : '') . ': ', $node->else + ); + } + // a bit of cheating: we treat the ternary as a binary op where the ?...: part is the operator. // this is okay because the part between ? and : never needs parentheses. return $this->pInfixOp(Expr\Ternary::class, diff --git a/test/code/prettyPrinter/expr/parentheses.test b/test/code/prettyPrinter/expr/parentheses.test index a49c1108d2..bda956e3d5 100644 --- a/test/code/prettyPrinter/expr/parentheses.test +++ b/test/code/prettyPrinter/expr/parentheses.test @@ -59,7 +59,7 @@ $a = $b = $c = $d = $f && true; ($a = $b = $c = $d = $f) && true; $a = $b = $c = $d = $f and true; $a = $b = $c = $d = ($f and true); -$a ? $b : $c ? $d : $e ? $f : $g; +(($a ? $b : $c) ? $d : $e) ? $f : $g; $a ? $b : ($c ? $d : ($e ? $f : $g)); $a ? $b ? $c : $d : $f; $a ?? $b ?? $c;