Skip to content

Commit a1cbc19

Browse files
Fix parsing of static return types
Support https://wiki.php.net/rfc/static_return_type
1 parent fda7e17 commit a1cbc19

File tree

7 files changed

+313
-1
lines changed

7 files changed

+313
-1
lines changed

src/Parser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public function __construct() {
132132
[TokenKind::ArrayKeyword, TokenKind::CallableKeyword, TokenKind::BoolReservedWord,
133133
TokenKind::FloatReservedWord, TokenKind::IntReservedWord, TokenKind::StringReservedWord,
134134
TokenKind::ObjectReservedWord, TokenKind::NullReservedWord, TokenKind::FalseReservedWord]; // TODO update spec
135-
$this->returnTypeDeclarationTokens = \array_merge([TokenKind::VoidReservedWord, TokenKind::NullReservedWord, TokenKind::FalseReservedWord], $this->parameterTypeDeclarationTokens);
135+
$this->returnTypeDeclarationTokens = \array_merge([TokenKind::VoidReservedWord, TokenKind::NullReservedWord, TokenKind::FalseReservedWord, TokenKind::StaticKeyword], $this->parameterTypeDeclarationTokens);
136136
}
137137

138138
/**
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
class A {
3+
public function f() : static {
4+
return $this;
5+
}
6+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
{
2+
"SourceFileNode": {
3+
"statementList": [
4+
{
5+
"InlineHtml": {
6+
"scriptSectionEndTag": null,
7+
"text": null,
8+
"scriptSectionStartTag": {
9+
"kind": "ScriptSectionStartTag",
10+
"textLength": 6
11+
}
12+
}
13+
},
14+
{
15+
"ClassDeclaration": {
16+
"abstractOrFinalModifier": null,
17+
"classKeyword": {
18+
"kind": "ClassKeyword",
19+
"textLength": 5
20+
},
21+
"name": {
22+
"kind": "Name",
23+
"textLength": 1
24+
},
25+
"classBaseClause": null,
26+
"classInterfaceClause": null,
27+
"classMembers": {
28+
"ClassMembersNode": {
29+
"openBrace": {
30+
"kind": "OpenBraceToken",
31+
"textLength": 1
32+
},
33+
"classMemberDeclarations": [
34+
{
35+
"MethodDeclaration": {
36+
"modifiers": [
37+
{
38+
"kind": "PublicKeyword",
39+
"textLength": 6
40+
}
41+
],
42+
"functionKeyword": {
43+
"kind": "FunctionKeyword",
44+
"textLength": 8
45+
},
46+
"byRefToken": null,
47+
"name": {
48+
"kind": "Name",
49+
"textLength": 1
50+
},
51+
"openParen": {
52+
"kind": "OpenParenToken",
53+
"textLength": 1
54+
},
55+
"parameters": null,
56+
"closeParen": {
57+
"kind": "CloseParenToken",
58+
"textLength": 1
59+
},
60+
"colonToken": {
61+
"kind": "ColonToken",
62+
"textLength": 1
63+
},
64+
"questionToken": null,
65+
"returnType": {
66+
"kind": "StaticKeyword",
67+
"textLength": 6
68+
},
69+
"otherReturnTypes": null,
70+
"compoundStatementOrSemicolon": {
71+
"CompoundStatementNode": {
72+
"openBrace": {
73+
"kind": "OpenBraceToken",
74+
"textLength": 1
75+
},
76+
"statements": [
77+
{
78+
"ReturnStatement": {
79+
"returnKeyword": {
80+
"kind": "ReturnKeyword",
81+
"textLength": 6
82+
},
83+
"expression": {
84+
"Variable": {
85+
"dollar": null,
86+
"name": {
87+
"kind": "VariableName",
88+
"textLength": 5
89+
}
90+
}
91+
},
92+
"semicolon": {
93+
"kind": "SemicolonToken",
94+
"textLength": 1
95+
}
96+
}
97+
}
98+
],
99+
"closeBrace": {
100+
"kind": "CloseBraceToken",
101+
"textLength": 1
102+
}
103+
}
104+
}
105+
}
106+
}
107+
],
108+
"closeBrace": {
109+
"kind": "CloseBraceToken",
110+
"textLength": 1
111+
}
112+
}
113+
}
114+
}
115+
}
116+
],
117+
"endOfFileToken": {
118+
"kind": "EndOfFileToken",
119+
"textLength": 0
120+
}
121+
}
122+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
class X {
3+
// Static is forbidden in param types
4+
public function test(static $x) {
5+
}
6+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
[
2+
{
3+
"kind": 0,
4+
"message": "')' expected.",
5+
"start": 83,
6+
"length": 0
7+
},
8+
{
9+
"kind": 0,
10+
"message": "'{' expected.",
11+
"start": 83,
12+
"length": 0
13+
},
14+
{
15+
"kind": 0,
16+
"message": "';' expected.",
17+
"start": 92,
18+
"length": 0
19+
},
20+
{
21+
"kind": 0,
22+
"message": "Unexpected ')'",
23+
"start": 92,
24+
"length": 1
25+
},
26+
{
27+
"kind": 0,
28+
"message": "'}' expected.",
29+
"start": 103,
30+
"length": 0
31+
}
32+
]
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
{
2+
"SourceFileNode": {
3+
"statementList": [
4+
{
5+
"InlineHtml": {
6+
"scriptSectionEndTag": null,
7+
"text": null,
8+
"scriptSectionStartTag": {
9+
"kind": "ScriptSectionStartTag",
10+
"textLength": 6
11+
}
12+
}
13+
},
14+
{
15+
"ClassDeclaration": {
16+
"abstractOrFinalModifier": null,
17+
"classKeyword": {
18+
"kind": "ClassKeyword",
19+
"textLength": 5
20+
},
21+
"name": {
22+
"kind": "Name",
23+
"textLength": 1
24+
},
25+
"classBaseClause": null,
26+
"classInterfaceClause": null,
27+
"classMembers": {
28+
"ClassMembersNode": {
29+
"openBrace": {
30+
"kind": "OpenBraceToken",
31+
"textLength": 1
32+
},
33+
"classMemberDeclarations": [
34+
{
35+
"MethodDeclaration": {
36+
"modifiers": [
37+
{
38+
"kind": "PublicKeyword",
39+
"textLength": 6
40+
}
41+
],
42+
"functionKeyword": {
43+
"kind": "FunctionKeyword",
44+
"textLength": 8
45+
},
46+
"byRefToken": null,
47+
"name": {
48+
"kind": "Name",
49+
"textLength": 4
50+
},
51+
"openParen": {
52+
"kind": "OpenParenToken",
53+
"textLength": 1
54+
},
55+
"parameters": null,
56+
"closeParen": {
57+
"error": "MissingToken",
58+
"kind": "CloseParenToken",
59+
"textLength": 0
60+
},
61+
"colonToken": null,
62+
"questionToken": null,
63+
"returnType": null,
64+
"otherReturnTypes": null,
65+
"compoundStatementOrSemicolon": {
66+
"CompoundStatementNode": {
67+
"openBrace": {
68+
"error": "MissingToken",
69+
"kind": "OpenBraceToken",
70+
"textLength": 0
71+
},
72+
"statements": [
73+
{
74+
"FunctionStaticDeclaration": {
75+
"staticKeyword": {
76+
"kind": "StaticKeyword",
77+
"textLength": 6
78+
},
79+
"staticVariableNameList": {
80+
"StaticVariableNameList": {
81+
"children": [
82+
{
83+
"StaticVariableDeclaration": {
84+
"variableName": {
85+
"kind": "VariableName",
86+
"textLength": 2
87+
},
88+
"equalsToken": null,
89+
"assignment": null
90+
}
91+
}
92+
]
93+
}
94+
},
95+
"semicolon": {
96+
"error": "MissingToken",
97+
"kind": "SemicolonToken",
98+
"textLength": 0
99+
}
100+
}
101+
},
102+
{
103+
"error": "SkippedToken",
104+
"kind": "CloseParenToken",
105+
"textLength": 1
106+
},
107+
{
108+
"CompoundStatementNode": {
109+
"openBrace": {
110+
"kind": "OpenBraceToken",
111+
"textLength": 1
112+
},
113+
"statements": [],
114+
"closeBrace": {
115+
"kind": "CloseBraceToken",
116+
"textLength": 1
117+
}
118+
}
119+
}
120+
],
121+
"closeBrace": {
122+
"kind": "CloseBraceToken",
123+
"textLength": 1
124+
}
125+
}
126+
}
127+
}
128+
}
129+
],
130+
"closeBrace": {
131+
"error": "MissingToken",
132+
"kind": "CloseBraceToken",
133+
"textLength": 0
134+
}
135+
}
136+
}
137+
}
138+
}
139+
],
140+
"endOfFileToken": {
141+
"kind": "EndOfFileToken",
142+
"textLength": 0
143+
}
144+
}
145+
}

0 commit comments

Comments
 (0)