Skip to content

Commit

Permalink
Fix ternary precedence printing
Browse files Browse the repository at this point in the history
The precedence table set the LHS and RHS precedence for the
ternary to -1, which is the dummy value used for unary operators.
Instead, it should be the same as the operator precedence, as
the ternary is non-associative since PHP 8.

Fixes #1009.
  • Loading branch information
nikic committed Jul 1, 2024
1 parent 3ef0811 commit a894652
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/PhpParser/PrettyPrinterAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ abstract class PrettyPrinterAbstract implements PrettyPrinter {
BinaryOp\BooleanAnd::class => [120, 121, 120],
BinaryOp\BooleanOr::class => [130, 131, 130],
BinaryOp\Coalesce::class => [140, 140, 141],
Expr\Ternary::class => [150, -1, -1],
Expr\Ternary::class => [150, 150, 150],
Expr\Assign::class => [160, -1, -1],
Expr\AssignRef::class => [160, -1, -1],
AssignOp\Plus::class => [160, -1, -1],
Expand Down
2 changes: 2 additions & 0 deletions test/code/prettyPrinter/expr/parentheses.test
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ $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 : $f;
$a === $b ? $c : $d;

$a ?? $b ?? $c;
($a ?? $b) ?? $c;
Expand Down Expand Up @@ -73,6 +74,7 @@ $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 : $f;
$a === $b ? $c : $d;
$a ?? $b ?? $c;
($a ?? $b) ?? $c;
$a ?? ($b ? $c : $d);
Expand Down

0 comments on commit a894652

Please sign in to comment.