From 97275835cb059f6f04a2f65a4a6880296771c2eb Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 10 Jul 2020 07:56:49 +0200 Subject: [PATCH] BCTokens: add `magicConstants()` method Upstream PR 3013 introduces a new `$magicConstants` tokens array to PHPCS, same as already existed in PHPCSUtils as `Collections::$magicConstants`. This PR backfills the PHPCS native array in the `BCTokens` class. Includes unit test. --- PHPCSUtils/BackCompat/BCTokens.php | 27 +++++++++++ .../BCTokens/MagicConstantsTest.php | 48 +++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 Tests/BackCompat/BCTokens/MagicConstantsTest.php diff --git a/PHPCSUtils/BackCompat/BCTokens.php b/PHPCSUtils/BackCompat/BCTokens.php index ee430758..df41e850 100644 --- a/PHPCSUtils/BackCompat/BCTokens.php +++ b/PHPCSUtils/BackCompat/BCTokens.php @@ -11,6 +11,7 @@ namespace PHPCSUtils\BackCompat; use PHP_CodeSniffer\Util\Tokens; +use PHPCSUtils\Tokens\Collections; /** * Token arrays related utility methods. @@ -395,4 +396,30 @@ public static function ooScopeTokens() return self::$ooScopeTokens; } + + /** + * Tokens representing PHP magic constants. + * + * Retrieve the PHP magic constants tokens array in a cross-version compatible manner. + * + * 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. + * + * @link https://www.php.net/language.constants.predefined PHP Manual on magic constants + * + * @since 1.0.0-alpha4 + * + * @return array => + */ + public static function magicConstants() + { + if (isset(Tokens::$magicConstants)) { + return Tokens::$magicConstants; + } + + return Collections::$magicConstants; + } } diff --git a/Tests/BackCompat/BCTokens/MagicConstantsTest.php b/Tests/BackCompat/BCTokens/MagicConstantsTest.php new file mode 100644 index 00000000..0148dd00 --- /dev/null +++ b/Tests/BackCompat/BCTokens/MagicConstantsTest.php @@ -0,0 +1,48 @@ + \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, + ]; + + $this->assertSame($expected, BCTokens::magicConstants()); + } +}