PHP: support new syntax (constructor property promotion, readonly modifier, etc.) #1829
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
empty
match
readonly
unset
readonly
modifiersDetails
Add missing keywords
empty
andunset
are function-like keywords. Previously they are parsed as built-in functions like this:cf. https://www.php.net/manual/en/reserved.keywords.php
They should be parsed as keywords as
isset()
anddie()
.Also,
match
expression is introduced since PHP 8.0.cf. https://www.php.net/manual/en/control-structures.match.php
Fix parsing of static properties
Previously, static property declaration with type hint like
public static int $x;
was parsed as the following:public
)static
)int
)$x
)int
should be parsed as a type.The previous parser state
:in_visibility
defines these rules:Because a parser pops the current state once it find
static
, it cannot recognize a type hint following thestatic
keyword.I split the rule into two,
static
and other. If a parser encountersstatic
, it stays in the current state and then parses a type hints.Support
readonly
modifiersreadonly
is a new property modifier, available in PHP 8.1 or later.cf. https://www.php.net/manual/en/language.oop5.properties.php#language.oop5.properties.readonly-properties
Support constructor property promotion
Since PHP 8.0, you can prepend visibility modifiers to parameters of constructors. These parameters are "promoted" to class properties.
Since 8.1, you can specify
readonly
modifier as class properties.cf. https://www.php.net/manual/en/language.oop5.decon.php#language.oop5.decon.constructor.promotion