Skip to content

Commit

Permalink
Merge pull request #366 from PHPCSStandards/php-8.2/add-support-const…
Browse files Browse the repository at this point in the history
…ants-in-traits

PHP 8.2 | Scopes::isOOConstant(): add support for constants in traits
  • Loading branch information
jrfnl authored Oct 15, 2022
2 parents dd41a73 + 67f4898 commit 50cb535
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 50cb535

Please # to comment.