Skip to content

Commit eab6be9

Browse files
committed
Merge branch 'feature/tokenizer-php-bugfix-context-sensitive-keywords' of https://github.com/jrfnl/PHP_CodeSniffer
2 parents 812d33c + 446d0a9 commit eab6be9

File tree

3 files changed

+244
-217
lines changed

3 files changed

+244
-217
lines changed

src/Tokenizers/PHP.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -608,8 +608,11 @@ protected function tokenize($string)
608608
) {
609609
$preserveKeyword = false;
610610

611-
// `new class` should be preserved
612-
if ($token[0] === T_CLASS && $finalTokens[$lastNotEmptyToken]['code'] === T_NEW) {
611+
// `new class`, and `new static` should be preserved.
612+
if ($finalTokens[$lastNotEmptyToken]['code'] === T_NEW
613+
&& ($token[0] === T_CLASS
614+
|| $token[0] === T_STATIC)
615+
) {
613616
$preserveKeyword = true;
614617
}
615618

@@ -1970,16 +1973,24 @@ function return types. We want to keep the parenthesis map clean,
19701973
&& $token[0] === T_STRING
19711974
&& isset($this->tstringContexts[$finalTokens[$lastNotEmptyToken]['code']]) === true
19721975
) {
1973-
// Special case for syntax like: return new self
1974-
// where self should not be a string.
1976+
// Special case for syntax like: return new self/new parent
1977+
// where self/parent should not be a string.
1978+
$tokenContentLower = strtolower($token[1]);
19751979
if ($finalTokens[$lastNotEmptyToken]['code'] === T_NEW
1976-
&& strtolower($token[1]) === 'self'
1980+
&& ($tokenContentLower === 'self' || $tokenContentLower === 'parent')
19771981
) {
19781982
$finalTokens[$newStackPtr] = [
19791983
'content' => $token[1],
1980-
'code' => T_SELF,
1981-
'type' => 'T_SELF',
19821984
];
1985+
if ($tokenContentLower === 'self') {
1986+
$finalTokens[$newStackPtr]['code'] = T_SELF;
1987+
$finalTokens[$newStackPtr]['type'] = 'T_SELF';
1988+
}
1989+
1990+
if ($tokenContentLower === 'parent') {
1991+
$finalTokens[$newStackPtr]['code'] = T_PARENT;
1992+
$finalTokens[$newStackPtr]['type'] = 'T_PARENT';
1993+
}
19831994
} else {
19841995
$finalTokens[$newStackPtr] = [
19851996
'content' => $token[1],

0 commit comments

Comments
 (0)