Skip to content

Commit 446d0a9

Browse files
committed
Tokenizer/PHP: bug fix - parent/static keywords in class instantiations
Follow up on 3484 Just like `new class`, `new parent`, `new self` and `new static` should also be preserved and with the `parent`, `self` and `static` keywords remaining as their dedicated token. * For `new static`, the tokenization changed due to the context sensitive keywords change. This has now been fixed. * `new self` was fine before and is still fine. * `new parent` apparently wasn't handled correctly, even before the change. The condition which was in place for handling the same situation for `self` has now been updated to also handle `parent`. Includes unit tests.
1 parent 3e6ce10 commit 446d0a9

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

@@ -1968,16 +1971,24 @@ function return types. We want to keep the parenthesis map clean,
19681971
&& $token[0] === T_STRING
19691972
&& isset($this->tstringContexts[$finalTokens[$lastNotEmptyToken]['code']]) === true
19701973
) {
1971-
// Special case for syntax like: return new self
1972-
// where self should not be a string.
1974+
// Special case for syntax like: return new self/new parent
1975+
// where self/parent should not be a string.
1976+
$tokenContentLower = strtolower($token[1]);
19731977
if ($finalTokens[$lastNotEmptyToken]['code'] === T_NEW
1974-
&& strtolower($token[1]) === 'self'
1978+
&& ($tokenContentLower === 'self' || $tokenContentLower === 'parent')
19751979
) {
19761980
$finalTokens[$newStackPtr] = [
19771981
'content' => $token[1],
1978-
'code' => T_SELF,
1979-
'type' => 'T_SELF',
19801982
];
1983+
if ($tokenContentLower === 'self') {
1984+
$finalTokens[$newStackPtr]['code'] = T_SELF;
1985+
$finalTokens[$newStackPtr]['type'] = 'T_SELF';
1986+
}
1987+
1988+
if ($tokenContentLower === 'parent') {
1989+
$finalTokens[$newStackPtr]['code'] = T_PARENT;
1990+
$finalTokens[$newStackPtr]['type'] = 'T_PARENT';
1991+
}
19811992
} else {
19821993
$finalTokens[$newStackPtr] = [
19831994
'content' => $token[1],

0 commit comments

Comments
 (0)