Skip to content

Commit 7507442

Browse files
committed
Tokenizer/PHP: simplify the "yield"/"yield from" polyfill code
By moving the check a `T_STRING` "yield" keyword up and always adjusting the token in the original token stream, we can remove a lot of duplicate code.
1 parent bd7137c commit 7507442

File tree

1 file changed

+20
-46
lines changed

1 file changed

+20
-46
lines changed

src/Tokenizers/PHP.php

Lines changed: 20 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1492,14 +1492,33 @@ protected function tokenize($string)
14921492
continue;
14931493
}//end if
14941494

1495+
/*
1496+
Before PHP 5.5, the yield keyword was tokenized as
1497+
T_STRING. So look for and change this token in
1498+
earlier versions.
1499+
*/
1500+
1501+
if (PHP_VERSION_ID < 50500
1502+
&& $tokenIsArray === true
1503+
&& $token[0] === T_STRING
1504+
&& strtolower($token[1]) === 'yield'
1505+
&& isset($this->tstringContexts[$finalTokens[$lastNotEmptyToken]['code']]) === false
1506+
) {
1507+
// Could still be "yield from" and potentially multi-line, so adjust the token stack.
1508+
$token[0] = T_YIELD;
1509+
1510+
if (PHP_CODESNIFFER_VERBOSITY > 1) {
1511+
echo "\t\t* token $stackPtr changed from T_STRING to T_YIELD".PHP_EOL;
1512+
}
1513+
}
1514+
14951515
/*
14961516
Before PHP 7.0, the "yield from" was tokenized as
14971517
T_YIELD, T_WHITESPACE and T_STRING. So look for
14981518
and change this token in earlier versions.
14991519
*/
15001520

15011521
if (PHP_VERSION_ID < 70000
1502-
&& PHP_VERSION_ID >= 50500
15031522
&& $tokenIsArray === true
15041523
&& $token[0] === T_YIELD
15051524
&& isset($tokens[($stackPtr + 1)]) === true
@@ -1524,51 +1543,6 @@ protected function tokenize($string)
15241543
$tokens[($stackPtr + 2)] = null;
15251544
}
15261545

1527-
/*
1528-
Before PHP 5.5, the yield keyword was tokenized as
1529-
T_STRING. So look for and change this token in
1530-
earlier versions.
1531-
Checks also if it is just "yield" or "yield from".
1532-
*/
1533-
1534-
if (PHP_VERSION_ID < 50500
1535-
&& $tokenIsArray === true
1536-
&& $token[0] === T_STRING
1537-
&& strtolower($token[1]) === 'yield'
1538-
&& isset($this->tstringContexts[$finalTokens[$lastNotEmptyToken]['code']]) === false
1539-
) {
1540-
if (isset($tokens[($stackPtr + 1)]) === true
1541-
&& isset($tokens[($stackPtr + 2)]) === true
1542-
&& $tokens[($stackPtr + 1)][0] === T_WHITESPACE
1543-
&& $tokens[($stackPtr + 2)][0] === T_STRING
1544-
&& strtolower($tokens[($stackPtr + 2)][1]) === 'from'
1545-
) {
1546-
// Could be multi-line, so just just the token stack.
1547-
$token[0] = T_YIELD_FROM;
1548-
$token[1] .= $tokens[($stackPtr + 1)][1].$tokens[($stackPtr + 2)][1];
1549-
1550-
if (PHP_CODESNIFFER_VERBOSITY > 1) {
1551-
for ($i = ($stackPtr + 1); $i <= ($stackPtr + 2); $i++) {
1552-
$type = Tokens::tokenName($tokens[$i][0]);
1553-
$content = Common::prepareForOutput($tokens[$i][1]);
1554-
echo "\t\t* token $i merged into T_YIELD_FROM; was: $type => $content".PHP_EOL;
1555-
}
1556-
}
1557-
1558-
$tokens[($stackPtr + 1)] = null;
1559-
$tokens[($stackPtr + 2)] = null;
1560-
} else {
1561-
$newToken = [];
1562-
$newToken['code'] = T_YIELD;
1563-
$newToken['type'] = 'T_YIELD';
1564-
$newToken['content'] = $token[1];
1565-
$finalTokens[$newStackPtr] = $newToken;
1566-
1567-
$newStackPtr++;
1568-
continue;
1569-
}//end if
1570-
}//end if
1571-
15721546
/*
15731547
Before PHP 5.6, the ... operator was tokenized as three
15741548
T_STRING_CONCAT tokens in a row. So look for and combine

0 commit comments

Comments
 (0)