From 67f489819240c1a2e9fa8cb4974c67033a955220 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Thu, 6 Oct 2022 04:50:42 +0200 Subject: [PATCH] PHP 8.2 | Scopes::isOOConstant(): add support for constants in traits 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 --- PHPCSUtils/Tokens/Collections.php | 7 +++++-- PHPCSUtils/Utils/Scopes.php | 3 ++- Tests/Utils/Scopes/IsOOConstantTest.inc | 2 +- Tests/Utils/Scopes/IsOOConstantTest.php | 2 +- 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/PHPCSUtils/Tokens/Collections.php b/PHPCSUtils/Tokens/Collections.php index 7b743dee..f000c103 100644 --- a/PHPCSUtils/Tokens/Collections.php +++ b/PHPCSUtils/Tokens/Collections.php @@ -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. * @@ -415,6 +416,7 @@ class Collections \T_ANON_CLASS => \T_ANON_CLASS, \T_INTERFACE => \T_INTERFACE, \T_ENUM => \T_ENUM, + \T_TRAIT => \T_TRAIT, ]; /** @@ -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 => */ diff --git a/PHPCSUtils/Utils/Scopes.php b/PHPCSUtils/Utils/Scopes.php index ae8d584d..4ec19d77 100644 --- a/PHPCSUtils/Utils/Scopes.php +++ b/PHPCSUtils/Utils/Scopes.php @@ -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 diff --git a/Tests/Utils/Scopes/IsOOConstantTest.inc b/Tests/Utils/Scopes/IsOOConstantTest.inc index 837b265c..31259737 100644 --- a/Tests/Utils/Scopes/IsOOConstantTest.inc +++ b/Tests/Utils/Scopes/IsOOConstantTest.inc @@ -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; } diff --git a/Tests/Utils/Scopes/IsOOConstantTest.php b/Tests/Utils/Scopes/IsOOConstantTest.php index a625eb5e..ff743cdb 100644 --- a/Tests/Utils/Scopes/IsOOConstantTest.php +++ b/Tests/Utils/Scopes/IsOOConstantTest.php @@ -107,7 +107,7 @@ public function dataIsOOConstant() ], 'trait-const' => [ 'testMarker' => '/* testTraitConst */', - 'expected' => false, + 'expected' => true, ], 'enum-const' => [ 'testMarker' => '/* testEnumConst */',