diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index edc2dd59..7283d2ce 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -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: diff --git a/src/Node/ClassConstDeclaration.php b/src/Node/ClassConstDeclaration.php index 2807bc23..bbbe0c69 100644 --- a/src/Node/ClassConstDeclaration.php +++ b/src/Node/ClassConstDeclaration.php @@ -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 { @@ -23,6 +24,9 @@ class ClassConstDeclaration extends Node implements ModifiedTypeInterface { /** @var Token */ public $constKeyword; + /** @var QualifiedNameList|null */ + public $typeDeclarationList; + /** @var DelimitedList\ConstElementList */ public $constElements; @@ -33,6 +37,7 @@ class ClassConstDeclaration extends Node implements ModifiedTypeInterface { 'attributes', 'modifiers', 'constKeyword', + 'typeDeclarationList', 'constElements', 'semicolon' ]; diff --git a/src/Parser.php b/src/Parser.php index 100c6c0c..8185ce22 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -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 @@ -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); @@ -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); diff --git a/tests/cases/parser/classConstDeclaration1.php.tree b/tests/cases/parser/classConstDeclaration1.php.tree index 664bc14f..1d2482b1 100644 --- a/tests/cases/parser/classConstDeclaration1.php.tree +++ b/tests/cases/parser/classConstDeclaration1.php.tree @@ -41,6 +41,7 @@ "kind": "ConstKeyword", "textLength": 5 }, + "typeDeclarationList": null, "constElements": { "ConstElementList": { "children": [ diff --git a/tests/cases/parser/classConstDeclaration10.php.tree b/tests/cases/parser/classConstDeclaration10.php.tree index 93a0dfa2..8e6057df 100644 --- a/tests/cases/parser/classConstDeclaration10.php.tree +++ b/tests/cases/parser/classConstDeclaration10.php.tree @@ -46,6 +46,7 @@ "kind": "ConstKeyword", "textLength": 5 }, + "typeDeclarationList": null, "constElements": { "ConstElementList": { "children": [ diff --git a/tests/cases/parser/classConstDeclaration11.php b/tests/cases/parser/classConstDeclaration11.php new file mode 100644 index 00000000..26a66806 --- /dev/null +++ b/tests/cases/parser/classConstDeclaration11.php @@ -0,0 +1,4 @@ +