mirror of
https://github.com/phabelio/PHP-Parser.git
synced 2024-11-30 04:29:15 +01:00
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:
parent
feb6bf7a0c
commit
8bcaa4261e
@ -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
|
||||
|
@ -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,
|
||||
],
|
||||
|
11
test/code/prettyPrinter/expr/newVariable.test
Normal file
11
test/code/prettyPrinter/expr/newVariable.test
Normal 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++);
|
Loading…
Reference in New Issue
Block a user