From d01fafcb40a5de95d3a8272947b38cb39d47c997 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Thu, 25 Jan 2018 22:27:37 +0100 Subject: [PATCH] Handle +(++$x) and -(--$x) as well --- lib/PhpParser/PrettyPrinter/Standard.php | 4 ++-- test/code/prettyPrinter/expr/parentheses.test | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/PhpParser/PrettyPrinter/Standard.php b/lib/PhpParser/PrettyPrinter/Standard.php index 888ef4a..1825a31 100644 --- a/lib/PhpParser/PrettyPrinter/Standard.php +++ b/lib/PhpParser/PrettyPrinter/Standard.php @@ -374,7 +374,7 @@ class Standard extends PrettyPrinterAbstract } protected function pExpr_UnaryMinus(Expr\UnaryMinus $node) { - if ($node->expr instanceof Expr\UnaryMinus) { + if ($node->expr instanceof Expr\UnaryMinus || $node->expr instanceof Expr\PreDec) { // Enforce -(-$expr) instead of --$expr return '-(' . $this->p($node->expr) . ')'; } @@ -382,7 +382,7 @@ class Standard extends PrettyPrinterAbstract } protected function pExpr_UnaryPlus(Expr\UnaryPlus $node) { - if ($node->expr instanceof Expr\UnaryPlus) { + if ($node->expr instanceof Expr\UnaryPlus || $node->expr instanceof Expr\PreInc) { // Enforce +(+$expr) instead of ++$expr return '+(' . $this->p($node->expr) . ')'; } diff --git a/test/code/prettyPrinter/expr/parentheses.test b/test/code/prettyPrinter/expr/parentheses.test index 0d1cfeb..a49c110 100644 --- a/test/code/prettyPrinter/expr/parentheses.test +++ b/test/code/prettyPrinter/expr/parentheses.test @@ -40,6 +40,8 @@ print ($a and print $b); -(-$a); +(+$a); +-(--$a); ++(++$a); // The following will currently add unnecessary parentheses, because the pretty printer is not aware that assignment // and incdec only work on variables. @@ -75,6 +77,8 @@ yield from ($a and yield from $b); print ($a and print $b); -(-$a); +(+$a); +-(--$a); ++(++$a); // The following will currently add unnecessary parentheses, because the pretty printer is not aware that assignment // and incdec only work on variables. !($a = $b);