Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PHP 8.0] Add constructor promotion #667

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions grammar/php7.y
Original file line number Diff line number Diff line change
Expand Up @@ -449,13 +449,25 @@ non_empty_parameter_list:
| non_empty_parameter_list ',' parameter { push($1, $3); }
;

optional_visibility_modifier:
/* empty */ { $$ = 0; }
| T_PUBLIC { $$ = Stmt\Class_::MODIFIER_PUBLIC; }
| T_PROTECTED { $$ = Stmt\Class_::MODIFIER_PROTECTED; }
| T_PRIVATE { $$ = Stmt\Class_::MODIFIER_PRIVATE; }
;

parameter:
optional_type optional_ref optional_ellipsis plain_variable
{ $$ = Node\Param[$4, null, $1, $2, $3]; $this->checkParam($$); }
| optional_type optional_ref optional_ellipsis plain_variable '=' expr
{ $$ = Node\Param[$4, $6, $1, $2, $3]; $this->checkParam($$); }
| optional_type optional_ref optional_ellipsis error
{ $$ = Node\Param[Expr\Error[], null, $1, $2, $3]; }
optional_visibility_modifier optional_type optional_ref optional_ellipsis plain_variable
{ $attrs = attributes();
$$ = Node\Param[$5, null, $2, $3, $4, $attrs, $1];
$this->checkParam($$); }
| optional_visibility_modifier optional_type optional_ref optional_ellipsis plain_variable '=' expr
{ $attrs = attributes();
$$ = Node\Param[$5, $7, $2, $3, $4, $attrs, $1];
$this->checkParam($$); }
| optional_visibility_modifier optional_type optional_ref optional_ellipsis error
{ $attrs = attributes();
$$ = Node\Param[Expr\Error[], null, $2, $3, $4, $attrs, $1]; }
;

type_expr:
Expand Down
10 changes: 8 additions & 2 deletions lib/PhpParser/Node/Param.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class Param extends NodeAbstract
public $var;
/** @var null|Expr Default value */
public $default;
/** @var int */
public $flags;

/**
* Constructs a parameter node.
Expand All @@ -25,22 +27,26 @@ class Param extends NodeAbstract
* @param null|string|Identifier|Name|NullableType|UnionType $type Type declaration
* @param bool $byRef Whether is passed by reference
* @param bool $variadic Whether this is a variadic argument
* @param array $flags Optional visibility flags
* @param array $attributes Additional attributes
*/
public function __construct(
$var, Expr $default = null, $type = null,
bool $byRef = false, bool $variadic = false, array $attributes = []
bool $byRef = false, bool $variadic = false,
array $attributes = [],
int $flags = 0
) {
$this->attributes = $attributes;
$this->type = \is_string($type) ? new Identifier($type) : $type;
$this->byRef = $byRef;
$this->variadic = $variadic;
$this->var = $var;
$this->default = $default;
$this->flags = $flags;
}

public function getSubNodeNames() : array {
return ['type', 'byRef', 'variadic', 'var', 'default'];
return ['flags', 'type', 'byRef', 'variadic', 'var', 'default'];
}

