Skip to content

Commit a4d4cc3

Browse files
committed
Run the tokenizer as normal after the T_FN backfill
The backfill already modified the original tokens array so that future passes would detect the token as T_FN, but the return type check needs to process the T_FN token itself, so continue processing and let the T_FN token get added as normal.
1 parent 0e4fe74 commit a4d4cc3

File tree

3 files changed

+19
-27
lines changed

3 files changed

+19
-27
lines changed

src/Tokenizers/PHP.php

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,6 +1208,21 @@ protected function tokenize($string)
12081208
continue;
12091209
}
12101210

1211+
/*
1212+
Backfill the T_FN token for PHP versions < 7.4.
1213+
*/
1214+
1215+
if ($tokenIsArray === true
1216+
&& $token[0] === T_STRING
1217+
&& strtolower($token[1]) === 'fn'
1218+
) {
1219+
// Modify the original token stack so that
1220+
// future checks (like looking for T_NULLABLE) can
1221+
// detect the T_FN token more easily.
1222+
$tokens[$stackPtr][0] = T_FN;
1223+
$token[0] = T_FN;
1224+
}
1225+
12111226
/*
12121227
The string-like token after a function keyword should always be
12131228
tokenized as T_STRING even if it appears to be a different token,
@@ -1357,33 +1372,6 @@ function return types. We want to keep the parenthesis map clean,
13571372
continue;
13581373
}
13591374

1360-
/*
1361-
Backfill the T_FN token for PHP versions < 7.4.
1362-
*/
1363-
1364-
if ($tokenIsArray === true
1365-
&& $token[0] === T_STRING
1366-
&& strtolower($token[1]) === 'fn'
1367-
) {
1368-
// Modify the original token stack so that
1369-
// future checks (like looking for T_NULLABLE) can
1370-
// detect the T_FN token more easily.
1371-
$tokens[$stackPtr][0] = T_FN;
1372-
1373-
$finalTokens[$newStackPtr] = [
1374-
'content' => $token[1],
1375-
'code' => T_FN,
1376-
'type' => 'T_FN',
1377-
];
1378-
1379-
if (PHP_CODESNIFFER_VERBOSITY > 1) {
1380-
echo "\t\t* token $stackPtr changed from T_STRING to T_FN".PHP_EOL;
1381-
}
1382-
1383-
$newStackPtr++;
1384-
continue;
1385-
}
1386-
13871375
/*
13881376
PHP doesn't assign a token to goto labels, so we have to.
13891377
These are just string tokens with a single colon after them. Double

tests/Core/Tokenizer/BackfillFnTokenTest.inc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,8 @@ fn(parent $a) : parent => $a;
6767
/* testCallableReturnType */
6868
fn(callable $a) : callable => $a;
6969

70+
/* testArrayReturnType */
71+
fn(array $a) : array => $a;
72+
7073
/* testTernary */
7174
$fn = fn($a) => $a ? /* testTernaryThen */ fn() : string => 'a' : /* testTernaryElse */ fn() : string => 'b';

tests/Core/Tokenizer/BackfillFnTokenTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@ public function testKeywordReturnTypes()
474474
'Self',
475475
'Parent',
476476
'Callable',
477+
'Array',
477478
];
478479

479480
foreach ($testMarkers as $marker) {

0 commit comments

Comments
 (0)