File tree Expand file tree Collapse file tree 2 files changed +97
-0
lines changed
Tests/BackCompat/BCTokens Expand file tree Collapse file tree 2 files changed +97
-0
lines changed Original file line number Diff line number Diff line change @@ -460,4 +460,30 @@ public static function magicConstants()
460460
461461 return Collections::$ magicConstants ;
462462 }
463+
464+ /**
465+ * Given a token, returns the name of the token.
466+ *
467+ * If passed an integer, the token name is sourced from PHP's token_name()
468+ * function. If passed a string, it is assumed to be a PHPCS-supplied token
469+ * that begins with PHPCS_T_, so the name is sourced from the token value itself.
470+ *
471+ * Changelog for the PHPCS native:
472+ * - Introduced in PHPCS 3.0.0.
473+ *
474+ * @see \PHP_CodeSniffer\Util\Tokens::tokenName() Original function.
475+ *
476+ * @param int|string $token The token to get the name for.
477+ *
478+ * @return string
479+ */
480+ public static function tokenName ($ token )
481+ {
482+ if (\is_string ($ token ) === false ) {
483+ // PHP-supplied token name.
484+ return \token_name ($ token );
485+ }
486+
487+ return \substr ($ token , 6 );
488+ }
463489}
Original file line number Diff line number Diff line change 1+ <?php
2+ /**
3+ * PHPCSUtils, utility functions and classes for PHP_CodeSniffer sniff developers.
4+ *
5+ * @package PHPCSUtils
6+ * @copyright 2019-2020 PHPCSUtils Contributors
7+ * @license https://opensource.org/licenses/LGPL-3.0 LGPL3
8+ * @link https://github.com/PHPCSStandards/PHPCSUtils
9+ */
10+
11+ namespace PHPCSUtils \Tests \BackCompat \BCTokens ;
12+
13+ use PHPCSUtils \BackCompat \BCTokens ;
14+ use PHPUnit \Framework \TestCase ;
15+
16+ /**
17+ * Test class.
18+ *
19+ * @covers \PHPCSUtils\BackCompat\BCTokens::tokenName
20+ *
21+ * @group tokens
22+ *
23+ * @since 1.0.0
24+ */
25+ class TokenNameTest extends TestCase
26+ {
27+
28+ /**
29+ * Test the method.
30+ *
31+ * @dataProvider dataTokenName
32+ *
33+ * @param int|string $tokenCode The PHP/PHPCS token code to get the name for.
34+ * @param string $expected The expected token name.
35+ *
36+ * @return void
37+ */
38+ public function testTokenName ($ tokenCode , $ expected )
39+ {
40+ $ this ->assertSame ($ expected , BCTokens::tokenName ($ tokenCode ));
41+ }
42+
43+ /**
44+ * Data provider.
45+ *
46+ * @see testTokenName() For the array format.
47+ *
48+ * @return array
49+ */
50+ public function dataTokenName ()
51+ {
52+ return [
53+ 'PHP native token: T_COMMA ' => [
54+ \T_COMMA ,
55+ 'T_COMMA ' ,
56+ ],
57+ 'PHP native token: T_SELF ' => [
58+ \T_SELF ,
59+ 'T_SELF ' ,
60+ ],
61+ 'PHPCS native token: T_CLOSURE ' => [
62+ \T_CLOSURE ,
63+ 'T_CLOSURE ' ,
64+ ],
65+ 'PHPCS native token: T_STRING_CONCAT ' => [
66+ \T_STRING_CONCAT ,
67+ 'T_STRING_CONCAT ' ,
68+ ],
69+ ];
70+ }
71+ }
You can’t perform that action at this time.
0 commit comments