public function getType() : string {
Expand Down
1,435 changes: 726 additions & 709 deletions lib/PhpParser/Parser/Php7.php

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions lib/PhpParser/PrettyPrinter/Standard.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class Standard extends PrettyPrinterAbstract
// Special nodes

protected function pParam(Node\Param $node) {
return ($node->type ? $this->p($node->type) . ' ' : '')
return ($this->pModifiers($node->flags))
. ($node->type ? $this->p($node->type) . ' ' : '')
. ($node->byRef ? '&' : '')
. ($node->variadic ? '...' : '')
. $this->p($node->var)
Expand Down Expand Up @@ -725,7 +726,7 @@ protected function pStmt_PropertyProperty(Stmt\PropertyProperty $node) {
protected function pStmt_ClassMethod(Stmt\ClassMethod $node) {
return $this->pModifiers($node->flags)
. 'function ' . ($node->byRef ? '&' : '') . $node->name
. '(' . $this->pCommaSeparated($node->params) . ')'
. '(' . $this->pMaybeMultiline($node->params) . ')'
Copy link
Contributor Author

@TomasVotruba TomasVotruba Jun 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems needed to keep param-per-line formatting, to persist the doc block annotation above the param

. (null !== $node->returnType ? ' : ' . $this->p($node->returnType) : '')
. (null !== $node->stmts
? $this->nl . '{' . $this->pStmts($node->stmts) . $this->nl . '}'
Expand Down
2 changes: 2 additions & 0 deletions test/PhpParser/NodeAbstractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ function functionName(&$a = 0, $b = 1.0) {
"kind": 10
}
},
"flags": 0,
"attributes": {
"startLine": 4,
"endLine": 4
Expand All @@ -273,6 +274,7 @@ function functionName(&$a = 0, $b = 1.0) {
"endLine": 4
}
},
"flags": 0,
"attributes": {
"startLine": 4,
"endLine": 4
Expand Down
9 changes: 9 additions & 0 deletions test/code/parser/errorHandling/recovery.test
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,7 @@ array(
byRef: false
params: array(
0: Param(
flags: 0
type: null
byRef: false
variadic: false
Expand Down Expand Up @@ -1052,6 +1053,7 @@ array(
)
params: array(
0: Param(
flags: 0
type: Name(
parts: array(
0: Type
Expand Down Expand Up @@ -1080,6 +1082,7 @@ array(
)
params: array(
0: Param(
flags: 0
type: Name(
parts: array(
0: Type1
Expand All @@ -1093,6 +1096,7 @@ array(
default: null
)
1: Param(
flags: 0
type: Name(
parts: array(
0: Type2
Expand Down Expand Up @@ -1121,6 +1125,7 @@ array(
)
params: array(
0: Param(
flags: 0
type: null
byRef: false
variadic: true
Expand All @@ -1145,6 +1150,7 @@ array(
)
params: array(
0: Param(
flags: 0
type: null
byRef: true
variadic: false
Expand All @@ -1169,6 +1175,7 @@ array(
)
params: array(
0: Param(
flags: 0
type: Name(
parts: array(
0: Bar
Expand Down Expand Up @@ -1202,6 +1209,7 @@ array(
)
params: array(
0: Param(
flags: 0
type: Name(
parts: array(
0: Baz
Expand All @@ -1226,6 +1234,7 @@ array(
byRef: false
params: array(
0: Param(
flags: 0
type: Name(
parts: array(
0: Foo
Expand Down
8 changes: 7 additions & 1 deletion test/code/parser/expr/arrow_function.test
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ array(
byRef: false
params: array(
0: Param(
flags: 0
type: Identifier(
name: bool
)
Expand All @@ -39,6 +40,7 @@ array(
byRef: false
params: array(
0: Param(
flags: 0
type: null
byRef: false
variadic: false
Expand All @@ -62,6 +64,7 @@ array(
byRef: false
params: array(
0: Param(
flags: 0
type: null
byRef: true
variadic: false
Expand All @@ -83,6 +86,7 @@ array(
byRef: true
params: array(
0: Param(
flags: 0
type: null
byRef: false
variadic: false
Expand All @@ -104,6 +108,7 @@ array(
byRef: false
params: array(
0: Param(
flags: 0
type: null
byRef: false
variadic: false
Expand All @@ -113,6 +118,7 @@ array(
default: null
)
1: Param(
flags: 0
type: null
byRef: false
variadic: true
Expand Down Expand Up @@ -142,4 +148,4 @@ array(
)
)
)
)
)
4 changes: 4 additions & 0 deletions test/code/parser/expr/closure.test
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ array(
byRef: false
params: array(
0: Param(
flags: 0
type: null
byRef: false
variadic: false
Expand Down Expand Up @@ -43,6 +44,7 @@ array(
byRef: false
params: array(
0: Param(
flags: 0
type: null
byRef: false
variadic: false
Expand Down Expand Up @@ -96,6 +98,7 @@ array(
byRef: true
params: array(
0: Param(
flags: 0
type: null
byRef: false
variadic: false
Expand Down Expand Up @@ -131,6 +134,7 @@ array(
byRef: false
params: array(
0: Param(
flags: 0
type: null
byRef: false
variadic: false
Expand Down
2 changes: 2 additions & 0 deletions test/code/parser/expr/uvs/indirectCall.test
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ array(
byRef: false
params: array(
0: Param(
flags: 0
type: null
byRef: false
variadic: false
Expand Down Expand Up @@ -264,6 +265,7 @@ array(
byRef: false
params: array(
0: Param(
flags: 0
type: null
byRef: false
variadic: false
Expand Down
2 changes: 1 addition & 1 deletion test/code/parser/stmt/class/propertyTypes.test
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,4 @@ array(
)
)
)
)
)
81 changes: 81 additions & 0 deletions test/code/parser/stmt/class/property_promotion.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
Property promotion
-----
<?php

class Point {
public function __construct(
public float $x = 0.0,
protected array $y = [],
private string $z = 'hello'
) {}
}
-----
!!php7
array(
0: Stmt_Class(
flags: 0
name: Identifier(
name: Point
)
extends: null
implements: array(
)
stmts: array(
0: Stmt_ClassMethod(
flags: MODIFIER_PUBLIC (1)
byRef: false
name: Identifier(
name: __construct
)
params: array(
0: Param(
flags: MODIFIER_PUBLIC (1)
type: Identifier(
name: float
)
byRef: false
variadic: false
var: Expr_Variable(
name: x
)
default: Scalar_DNumber(
value: 0
)
)
1: Param(
flags: MODIFIER_PROTECTED (2)
type: Identifier(
name: array
)
byRef: false
variadic: false
var: Expr_Variable(
name: y
)
default: Expr_Array(
items: array(
)
)
)
2: Param(
flags: MODIFIER_PRIVATE (4)
type: Identifier(
name: string
)
byRef: false
variadic: false
var: Expr_Variable(
name: z
)
default: Scalar_String(
value: hello
)
)
)
returnType: null
stmts: array(
)
)
)
)
)
1 change: 1 addition & 0 deletions test/code/parser/stmt/class/simple.test
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ array(
)
params: array(
0: Param(
flags: 0
type: null
byRef: false
variadic: false
Expand Down
Loading