Skip to content

Commit b58b19e

Browse files
TomasVotrubanikic
authored andcommitted
Add constructor promotion support
1 parent 0d2d8f9 commit b58b19e

26 files changed

+975
-725
lines changed

grammar/php7.y

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -449,13 +449,22 @@ non_empty_parameter_list:
449449
| non_empty_parameter_list ',' parameter { push($1, $3); }
450450
;
451451

452+
optional_visibility_modifier:
453+
/* empty */ { $$ = 0; }
454+
| T_PUBLIC { $$ = Stmt\Class_::MODIFIER_PUBLIC; }
455+
| T_PROTECTED { $$ = Stmt\Class_::MODIFIER_PROTECTED; }
456+
| T_PRIVATE { $$ = Stmt\Class_::MODIFIER_PRIVATE; }
457+
;
458+
452459
parameter:
453-
optional_type optional_ref optional_ellipsis plain_variable
454-
{ $$ = Node\Param[$4, null, $1, $2, $3]; $this->checkParam($$); }
455-
| optional_type optional_ref optional_ellipsis plain_variable '=' expr
456-
{ $$ = Node\Param[$4, $6, $1, $2, $3]; $this->checkParam($$); }
457-
| optional_type optional_ref optional_ellipsis error
458-
{ $$ = Node\Param[Expr\Error[], null, $1, $2, $3]; }
460+
optional_visibility_modifier optional_type optional_ref optional_ellipsis plain_variable
461+
{ $$ = new Node\Param($5, null, $2, $3, $4, attributes(), $1);
462+
$this->checkParam($$); }
463+
| optional_visibility_modifier optional_type optional_ref optional_ellipsis plain_variable '=' expr
464+
{ $$ = new Node\Param($5, $7, $2, $3, $4, attributes(), $1);
465+
$this->checkParam($$); }
466+
| optional_visibility_modifier optional_type optional_ref optional_ellipsis error
467+
{ $$ = new Node\Param(Expr\Error[], null, $2, $3, $4, attributes(), $1); }
459468
;
460469

461470
type_expr:

lib/PhpParser/Node/Param.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ class Param extends NodeAbstract
1616
public $var;
1717
/** @var null|Expr Default value */
1818
public $default;
19+
/** @var int */
20+
public $flags;
1921

2022
/**
2123
* Constructs a parameter node.
@@ -25,22 +27,26 @@ class Param extends NodeAbstract
2527
* @param null|string|Identifier|Name|NullableType|UnionType $type Type declaration
2628
* @param bool $byRef Whether is passed by reference
2729
* @param bool $variadic Whether this is a variadic argument
30+
* @param array $flags Optional visibility flags
2831
* @param array $attributes Additional attributes
2932
*/
3033
public function __construct(
3134
$var, Expr $default = null, $type = null,
32-
bool $byRef = false, bool $variadic = false, array $attributes = []
35+
bool $byRef = false, bool $variadic = false,
36+
array $attributes = [],
37+
int $flags = 0
3338
) {
3439
$this->attributes = $attributes;
3540
$this->type = \is_string($type) ? new Identifier($type) : $type;
3641
$this->byRef = $byRef;
3742
$this->variadic = $variadic;
3843
$this->var = $var;
3944
$this->default = $default;
45+
$this->flags = $flags;
4046
}
4147

4248
public function getSubNodeNames() : array {
43-
return ['type', 'byRef', 'variadic', 'var', 'default'];
49+
return ['flags', 'type', 'byRef', 'variadic', 'var', 'default'];
4450
}
4551

