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.
This commit is contained in:
Nikita Popov 2020-08-09 22:06:34 +02:00
parent feb6bf7a0c
commit 8bcaa4261e
3 changed files with 25 additions and 6 deletions

View File

@ -387,7 +387,10 @@ class Standard extends PrettyPrinterAbstract
} }
protected function pExpr_Instanceof(Expr\Instanceof_ $node) { 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 // Unary expressions
@ -635,7 +638,8 @@ class Standard extends PrettyPrinterAbstract
$args = $node->args ? '(' . $this->pMaybeMultiline($node->args) . ')' : ''; $args = $node->args ? '(' . $this->pMaybeMultiline($node->args) . ')' : '';
return 'new ' . $this->pClassCommon($node->class, $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) { 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 * @param Node[] $nodes
* @return bool * @return bool

View File

@ -1127,7 +1127,7 @@ abstract class PrettyPrinterAbstract
Expr\PostDec::class => ['var' => self::FIXUP_PREC_LEFT], Expr\PostDec::class => ['var' => self::FIXUP_PREC_LEFT],
Expr\Instanceof_::class => [ Expr\Instanceof_::class => [
'expr' => self::FIXUP_PREC_LEFT, 'expr' => self::FIXUP_PREC_LEFT,
'class' => self::FIXUP_PREC_RIGHT, 'class' => self::FIXUP_PREC_RIGHT, // TODO: FIXUP_NEW_VARIABLE
], ],
Expr\Ternary::class => [ Expr\Ternary::class => [
'cond' => self::FIXUP_PREC_LEFT, 'cond' => self::FIXUP_PREC_LEFT,
@ -1137,6 +1137,8 @@ abstract class PrettyPrinterAbstract
Expr\FuncCall::class => ['name' => self::FIXUP_CALL_LHS], Expr\FuncCall::class => ['name' => self::FIXUP_CALL_LHS],
Expr\StaticCall::class => ['class' => self::FIXUP_DEREF_LHS], Expr\StaticCall::class => ['class' => self::FIXUP_DEREF_LHS],
Expr\ArrayDimFetch::class => ['var' => 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 => [ Expr\MethodCall::class => [
'var' => self::FIXUP_DEREF_LHS, 'var' => self::FIXUP_DEREF_LHS,
'name' => self::FIXUP_BRACED_NAME, 'name' => self::FIXUP_BRACED_NAME,
@ -1157,9 +1159,6 @@ abstract class PrettyPrinterAbstract
'var' => self::FIXUP_DEREF_LHS, 'var' => self::FIXUP_DEREF_LHS,
'name' => self::FIXUP_BRACED_NAME, 'name' => self::FIXUP_BRACED_NAME,
], ],
Expr\ClassConstFetch::class => [
'var' => self::FIXUP_DEREF_LHS,
],
Scalar\Encapsed::class => [ Scalar\Encapsed::class => [
'parts' => self::FIXUP_ENCAPSED, 'parts' => self::FIXUP_ENCAPSED,
], ],

View File

@ -0,0 +1,11 @@
Parentheses for complex new/instanceof expressions
-----
<?php
new ('a' . 'b');
$x instanceof ('a' . 'b');
$x instanceof ($y++);
-----
!!php7
new ('a' . 'b')();
$x instanceof ('a' . 'b');
$x instanceof ($y++);