Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 75 additions & 23 deletions src/Tokenizers/PHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,36 @@ protected function tokenize($string)
}
}//end if

/*
Special case for `static` used as a function name, i.e. `static()`.
*/

if ($tokenIsArray === true
&& $token[0] === T_STATIC
&& $finalTokens[$lastNotEmptyToken]['code'] !== T_NEW
) {
for ($i = ($stackPtr + 1); $i < $numTokens; $i++) {
if (is_array($tokens[$i]) === true
&& isset(Util\Tokens::$emptyTokens[$tokens[$i][0]]) === true
) {
continue;
}

if ($tokens[$i][0] === '(') {
$finalTokens[$newStackPtr] = [
'code' => T_STRING,
'type' => 'T_STRING',
'content' => $token[1],
];

$newStackPtr++;
continue 2;
}

break;
}
}//end if

/*
Parse doc blocks into something that can be easily iterated over.
*/
Expand Down Expand Up @@ -2103,38 +2133,60 @@ function return types. We want to keep the parenthesis map clean,
}
} else {
// Some T_STRING tokens should remain that way due to their context.
if ($tokenIsArray === true
&& $token[0] === T_STRING
&& isset($this->tstringContexts[$finalTokens[$lastNotEmptyToken]['code']]) === true
) {
// Special case for syntax like: return new self/new parent
// where self/parent should not be a string.
$tokenContentLower = strtolower($token[1]);
if ($finalTokens[$lastNotEmptyToken]['code'] === T_NEW
&& ($tokenContentLower === 'self' || $tokenContentLower === 'parent')
) {
$finalTokens[$newStackPtr] = [
'content' => $token[1],
];
if ($tokenContentLower === 'self') {
$finalTokens[$newStackPtr]['code'] = T_SELF;
$finalTokens[$newStackPtr]['type'] = 'T_SELF';
if ($tokenIsArray === true && $token[0] === T_STRING) {
$preserveTstring = false;

if (isset($this->tstringContexts[$finalTokens[$lastNotEmptyToken]['code']]) === true) {
$preserveTstring = true;

// Special case for syntax like: return new self/new parent
// where self/parent should not be a string.
$tokenContentLower = strtolower($token[1]);
if ($finalTokens[$lastNotEmptyToken]['code'] === T_NEW
&& ($tokenContentLower === 'self' || $tokenContentLower === 'parent')
) {
$preserveTstring = false;
}
} else if ($finalTokens[$lastNotEmptyToken]['content'] === '&') {
// Function names for functions declared to return by reference.
for ($i = ($lastNotEmptyToken - 1); $i >= 0; $i--) {
if (isset(Util\Tokens::$emptyTokens[$finalTokens[$i]['code']]) === true) {
continue;
}

if ($finalTokens[$i]['code'] === T_FUNCTION) {
$preserveTstring = true;
}

if ($tokenContentLower === 'parent') {
$finalTokens[$newStackPtr]['code'] = T_PARENT;
$finalTokens[$newStackPtr]['type'] = 'T_PARENT';
break;
}
} else {
// Keywords with special PHPCS token when used as a function call.
for ($i = ($stackPtr + 1); $i < $numTokens; $i++) {
if (is_array($tokens[$i]) === true
&& isset(Util\Tokens::$emptyTokens[$tokens[$i][0]]) === true
) {
continue;
}

if ($tokens[$i][0] === '(') {
$preserveTstring = true;
}

break;
}
}//end if

if ($preserveTstring === true) {
$finalTokens[$newStackPtr] = [
'content' => $token[1],
'code' => T_STRING,
'type' => 'T_STRING',
'content' => $token[1],
];
}

$newStackPtr++;
continue;
$newStackPtr++;
continue;
}
}//end if

$newToken = null;
Expand Down
Loading