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.1: add support for enums #358

Merged
merged 9 commits into from
Oct 15, 2022
Merged

PHP 8.1: add support for enums #358

merged 9 commits into from
Oct 15, 2022

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Oct 15, 2022

PHP 8.1 introduced a new type of OO structure: enumerations, which uses the enum keyword / T_ENUM token.

Refs:


PHP 8.1 | Collections::closedScopes(): add the T_ENUM token

Enums, like classes, interfaces and traits, are a closed scope structure, so the Collections::closedScopes() token array should include it.

PHP 8.1 | Scopes::isOOConstant(): add support for constants in enums

Alike classes, constants can be declared in an enum too.

This commit adds support for detecting whether a constant declared using the const keyword is within an enum structure to the Scopes::isOOConstant() method.

Includes adding the T_ENUM token to the Collections::ooConstantScopes() token array.

PHP 8.1 | Scopes::isOOMethod(): add support for enums

Alike classes, methods can be declared in an enum.

As the Scopes::isOOMethod() method uses the PHPCS based [BC]Tokens::$ooScopeTokens token array, in which T_ENUM was added in PHPCS 3.7.0, the Scopes::isOOMethod() method will correctly recognize methods within enums as OO methods.

This commit updates the documentation to indicate support for enums + adds tests to safeguard support.

PHP 8.1 | Scopes::isOOProperty(): add tests for enums

Unlike classes, properties are not allowed in enums.

This commit adds tests to ensure the Scopes::isOOProperty() method handles this correctly.

PHP 8.1 | BCFile/ObjectDeclarations::get[Declaration]Name(): sync with PHPCS / support enums

Enums are a named structure, so the BCFile/ObjectDeclarations::get[Declaration]Name() method should handle this correctly.

This commit syncs in the changes from upstream for the same.

Refs:

PHP 8.1 | BCFile/ObjectDeclarations::getClassProperties(): add test with enum

... to verify the method correctly throws an exception.

PHP 8.1 | BCFile/ObjectDeclarations::findImplementedInterfaceNames(): sync with PHPCS / add support for enums

An enum declaration can implement one or more interfaces.

This commit updates the File::findImplementedInterfaceNames() method to allow it to find the interfaces implemented by an enum as well.

Includes unit tests.

Includes adding the T_ENUM token to the Collections::ooCanImplement() token array.

Refs:

PHP 8.1 | BCFile/Variables::getMemberProperties(): throw exception for properties in enum

Handle enums in the same way as interface properties (neither are supported by PHP).

Neither enums nor interfaces support properties being declared on them.

This adjusts the File::getMemberProperties() method to handle properties declared on an enumin the same way as properties declared on aninterface` were already being handled.

Includes unit tests.

Refs:

PHP 8.1 | Utils\UseStatements::getType(): update comment to document enum support

jrfnl and others added 9 commits October 15, 2022 06:39
PHP 8.1 introduced a new type of OO structure: enumerations, which uses the `enum` keyword / `T_ENUM` token.

Enums, like classes, interfaces and traits, are a closed scope structure, so the `Collections::closedScopes()` token array should include it.

Refs:
* https://wiki.php.net/rfc/enumerations
PHP 8.1 introduced a new type of OO structure: enums.

Alike classes, constants can be declared in an enum too.

This commit adds support for detecting whether a constant declared using the `const` keyword is within an enum structure to the `Scopes::isOOConstant()` method.

Includes adding the `T_ENUM` token to the `Collections::ooConstantScopes()` token array.

Refs:
* https://wiki.php.net/rfc/enumerations
PHP 8.1 introduced a new type of OO structure: enums.

Alike classes, methods can be declared in an enum.

As the `Scopes::isOOMethod()` method uses the PHPCS based `[BC]Tokens::$ooScopeTokens` token array, in which `T_ENUM` was added in PHPCS 3.7.0, the `Scopes::isOOMethod()` method will correctly recognize methods within enums as OO methods.

This commit updates the documentation to indicate support for enums + adds tests to safeguard support.

Refs:
* https://wiki.php.net/rfc/enumerations
PHP 8.1 introduced a new type of OO structure: enums.

Unlike classes, properties are not allowed in enums.

This commit adds tests to ensure the `Scopes::isOOProperty()` method handles this correctly.

Refs:
* https://wiki.php.net/rfc/enumerations
…h PHPCS / support enums

PHP 8.1 introduced a new type of OO structure: enums.

Enums are a named structure, so the `BCFile/ObjectDeclarations::get[Declaration]Name()` method should handle this correctly.

This commit syncs in the changes from upstream for the same.

Refs:
* https://wiki.php.net/rfc/enumerations
* squizlabs/PHP_CodeSniffer 3482

Co-authored-by: Jaroslav Hanslík <kukulich@kukulich.cz>
…ith enum

... to verify the method correctly throws an exception.
… sync with PHPCS / add support for enums

> An `enum` declaration can implement one or more interfaces.
>
> This commit updates the `File::findImplementedInterfaceNames()` method to allow it to find the interfaces implemented by an `enum` as well.
>
> Includes unit tests.

Includes adding the `T_ENUM` token to the `Collections::ooCanImplement()` token array.

Refs:
* https://wiki.php.net/rfc/enumerations
* squizlabs/PHP_CodeSniffer 3578
…r properties in enum

Handle enums in the same way as interface properties (neither are supported by PHP).

> Neither enums nor interfaces support properties being declared on them.
>
> This adjusts the `File::getMemberProperties() method to handle properties declared on an `enum` in the same way as properties declared on an `interface` were already being handled.

Includes unit tests.

Refs:
* squizlabs/PHP_CodeSniffer 3577
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.

1 participant