Skip to content

Commit 8bcaa42

Browse files
committed
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.
1 parent feb6bf7 commit 8bcaa42

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

lib/PhpParser/PrettyPrinter/Standard.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,10 @@ protected function pExpr_BinaryOp_Coalesce(BinaryOp\Coalesce $node) {
387387
}
388388

389389
protected function pExpr_Instanceof(Expr\Instanceof_ $node) {
390-
return $this->pInfixOp(Expr\Instanceof_::class, $node->expr, ' instanceof ', $node->class);
390+
list($precedence, $associativity) = $this->precedenceMap[Expr\Instanceof_::class];
391+
return $this->pPrec($node->expr, $precedence, $associativity, -1)
392+
. ' instanceof '
393+
. $this->pNewVariable($node->class);
391394
}
392395

393396
// Unary expressions
@@ -635,7 +638,8 @@ protected function pExpr_New(Expr\New_ $node) {
635638
$args = $node->args ? '(' . $this->pMaybeMultiline($node->args) . ')' : '';
636639
return 'new ' . $this->pClassCommon($node->class, $args);
637640
}
638-
return 'new ' . $this->p($node->class) . '(' . $this->pMaybeMultiline($node->args) . ')';
641+
return 'new ' . $this->pNewVariable($node->class)
642+
. '(' . $this->pMaybeMultiline($node->args) . ')';
639643
}
640644

641645
protected function pExpr_Clone(Expr\Clone_ $node) {
@@ -1008,6 +1012,11 @@ protected function pCallLhs(Node $node) {
10081012
}
10091013
}
10101014

1015+
protected function pNewVariable(Node $node) {
1016+
// TODO: This is not fully accurate.
1017+
return $this->pDereferenceLhs($node);
1018+
}
1019+
10111020
/**
10121021
* @param Node[] $nodes
10131022
* @return bool

lib/PhpParser/PrettyPrinterAbstract.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,7 +1127,7 @@ protected function initializeFixupMap() {
11271127
Expr\PostDec::class => ['var' => self::FIXUP_PREC_LEFT],
11281128
Expr\Instanceof_::class => [
11291129
'expr' => self::FIXUP_PREC_LEFT,
1130-
'class' => self::FIXUP_PREC_RIGHT,
1130+
'class' => self::FIXUP_PREC_RIGHT, // TODO: FIXUP_NEW_VARIABLE
11311131
],
11321132
Expr\Ternary::class => [
11331133
'cond' => self::FIXUP_PREC_LEFT,
@@ -1137,6 +1137,8 @@ protected function initializeFixupMap() {
11371137
Expr\FuncCall::class => ['name' => self::FIXUP_CALL_LHS],
11381138
Expr\StaticCall::class => ['class' => self::FIXUP_DEREF_LHS],
11391139
Expr\ArrayDimFetch::class => ['var' => self::FIXUP_DEREF_LHS],
1140+
Expr\ClassConstFetch::class => ['var' => self::FIXUP_DEREF_LHS],
1141+
Expr\New_::class => ['class' => self::FIXUP_DEREF_LHS], // TODO: FIXUP_NEW_VARIABLE
11401142
Expr\MethodCall::class => [
11411143
'var' => self::FIXUP_DEREF_LHS,
11421144
'name' => self::FIXUP_BRACED_NAME,
@@ -1157,9 +1159,6 @@ protected function initializeFixupMap() {
11571159
'var' => self::FIXUP_DEREF_LHS,
11581160
'name' => self::FIXUP_BRACED_NAME,
11591161
],
1160-
Expr\ClassConstFetch::class => [
1161-
'var' => self::FIXUP_DEREF_LHS,
1162-
],
11631162
Scalar\Encapsed::class => [
11641163
'parts' => self::FIXUP_ENCAPSED,
11651164
],
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Parentheses for complex new/instanceof expressions
2+
-----
3+
<?php
4+
new ('a' . 'b');
5+
$x instanceof ('a' . 'b');
6+
$x instanceof ($y++);
7+
-----
8+
!!php7
9+
new ('a' . 'b')();
10+
$x instanceof ('a' . 'b');
11+
$x instanceof ($y++);

0 commit comments

Comments
 (0)