Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PrettyPrinter\\Standard: fixes nested ternaries output; #719

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/PhpParser/PrettyPrinter/Standard.php
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion test/code/prettyPrinter/expr/parentheses.test
Expand Up @@ -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;
Expand Down