Skip to content

Commit

Permalink
Real fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jrfnl committed Nov 9, 2023
1 parent 54a4bb8 commit 56fa999
Showing 1 changed file with 86 additions and 5 deletions.
91 changes: 86 additions & 5 deletions src/Tokenizers/PHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -1313,32 +1313,113 @@ protected function tokenize($string)
"readonly" keyword for PHP < 8.1
*/

if (PHP_VERSION_ID < 80100
&& $tokenIsArray === true
if ($tokenIsArray === true
&& strtolower($token[1]) === 'readonly'
&& isset($this->tstringContexts[$finalTokens[$lastNotEmptyToken]['code']]) === false
) {
// Get the next non-whitespace token.
for ($i = ($stackPtr + 1); $i < $numTokens; $i++) {
if (is_array($tokens[$i]) === false
|| $tokens[$i][0] !== T_WHITESPACE
|| isset(Util\Tokens::$emptyTokens[$tokens[$i][0]]) === false
) {
break;
}
}

$isReadonlyKeyword = false;

if (isset($tokens[$i]) === false
|| $tokens[$i] !== '('
) {
$isReadonlyKeyword = true;
} else if ($tokens[$i] === '(') {
/*
* Skip over tokens which can be used in type declarations.
* At this point, the only token types which need to be taken into consideration
* as potential type declarations are identifier names, T_ARRAY, T_CALLABLE and T_NS_SEPARATOR
* and the union/intersection/dnf parentheses.
*/

$foundDNFCloseParen = 0;
$foundDNFPipe = 0;

for (++$i; $i < $numTokens; $i++) {
if (is_array($tokens[$i]) === true) {
$tokenType = $tokens[$i][0];
} else {
$tokenType = $tokens[$i];
}

if (isset(Util\Tokens::$emptyTokens[$tokenType]) === true) {
continue;
}

if ($tokenType === '|') {
++$foundDNFPipe;
continue;
}

if ($tokenType === ')') {
++$foundDNFCloseParen;
continue;
}

if ($tokenType === '(') {
--$foundDNFCloseParen;
continue;
}

if ($tokenType === T_STRING
|| $tokenType === T_NAME_FULLY_QUALIFIED
|| $tokenType === T_NAME_RELATIVE
|| $tokenType === T_NAME_QUALIFIED
|| $tokenType === T_ARRAY
|| $tokenType === T_NAMESPACE
|| $tokenType === T_NS_SEPARATOR
|| $tokenType === T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG // PHP 8.0+.
|| $tokenType === '&' // PHP < 8.0.
) {
continue;
}

// Reached the next token after.
if ($foundDNFCloseParen === 1
&& $foundDNFPipe === 1
&& ($tokenType === T_VARIABLE
|| $tokenType === T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG)
) {
$isReadonlyKeyword = true;
}

break;
}//end for
}//end if

if ($isReadonlyKeyword === true) {
$finalTokens[$newStackPtr] = [
'code' => T_READONLY,
'type' => 'T_READONLY',
'content' => $token[1],
];
$newStackPtr++;

continue;
}
if (PHP_CODESNIFFER_VERBOSITY > 1 && $type !== T_READONLY) {
echo "\t\t* token $stackPtr changed from $type to T_READONLY".PHP_EOL;
}
} else {
$finalTokens[$newStackPtr] = [
'code' => T_STRING,
'type' => 'T_STRING',
'content' => $token[1],
];
$newStackPtr++;

if (PHP_CODESNIFFER_VERBOSITY > 1 && $type !== T_STRING) {
echo "\t\t* token $stackPtr changed from $type to T_STRING".PHP_EOL;
}
}//end if

continue;
}//end if

/*
Expand Down

0 comments on commit 56fa999

Please sign in to comment.