Skip to content

Commit 1fb03d5

Browse files
authored
Merge pull request #250 from PHPCSStandards/bctokens/new-tokenname-method-polyfill
BCTokens: polyfill the tokenName() method
2 parents d2e0918 + 183274c commit 1fb03d5

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

PHPCSUtils/BackCompat/BCTokens.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
}

0 commit comments

Comments
 (0)