diff --git a/PHPCSUtils/BackCompat/BCFile.php b/PHPCSUtils/BackCompat/BCFile.php index f2dc106d..b66b46e6 100644 --- a/PHPCSUtils/BackCompat/BCFile.php +++ b/PHPCSUtils/BackCompat/BCFile.php @@ -859,7 +859,7 @@ public static function getMemberProperties(File $phpcsFile, $stackPtr) } } - $valid = Collections::$propertyModifierKeywords; + $valid = Collections::propertyModifierKeywords(); $valid += Tokens::$emptyTokens; $scope = 'public'; diff --git a/PHPCSUtils/BackCompat/BCTokens.php b/PHPCSUtils/BackCompat/BCTokens.php index e1b62005..5f648883 100644 --- a/PHPCSUtils/BackCompat/BCTokens.php +++ b/PHPCSUtils/BackCompat/BCTokens.php @@ -111,6 +111,24 @@ class BCTokens \T_NOWDOC => \T_NOWDOC, ]; + /** + * Tokens representing PHP magic constants. + * + * @since 1.0.0-alpha4 + * + * @var array => + */ + private static $magicConstants = [ + \T_CLASS_C => \T_CLASS_C, + \T_DIR => \T_DIR, + \T_FILE => \T_FILE, + \T_FUNC_C => \T_FUNC_C, + \T_LINE => \T_LINE, + \T_METHOD_C => \T_METHOD_C, + \T_NS_C => \T_NS_C, + \T_TRAIT_C => \T_TRAIT_C, + ]; + /** * Handle calls to (undeclared) methods for token arrays which haven't received any * changes since PHPCS 2.6.0. @@ -467,8 +485,7 @@ public static function ooScopeTokens() * Changelog for the PHPCS native array: * - Introduced in PHPCS 3.5.6. * - * @see \PHP_CodeSniffer\Util\Tokens::$magicConstants Original array. - * @see \PHPCSUtils\Tokens\Collections::$magicConstants Same array, pre-dating the PHPCS change. + * @see \PHP_CodeSniffer\Util\Tokens::$magicConstants Original array. * * @link https://www.php.net/language.constants.predefined PHP Manual on magic constants * @@ -482,7 +499,7 @@ public static function magicConstants() return Tokens::$magicConstants; } - return Collections::$magicConstants; + return self::$magicConstants; } /** diff --git a/PHPCSUtils/Tokens/Collections.php b/PHPCSUtils/Tokens/Collections.php index 47e60763..93e1ddaf 100644 --- a/PHPCSUtils/Tokens/Collections.php +++ b/PHPCSUtils/Tokens/Collections.php @@ -27,10 +27,13 @@ class Collections { /** - * Control structures which can use the alternative control structure syntax. + * DEPRECATED: Control structures which can use the alternative control structure syntax. * * @since 1.0.0-alpha2 * + * @deprecated 1.0.0-alpha4 Use the {@see Collections::alternativeControlStructureSyntaxes()} + * method instead. + * * @var array => */ public static $alternativeControlStructureSyntaxTokens = [ @@ -45,10 +48,13 @@ class Collections ]; /** - * Alternative control structure syntax closer keyword tokens. + * DEPRECATED: Alternative control structure syntax closer keyword tokens. * * @since 1.0.0-alpha2 * + * @deprecated 1.0.0-alpha4 Use the {@see Collections::alternativeControlStructureSyntaxClosers()} + * method instead. + * * @var array => */ public static $alternativeControlStructureSyntaxCloserTokens = [ @@ -61,29 +67,12 @@ class Collections ]; /** - * Tokens which can open an array. - * - * PHPCS cross-version compatible. - * - * @since 1.0.0-alpha4 - * - * @var array => - */ - public static $arrayOpenTokensBC = [ - \T_ARRAY => \T_ARRAY, - \T_OPEN_SHORT_ARRAY => \T_OPEN_SHORT_ARRAY, - \T_OPEN_SQUARE_BRACKET => \T_OPEN_SQUARE_BRACKET, - ]; - - /** - * Tokens which are used to create arrays. + * DEPRECATED: Tokens which are used to create arrays. * - * @see \PHPCSUtils\Tokens\Collections::$arrayTokensBC Related property containing tokens used - * for arrays (PHPCS cross-version). - * @see \PHPCSUtils\Tokens\Collections::$shortArrayTokens Related property containing only tokens used - * for short arrays. + * @since 1.0.0-alpha1 * - * @since 1.0.0 + * @deprecated 1.0.0-alpha4 Use the {@see Collections::arrayTokens()} or + * {@see Collections::arrayTokensBC()} method instead. * * @var array => */ @@ -94,15 +83,11 @@ class Collections ]; /** - * Tokens which are used to create arrays. + * DEPRECATED: Tokens which are used to create arrays. * - * List which is backward-compatible with PHPCS < 3.3.0. - * Should only be used selectively. - * - * @see \PHPCSUtils\Tokens\Collections::$shortArrayTokensBC Related property containing only tokens used - * for short arrays (PHPCS cross-version). + * @since 1.0.0-alpha1 * - * @since 1.0.0 + * @deprecated 1.0.0-alpha4 Use the {@see Collections::arrayTokensBC()} method instead. * * @var array => */ @@ -115,9 +100,11 @@ class Collections ]; /** - * Modifier keywords which can be used for a class declaration. + * DEPRECATED: Modifier keywords which can be used for a class declaration. + * + * @since 1.0.0-alpha1 * - * @since 1.0.0 + * @deprecated 1.0.0-alpha4 Use the {@see Collections::classModifierKeywords()} method instead. * * @var array => */ @@ -127,15 +114,11 @@ class Collections ]; /** - * List of tokens which represent "closed" scopes. - * - * I.e. anything declared within that scope - except for other closed scopes - is - * outside of the global namespace. + * DEPRECATED: List of tokens which represent "closed" scopes. * - * This list doesn't contain the `T_NAMESPACE` token on purpose as variables declared - * within a namespace scope are still global and not limited to that namespace. + * @since 1.0.0-alpha1 * - * @since 1.0.0 + * @deprecated 1.0.0-alpha4 Use the {@see Collections::closedScopes()} method instead. * * @var array => */ @@ -149,10 +132,12 @@ class Collections ]; /** - * Control structure tokens. + * DEPRECATED: Control structure tokens. * * @since 1.0.0-alpha2 * + * @deprecated 1.0.0-alpha4 Use the {@see Collections::controlStructureTokens()} method instead. + * * @var array => */ public static $controlStructureTokens = [ @@ -168,10 +153,12 @@ class Collections ]; /** - * Increment/decrement operator tokens. + * DEPRECATED: Increment/decrement operator tokens. * * @since 1.0.0-alpha3 * + * @deprecated 1.0.0-alpha4 Use the {@see Collections::incrementDecrementOperators()} method instead. + * * @var array => */ public static $incrementDecrementOperators = [ @@ -180,14 +167,12 @@ class Collections ]; /** - * Tokens which are used to create lists. + * DEPRECATED: Tokens which are used to create lists. * - * @see \PHPCSUtils\Tokens\Collections::$listTokensBC Related property containing tokens used - * for lists (PHPCS cross-version). - * @see \PHPCSUtils\Tokens\Collections::$shortListTokens Related property containing only tokens used - * for short lists. + * @since 1.0.0-alpha1 * - * @since 1.0.0 + * @deprecated 1.0.0-alpha4 Use the {@see Collections::listTokens()} or + * {@see Collections::listTokensBC()} method instead. * * @var array => */ @@ -198,15 +183,11 @@ class Collections ]; /** - * Tokens which are used to create lists. - * - * List which is backward-compatible with PHPCS < 3.3.0. - * Should only be used selectively. + * DEPRECATED: Tokens which are used to create lists. * - * @see \PHPCSUtils\Tokens\Collections::$shortListTokensBC Related property containing only tokens used - * for short lists (cross-version). + * @since 1.0.0-alpha1 * - * @since 1.0.0 + * @deprecated 1.0.0-alpha4 Use the {@see Collections::listTokensBC()} method instead. * * @var array => */ @@ -219,12 +200,12 @@ class Collections ]; /** - * Tokens for the PHP magic constants. - * - * @link https://www.php.net/language.constants.predefined PHP Manual on magic constants + * DEPRECATED: Tokens for the PHP magic constants. * * @since 1.0.0-alpha3 * + * @deprecated 1.0.0-alpha4 Use the {@see \PHPCSUtils\BackCompat\BCTokens::magicConstants()} method instead. + * * @var array => */ public static $magicConstants = [ @@ -239,9 +220,11 @@ class Collections ]; /** - * List of tokens which can end a namespace declaration statement. + * DEPRECATED: List of tokens which can end a namespace declaration statement. + * + * @since 1.0.0-alpha1 * - * @since 1.0.0 + * @deprecated 1.0.0-alpha4 Use the {@see Collections::namespaceDeclarationClosers()} method instead. * * @var array => */ @@ -266,9 +249,11 @@ class Collections ]; /** - * OO structures which can use the "extends" keyword. + * DEPRECATED: OO structures which can use the "extends" keyword. * - * @since 1.0.0 + * @since 1.0.0-alpha1 + * + * @deprecated 1.0.0-alpha4 Use the {@see Collections::ooCanExtend()} method instead. * * @var array => */ @@ -279,9 +264,11 @@ class Collections ]; /** - * OO structures which can use the "implements" keyword. + * DEPRECATED: OO structures which can use the "implements" keyword. * - * @since 1.0.0 + * @since 1.0.0-alpha1 + * + * @deprecated 1.0.0-alpha4 Use the {@see Collections::ooCanImplement()} method instead. * * @var array => */ @@ -291,11 +278,11 @@ class Collections ]; /** - * OO scopes in which constants can be declared. + * DEPRECATED: OO scopes in which constants can be declared. * - * Note: traits can not declare constants. + * @since 1.0.0-alpha1 * - * @since 1.0.0 + * @deprecated 1.0.0-alpha4 Use the {@see Collections::ooConstantScopes()} method instead. * * @var array => */ @@ -306,12 +293,12 @@ class Collections ]; /** - * Tokens types used for "forwarding" calls within OO structures. - * - * @link https://www.php.net/language.oop5.paamayim-nekudotayim PHP Manual on OO forwarding calls + * DEPRECATED: Tokens types used for "forwarding" calls within OO structures. * * @since 1.0.0-alpha3 * + * @deprecated 1.0.0-alpha4 Use the {@see Collections::ooHierarchyKeywords()} method instead. + * * @var array => */ public static $OOHierarchyKeywords = [ @@ -336,11 +323,11 @@ class Collections ]; /** - * OO scopes in which properties can be declared. + * DEPRECATED: OO scopes in which properties can be declared. * - * Note: interfaces can not declare properties. + * @since 1.0.0-alpha1 * - * @since 1.0.0 + * @deprecated 1.0.0-alpha4 Use the {@see Collections::ooPropertyScopes()} method instead. * * @var array => */ @@ -369,9 +356,11 @@ class Collections ]; /** - * Modifier keywords which can be used for a property declaration. + * DEPRECATED: Modifier keywords which can be used for a property declaration. + * + * @since 1.0.0-alpha1 * - * @since 1.0.0 + * @deprecated 1.0.0-alpha4 Use the {@see Collections::propertyModifierKeywords()} method instead. * * @var array => */ @@ -421,11 +410,12 @@ class Collections ]; /** - * Tokens which are used for short arrays. + * DEPRECATED: Tokens which are used for short arrays. * - * @see \PHPCSUtils\Tokens\Collections::$arrayTokens Related property containing all tokens used for arrays. + * @since 1.0.0-alpha1 * - * @since 1.0.0 + * @deprecated 1.0.0-alpha4 Use the {@see Collections::shortArrayTokens()} or + * {@see Collections::shortArrayTokensBC()} method instead. * * @var array => */ @@ -435,15 +425,11 @@ class Collections ]; /** - * Tokens which are used for short arrays. - * - * List which is backward-compatible with PHPCS < 3.3.0. - * Should only be used selectively. + * DEPRECATED: Tokens which are used for short arrays. * - * @see \PHPCSUtils\Tokens\Collections::$arrayTokensBC Related property containing all tokens used for arrays - * (cross-version). + * @since 1.0.0-alpha1 * - * @since 1.0.0 + * @deprecated 1.0.0-alpha4 Use the {@see Collections::shortArrayTokensBC()} method instead. * * @var array => */ @@ -455,11 +441,12 @@ class Collections ]; /** - * Tokens which are used for short lists. + * DEPRECATED: Tokens which are used for short lists. * - * @see \PHPCSUtils\Tokens\Collections::$listTokens Related property containing all tokens used for lists. + * @since 1.0.0-alpha1 * - * @since 1.0.0 + * @deprecated 1.0.0-alpha4 Use the {@see Collections::shortListTokens()} or + * {@see Collections::shortListTokensBC()} method instead. * * @var array => */ @@ -469,15 +456,11 @@ class Collections ]; /** - * Tokens which are used for short lists. + * DEPRECATED: Tokens which are used for short lists. * - * List which is backward-compatible with PHPCS < 3.3.0. - * Should only be used selectively. - * - * @see \PHPCSUtils\Tokens\Collections::$listTokensBC Related property containing all tokens used for lists - * (cross-version). + * @since 1.0.0-alpha1 * - * @since 1.0.0 + * @deprecated 1.0.0-alpha4 Use the {@see Collections::shortListTokensBC()} method instead. * * @var array => */ @@ -489,9 +472,11 @@ class Collections ]; /** - * Tokens which can start a - potentially multi-line - text string. + * DEPRECATED: Tokens which can start a - potentially multi-line - text string. * - * @since 1.0.0 + * @since 1.0.0-alpha1 + * + * @deprecated 1.0.0-alpha4 Use the {@see Collections::textStingStartTokens()} method instead. * * @var array => */ @@ -502,6 +487,85 @@ class Collections \T_DOUBLE_QUOTED_STRING => \T_DOUBLE_QUOTED_STRING, ]; + /** + * Tokens for control structures which can use the alternative control structure syntax. + * + * @since 1.0.0-alpha4 This method replaces the {@see Collections::$alternativeControlStructureSyntaxTokens} + * property. + * + * @return array => + */ + public static function alternativeControlStructureSyntaxes() + { + return self::$alternativeControlStructureSyntaxTokens; + } + + /** + * Tokens representing alternative control structure syntax closer keywords. + * + * @since 1.0.0-alpha4 This method replaces the {@see Collections::$alternativeControlStructureSyntaxCloserTokens} + * property. + * + * @return array => + */ + public static function alternativeControlStructureSyntaxClosers() + { + return self::$alternativeControlStructureSyntaxCloserTokens; + } + + /** + * Tokens which can open an array. + * + * PHPCS cross-version compatible. + * + * @since 1.0.0-alpha4 + * + * @return array => + */ + public static function arrayOpenTokensBC() + { + return [ + \T_ARRAY => \T_ARRAY, + \T_OPEN_SHORT_ARRAY => \T_OPEN_SHORT_ARRAY, + \T_OPEN_SQUARE_BRACKET => \T_OPEN_SQUARE_BRACKET, + ]; + } + + /** + * Tokens which are used to create arrays. + * + * @see \PHPCSUtils\Tokens\Collections::arrayTokensBC() Related method to retrieve tokens used + * for arrays (PHPCS cross-version). + * @see \PHPCSUtils\Tokens\Collections::shortArrayTokens() Related method to retrieve only tokens used + * for short arrays. + * + * @since 1.0.0-alpha4 This method replaces the {@see Collections::$arrayTokens} property. + * + * @return array => + */ + public static function arrayTokens() + { + return self::$arrayTokens; + } + + /** + * Tokens which are used to create arrays. + * + * List which is backward-compatible with PHPCS < 3.3.0. + * Should only be used selectively. + * + * @see \PHPCSUtils\Tokens\Collections::shortArrayTokensBC() Related method to retrieve only tokens used + * for short arrays (PHPCS cross-version). + * + * @since 1.0.0-alpha4 This method replaces the {@see Collections::$arrayTokensBC} property. + * + * @return array => + */ + public static function arrayTokensBC() + { + return self::$arrayTokensBC; + } + /** * Tokens which can represent the arrow function keyword. * @@ -531,6 +595,48 @@ public static function arrowFunctionTokensBC() return $tokens; } + /** + * Modifier keywords which can be used for a class declaration. + * + * @since 1.0.0-alpha4 This method replaces the {@see Collections::$classModifierKeywords} property. + * + * @return array => + */ + public static function classModifierKeywords() + { + return self::$classModifierKeywords; + } + + /** + * List of tokens which represent "closed" scopes. + * + * I.e. anything declared within that scope - except for other closed scopes - is + * outside of the global namespace. + * + * This list doesn't contain the `T_NAMESPACE` token on purpose as variables declared + * within a namespace scope are still global and not limited to that namespace. + * + * @since 1.0.0-alpha4 This method replaces the {@see Collections::$closedScopes} property. + * + * @return array => + */ + public static function closedScopes() + { + return self::$closedScopes; + } + + /** + * Control structure tokens. + * + * @since 1.0.0-alpha4 This method replaces the {@see Collections::$controlStructureTokens} property. + * + * @return array => + */ + public static function controlStructureTokens() + { + return self::$controlStructureTokens; + } + /** * Tokens which can represent function calls and function-call-like language constructs. * @@ -622,6 +728,65 @@ public static function functionDeclarationTokensBC() return $tokens; } + /** + * Increment/decrement operator tokens. + * + * @since 1.0.0-alpha4 This method replaces the {@see Collections::$incrementDecrementOperators} property. + * + * @return array => + */ + public static function incrementDecrementOperators() + { + return self::$incrementDecrementOperators; + } + + /** + * Tokens which are used to create lists. + * + * @see \PHPCSUtils\Tokens\Collections::listTokensBC() Related method to retrieve tokens used + * for lists (PHPCS cross-version). + * @see \PHPCSUtils\Tokens\Collections::shortListTokens() Related method to retrieve only tokens used + * for short lists. + * + * @since 1.0.0-alpha4 This method replaces the {@see Collections::$listTokens} property. + * + * @return array => + */ + public static function listTokens() + { + return self::$listTokens; + } + + /** + * Tokens which are used to create lists. + * + * List which is backward-compatible with PHPCS < 3.3.0. + * Should only be used selectively. + * + * @see \PHPCSUtils\Tokens\Collections::shortListTokensBC() Related method to retrieve only tokens used + * for short lists (cross-version). + * + * @since 1.0.0-alpha4 This method replaces the {@see Collections::$listTokensBC} property. + * + * @return array => + */ + public static function listTokensBC() + { + return self::$listTokensBC; + } + + /** + * List of tokens which can end a namespace declaration statement. + * + * @since 1.0.0-alpha4 This method replaces the {@see Collections::$namespaceDeclarationClosers} property. + * + * @return array => + */ + public static function namespaceDeclarationClosers() + { + return self::$namespaceDeclarationClosers; + } + /** * Tokens types which can be encountered in a fully, partially or unqualified name. * @@ -765,6 +930,72 @@ public static function objectOperatorsBC() return $tokens; } + /** + * OO structures which can use the "extends" keyword. + * + * @since 1.0.0-alpha4 This method replaces the {@see Collections::$OOCanExtend} property. + * + * @return array => + */ + public static function ooCanExtend() + { + return self::$OOCanExtend; + } + + /** + * OO structures which can use the "implements" keyword. + * + * @since 1.0.0-alpha4 This method replaces the {@see Collections::$OOCanImplement} property. + * + * @return array => + */ + public static function ooCanImplement() + { + return self::$OOCanImplement; + } + + /** + * OO scopes in which constants can be declared. + * + * Note: traits can not declare constants. + * + * @since 1.0.0-alpha4 This method replaces the {@see Collections::$OOConstantScopes} property. + * + * @return array => + */ + public static function ooConstantScopes() + { + return self::$OOConstantScopes; + } + + /** + * Tokens types used for "forwarding" calls within OO structures. + * + * @link https://www.php.net/language.oop5.paamayim-nekudotayim PHP Manual on OO forwarding calls + * + * @since 1.0.0-alpha4 This method replaces the {@see Collections::$OOHierarchyKeywords} property. + * + * @return array => + */ + public static function ooHierarchyKeywords() + { + return self::$OOHierarchyKeywords; + } + + /** + * OO scopes in which properties can be declared. + * + * Note: interfaces can not declare properties. + * + * @since 1.0.0-alpha4 This method replaces the {@see Collections::$OOPropertyScopes} property. + * + * @return array => + */ + public static function ooPropertyScopes() + { + return self::$OOPropertyScopes; + } + /** * Tokens which can represent the nullsafe object operator. * @@ -817,7 +1048,7 @@ public static function parameterPassingTokens() $tokens[\T_UNSET] = \T_UNSET; // Array tokens. - $tokens += self::$arrayOpenTokensBC; + $tokens += self::arrayOpenTokensBC(); return $tokens; } @@ -918,6 +1149,18 @@ public static function phpOpenTags() ]; } + /** + * Modifier keywords which can be used for a property declaration. + * + * @since 1.0.0-alpha4 This method replaces the {@see Collections::$propertyModifierKeywords} property. + * + * @return array => + */ + public static function propertyModifierKeywords() + { + return self::$propertyModifierKeywords; + } + /** * Token types which can be encountered in a property type declaration. * @@ -1093,4 +1336,80 @@ public static function returnTypeTokensBC() return $tokens; } + + /** + * Tokens which are used for short arrays. + * + * @see \PHPCSUtils\Tokens\Collections::arrayTokens() Related method to retrieve all tokens used for arrays. + * + * @since 1.0.0-alpha4 This method replaces the {@see Collections::$shortArrayTokens} property. + * + * @return array => + */ + public static function shortArrayTokens() + { + return self::$shortArrayTokens; + } + + /** + * Tokens which are used for short arrays. + * + * List which is backward-compatible with PHPCS < 3.3.0. + * Should only be used selectively. + * + * @see \PHPCSUtils\Tokens\Collections::arrayTokensBC() Related method to retrieve all tokens used for arrays + * (cross-version). + * + * @since 1.0.0-alpha4 This method replaces the {@see Collections::$shortArrayTokensBC} property. + * + * @return array => + */ + public static function shortArrayTokensBC() + { + return self::$shortArrayTokensBC; + } + + /** + * Tokens which are used for short lists. + * + * @see \PHPCSUtils\Tokens\Collections::listTokens() Related method to retrieve all tokens used for lists. + * + * @since 1.0.0-alpha4 This method replaces the {@see Collections::$shortListTokens} property. + * + * @return array => + */ + public static function shortListTokens() + { + return self::$shortListTokens; + } + + /** + * Tokens which are used for short lists. + * + * List which is backward-compatible with PHPCS < 3.3.0. + * Should only be used selectively. + * + * @see \PHPCSUtils\Tokens\Collections::listTokensBC() Related method to retrieve all tokens used for lists + * (cross-version). + * + * @since 1.0.0-alpha4 This method replaces the {@see Collections::$shortListTokensBC} property. + * + * @return array => + */ + public static function shortListTokensBC() + { + return self::$shortListTokensBC; + } + + /** + * Tokens which can start a - potentially multi-line - text string. + * + * @since 1.0.0-alpha4 This method replaces the {@see Collections::$textStingStartTokens} property. + * + * @return array => + */ + public static function textStingStartTokens() + { + return self::$textStingStartTokens; + } } diff --git a/PHPCSUtils/Utils/Arrays.php b/PHPCSUtils/Utils/Arrays.php index 84b38b82..fffe513c 100644 --- a/PHPCSUtils/Utils/Arrays.php +++ b/PHPCSUtils/Utils/Arrays.php @@ -13,6 +13,7 @@ use PHP_CodeSniffer\Exceptions\RuntimeException; use PHP_CodeSniffer\Files\File; use PHP_CodeSniffer\Util\Tokens; +use PHPCSUtils\BackCompat\BCTokens; use PHPCSUtils\BackCompat\Helper; use PHPCSUtils\Tokens\Collections; use PHPCSUtils\Utils\FunctionDeclarations; @@ -66,7 +67,7 @@ public static function isShortArray(File $phpcsFile, $stackPtr) // Is this one of the tokens this function handles ? if (isset($tokens[$stackPtr]) === false - || isset(Collections::$shortArrayTokensBC[$tokens[$stackPtr]['code']]) === false + || isset(Collections::shortArrayTokensBC()[$tokens[$stackPtr]['code']]) === false ) { return false; } @@ -77,7 +78,7 @@ public static function isShortArray(File $phpcsFile, $stackPtr) /* * Deal with square brackets which may be incorrectly tokenized short arrays. */ - if (isset(Collections::$shortArrayTokens[$tokens[$stackPtr]['code']]) === false) { + if (isset(Collections::shortArrayTokens()[$tokens[$stackPtr]['code']]) === false) { if (\version_compare($phpcsVersion, '3.3.0', '>=')) { // These will just be properly tokenized, plain square brackets. No need for further checks. return false; @@ -160,7 +161,7 @@ public static function isShortArray(File $phpcsFile, $stackPtr) * * @link https://github.com/squizlabs/PHP_CodeSniffer/pull/3013 */ - if (isset(Collections::$magicConstants[$tokens[$prevNonEmpty]['code']]) === true) { + if (isset(BCTokens::magicConstants()[$tokens[$prevNonEmpty]['code']]) === true) { return false; } } @@ -236,7 +237,7 @@ public static function getOpenClose(File $phpcsFile, $stackPtr, $isShortArray = // Is this one of the tokens this function handles ? if (isset($tokens[$stackPtr]) === false - || isset(Collections::$arrayTokensBC[$tokens[$stackPtr]['code']]) === false + || isset(Collections::arrayTokensBC()[$tokens[$stackPtr]['code']]) === false ) { return false; } @@ -300,7 +301,7 @@ public static function getDoubleArrowPtr(File $phpcsFile, $start, $end) } $targets = self::$doubleArrowTargets; - $targets += Collections::$closedScopes; + $targets += Collections::closedScopes(); $targets += Collections::arrowFunctionTokensBC(); $doubleArrow = ($start - 1); @@ -330,7 +331,7 @@ public static function getDoubleArrowPtr(File $phpcsFile, $start, $end) } // Skip over closed scopes which may contain foreach structures or generators. - if (isset(Collections::$closedScopes[$tokens[$doubleArrow]['code']]) === true + if (isset(Collections::closedScopes()[$tokens[$doubleArrow]['code']]) === true && isset($tokens[$doubleArrow]['scope_closer']) === true ) { $doubleArrow = $tokens[$doubleArrow]['scope_closer']; diff --git a/PHPCSUtils/Utils/ControlStructures.php b/PHPCSUtils/Utils/ControlStructures.php index 1798369b..b4b6c120 100644 --- a/PHPCSUtils/Utils/ControlStructures.php +++ b/PHPCSUtils/Utils/ControlStructures.php @@ -53,7 +53,7 @@ public static function hasBody(File $phpcsFile, $stackPtr, $allowEmpty = true) // Check for the existence of the token. if (isset($tokens[$stackPtr]) === false - || isset(Collections::$controlStructureTokens[$tokens[$stackPtr]['code']]) === false + || isset(Collections::controlStructureTokens()[$tokens[$stackPtr]['code']]) === false ) { return false; } diff --git a/PHPCSUtils/Utils/Lists.php b/PHPCSUtils/Utils/Lists.php index 9f79de76..6cd56279 100644 --- a/PHPCSUtils/Utils/Lists.php +++ b/PHPCSUtils/Utils/Lists.php @@ -13,6 +13,7 @@ use PHP_CodeSniffer\Exceptions\RuntimeException; use PHP_CodeSniffer\Files\File; use PHP_CodeSniffer\Util\Tokens; +use PHPCSUtils\BackCompat\BCTokens; use PHPCSUtils\BackCompat\Helper; use PHPCSUtils\Tokens\Collections; use PHPCSUtils\Utils\GetTokensAsString; @@ -69,7 +70,7 @@ public static function isShortList(File $phpcsFile, $stackPtr) // Is this one of the tokens this function handles ? if (isset($tokens[$stackPtr]) === false - || isset(Collections::$shortListTokensBC[$tokens[$stackPtr]['code']]) === false + || isset(Collections::shortListTokensBC()[$tokens[$stackPtr]['code']]) === false ) { return false; } @@ -150,7 +151,7 @@ public static function isShortList(File $phpcsFile, $stackPtr) * * @link https://github.com/squizlabs/PHP_CodeSniffer/pull/3013 */ - if (isset(Collections::$magicConstants[$tokens[$prevNonEmpty]['code']]) === true) { + if (isset(BCTokens::magicConstants()[$tokens[$prevNonEmpty]['code']]) === true) { return false; } } @@ -271,7 +272,7 @@ public static function getOpenClose(File $phpcsFile, $stackPtr, $isShortList = n // Is this one of the tokens this function handles ? if (isset($tokens[$stackPtr]) === false - || isset(Collections::$listTokensBC[$tokens[$stackPtr]['code']]) === false + || isset(Collections::listTokensBC()[$tokens[$stackPtr]['code']]) === false ) { return false; } diff --git a/PHPCSUtils/Utils/Namespaces.php b/PHPCSUtils/Utils/Namespaces.php index 6dd591b5..64ab6c7d 100644 --- a/PHPCSUtils/Utils/Namespaces.php +++ b/PHPCSUtils/Utils/Namespaces.php @@ -62,7 +62,7 @@ public static function getType(File $phpcsFile, $stackPtr) + BCTokens::operators() + Tokens::$castTokens + Tokens::$blockOpeners - + Collections::$incrementDecrementOperators + + Collections::incrementDecrementOperators() + Collections::objectOperators(); $findAfter[\T_OPEN_CURLY_BRACKET] = \T_OPEN_CURLY_BRACKET; @@ -188,7 +188,7 @@ public static function getDeclaredName(File $phpcsFile, $stackPtr, $clean = true return false; } - $endOfStatement = $phpcsFile->findNext(Collections::$namespaceDeclarationClosers, ($stackPtr + 1)); + $endOfStatement = $phpcsFile->findNext(Collections::namespaceDeclarationClosers(), ($stackPtr + 1)); if ($endOfStatement === false) { // Live coding or parse error. return false; @@ -338,7 +338,7 @@ public static function findNamespacePtr(File $phpcsFile, $stackPtr) && self::isDeclaration($phpcsFile, $prev) === true ) { // Now make sure the token was not part of the declaration. - $endOfStatement = $phpcsFile->findNext(Collections::$namespaceDeclarationClosers, ($prev + 1)); + $endOfStatement = $phpcsFile->findNext(Collections::namespaceDeclarationClosers(), ($prev + 1)); if ($endOfStatement > $stackPtr) { return false; } diff --git a/PHPCSUtils/Utils/ObjectDeclarations.php b/PHPCSUtils/Utils/ObjectDeclarations.php index f721bd02..8a835ec9 100644 --- a/PHPCSUtils/Utils/ObjectDeclarations.php +++ b/PHPCSUtils/Utils/ObjectDeclarations.php @@ -188,7 +188,7 @@ public static function getClassProperties(File $phpcsFile, $stackPtr) throw new RuntimeException('$stackPtr must be of type T_CLASS'); } - $valid = Collections::$classModifierKeywords + Tokens::$emptyTokens; + $valid = Collections::classModifierKeywords() + Tokens::$emptyTokens; $properties = [ 'is_abstract' => false, 'is_final' => false, @@ -245,7 +245,7 @@ public static function getClassProperties(File $phpcsFile, $stackPtr) */ public static function findExtendedClassName(File $phpcsFile, $stackPtr) { - $names = self::findNames($phpcsFile, $stackPtr, \T_EXTENDS, Collections::$OOCanExtend); + $names = self::findNames($phpcsFile, $stackPtr, \T_EXTENDS, Collections::ooCanExtend()); if ($names === false) { return false; } @@ -280,7 +280,7 @@ public static function findExtendedClassName(File $phpcsFile, $stackPtr) */ public static function findImplementedInterfaceNames(File $phpcsFile, $stackPtr) { - return self::findNames($phpcsFile, $stackPtr, \T_IMPLEMENTS, Collections::$OOCanImplement); + return self::findNames($phpcsFile, $stackPtr, \T_IMPLEMENTS, Collections::ooCanImplement()); } /** diff --git a/PHPCSUtils/Utils/Operators.php b/PHPCSUtils/Utils/Operators.php index b9d085d2..b526cd4a 100644 --- a/PHPCSUtils/Utils/Operators.php +++ b/PHPCSUtils/Utils/Operators.php @@ -152,7 +152,7 @@ public static function isReference(File $phpcsFile, $stackPtr) } else { $skip = Tokens::$emptyTokens; $skip += Collections::namespacedNameTokens(); - $skip += Collections::$OOHierarchyKeywords; + $skip += Collections::ooHierarchyKeywords(); $skip[] = \T_DOUBLE_COLON; $nextSignificantAfter = $phpcsFile->findNext( diff --git a/PHPCSUtils/Utils/Scopes.php b/PHPCSUtils/Utils/Scopes.php index 518a910e..9687e7c5 100644 --- a/PHPCSUtils/Utils/Scopes.php +++ b/PHPCSUtils/Utils/Scopes.php @@ -74,7 +74,7 @@ public static function isOOConstant(File $phpcsFile, $stackPtr) return false; } - if (self::validDirectScope($phpcsFile, $stackPtr, Collections::$OOConstantScopes) !== false) { + if (self::validDirectScope($phpcsFile, $stackPtr, Collections::ooConstantScopes()) !== false) { return true; } @@ -100,7 +100,7 @@ public static function isOOProperty(File $phpcsFile, $stackPtr) return false; } - $scopePtr = self::validDirectScope($phpcsFile, $stackPtr, Collections::$OOPropertyScopes); + $scopePtr = self::validDirectScope($phpcsFile, $stackPtr, Collections::ooPropertyScopes()); if ($scopePtr !== false) { // Make sure it's not a method parameter. $deepestOpen = Parentheses::getLastOpener($phpcsFile, $stackPtr); diff --git a/PHPCSUtils/Utils/TextStrings.php b/PHPCSUtils/Utils/TextStrings.php index a9a344f0..384b5985 100644 --- a/PHPCSUtils/Utils/TextStrings.php +++ b/PHPCSUtils/Utils/TextStrings.php @@ -55,7 +55,7 @@ public static function getCompleteTextString(File $phpcsFile, $stackPtr, $stripQ $tokens = $phpcsFile->getTokens(); // Must be the start of a text string token. - if (isset($tokens[$stackPtr], Collections::$textStingStartTokens[$tokens[$stackPtr]['code']]) === false) { + if (isset($tokens[$stackPtr], Collections::textStingStartTokens()[$tokens[$stackPtr]['code']]) === false) { throw new RuntimeException( '$stackPtr must be of type T_START_HEREDOC, T_START_NOWDOC, T_CONSTANT_ENCAPSED_STRING' . ' or T_DOUBLE_QUOTED_STRING' diff --git a/PHPCSUtils/Utils/Variables.php b/PHPCSUtils/Utils/Variables.php index affbbadd..c4a8aedc 100644 --- a/PHPCSUtils/Utils/Variables.php +++ b/PHPCSUtils/Utils/Variables.php @@ -126,7 +126,7 @@ public static function getMemberProperties(File $phpcsFile, $stackPtr) throw new RuntimeException('$stackPtr is not a class member var'); } - $valid = Collections::$propertyModifierKeywords + Tokens::$emptyTokens; + $valid = Collections::propertyModifierKeywords() + Tokens::$emptyTokens; $scope = 'public'; $scopeSpecified = false; diff --git a/Tests/Tokens/Collections/AlternativeControlStructureSyntaxClosersTest.php b/Tests/Tokens/Collections/AlternativeControlStructureSyntaxClosersTest.php new file mode 100644 index 00000000..c9a5673d --- /dev/null +++ b/Tests/Tokens/Collections/AlternativeControlStructureSyntaxClosersTest.php @@ -0,0 +1,40 @@ +assertSame( + Collections::$alternativeControlStructureSyntaxCloserTokens, + Collections::alternativeControlStructureSyntaxClosers() + ); + } +} diff --git a/Tests/Tokens/Collections/AlternativeControlStructureSyntaxesTest.php b/Tests/Tokens/Collections/AlternativeControlStructureSyntaxesTest.php new file mode 100644 index 00000000..1a7e498b --- /dev/null +++ b/Tests/Tokens/Collections/AlternativeControlStructureSyntaxesTest.php @@ -0,0 +1,40 @@ +assertSame( + Collections::$alternativeControlStructureSyntaxTokens, + Collections::alternativeControlStructureSyntaxes() + ); + } +} diff --git a/Tests/Tokens/Collections/ArrayOpenTokensBCTest.php b/Tests/Tokens/Collections/ArrayOpenTokensBCTest.php new file mode 100644 index 00000000..04bbabc3 --- /dev/null +++ b/Tests/Tokens/Collections/ArrayOpenTokensBCTest.php @@ -0,0 +1,41 @@ +assertIsArray($arrayOpenTokens); + $this->assertCount(3, $arrayOpenTokens); + } +} diff --git a/Tests/Tokens/Collections/ArrayTokensBCTest.php b/Tests/Tokens/Collections/ArrayTokensBCTest.php new file mode 100644 index 00000000..2f466eef --- /dev/null +++ b/Tests/Tokens/Collections/ArrayTokensBCTest.php @@ -0,0 +1,37 @@ +assertSame(Collections::$arrayTokensBC, Collections::arrayTokensBC()); + } +} diff --git a/Tests/Tokens/Collections/ArrayTokensTest.php b/Tests/Tokens/Collections/ArrayTokensTest.php new file mode 100644 index 00000000..1bad9422 --- /dev/null +++ b/Tests/Tokens/Collections/ArrayTokensTest.php @@ -0,0 +1,37 @@ +assertSame(Collections::$arrayTokens, Collections::arrayTokens()); + } +} diff --git a/Tests/Tokens/Collections/ClassModifierKeywordsTest.php b/Tests/Tokens/Collections/ClassModifierKeywordsTest.php new file mode 100644 index 00000000..b6ef5341 --- /dev/null +++ b/Tests/Tokens/Collections/ClassModifierKeywordsTest.php @@ -0,0 +1,37 @@ +assertSame(Collections::$classModifierKeywords, Collections::classModifierKeywords()); + } +} diff --git a/Tests/Tokens/Collections/ClosedScopesTest.php b/Tests/Tokens/Collections/ClosedScopesTest.php new file mode 100644 index 00000000..10b81581 --- /dev/null +++ b/Tests/Tokens/Collections/ClosedScopesTest.php @@ -0,0 +1,37 @@ +assertSame(Collections::$closedScopes, Collections::closedScopes()); + } +} diff --git a/Tests/Tokens/Collections/ControlStructureTokensTest.php b/Tests/Tokens/Collections/ControlStructureTokensTest.php new file mode 100644 index 00000000..e2346b33 --- /dev/null +++ b/Tests/Tokens/Collections/ControlStructureTokensTest.php @@ -0,0 +1,37 @@ +assertSame(Collections::$controlStructureTokens, Collections::controlStructureTokens()); + } +} diff --git a/Tests/Tokens/Collections/IncrementDecrementOperatorsTest.php b/Tests/Tokens/Collections/IncrementDecrementOperatorsTest.php new file mode 100644 index 00000000..7dc2507f --- /dev/null +++ b/Tests/Tokens/Collections/IncrementDecrementOperatorsTest.php @@ -0,0 +1,37 @@ +assertSame(Collections::$incrementDecrementOperators, Collections::incrementDecrementOperators()); + } +} diff --git a/Tests/Tokens/Collections/ListTokensBCTest.php b/Tests/Tokens/Collections/ListTokensBCTest.php new file mode 100644 index 00000000..ee48982b --- /dev/null +++ b/Tests/Tokens/Collections/ListTokensBCTest.php @@ -0,0 +1,37 @@ +assertSame(Collections::$listTokensBC, Collections::listTokensBC()); + } +} diff --git a/Tests/Tokens/Collections/ListTokensTest.php b/Tests/Tokens/Collections/ListTokensTest.php new file mode 100644 index 00000000..7259a38e --- /dev/null +++ b/Tests/Tokens/Collections/ListTokensTest.php @@ -0,0 +1,37 @@ +assertSame(Collections::$listTokens, Collections::listTokens()); + } +} diff --git a/Tests/Tokens/Collections/NamespaceDeclarationClosersTest.php b/Tests/Tokens/Collections/NamespaceDeclarationClosersTest.php new file mode 100644 index 00000000..49de3872 --- /dev/null +++ b/Tests/Tokens/Collections/NamespaceDeclarationClosersTest.php @@ -0,0 +1,37 @@ +assertSame(Collections::$namespaceDeclarationClosers, Collections::namespaceDeclarationClosers()); + } +} diff --git a/Tests/Tokens/Collections/OoCanExtendTest.php b/Tests/Tokens/Collections/OoCanExtendTest.php new file mode 100644 index 00000000..f3ce893c --- /dev/null +++ b/Tests/Tokens/Collections/OoCanExtendTest.php @@ -0,0 +1,37 @@ +assertSame(Collections::$OOCanExtend, Collections::ooCanExtend()); + } +} diff --git a/Tests/Tokens/Collections/OoCanImplementTest.php b/Tests/Tokens/Collections/OoCanImplementTest.php new file mode 100644 index 00000000..ea880f68 --- /dev/null +++ b/Tests/Tokens/Collections/OoCanImplementTest.php @@ -0,0 +1,37 @@ +assertSame(Collections::$OOCanImplement, Collections::ooCanImplement()); + } +} diff --git a/Tests/Tokens/Collections/OoConstantScopesTest.php b/Tests/Tokens/Collections/OoConstantScopesTest.php new file mode 100644 index 00000000..9e7807ef --- /dev/null +++ b/Tests/Tokens/Collections/OoConstantScopesTest.php @@ -0,0 +1,37 @@ +assertSame(Collections::$OOConstantScopes, Collections::ooConstantScopes()); + } +} diff --git a/Tests/Tokens/Collections/OoHierarchyKeywordsTest.php b/Tests/Tokens/Collections/OoHierarchyKeywordsTest.php new file mode 100644 index 00000000..97118cd3 --- /dev/null +++ b/Tests/Tokens/Collections/OoHierarchyKeywordsTest.php @@ -0,0 +1,37 @@ +assertSame(Collections::$OOHierarchyKeywords, Collections::ooHierarchyKeywords()); + } +} diff --git a/Tests/Tokens/Collections/OoPropertyScopesTest.php b/Tests/Tokens/Collections/OoPropertyScopesTest.php new file mode 100644 index 00000000..6a76f110 --- /dev/null +++ b/Tests/Tokens/Collections/OoPropertyScopesTest.php @@ -0,0 +1,37 @@ +assertSame(Collections::$OOPropertyScopes, Collections::ooPropertyScopes()); + } +} diff --git a/Tests/Tokens/Collections/PropertyModifierKeywordsTest.php b/Tests/Tokens/Collections/PropertyModifierKeywordsTest.php new file mode 100644 index 00000000..6b7fee4a --- /dev/null +++ b/Tests/Tokens/Collections/PropertyModifierKeywordsTest.php @@ -0,0 +1,37 @@ +assertSame(Collections::$propertyModifierKeywords, Collections::propertyModifierKeywords()); + } +} diff --git a/Tests/Tokens/Collections/ShortArrayTokensBCTest.php b/Tests/Tokens/Collections/ShortArrayTokensBCTest.php new file mode 100644 index 00000000..0ec2631c --- /dev/null +++ b/Tests/Tokens/Collections/ShortArrayTokensBCTest.php @@ -0,0 +1,37 @@ +assertSame(Collections::$shortArrayTokensBC, Collections::shortArrayTokensBC()); + } +} diff --git a/Tests/Tokens/Collections/ShortArrayTokensTest.php b/Tests/Tokens/Collections/ShortArrayTokensTest.php new file mode 100644 index 00000000..afa6b3c2 --- /dev/null +++ b/Tests/Tokens/Collections/ShortArrayTokensTest.php @@ -0,0 +1,37 @@ +assertSame(Collections::$shortArrayTokens, Collections::shortArrayTokens()); + } +} diff --git a/Tests/Tokens/Collections/ShortListTokensBCTest.php b/Tests/Tokens/Collections/ShortListTokensBCTest.php new file mode 100644 index 00000000..a50cff4c --- /dev/null +++ b/Tests/Tokens/Collections/ShortListTokensBCTest.php @@ -0,0 +1,37 @@ +assertSame(Collections::$shortListTokensBC, Collections::shortListTokensBC()); + } +} diff --git a/Tests/Tokens/Collections/ShortListTokensTest.php b/Tests/Tokens/Collections/ShortListTokensTest.php new file mode 100644 index 00000000..fba0bb51 --- /dev/null +++ b/Tests/Tokens/Collections/ShortListTokensTest.php @@ -0,0 +1,37 @@ +assertSame(Collections::$shortListTokens, Collections::shortListTokens()); + } +} diff --git a/Tests/Tokens/Collections/TextStingStartTokensTest.php b/Tests/Tokens/Collections/TextStingStartTokensTest.php new file mode 100644 index 00000000..1207fdac --- /dev/null +++ b/Tests/Tokens/Collections/TextStingStartTokensTest.php @@ -0,0 +1,37 @@ +assertSame(Collections::$textStingStartTokens, Collections::textStingStartTokens()); + } +} diff --git a/Tests/Utils/ControlStructures/HasBodyTest.php b/Tests/Utils/ControlStructures/HasBodyTest.php index 7182cc47..ba3a2cc6 100644 --- a/Tests/Utils/ControlStructures/HasBodyTest.php +++ b/Tests/Utils/ControlStructures/HasBodyTest.php @@ -62,7 +62,7 @@ public function testNotControlStructure() */ public function testHasBody($testMarker, $hasBody, $hasNonEmptyBody) { - $stackPtr = $this->getTargetToken($testMarker, Collections::$controlStructureTokens); + $stackPtr = $this->getTargetToken($testMarker, Collections::controlStructureTokens()); $result = ControlStructures::hasBody(self::$phpcsFile, $stackPtr); $this->assertSame($hasBody, $result, 'Failed hasBody check with $allowEmpty = true');