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

Support parsing php 8.3 typed class constants #406

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ jobs:
- PHP_VERSION: '7.4'
- PHP_VERSION: '8.0'
- PHP_VERSION: '8.1'
- PHP_VERSION: '8.2.0beta2'
- PHP_VERSION: '8.2'
- PHP_VERSION: '8.3'

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
Expand Down
5 changes: 5 additions & 0 deletions src/Node/ClassConstDeclaration.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Microsoft\PhpParser\ModifiedTypeInterface;
use Microsoft\PhpParser\ModifiedTypeTrait;
use Microsoft\PhpParser\Node;
use Microsoft\PhpParser\Node\DelimitedList\QualifiedNameList;
use Microsoft\PhpParser\Token;

class ClassConstDeclaration extends Node implements ModifiedTypeInterface {
Expand All @@ -23,6 +24,9 @@ class ClassConstDeclaration extends Node implements ModifiedTypeInterface {
/** @var Token */
public $constKeyword;

/** @var QualifiedNameList|null */
public $typeDeclarationList;

/** @var DelimitedList\ConstElementList */
public $constElements;

Expand All @@ -33,6 +37,7 @@ class ClassConstDeclaration extends Node implements ModifiedTypeInterface {
'attributes',
'modifiers',
'constKeyword',
'typeDeclarationList',
'constElements',
'semicolon'
];
Expand Down
18 changes: 13 additions & 5 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -1417,7 +1417,7 @@ private function parseStringLiteralExpression2($parentNode): StringLiteral {
case TokenKind::DollarOpenBraceToken:
case TokenKind::OpenBraceDollarToken:
$expression->children[] = $this->eat(TokenKind::DollarOpenBraceToken, TokenKind::OpenBraceDollarToken);
/**
/**
* @phpstan-ignore-next-line "Strict comparison using
* === between 403|404 and 408 will always evaluate to
* false" is wrong because those tokens were eaten above
Expand Down Expand Up @@ -2802,10 +2802,6 @@ private function parseListIntrinsicExpression($parentNode) {
return $listExpression;
}

private function isArrayElementStart($token) {
return ($this->isArrayElementStartFn())($token);
}

private function isArrayElementStartFn() {
return function ($token) {
return $token->kind === TokenKind::AmpersandToken || $token->kind === TokenKind::DotDotDotToken || $this->isExpressionStart($token);
Expand Down Expand Up @@ -3370,6 +3366,18 @@ private function parseClassConstDeclaration($parentNode, $modifiers) {

$classConstDeclaration->modifiers = $modifiers;
$classConstDeclaration->constKeyword = $this->eat1(TokenKind::ConstKeyword);
// Handle class constant declarations such as `const X|Y Z = 123;` or `const X = 123;`.
// This is similar to lookahead().
$startPos = $this->lexer->getCurrentPosition();
$startToken = $this->token;
$classConstDeclaration->typeDeclarationList = $this->tryParseParameterTypeDeclarationList($classConstDeclaration);
if ($startToken->kind !== TokenKind::BackslashToken &&
in_array($this->token->kind, [TokenKind::EqualsToken, TokenKind::CommaToken, TokenKind::SemicolonToken, TokenKind::EndOfFileToken]) &&
$this->lexer->getCurrentPosition() <= $startPos + 1) {
$classConstDeclaration->typeDeclarationList = null;
$this->lexer->setCurrentPosition($startPos);
$this->token = $startToken;
}
$classConstDeclaration->constElements = $this->parseConstElements($classConstDeclaration);
$classConstDeclaration->semicolon = $this->eat1(TokenKind::SemicolonToken);

Expand Down
1 change: 1 addition & 0 deletions tests/cases/parser/classConstDeclaration1.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"kind": "ConstKeyword",
"textLength": 5
},
"typeDeclarationList": null,
"constElements": {
"ConstElementList": {
"children": [
Expand Down
1 change: 1 addition & 0 deletions tests/cases/parser/classConstDeclaration10.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"kind": "ConstKeyword",
"textLength": 5
},
"typeDeclarationList": null,
"constElements": {
"ConstElementList": {
"children": [
Expand Down
4 changes: 4 additions & 0 deletions tests/cases/parser/classConstDeclaration11.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
class A {
const int|string a = 3;
}
1 change: 1 addition & 0 deletions tests/cases/parser/classConstDeclaration11.php.diag
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
109 changes: 109 additions & 0 deletions tests/cases/parser/classConstDeclaration11.php.tree
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
{
"SourceFileNode": {
"statementList": [
{
"InlineHtml": {
"scriptSectionEndTag": null,
"text": null,
"scriptSectionStartTag": {
"kind": "ScriptSectionStartTag",
"textLength": 6
}
}
},
{
"ClassDeclaration": {
"attributes": null,
"abstractOrFinalModifier": null,
"modifiers": [],
"classKeyword": {
"kind": "ClassKeyword",
"textLength": 5
},
"name": {
"kind": "Name",
"textLength": 1
},
"classBaseClause": null,
"classInterfaceClause": null,
"classMembers": {
"ClassMembersNode": {
"openBrace": {
"kind": "OpenBraceToken",
"textLength": 1
},
"classMemberDeclarations": [
{
"ClassConstDeclaration": {
"attributes": null,
"modifiers": [],
"constKeyword": {
"kind": "ConstKeyword",
"textLength": 5
},
"typeDeclarationList": {
"QualifiedNameList": {
"children": [
{
"kind": "IntReservedWord",
"textLength": 3
},
{
"kind": "BarToken",
"textLength": 1
},
{
"kind": "StringReservedWord",
"textLength": 6
}
]
}
},
"constElements": {
"ConstElementList": {
"children": [
{
"ConstElement": {
"name": {
"kind": "Name",
"textLength": 1
},
"equalsToken": {
"kind": "EqualsToken",
"textLength": 1
},
"assignment": {
"NumericLiteral": {
"children": {
"kind": "IntegerLiteralToken",
"textLength": 1
}
}
}
}
}
]
}
},
"semicolon": {
"kind": "SemicolonToken",
"textLength": 1
}
}
}
],
"closeBrace": {
"kind": "CloseBraceToken",
"textLength": 1
}
}
}
}
}
],
"endOfFileToken": {
"kind": "EndOfFileToken",
"textLength": 0
}
}
}
4 changes: 4 additions & 0 deletions tests/cases/parser/classConstDeclaration12.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
class A {
const int| a = 3;
}
26 changes: 26 additions & 0 deletions tests/cases/parser/classConstDeclaration12.php.diag
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[
{
"kind": 0,
"message": "';' expected.",
"start": 32,
"length": 0
},
{
"kind": 0,
"message": "Unexpected '='",
"start": 33,
"length": 1
},
{
"kind": 0,
"message": "'}' expected.",
"start": 34,
"length": 0
},
{
"kind": 0,
"message": "Unexpected '}'",
"start": 38,
"length": 1
}
]
120 changes: 120 additions & 0 deletions tests/cases/parser/classConstDeclaration12.php.tree
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
{
"SourceFileNode": {
"statementList": [
{
"InlineHtml": {
"scriptSectionEndTag": null,
"text": null,
"scriptSectionStartTag": {
"kind": "ScriptSectionStartTag",
"textLength": 6
}
}
},
{
"ClassDeclaration": {
"attributes": null,
"abstractOrFinalModifier": null,
"modifiers": [],
"classKeyword": {
"kind": "ClassKeyword",
"textLength": 5
},
"name": {
"kind": "Name",
"textLength": 1
},
"classBaseClause": null,
"classInterfaceClause": null,
"classMembers": {
"ClassMembersNode": {
"openBrace": {
"kind": "OpenBraceToken",
"textLength": 1
},
"classMemberDeclarations": [
{
"ClassConstDeclaration": {
"attributes": null,
"modifiers": [],
"constKeyword": {
"kind": "ConstKeyword",
"textLength": 5
},
"typeDeclarationList": {
"QualifiedNameList": {
"children": [
{
"kind": "IntReservedWord",
"textLength": 3
},
{
"kind": "BarToken",
"textLength": 1
},
{
"QualifiedName": {
"globalSpecifier": null,
"relativeSpecifier": null,
"nameParts": [
{
"kind": "Name",
"textLength": 1
}
]
}
}
]
}
},
"constElements": null,
"semicolon": {
"error": "MissingToken",
"kind": "SemicolonToken",
"textLength": 0
}
}
},
{
"error": "SkippedToken",
"kind": "EqualsToken",
"textLength": 1
}
],
"closeBrace": {
"error": "MissingToken",
"kind": "CloseBraceToken",
"textLength": 0
}
}
}
}
},
{
"ExpressionStatement": {
"expression": {
"NumericLiteral": {
"children": {
"kind": "IntegerLiteralToken",
"textLength": 1
}
}
},
"semicolon": {
"kind": "SemicolonToken",
"textLength": 1
}
}
},
{
"error": "SkippedToken",
"kind": "CloseBraceToken",
"textLength": 1
}
],
"endOfFileToken": {
"kind": "EndOfFileToken",
"textLength": 0
}
}
}
4 changes: 4 additions & 0 deletions tests/cases/parser/classConstDeclaration13.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
class A {
const \
}
8 changes: 8 additions & 0 deletions tests/cases/parser/classConstDeclaration13.php.diag
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
"kind": 0,
"message": "';' expected.",
"start": 27,
"length": 0
}
]
Loading
Loading