Skip to content

Commit

Permalink
PHP 8.2 | Scopes::isOOConstant(): add support for constants in traits
Browse files Browse the repository at this point in the history
As of PHP 8.2, it is allowed to declare constants in traits.

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

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

Refs:
* https://wiki.php.net/rfc/constants_in_traits
  • Loading branch information
jrfnl committed Oct 15, 2022
1 parent dd41a73 commit 67f4898
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 5 deletions.
7 changes: 5 additions & 2 deletions PHPCSUtils/Tokens/Collections.php
Expand Up @@ -401,10 +401,11 @@ class Collections
/**
* DEPRECATED: OO scopes in which constants can be declared.
*
* Note: traits can not declare constants.
* Note: traits can only declare constants since PHP 8.2.
*
* @since 1.0.0-alpha1
* @since 1.0.0-alpha4 Added the PHP 8.1 T_ENUM token.
* @since 1.0.0-alpha4 Added the T_TRAIT token for PHP 8.2 constants in traits.
*
* @deprecated 1.0.0-alpha4 Use the {@see Collections::ooConstantScopes()} method instead.
*
Expand All @@ -415,6 +416,7 @@ class Collections
\T_ANON_CLASS => \T_ANON_CLASS,
\T_INTERFACE => \T_INTERFACE,
\T_ENUM => \T_ENUM,
\T_TRAIT => \T_TRAIT,
];

/**
Expand Down Expand Up @@ -846,10 +848,11 @@ public static function ooCanImplement()
/**
* OO scopes in which constants can be declared.
*
* Note: traits can not declare constants.
* Note: traits can only declare constants since PHP 8.2.
*
* @since 1.0.0-alpha4 This method replaces the {@see Collections::$OOConstantScopes} property.
* @since 1.0.0-alpha4 Added the PHP 8.1 T_ENUM token.
* @since 1.0.0-alpha4 Added the T_TRAIT token for PHP 8.2 constants in traits.
*
* @return array <int|string> => <int|string>
*/
Expand Down
3 changes: 2 additions & 1 deletion PHPCSUtils/Utils/Scopes.php
Expand Up @@ -56,10 +56,11 @@ public static function validDirectScope(File $phpcsFile, $stackPtr, $validScopes
}

/**
* Check whether a T_CONST token is a class/interface/enum constant declaration.
* Check whether a T_CONST token is a class/interface/trait/enum constant declaration.
*
* @since 1.0.0
* @since 1.0.0-alpha4 Added support for PHP 8.1 enums.
* @since 1.0.0-alpha4 Added support for PHP 8.2 constants in traits.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found.
* @param int $stackPtr The position in the stack of the
Expand Down
2 changes: 1 addition & 1 deletion Tests/Utils/Scopes/IsOOConstantTest.inc
Expand Up @@ -33,7 +33,7 @@ interface MyInterface {
}

trait MyTrait {
// Intentional parse error. Constants are not allowed in traits.
// Prior to PHP 8.2, this was a parse error. Since PHP 8.2, constants are allowed in traits.
/* testTraitConst */
const BAR = false;
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/Utils/Scopes/IsOOConstantTest.php
Expand Up @@ -107,7 +107,7 @@ public function dataIsOOConstant()
],
'trait-const' => [
'testMarker' => '/* testTraitConst */',
'expected' => false,
'expected' => true,
],
'enum-const' => [
'testMarker' => '/* testEnumConst */',
Expand Down

0 comments on commit 67f4898

Please sign in to comment.