From 8bcaa4261e408d65406481b65c99e69c4f0bba4c Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Sun, 9 Aug 2020 22:06:34 +0200 Subject: [PATCH] Add parens for new/instanceof with complex expression This is not fully accurate because the rules for "new variables" are different than the rules for dereferenceable LHS. --- lib/PhpParser/PrettyPrinter/Standard.php | 13 +++++++++++-- lib/PhpParser/PrettyPrinterAbstract.php | 7 +++---- test/code/prettyPrinter/expr/newVariable.test | 11 +++++++++++ 3 files changed, 25 insertions(+), 6 deletions(-) create mode 100644 test/code/prettyPrinter/expr/newVariable.test diff --git a/lib/PhpParser/PrettyPrinter/Standard.php b/lib/PhpParser/PrettyPrinter/Standard.php index 14da6b9..e659593 100644 --- a/lib/PhpParser/PrettyPrinter/Standard.php +++ b/lib/PhpParser/PrettyPrinter/Standard.php @@ -387,7 +387,10 @@ class Standard extends PrettyPrinterAbstract } protected function pExpr_Instanceof(Expr\Instanceof_ $node) { - return $this->pInfixOp(Expr\Instanceof_::class, $node->expr, ' instanceof ', $node->class); + list($precedence, $associativity) = $this->precedenceMap[Expr\Instanceof_::class]; + return $this->pPrec($node->expr, $precedence, $associativity, -1) + . ' instanceof ' + . $this->pNewVariable($node->class); } // Unary expressions @@ -635,7 +638,8 @@ class Standard extends PrettyPrinterAbstract $args = $node->args ? '(' . $this->pMaybeMultiline($node->args) . ')' : ''; return 'new ' . $this->pClassCommon($node->class, $args); } - return 'new ' . $this->p($node->class) . '(' . $this->pMaybeMultiline($node->args) . ')'; + return 'new ' . $this->pNewVariable($node->class) + . '(' . $this->pMaybeMultiline($node->args) . ')'; } protected function pExpr_Clone(Expr\Clone_ $node) { @@ -1008,6 +1012,11 @@ class Standard extends PrettyPrinterAbstract } } + protected function pNewVariable(Node $node) { + // TODO: This is not fully accurate. + return $this->pDereferenceLhs($node); + } + /** * @param Node[] $nodes * @return bool diff --git a/lib/PhpParser/PrettyPrinterAbstract.php b/lib/PhpParser/PrettyPrinterAbstract.php index 22a1247..1a6ba39 100644 --- a/lib/PhpParser/PrettyPrinterAbstract.php +++ b/lib/PhpParser/PrettyPrinterAbstract.php @@ -1127,7 +1127,7 @@ abstract class PrettyPrinterAbstract Expr\PostDec::class => ['var' => self::FIXUP_PREC_LEFT], Expr\Instanceof_::class => [ 'expr' => self::FIXUP_PREC_LEFT, - 'class' => self::FIXUP_PREC_RIGHT, + 'class' => self::FIXUP_PREC_RIGHT, // TODO: FIXUP_NEW_VARIABLE ], Expr\Ternary::class => [ 'cond' => self::FIXUP_PREC_LEFT, @@ -1137,6 +1137,8 @@ abstract class PrettyPrinterAbstract Expr\FuncCall::class => ['name' => self::FIXUP_CALL_LHS], Expr\StaticCall::class => ['class' => self::FIXUP_DEREF_LHS], Expr\ArrayDimFetch::class => ['var' => self::FIXUP_DEREF_LHS], + Expr\ClassConstFetch::class => ['var' => self::FIXUP_DEREF_LHS], + Expr\New_::class => ['class' => self::FIXUP_DEREF_LHS], // TODO: FIXUP_NEW_VARIABLE Expr\MethodCall::class => [ 'var' => self::FIXUP_DEREF_LHS, 'name' => self::FIXUP_BRACED_NAME, @@ -1157,9 +1159,6 @@ abstract class PrettyPrinterAbstract 'var' => self::FIXUP_DEREF_LHS, 'name' => self::FIXUP_BRACED_NAME, ], - Expr\ClassConstFetch::class => [ - 'var' => self::FIXUP_DEREF_LHS, - ], Scalar\Encapsed::class => [ 'parts' => self::FIXUP_ENCAPSED, ], diff --git a/test/code/prettyPrinter/expr/newVariable.test b/test/code/prettyPrinter/expr/newVariable.test new file mode 100644 index 0000000..d5283b3 --- /dev/null +++ b/test/code/prettyPrinter/expr/newVariable.test @@ -0,0 +1,11 @@ +Parentheses for complex new/instanceof expressions +----- +