Skip to content

Commit

Permalink
Fix logic for new operand parentheses requirement
Browse files Browse the repository at this point in the history
We need to perform this check recursively.

(cherry picked from commit cc34c24)
  • Loading branch information
nikic committed Aug 13, 2023
1 parent 80a680b commit 0aad06b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
18 changes: 12 additions & 6 deletions lib/PhpParser/PrettyPrinterAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -1078,12 +1078,18 @@ protected function dereferenceLhsRequiresParens(Node $node) : bool {
* @return bool Whether parentheses are required
*/
protected function newOperandRequiresParens(Node $node): bool {
return !($node instanceof Node\Name
|| $node instanceof Expr\Variable
|| $node instanceof Expr\ArrayDimFetch
|| $node instanceof Expr\PropertyFetch
|| $node instanceof Expr\NullsafePropertyFetch
|| $node instanceof Expr\StaticPropertyFetch);
if ($node instanceof Node\Name || $node instanceof Expr\Variable) {
return false;
}
if ($node instanceof Expr\ArrayDimFetch || $node instanceof Expr\PropertyFetch ||
$node instanceof Expr\NullsafePropertyFetch
) {
return $this->newOperandRequiresParens($node->var);
}
if ($node instanceof Expr\StaticPropertyFetch) {
return $this->newOperandRequiresParens($node->class);
}
return true;
}

/**
Expand Down
6 changes: 6 additions & 0 deletions test/code/prettyPrinter/expr/newVariable.test
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ new ('a' . 'b');
new (x);
new (foo());
new ('foo');
new (x[0]);
new (x->y);
new ((x)::$y);
$x instanceof ('a' . 'b');
$x instanceof ($y++);
-----
Expand All @@ -13,5 +16,8 @@ new ('a' . 'b')();
new (x)();
new (foo())();
new ('foo')();
new (x[0])();
new (x->y)();
new ((x)::$y)();
$x instanceof ('a' . 'b');
$x instanceof ($y++);

0 comments on commit 0aad06b

Please sign in to comment.