Skip to content

Commit 658926f

Browse files
committed
TokenNameTest: ensure all polyfilled tokens are tested
Other tooling packages will often also polyfill PHP native tokens and are likely to use different values for the token constant (generally an integer above 10.000). A number of these tool have a habit to autoload the file containing the polyfilled token constants via the Composer `autoload - files` directives. When this happens, this can interfer with the functioning of PHPCS. While it will be rare for this to become problematic for normal PHPCS runs (as translation between token constants and their names and visa versa is rarely done in runtime-code), it is definitely something which can cause problems while running the tests for PHPCS itself. Those type of issues will be hard to debug in the tests as this is a sort of race-condition. So to make it more obvious what is going on if this specific race-condition is happening, I'm going to make it a requirement for all PHP native polyfilled tokens to be tested via the `TokenNameTest` class. This will ensure that if the race-condition is happening, that test will fail, providing a valuable clue for solving the other resulting test failure(s).
1 parent 9981876 commit 658926f

File tree

2 files changed

+112
-12
lines changed

2 files changed

+112
-12
lines changed

src/Util/Tokens.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@
8585
define('T_TYPE_OPEN_PARENTHESIS', 'PHPCS_T_TYPE_OPEN_PARENTHESIS');
8686
define('T_TYPE_CLOSE_PARENTHESIS', 'PHPCS_T_TYPE_CLOSE_PARENTHESIS');
8787

