Support new variables in fixup

(cherry picked from commit 6a88bdb05a169fcd38c2ed1e6cf129a5e9d51cf0)
This commit is contained in:
Nikita Popov 2023-02-26 15:17:07 +01:00
parent 0aad06bce3
commit 8f8e47b6c1
3 changed files with 17 additions and 4 deletions

View File

@ -21,6 +21,7 @@ abstract class PrettyPrinterAbstract
const FIXUP_BRACED_NAME = 4; // Name operand that may require bracing
const FIXUP_VAR_BRACED_NAME = 5; // Name operand that may require ${} bracing
const FIXUP_ENCAPSED = 6; // Encapsed string part
const FIXUP_NEW = 7; // New/instanceof operand
protected $precedenceMap = [
// [precedence, associativity]
@ -977,6 +978,12 @@ abstract class PrettyPrinterAbstract
return '(' . $this->p($subNode) . ')';
}
break;
case self::FIXUP_NEW:
if ($this->newOperandRequiresParens($subNode)
&& !$this->origTokens->haveParens($subStartPos, $subEndPos)) {
return '(' . $this->p($subNode) . ')';
}
break;
case self::FIXUP_BRACED_NAME:
case self::FIXUP_VAR_BRACED_NAME:
if ($subNode instanceof Expr
@ -1193,7 +1200,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, // TODO: FIXUP_NEW_VARIABLE
'class' => self::FIXUP_NEW,
],
Expr\Ternary::class => [
'cond' => self::FIXUP_PREC_LEFT,
@ -1204,7 +1211,7 @@ abstract class PrettyPrinterAbstract
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\New_::class => ['class' => self::FIXUP_NEW],
Expr\MethodCall::class => [
'var' => self::FIXUP_DEREF_LHS,
'name' => self::FIXUP_BRACED_NAME,

View File

@ -42,6 +42,8 @@ $foo -> bar;
$foo -> bar;
self :: $foo;
self :: $foo;
new Foo();
$x instanceof Foo;
-----
$stmts[0]->expr->name = new Expr\Variable('a');
$stmts[1]->expr->name = new Expr\BinaryOp\Concat(new Expr\Variable('a'), new Expr\Variable('b'));
@ -54,6 +56,8 @@ $stmts[5]->expr->name = new Expr\Variable('bar');
$stmts[6]->expr->name = new Expr\BinaryOp\Concat(new Expr\Variable('a'), new Expr\Variable('b'));
$stmts[7]->expr->name = new Node\VarLikeIdentifier('bar');
$stmts[8]->expr->name = new Expr\BinaryOp\Concat(new Expr\Variable('a'), new Expr\Variable('b'));
$stmts[9]->expr->class = new Scalar\String_('Foo');
$stmts[10]->expr->class = new Scalar\String_('Foo');
-----
<?php
$a ();
@ -64,4 +68,6 @@ $foo -> foo;
$foo -> {$bar};
$foo -> {$a . $b};
self :: $bar;
self :: ${$a . $b};
self :: ${$a . $b};
new ('Foo')();
$x instanceof ('Foo');

View File

@ -18,6 +18,6 @@ new (foo())();
new ('foo')();
new (x[0])();
new (x->y)();
new ((x)::$y)();
new (x::$y)();
$x instanceof ('a' . 'b');
$x instanceof ($y++);