Add named argument support

Not adding any explicit FPPP support, as I don't think add/remove
of names can be handled any better than full reformatting.
This commit is contained in:
Nikita Popov 2020-08-09 17:37:31 +02:00
parent 722119502f
commit 98f7f39d1c
21 changed files with 991 additions and 780 deletions

View File

@ -1,6 +1,10 @@
Version 4.8.1-dev
-----------------
### Added
* [PHP 8.0] Added support for named arguments, represented using a new `name` subnode on `Arg`.
Version 4.8.0 (2020-08-09)
--------------------------

View File

@ -527,6 +527,8 @@ argument:
expr { $$ = Node\Arg[$1, false, false]; }
| '&' variable { $$ = Node\Arg[$2, true, false]; }
| T_ELLIPSIS expr { $$ = Node\Arg[$2, false, true]; }
| identifier_ex ':' expr
{ $$ = new Node\Arg($3, false, false, attributes(), $1); }
;
global_var_list:

View File

@ -6,6 +6,8 @@ use PhpParser\NodeAbstract;
class Arg extends NodeAbstract
{
/** @var Identifier|null Parameter name (for named parameters) */
public $name;
/** @var Expr Value to pass */
public $value;
/** @var bool Whether to pass by ref */
@ -20,16 +22,21 @@ class Arg extends NodeAbstract
* @param bool $byRef Whether to pass by ref
* @param bool $unpack Whether to unpack the argument
* @param array $attributes Additional attributes
* @param Identifier|null $name Parameter name (for named parameters)
*/
public function __construct(Expr $value, bool $byRef = false, bool $unpack = false, array $attributes = []) {
public function __construct(
Expr $value, bool $byRef = false, bool $unpack = false, array $attributes = [],
Identifier $name = null
) {
$this->attributes = $attributes;
$this->name = $name;
$this->value = $value;
$this->byRef = $byRef;
$this->unpack = $unpack;
}
public function getSubNodeNames() : array {
return ['value', 'byRef', 'unpack'];
return ['name', 'value', 'byRef', 'unpack'];
}
public function getType() : string {

File diff suppressed because it is too large Load Diff

View File

@ -27,7 +27,9 @@ class Standard extends PrettyPrinterAbstract
}
protected function pArg(Node\Arg $node) {
return ($node->byRef ? '&' : '') . ($node->unpack ? '...' : '') . $this->p($node->value);
return ($node->name ? $node->name->toString() . ': ' : '')
. ($node->byRef ? '&' : '') . ($node->unpack ? '...' : '')
. $this->p($node->value);
}
protected function pConst(Node\Const_ $node) {

View File

@ -0,0 +1,39 @@
Named arguments
-----
<?php
foo(
a: $b,
);
-----
$stmts[0]->expr->args[0]->name = null;
-----
<?php
foo(
$b,
);
-----
<?php
foo(
$b,
);
-----
$stmts[0]->expr->args[0]->name = new Node\Identifier('a');
-----
<?php
foo(
a: $b,
);
-----
<?php
foo(
a:
$b,
);
-----
$stmts[0]->expr->args[0]->name = new Node\Identifier('XYZ');
-----
<?php
foo(
XYZ:
$b,
);

View File

@ -866,6 +866,7 @@ array(
)
args: array(
0: Arg[3:5 - 3:9](
name: null
value: Expr_ClassConstFetch[3:5 - 3:9](
class: Name[3:5 - 3:7](
parts: array(

View File

@ -262,6 +262,7 @@ array(
)
args: array(
0: Arg(
name: null
value: Expr_Array(
items: array(
0: Expr_ArrayItem(

View File

@ -29,6 +29,7 @@ array(
)
args: array(
0: Arg(
name: null
value: Expr_Variable(
name: a
)
@ -47,6 +48,7 @@ array(
)
args: array(
0: Arg(
name: null
value: Expr_Variable(
name: a
)
@ -54,6 +56,7 @@ array(
unpack: false
)
1: Arg(
name: null
value: Expr_Variable(
name: b
)
@ -72,6 +75,7 @@ array(
)
args: array(
0: Arg(
name: null
value: Expr_Variable(
name: a
)
@ -90,6 +94,7 @@ array(
)
args: array(
0: Arg(
name: null
value: Expr_Variable(
name: a
)
@ -97,6 +102,7 @@ array(
unpack: false
)
1: Arg(
name: null
value: Expr_Variable(
name: b
)

View File

@ -0,0 +1,61 @@
Named arguments
-----
<?php
foo(a: $b, c: $d);
bar(class: 0);
-----
!!php7
array(
0: Stmt_Expression(
expr: Expr_FuncCall(
name: Name(
parts: array(
0: foo
)
)
args: array(
0: Arg(
name: Identifier(
name: a
)
value: Expr_Variable(
name: b
)
byRef: false
unpack: false
)
1: Arg(
name: Identifier(
name: c
)
value: Expr_Variable(
name: d
)
byRef: false
unpack: false
)
)
)
)
1: Stmt_Expression(
expr: Expr_FuncCall(
name: Name(
parts: array(
0: bar
)
)
args: array(
0: Arg(
name: Identifier(
name: class
)
value: Scalar_LNumber(
value: 0
)
byRef: false
unpack: false
)
)
)
)
)

View File

@ -39,6 +39,7 @@ array(
)
args: array(
0: Arg(
name: null
value: Expr_Variable(
name: b
)

View File

@ -30,6 +30,7 @@ array(
)
args: array(
0: Arg(
name: null
value: Expr_Variable(
name: c
)

View File

@ -20,6 +20,7 @@ array(
)
args: array(
0: Arg(
name: null
value: Expr_Variable(
name: a
)
@ -27,6 +28,7 @@ array(
unpack: false
)
1: Arg(
name: null
value: Expr_Variable(
name: b
)
@ -46,6 +48,7 @@ array(
)
args: array(
0: Arg(
name: null
value: Expr_Variable(
name: a
)
@ -53,6 +56,7 @@ array(
unpack: false
)
1: Arg(
name: null
value: Expr_Variable(
name: b
)
@ -74,6 +78,7 @@ array(
)
args: array(
0: Arg(
name: null
value: Expr_Variable(
name: a
)
@ -81,6 +86,7 @@ array(
unpack: false
)
1: Arg(
name: null
value: Expr_Variable(
name: b
)
@ -99,6 +105,7 @@ array(
)
args: array(
0: Arg(
name: null
value: Expr_Variable(
name: a
)
@ -106,6 +113,7 @@ array(
unpack: false
)
1: Arg(
name: null
value: Expr_Variable(
name: b
)

View File

@ -27,6 +27,7 @@ array(
)
args: array(
0: Arg(
name: null
value: Scalar_String(
value: var_dump
)
@ -37,6 +38,7 @@ array(
)
args: array(
0: Arg(
name: null
value: Scalar_LNumber(
value: 1
)
@ -57,6 +59,7 @@ array(
)
args: array(
0: Arg(
name: null
value: Scalar_String(
value: id
)
@ -67,6 +70,7 @@ array(
)
args: array(
0: Arg(
name: null
value: Scalar_String(
value: var_dump
)
@ -77,6 +81,7 @@ array(
)
args: array(
0: Arg(
name: null
value: Scalar_LNumber(
value: 2
)
@ -104,6 +109,7 @@ array(
)
args: array(
0: Arg(
name: null
value: Scalar_String(
value: var_dump
)
@ -114,6 +120,7 @@ array(
)
args: array(
0: Arg(
name: null
value: Scalar_LNumber(
value: 4
)
@ -136,6 +143,7 @@ array(
)
args: array(
0: Arg(
name: null
value: Expr_Array(
items: array(
0: Expr_ArrayItem(
@ -170,6 +178,7 @@ array(
)
args: array(
0: Arg(
name: null
value: Scalar_String(
value: var_dump
)
@ -180,6 +189,7 @@ array(
)
args: array(
0: Arg(
name: null
value: Scalar_LNumber(
value: 5
)
@ -221,6 +231,7 @@ array(
)
args: array(
0: Arg(
name: null
value: Scalar_String(
value: id
)
@ -231,6 +242,7 @@ array(
)
args: array(
0: Arg(
name: null
value: Scalar_String(
value: var_dump
)
@ -241,6 +253,7 @@ array(
)
args: array(
0: Arg(
name: null
value: Scalar_LNumber(
value: 8
)
@ -316,6 +329,7 @@ array(
)
args: array(
0: Arg(
name: null
value: Scalar_String(
value: var_dump
)
@ -326,6 +340,7 @@ array(
)
args: array(
0: Arg(
name: null
value: Scalar_LNumber(
value: 9
)
@ -366,6 +381,7 @@ array(
)
args: array(
0: Arg(
name: null
value: Scalar_String(
value: id
)
@ -376,6 +392,7 @@ array(
)
args: array(
0: Arg(
name: null
value: Expr_Variable(
name: id
)
@ -386,6 +403,7 @@ array(
)
args: array(
0: Arg(
name: null
value: Scalar_String(
value: var_dump
)
@ -396,6 +414,7 @@ array(
)
args: array(
0: Arg(
name: null
value: Scalar_LNumber(
value: 10
)
@ -418,6 +437,7 @@ array(
)
args: array(
0: Arg(
name: null
value: Scalar_String(
value: id
)
@ -428,6 +448,7 @@ array(
)
args: array(
0: Arg(
name: null
value: Scalar_String(
value: var_dump
)
@ -438,6 +459,7 @@ array(
)
args: array(
0: Arg(
name: null
value: Scalar_LNumber(
value: 12
)
@ -464,6 +486,7 @@ array(
)
args: array(
0: Arg(
name: null
value: Scalar_String(
value: var_dump
)
@ -474,6 +497,7 @@ array(
)
args: array(
0: Arg(
name: null
value: Scalar_LNumber(
value: 13
)
@ -491,6 +515,7 @@ array(
)
args: array(
0: Arg(
name: null
value: Scalar_String(
value: var_dump
)
@ -501,6 +526,7 @@ array(
)
args: array(
0: Arg(
name: null
value: Scalar_LNumber(
value: 14
)

View File

@ -77,6 +77,7 @@ array(
)
args: array(
0: Arg(
name: null
value: Scalar_LNumber(
value: 1
)

View File

@ -34,6 +34,7 @@ array(
)
args: array(
0: Arg(
name: null
value: Expr_Variable(
name: arg
)

View File

@ -133,6 +133,7 @@ array(
)
args: array(
0: Arg(
name: null
value: Expr_Variable(
name: a
)
@ -140,6 +141,7 @@ array(
unpack: false
)
1: Arg(
name: null
value: Expr_Variable(
name: b
)
@ -198,6 +200,7 @@ array(
)
args: array(
0: Arg(
name: null
value: Expr_Variable(
name: this
)

View File

@ -220,6 +220,7 @@ array(
)
args: array(
0: Arg(
name: null
value: Expr_Yield(
key: null
value: Expr_Variable(
@ -248,6 +249,7 @@ array(
)
args: array(
0: Arg(
name: null
value: Expr_Yield(
key: null
value: Expr_Variable(
@ -269,6 +271,7 @@ array(
)
args: array(
0: Arg(
name: null
value: Expr_Yield(
key: null
value: Expr_Variable(

View File

@ -90,6 +90,7 @@ array(
)
args: array(
0: Arg(
name: null
value: Expr_Array(
items: array(
0: Expr_ArrayItem(
@ -173,6 +174,7 @@ array(
)
args: array(
0: Arg(
name: null
value: Expr_Array(
items: array(
0: Expr_ArrayItem(
@ -215,6 +217,7 @@ array(
)
args: array(
0: Arg(
name: null
value: Expr_Array(
items: array(
0: Expr_ArrayItem(

View File

@ -16,6 +16,7 @@ array(
)
args: array(
0: Arg(
name: null
value: Expr_ConstFetch(
name: Name(
parts: array(

View File

@ -0,0 +1,9 @@
Named arguments
-----
<?php
foo(a: $b, c: $d);
bar(class: 0);
-----
!!php7
foo(a: $b, c: $d);
bar(class: 0);