4652
public function getType() : string {

lib/PhpParser/Parser/Php7.php

Lines changed: 723 additions & 709 deletions
Large diffs are not rendered by default.

lib/PhpParser/PrettyPrinter/Standard.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ class Standard extends PrettyPrinterAbstract
1818
// Special nodes
1919

2020
protected function pParam(Node\Param $node) {
21-
return ($node->type ? $this->p($node->type) . ' ' : '')
21+
return ($this->pModifiers($node->flags))
22+
. ($node->type ? $this->p($node->type) . ' ' : '')
2223
. ($node->byRef ? '&' : '')
2324
. ($node->variadic ? '...' : '')
2425
. $this->p($node->var)
@@ -725,7 +726,7 @@ protected function pStmt_PropertyProperty(Stmt\PropertyProperty $node) {
725726
protected function pStmt_ClassMethod(Stmt\ClassMethod $node) {
726727
return $this->pModifiers($node->flags)
727728
. 'function ' . ($node->byRef ? '&' : '') . $node->name
728-
. '(' . $this->pCommaSeparated($node->params) . ')'
729+
. '(' . $this->pMaybeMultiline($node->params) . ')'
729730
. (null !== $node->returnType ? ' : ' . $this->p($node->returnType) : '')
730731
. (null !== $node->stmts
731732
? $this->nl . '{' . $this->pStmts($node->stmts) . $this->nl . '}'

lib/PhpParser/PrettyPrinterAbstract.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,6 +1415,7 @@ protected function initializeModifierChangeMap() {
14151415
'Stmt_ClassMethod->flags' => \T_FUNCTION,
14161416
'Stmt_Class->flags' => \T_CLASS,
14171417
'Stmt_Property->flags' => \T_VARIABLE,
1418+
'Param->flags' => \T_VARIABLE,
14181419
//'Stmt_TraitUseAdaptation_Alias->newModifier' => 0, // TODO
14191420
];
14201421

test/PhpParser/NodeAbstractTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ function functionName(&$a = 0, $b = 1.0) {
247247
"kind": 10
248248
}
249249
},
250+
"flags": 0,
250251
"attributes": {
251252
"startLine": 4,
252253
"endLine": 4
@@ -273,6 +274,7 @@ function functionName(&$a = 0, $b = 1.0) {
273274
"endLine": 4
274275
}
275276
},
277+
"flags": 0,
276278
"attributes": {
277279
"startLine": 4,
278280
"endLine": 4

test/code/formatPreservation/modifierChange.test

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,28 @@ class Bar {
3030

3131
public final function
3232
foo() {}
33-
}
33+
}
34+
-----
35+
<?php
36+
function test(
37+
public T1 $x
38+
= 'y',
39+
private T2 $y
40+
= 'z',
41+
T3 $z
42+
= 'x',
43+
) {}
44+
-----
45+
$stmts[0]->params[0]->flags = Stmt\Class_::MODIFIER_PRIVATE;
46+
$stmts[0]->params[1]->flags = 0;
47+
$stmts[0]->params[2]->flags = Stmt\Class_::MODIFIER_PUBLIC;
48+
-----
49+
<?php
50+
function test(
51+
private T1 $x
52+
= 'y',
53+
T2 $y
54+
= 'z',
55+
public T3 $z
56+
= 'x',
57+
) {}

test/code/parser/errorHandling/recovery.test

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,7 @@ array(
875875
byRef: false
876876
params: array(
877877
0: Param(
878+
flags: 0
878879
type: null
879880
byRef: false
880881
variadic: false
@@ -1052,6 +1053,7 @@ array(
10521053
)
10531054
params: array(
10541055
0: Param(
1056+
flags: 0
10551057
type: Name(
10561058
parts: array(
10571059
0: Type
@@ -1080,6 +1082,7 @@ array(
10801082
)
10811083
params: array(
10821084
0: Param(
1085+
flags: 0
10831086
type: Name(
10841087
parts: array(
10851088
0: Type1
@@ -1093,6 +1096,7 @@ array(
10931096
default: null
10941097
)
10951098
1: Param(
1099+
flags: 0
10961100
type: Name(
10971101
parts: array(
10981102
0: Type2
@@ -1121,6 +1125,7 @@ array(
11211125
)
11221126
params: array(
11231127
0: Param(
1128+
flags: 0
11241129
type: null
11251130
byRef: false
11261131
variadic: true
@@ -1145,6 +1150,7 @@ array(
11451150
)
11461151
params: array(
11471152
0: Param(
1153+
flags: 0
11481154
type: null
11491155
byRef: true
11501156
variadic: false
@@ -1169,6 +1175,7 @@ array(
11691175
)
11701176
params: array(
11711177
0: Param(
1178+
flags: 0
11721179
type: Name(
11731180
parts: array(
11741181
0: Bar
@@ -1202,6 +1209,7 @@ array(
12021209
)
12031210
params: array(
12041211
0: Param(
1212+
flags: 0
12051213
type: Name(
12061214
parts: array(
12071215
0: Baz
@@ -1226,6 +1234,7 @@ array(
12261234
byRef: false
12271235
params: array(
12281236
0: Param(
1237+
flags: 0
12291238
type: Name(
12301239
parts: array(
12311240
0: Foo

test/code/parser/expr/arrow_function.test

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ array(
1616
byRef: false
1717
params: array(
1818
0: Param(
19+
flags: 0
1920
type: Identifier(
2021
name: bool
2122
)
@@ -39,6 +40,7 @@ array(
3940
byRef: false
4041
params: array(
4142
0: Param(
43+
flags: 0
4244
type: null
4345
byRef: false
4446
variadic: false
@@ -62,6 +64,7 @@ array(
6264
byRef: false
6365
params: array(
6466
0: Param(
67+
flags: 0
6568
type: null
6669
byRef: true
6770
variadic: false
@@ -83,6 +86,7 @@ array(
8386
byRef: true
8487
params: array(
8588
0: Param(
89+
flags: 0
8690
type: null
8791
byRef: false
8892
variadic: false
@@ -104,6 +108,7 @@ array(
104108
byRef: false
105109
params: array(
106110
0: Param(
111+
flags: 0
107112
type: null
108113
byRef: false
109114
variadic: false
@@ -113,6 +118,7 @@ array(
113118
default: null
114119
)
115120
1: Param(
121+
flags: 0
116122
type: null
117123
byRef: false
118124
variadic: true
@@ -142,4 +148,4 @@ array(
142148
)
143149
)
144150
)
145-
)
151+
)

test/code/parser/expr/closure.test

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ array(
1616
byRef: false
1717
params: array(
1818
0: Param(
19+
flags: 0
1920
type: null
2021
byRef: false
2122
variadic: false
@@ -43,6 +44,7 @@ array(
4344
byRef: false
4445
params: array(
4546
0: Param(
47+
flags: 0
4648
type: null
4749
byRef: false
4850
variadic: false
@@ -96,6 +98,7 @@ array(
9698
byRef: true
9799
params: array(
98100
0: Param(
101+
flags: 0
99102
type: null
100103
byRef: false
101104
variadic: false
@@ -131,6 +134,7 @@ array(
131134
byRef: false
132135
params: array(
133136
0: Param(
137+
flags: 0
134138
type: null
135139
byRef: false
136140
variadic: false

0 commit comments

Comments
 (0)