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

PHP 8.3 | Tokenizer/PHP: add support for typed OO constants #321

Merged
merged 1 commit into from
Feb 8, 2024

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Feb 1, 2024

Description

PHP 8.3 introduced typed OO constants, where the type is between the const keyword and the constant name.

All type variations are supported, including nullable types, union types, intersection types, with the exception of callable, void and never. self and static types are only allowed in Enum constants.

This PR adds support for typed OO constants in the Tokenizer layer of PHPCS.

The following issues had to be fixed to support typed constants:

  1. Consistently tokenizing the constant name as T_STRING, even if the name mirrors a reserved keyword, like foreach or a special keyword, like self or true.
  2. Tokenizing a ? at the start of a constant type declaration as T_NULLABLE.
  3. Tokenizing a | and & operators within a constant type declaration as T_TYPE_UNION and T_TYPE_INTERSECTION respectively.

Each and every part of the above has been covered by extensive tests.
Includes additional tests safeguarding that the array keyword when used in a type declaration for a constant is tokenized as T_STRING.

Ref: https://wiki.php.net/rfc/typed_class_constants

Suggested changelog entry

  • Added: Tokenizer support for PHP 8.3 typed class constants.

Related issues/external references

Related to #106

Types of changes

  • New feature (non-breaking change which adds functionality)

@calinblaga
Copy link

Thanks for these adjustments!
I'm having this error Class constants must be uppercase; expected INT but found int using this branch as a dependency.
I know it's not included in the description of this PR, but any idea how can we address this case?

@jrfnl
Copy link
Member Author

jrfnl commented Feb 1, 2024

Thanks for these adjustments! I'm having this error Class constants must be uppercase; expected INT but found int using this branch as a dependency. I know it's not included in the description of this PR, but any idea how can we address this case?

@calinblaga You don't need to. This PR is a prerequisite for fixing the sniff and those changes have already been lined up and will be pulled once this PR has been merged. Also see #106.

@jrfnl
Copy link
Member Author

jrfnl commented Feb 7, 2024

Unless there are objections/someone leaves a comments on this PR, I will merge this tomorrow.

PHP 8.3 introduced typed OO constants, where the type is between the `const` keyword and the constant name.

All type variations are supported, including nullable types, union types, intersection types, with the exception of `callable`, `void` and `never`.
`self` and `static` types are only allowed in Enum constants.

This PR adds support for typed OO constants in the Tokenizer layer of PHPCS.

The following issues had to be fixed to support typed constants:
1. Consistently tokenizing the constant _name_ as `T_STRING`, even if the name mirrors a reserved keyword, like `foreach` or a special keyword, like `self` or `true`.
2. Tokenizing a `?` at the start of a constant type declaration as `T_NULLABLE`.
3. Tokenizing a `|` and `&` operators within a constant type declaration as `T_TYPE_UNION` and `T_TYPE_INTERSECTION` respectively.

Each and every part of the above has been covered by extensive tests.
Includes additional tests safeguarding that the `array` keyword when used in a type declaration for a constant is tokenized as `T_STRING`.

Ref: https://wiki.php.net/rfc/typed_class_constants
@jrfnl
Copy link
Member Author

jrfnl commented Feb 8, 2024

Rebased without changes. Merging once the build passes.

@jrfnl jrfnl force-pushed the php-8.3/tokenizer-php-allow-for-typed-constants branch from 7062488 to 53dd20c Compare February 8, 2024 10:57
@jrfnl jrfnl merged commit de3b745 into master Feb 8, 2024
45 checks passed
@jrfnl jrfnl deleted the php-8.3/tokenizer-php-allow-for-typed-constants branch February 8, 2024 11:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants