Skip to content

Commit 50911f6

Browse files
committed
Support property hooks default value parsing
These tests are cursed.
1 parent 1492f47 commit 50911f6

File tree

8 files changed

+635
-31
lines changed

8 files changed

+635
-31
lines changed

src/Node.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,11 +341,11 @@ public function getFullWidth() : int {
341341
* Gets string representing Node text (not including leading comment + whitespace trivia)
342342
* @return string
343343
*/
344-
public function getText() : string {
344+
public function getText(?string $fileContents= null) : string {
345345
$start = $this->getStartPosition();
346346
$end = $this->getEndPosition();
347347

348-
$fileContents = $this->getFileContents();
348+
$fileContents = $fileContents ?? $this->getFileContents();
349349
return \substr($fileContents, $start, $end - $start);
350350
}
351351

src/Parser.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2192,6 +2192,7 @@ private function parseBinaryExpressionOrHigher($precedence, $parentNode) {
21922192
// the original operator, and the newly constructed exponentiation-expression as the operand.
21932193
$shouldOperatorTakePrecedenceOverUnary = false;
21942194
switch ($token->kind) {
2195+
case TokenKind::OpenBraceToken:
21952196
case TokenKind::AsteriskAsteriskToken:
21962197
$shouldOperatorTakePrecedenceOverUnary = $leftOperand instanceof UnaryExpression;
21972198
break;
@@ -3056,6 +3057,9 @@ private function parsePostfixExpressionRest($expression, $allowUpdateExpression
30563057
)) {
30573058
return $expression;
30583059
}
3060+
if ($tokenKind === TokenKind::OpenBraceToken) {
3061+
return $expression;
3062+
}
30593063
if ($tokenKind === TokenKind::ColonColonToken) {
30603064
$expression = $this->parseScopedPropertyAccessExpression($expression, null);
30613065
return $this->parsePostfixExpressionRest($expression);
@@ -3462,7 +3466,7 @@ private function parsePropertyNameList($parentNode) {
34623466
return $this->parseDelimitedList(
34633467
DelimitedList\ExpressionList::class,
34643468
TokenKind::CommaToken,
3465-
function ($token) {
3469+
function (Token $token) {
34663470
return $token->kind === TokenKind::VariableName;
34673471
},
34683472
$this->parsePropertyVariableNameAndDefault(),

tests/cases/lexical/keyword5.php.tokens

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,8 @@
44
"textLength": 6
55
},
66
{
7-
"kind": "YieldKeyword",
8-
"textLength": 5
9-
},
10-
{
11-
"kind": "Name",
12-
"textLength": 4
7+
"kind": "YieldFromKeyword",
8+
"textLength": 22
139
},
1410
{
1511
"kind": "EndOfFileToken",

tests/cases/parser/propertyHook4.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
class User
4+
{
5+
public function __construct(public string $first, public string $last) {}
6+
7+
public string $fullName = 'hello world' {
8+
get {
9+
return "$this->first $this->last";
10+
}
11+
set(string $value) {
12+
[$this->first, $this->last] = explode(' ', $value, 2);
13+
}
14+
}
15+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]

tests/cases/parser/propertyHook4.php.tree

Lines changed: 588 additions & 0 deletions
Large diffs are not rendered by default.

tests/cases/parser81/enums6.php.diag

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
{
2121
"kind": 0,
2222
"message": "';' expected.",
23-
"start": 97,
23+
"start": 93,
2424
"length": 0
2525
}
2626
]

tests/cases/parser81/enums6.php.tree

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -57,28 +57,15 @@
5757
{
5858
"ExpressionStatement": {
5959
"expression": {
60-
"SubscriptExpression": {
61-
"postfixExpression": {
62-
"QualifiedName": {
63-
"globalSpecifier": null,
64-
"relativeSpecifier": null,
65-
"nameParts": [
66-
{
67-
"kind": "Name",
68-
"textLength": 6
69-
}
70-
]
60+
"QualifiedName": {
61+
"globalSpecifier": null,
62+
"relativeSpecifier": null,
63+
"nameParts": [
64+
{
65+
"kind": "Name",
66+
"textLength": 6
7167
}
72-
},
73-
"openBracketOrBrace": {
74-
"kind": "OpenBraceToken",
75-
"textLength": 1
76-
},
77-
"accessExpression": null,
78-
"closeBracketOrBrace": {
79-
"kind": "CloseBraceToken",
80-
"textLength": 1
81-
}
68+
]
8269
}
8370
},
8471
"semicolon": {
@@ -87,6 +74,19 @@
8774
"textLength": 0
8875
}
8976
}
77+
},
78+
{
79+
"CompoundStatementNode": {
80+
"openBrace": {
81+
"kind": "OpenBraceToken",
82+
"textLength": 1
83+
},
84+
"statements": [],
85+
"closeBrace": {
86+
"kind": "CloseBraceToken",
87+
"textLength": 1
88+
}
89+
}
9090
}
9191
],
9292
"endOfFileToken": {

0 commit comments

Comments
 (0)