Skip to content

Commit cf0c547

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 interfere 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 8dce54e commit cf0c547

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

src/Util/Tokens.php

+5
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

+103
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
*/
@@ -78,4 +79,106 @@ public static function dataTokenName()
7879
}//end dataTokenName()
7980

8081

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

0 commit comments

Comments
 (0)