Skip to content

Commit 2096712

Browse files
authored
Merge pull request microsoft#324 from TysonAndre/parameter-promotion
Support php 8.0 constructor property promotion
2 parents 0c858c9 + 22f7813 commit 2096712

35 files changed

+481
-1
lines changed

src/Node/Parameter.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use Microsoft\PhpParser\Token;
1111

1212
class Parameter extends Node {
13+
/** @var Token|null */
14+
public $visibilityToken;
1315
/** @var Token|null */
1416
public $questionToken;
1517
/** @var QualifiedName|Token|null */
@@ -31,6 +33,7 @@ class Parameter extends Node {
3133
public $default;
3234

3335
const CHILD_NAMES = [
36+
'visibilityToken',
3437
'questionToken',
3538
'typeDeclaration',
3639
'otherTypeDeclarations',

src/Parser.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,7 @@ private function parseParameterFn() {
656656
return function ($parentNode) {
657657
$parameter = new Parameter();
658658
$parameter->parent = $parentNode;
659+
$parameter->visibilityToken = $this->eatOptional([TokenKind::PublicKeyword, TokenKind::ProtectedKeyword, TokenKind::PrivateKeyword]);
659660
$parameter->questionToken = $this->eatOptional1(TokenKind::QuestionToken);
660661
$typeDeclarationList = $this->tryParseParameterTypeDeclarationList($parameter);
661662
if ($typeDeclarationList) {
@@ -1294,10 +1295,14 @@ private function isParameterStartFn() {
12941295
case TokenKind::AmpersandToken:
12951296

12961297
case TokenKind::VariableName:
1297-
return true;
12981298

12991299
// nullable-type
13001300
case TokenKind::QuestionToken:
1301+
1302+
// parameter promotion
1303+
case TokenKind::PublicKeyword:
1304+
case TokenKind::ProtectedKeyword:
1305+
case TokenKind::PrivateKeyword:
13011306
return true;
13021307
}
13031308

tests/cases/parser/classMethods3.php.tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"children": [
5858
{
5959
"Parameter": {
60+
"visibilityToken": null,
6061
"questionToken": null,
6162
"typeDeclaration": null,
6263
"otherTypeDeclarations": null,
@@ -76,6 +77,7 @@
7677
},
7778
{
7879
"Parameter": {
80+
"visibilityToken": null,
7981
"questionToken": null,
8082
"typeDeclaration": null,
8183
"otherTypeDeclarations": null,

tests/cases/parser/classMethods4.php.tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"children": [
5858
{
5959
"Parameter": {
60+
"visibilityToken": null,
6061
"questionToken": null,
6162
"typeDeclaration": null,
6263
"otherTypeDeclarations": null,
@@ -76,6 +77,7 @@
7677
},
7778
{
7879
"Parameter": {
80+
"visibilityToken": null,
7981
"questionToken": null,
8082
"typeDeclaration": null,
8183
"otherTypeDeclarations": null,

tests/cases/parser/functions10.php.tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"children": [
3232
{
3333
"Parameter": {
34+
"visibilityToken": null,
3435
"questionToken": null,
3536
"typeDeclaration": null,
3637
"otherTypeDeclarations": null,
@@ -50,6 +51,7 @@
5051
},
5152
{
5253
"Parameter": {
54+
"visibilityToken": null,
5355
"questionToken": null,
5456
"typeDeclaration": null,
5557
"otherTypeDeclarations": null,

tests/cases/parser/functions11.php.tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"children": [
3232
{
3333
"Parameter": {
34+
"visibilityToken": null,
3435
"questionToken": null,
3536
"typeDeclaration": null,
3637
"otherTypeDeclarations": null,
@@ -50,6 +51,7 @@
5051
},
5152
{
5253
"Parameter": {
54+
"visibilityToken": null,
5355
"questionToken": null,
5456
"typeDeclaration": {
5557
"QualifiedName": {

tests/cases/parser/functions12.php.tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"children": [
3232
{
3333
"Parameter": {
34+
"visibilityToken": null,
3435
"questionToken": null,
3536
"typeDeclaration": null,
3637
"otherTypeDeclarations": null,
@@ -50,6 +51,7 @@
5051
},
5152
{
5253
"Parameter": {
54+
"visibilityToken": null,
5355
"questionToken": null,
5456
"typeDeclaration": {
5557
"QualifiedName": {

tests/cases/parser/functions13.php.tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"children": [
3232
{
3333
"Parameter": {
34+
"visibilityToken": null,
3435
"questionToken": null,
3536
"typeDeclaration": null,
3637
"otherTypeDeclarations": null,
@@ -50,6 +51,7 @@
5051
},
5152
{
5253
"Parameter": {
54+
"visibilityToken": null,
5355
"questionToken": null,
5456
"typeDeclaration": {
5557
"QualifiedName": {

tests/cases/parser/functions14.php.tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"children": [
3232
{
3333
"Parameter": {
34+
"visibilityToken": null,
3435
"questionToken": null,
3536
"typeDeclaration": null,
3637
"otherTypeDeclarations": null,
@@ -50,6 +51,7 @@
5051
},
5152
{
5253
"Parameter": {
54+
"visibilityToken": null,
5355
"questionToken": null,
5456
"typeDeclaration": {
5557
"QualifiedName": {
@@ -83,6 +85,7 @@
8385
},
8486
{
8587
"Parameter": {
88+
"visibilityToken": null,
8689
"questionToken": null,
8790
"typeDeclaration": null,
8891
"otherTypeDeclarations": null,

tests/cases/parser/functions15.php.tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"children": [
3232
{
3333
"Parameter": {
34+
"visibilityToken": null,
3435
"questionToken": null,
3536
"typeDeclaration": null,
3637
"otherTypeDeclarations": null,
@@ -50,6 +51,7 @@
5051
},
5152
{
5253
"Parameter": {
54+
"visibilityToken": null,
5355
"questionToken": null,
5456
"typeDeclaration": {
5557
"QualifiedName": {
@@ -83,6 +85,7 @@
8385
},
8486
{
8587
"Parameter": {
88+
"visibilityToken": null,
8689
"questionToken": null,
8790
"typeDeclaration": null,
8891
"otherTypeDeclarations": null,

0 commit comments

Comments
 (0)