Skip to content

Commit bf40ea2

Browse files
committed
Merge branch 'feature/tokenizer-php-more-context-sensitive-keyword-issues' of https://github.com/jrfnl/PHP_CodeSniffer
2 parents 934756b + cb764c7 commit bf40ea2

File tree

3 files changed

+419
-252
lines changed

3 files changed

+419
-252
lines changed

src/Tokenizers/PHP.php

Lines changed: 75 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,36 @@ protected function tokenize($string)
681681
}
682682
}//end if
683683

684+
/*
685+
Special case for `static` used as a function name, i.e. `static()`.
686+
*/
687+
688+
if ($tokenIsArray === true
689+
&& $token[0] === T_STATIC
690+
&& $finalTokens[$lastNotEmptyToken]['code'] !== T_NEW
691+
) {
692+
for ($i = ($stackPtr + 1); $i < $numTokens; $i++) {
693+
if (is_array($tokens[$i]) === true
694+
&& isset(Util\Tokens::$emptyTokens[$tokens[$i][0]]) === true
695+
) {
696+
continue;
697+
}
698+
699+
if ($tokens[$i][0] === '(') {
700+
$finalTokens[$newStackPtr] = [
701+
'code' => T_STRING,
702+
'type' => 'T_STRING',
703+
'content' => $token[1],
704+
];
705+
706+
$newStackPtr++;
707+
continue 2;
708+
}
709+
710+
break;
711+
}
712+
}//end if
713+
684714
/*
685715
Parse doc blocks into something that can be easily iterated over.
686716
*/
@@ -2104,38 +2134,60 @@ function return types. We want to keep the parenthesis map clean,
21042134
}
21052135
} else {
21062136
// Some T_STRING tokens should remain that way due to their context.
2107-
if ($tokenIsArray === true
2108-
&& $token[0] === T_STRING
2109-
&& isset($this->tstringContexts[$finalTokens[$lastNotEmptyToken]['code']]) === true
2110-
) {
2111-
// Special case for syntax like: return new self/new parent
2112-
// where self/parent should not be a string.
2113-
$tokenContentLower = strtolower($token[1]);
2114-
if ($finalTokens[$lastNotEmptyToken]['code'] === T_NEW
2115-
&& ($tokenContentLower === 'self' || $tokenContentLower === 'parent')
2116-
) {
2117-
$finalTokens[$newStackPtr] = [
2118-
'content' => $token[1],
2119-
];
2120-
if ($tokenContentLower === 'self') {
2121-
$finalTokens[$newStackPtr]['code'] = T_SELF;
2122-
$finalTokens[$newStackPtr]['type'] = 'T_SELF';
2137+
if ($tokenIsArray === true && $token[0] === T_STRING) {
2138+
$preserveTstring = false;
2139+
2140+
if (isset($this->tstringContexts[$finalTokens[$lastNotEmptyToken]['code']]) === true) {
2141+
$preserveTstring = true;
2142+
2143+
// Special case for syntax like: return new self/new parent
2144+
// where self/parent should not be a string.
2145+
$tokenContentLower = strtolower($token[1]);
2146+
if ($finalTokens[$lastNotEmptyToken]['code'] === T_NEW
2147+
&& ($tokenContentLower === 'self' || $tokenContentLower === 'parent')
2148+
) {
2149+
$preserveTstring = false;
21232150
}
2151+
} else if ($finalTokens[$lastNotEmptyToken]['content'] === '&') {
2152+
// Function names for functions declared to return by reference.
2153+
for ($i = ($lastNotEmptyToken - 1); $i >= 0; $i--) {
2154+
if (isset(Util\Tokens::$emptyTokens[$finalTokens[$i]['code']]) === true) {
2155+
continue;
2156+
}
2157+
2158+
if ($finalTokens[$i]['code'] === T_FUNCTION) {
2159+
$preserveTstring = true;
2160+
}
21242161

2125-
if ($tokenContentLower === 'parent') {
2126-
$finalTokens[$newStackPtr]['code'] = T_PARENT;
2127-
$finalTokens[$newStackPtr]['type'] = 'T_PARENT';
2162+
break;
21282163
}
21292164
} else {
2165+
// Keywords with special PHPCS token when used as a function call.
2166+
for ($i = ($stackPtr + 1); $i < $numTokens; $i++) {
2167+
if (is_array($tokens[$i]) === true
2168+
&& isset(Util\Tokens::$emptyTokens[$tokens[$i][0]]) === true
2169+
) {
2170+
continue;
2171+
}
2172+
2173+
if ($tokens[$i][0] === '(') {
2174+
$preserveTstring = true;
2175+
}
2176+
2177+
break;
2178+
}
2179+
}//end if
2180+
2181+
if ($preserveTstring === true) {
21302182
$finalTokens[$newStackPtr] = [
2131-
'content' => $token[1],
21322183
'code' => T_STRING,
21332184
'type' => 'T_STRING',
2185+
'content' => $token[1],
21342186
];
2135-
}
21362187

2137-
$newStackPtr++;
2138-
continue;
2188+
$newStackPtr++;
2189+
continue;
2190+
}
21392191
}//end if
21402192

21412193
$newToken = null;

0 commit comments

Comments
 (0)