88+
/*
89+
* {@internal IMPORTANT: all PHP native polyfilled tokens MUST be added to the
90+
* `PHP_CodeSniffer\Tests\Core\Util\Tokens\TokenNameTest::dataPolyfilledPHPNativeTokens()` test method!}
91+
*/
92+
8893
// Some PHP 5.5 tokens, replicated for lower versions.
8994
if (defined('T_FINALLY') === false) {
9095
define('T_FINALLY', 'PHPCS_T_FINALLY');

tests/Core/Util/Tokens/TokenNameTest.php

Lines changed: 107 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ final class TokenNameTest extends TestCase
2828
* @param string $expected The expected token name.
2929
*
3030
* @dataProvider dataTokenName
31+
* @dataProvider dataPolyfilledPHPNativeTokens
3132
*
3233
* @return void
3334
*/
@@ -46,30 +47,22 @@ public function testTokenName($tokenCode, $expected)
4647
public static function dataTokenName()
4748
{
4849
return [
49-
'PHP native token: T_ECHO' => [
50+
'PHP native token: T_ECHO' => [
5051
'tokenCode' => T_ECHO,
5152
'expected' => 'T_ECHO',
5253
],
53-
'PHP native token: T_FUNCTION' => [
54+
'PHP native token: T_FUNCTION' => [
5455
'tokenCode' => T_FUNCTION,
5556
'expected' => 'T_FUNCTION',
5657
],
57-
'PHPCS native token: T_CLOSURE' => [
58-
'tokenCode' => T_CLOSURE,
59-
'expected' => 'T_CLOSURE',
60-
],
61-
'PHPCS native token: T_STRING_CONCAT' => [
62-
'tokenCode' => T_STRING_CONCAT,
63-
'expected' => 'T_STRING_CONCAT',
64-
],
6558

6659
// Document the current behaviour for invalid input.
6760
// This behaviour is subject to change.
68-
'Non-token integer passed' => [
61+
'Non-token integer passed' => [
6962
'tokenCode' => 100000,
7063
'expected' => 'UNKNOWN',
7164
],
72-
'Non-token string passed' => [
65+
'Non-token string passed' => [
7366
'tokenCode' => 'something',
7467
'expected' => 'ing',
7568
],
@@ -78,4 +71,106 @@ public static function dataTokenName()
7871
}//end dataTokenName()
7972

8073

74+
/**
75+
* Data provider.
76+
*
77+
* @return array<string, array<string, int|string>>
78+
*/
79+
public static function dataPolyfilledPHPNativeTokens()
80+
{
81+
return [
82+
'PHP 5.5 native token, polyfilled: T_FINALLY' => [
83+
'tokenCode' => T_FINALLY,
84+
'expected' => 'T_FINALLY',
85+
],
86+
'PHP 5.5 native token, polyfilled: T_YIELD' => [
87+
'tokenCode' => T_YIELD,
88+
'expected' => 'T_YIELD',
89+
],
90+
91+
'PHP 5.6 native token, polyfilled: T_ELLIPSIS' => [
92+
'tokenCode' => T_ELLIPSIS,
93+
'expected' => 'T_ELLIPSIS',
94+
],
95+
'PHP 5.6 native token, polyfilled: T_POW' => [
96+
'tokenCode' => T_POW,
97+
'expected' => 'T_POW',
98+
],
99+
'PHP 5.6 native token, polyfilled: T_POW_EQUAL' => [
100+
'tokenCode' => T_POW_EQUAL,
101+
'expected' => 'T_POW_EQUAL',
102+
],
103+
104+
'PHP 7.0 native token, polyfilled: T_SPACESHIP' => [
105+
'tokenCode' => T_SPACESHIP,
106+
'expected' => 'T_SPACESHIP',
107+
],
108+
'PHP 7.0 native token, polyfilled: T_COALESCE' => [
109+
'tokenCode' => T_COALESCE,
110+
'expected' => 'T_COALESCE',
111+
],
112+
'PHP 7.0 native token, polyfilled: T_YIELD_FROM' => [
113+
'tokenCode' => T_YIELD_FROM,
114+
'expected' => 'T_YIELD_FROM',
115+
],
116+
117+
'PHP 7.4 native token, polyfilled: T_COALESCE_EQUAL' => [
118+
'tokenCode' => T_COALESCE_EQUAL,
119+
'expected' => 'T_COALESCE_EQUAL',
120+
],
121+
'PHP 7.4 native token, polyfilled: T_BAD_CHARACTER' => [
122+
'tokenCode' => T_BAD_CHARACTER,
123+
'expected' => 'T_BAD_CHARACTER',
124+
],
125+
'PHP 7.4 native token, polyfilled: T_FN' => [
126+
'tokenCode' => T_FN,
127+
'expected' => 'T_FN',
128+
],
129+
130+
'PHP 8.0 native token, polyfilled: T_NULLSAFE_OBJECT_OPERATOR' => [
131+
'tokenCode' => T_NULLSAFE_OBJECT_OPERATOR,
132+
'expected' => 'T_NULLSAFE_OBJECT_OPERATOR',
133+
],
134+
'PHP 8.0 native token, polyfilled: T_NAME_QUALIFIED' => [
135+
'tokenCode' => T_NAME_QUALIFIED,
136+
'expected' => 'T_NAME_QUALIFIED',
137+
],
138+
'PHP 8.0 native token, polyfilled: T_NAME_FULLY_QUALIFIED' => [
139+
'tokenCode' => T_NAME_FULLY_QUALIFIED,
140+
'expected' => 'T_NAME_FULLY_QUALIFIED',
141+
],
142+
'PHP 8.0 native token, polyfilled: T_NAME_RELATIVE' => [
143+
'tokenCode' => T_NAME_RELATIVE,
144+
'expected' => 'T_NAME_RELATIVE',
145+
],
146+
'PHP 8.0 native token, polyfilled: T_MATCH' => [
147+
'tokenCode' => T_MATCH,
148+
'expected' => 'T_MATCH',
149+
],
150+
'PHP 8.0 native token, polyfilled: T_ATTRIBUTE' => [
151+
'tokenCode' => T_ATTRIBUTE,
152+
'expected' => 'T_ATTRIBUTE',
153+
],
154+
155+
'PHP 8.1 native token, polyfilled: T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG' => [
156+
'tokenCode' => T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG,
157+
'expected' => 'T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG',
158+
],
159+
'PHP 8.1 native token, polyfilled: T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG' => [
160+
'tokenCode' => T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG,
161+
'expected' => 'T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG',
162+
],
163+
'PHP 8.1 native token, polyfilled: T_READONLY' => [
164+
'tokenCode' => T_READONLY,
165+
'expected' => 'T_READONLY',
166+
],
167+
'PHP 8.1 native token, polyfilled: T_ENUM' => [
168+
'tokenCode' => T_ENUM,
169+
'expected' => 'T_ENUM',
170+
],
171+
];
172+
173+
}//end dataPolyfilledPHPNativeTokens()
174+
175+
81176
}//end class

0 commit comments

Comments
 (0)