Skip to content

Commit fc4970b

Browse files
committed
PHP 8.0 | Tokenizer/PHP: improve retokenization of T_DEFAULT
This commit improves the tokenization of "default" keywords, by making sure that the keyword is not used as a constant or property. If it is, it will never be either `T_DEFAULT` or `T_MATCH_DEFAULT`, so it will be retokenized to `T_STRING` early. Fixes 3280
1 parent 14ac7eb commit fc4970b

File tree

1 file changed

+48
-25
lines changed

1 file changed

+48
-25
lines changed

src/Tokenizers/PHP.php

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,40 +1356,63 @@ protected function tokenize($string)
13561356
if ($tokenIsArray === true
13571357
&& $token[0] === T_DEFAULT
13581358
) {
1359-
for ($x = ($stackPtr + 1); $x < $numTokens; $x++) {
1360-
if ($tokens[$x] === ',') {
1361-
// Skip over potential trailing comma (supported in PHP).
1362-
continue;
1359+
$ignoreContext = [
1360+
T_OBJECT_OPERATOR => true,
1361+
T_NULLSAFE_OBJECT_OPERATOR => true,
1362+
T_NS_SEPARATOR => true,
1363+
T_PAAMAYIM_NEKUDOTAYIM => true,
1364+
];
1365+
1366+
if (isset($ignoreContext[$finalTokens[$lastNotEmptyToken]['code']]) === false) {
1367+
for ($x = ($stackPtr + 1); $x < $numTokens; $x++) {
1368+
if ($tokens[$x] === ',') {
1369+
// Skip over potential trailing comma (supported in PHP).
1370+
continue;
1371+
}
1372+
1373+
if (is_array($tokens[$x]) === false
1374+
|| isset(Util\Tokens::$emptyTokens[$tokens[$x][0]]) === false
1375+
) {
1376+
// Non-empty, non-comma content.
1377+
break;
1378+
}
13631379
}
13641380

1365-
if (is_array($tokens[$x]) === false
1366-
|| isset(Util\Tokens::$emptyTokens[$tokens[$x][0]]) === false
1381+
if (isset($tokens[$x]) === true
1382+
&& is_array($tokens[$x]) === true
1383+
&& $tokens[$x][0] === T_DOUBLE_ARROW
13671384
) {
1368-
// Non-empty, non-comma content.
1369-
break;
1370-
}
1371-
}
1385+
// Modify the original token stack for the double arrow so that
1386+
// future checks can disregard the double arrow token more easily.
1387+
// For match expression "case" statements, this is handled
1388+
// in PHP::processAdditional().
1389+
$tokens[$x][0] = T_MATCH_ARROW;
1390+
if (PHP_CODESNIFFER_VERBOSITY > 1) {
1391+
echo "\t\t* token $x changed from T_DOUBLE_ARROW to T_MATCH_ARROW".PHP_EOL;
1392+
}
13721393

1373-
if (isset($tokens[$x]) === true
1374-
&& is_array($tokens[$x]) === true
1375-
&& $tokens[$x][0] === T_DOUBLE_ARROW
1376-
) {
1377-
// Modify the original token stack for the double arrow so that
1378-
// future checks can disregard the double arrow token more easily.
1379-
// For match expression "case" statements, this is handled
1380-
// in PHP::processAdditional().
1381-
$tokens[$x][0] = T_MATCH_ARROW;
1382-
if (PHP_CODESNIFFER_VERBOSITY > 1) {
1383-
echo "\t\t* token $x changed from T_DOUBLE_ARROW to T_MATCH_ARROW".PHP_EOL;
1384-
}
1394+
$newToken = [];
1395+
$newToken['code'] = T_MATCH_DEFAULT;
1396+
$newToken['type'] = 'T_MATCH_DEFAULT';
1397+
$newToken['content'] = $token[1];
13851398

1399+
if (PHP_CODESNIFFER_VERBOSITY > 1) {
1400+
echo "\t\t* token $stackPtr changed from T_DEFAULT to T_MATCH_DEFAULT".PHP_EOL;
1401+
}
1402+
1403+
$finalTokens[$newStackPtr] = $newToken;
1404+
$newStackPtr++;
1405+
continue;
1406+
}//end if
1407+
} else {
1408+
// Definitely not the "default" keyword.
13861409
$newToken = [];
1387-
$newToken['code'] = T_MATCH_DEFAULT;
1388-
$newToken['type'] = 'T_MATCH_DEFAULT';
1410+
$newToken['code'] = T_STRING;
1411+
$newToken['type'] = 'T_STRING';
13891412
$newToken['content'] = $token[1];
13901413

13911414
if (PHP_CODESNIFFER_VERBOSITY > 1) {
1392-
echo "\t\t* token $stackPtr changed from T_DEFAULT to T_MATCH_DEFAULT".PHP_EOL;
1415+
echo "\t\t* token $stackPtr changed from T_DEFAULT to T_STRING".PHP_EOL;
13931416
}
13941417

13951418
$finalTokens[$newStackPtr] = $newToken;

0 commit comments

Comments
 (0)