Skip to content

Commit d9cf568

Browse files
committed
Tokenizer/PHP: bug fix in improved context sensitive keyword support [2]
While investigating 3607, I figured adding a test with a function declared to return by reference would also not be amiss and found that that situation was not accounted for. This commit fixes that. Includes unit test.
1 parent 854aba7 commit d9cf568

File tree

3 files changed

+43
-22
lines changed

3 files changed

+43
-22
lines changed

src/Tokenizers/PHP.php

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -607,38 +607,57 @@ protected function tokenize($string)
607607

608608
if ($tokenIsArray === true
609609
&& isset(Util\Tokens::$contextSensitiveKeywords[$token[0]]) === true
610-
&& isset($this->tstringContexts[$finalTokens[$lastNotEmptyToken]['code']]) === true
610+
&& (isset($this->tstringContexts[$finalTokens[$lastNotEmptyToken]['code']]) === true
611+
|| $finalTokens[$lastNotEmptyToken]['content'] === '&')
611612
) {
612-
$preserveKeyword = false;
613+
if (isset($this->tstringContexts[$finalTokens[$lastNotEmptyToken]['code']]) === true) {
614+
$preserveKeyword = false;
613615

614-
// `new class`, and `new static` should be preserved.
615-
if ($finalTokens[$lastNotEmptyToken]['code'] === T_NEW
616-
&& ($token[0] === T_CLASS
617-
|| $token[0] === T_STATIC)
618-
) {
619-
$preserveKeyword = true;
620-
}
616+
// `new class`, and `new static` should be preserved.
617+
if ($finalTokens[$lastNotEmptyToken]['code'] === T_NEW
618+
&& ($token[0] === T_CLASS
619+
|| $token[0] === T_STATIC)
620+
) {
621+
$preserveKeyword = true;
622+
}
621623

622-
// `new class extends` `new class implements` should be preserved
623-
if (($token[0] === T_EXTENDS || $token[0] === T_IMPLEMENTS)
624-
&& $finalTokens[$lastNotEmptyToken]['code'] === T_CLASS
625-
) {
626-
$preserveKeyword = true;
627-
}
624+
// `new class extends` `new class implements` should be preserved
625+
if (($token[0] === T_EXTENDS || $token[0] === T_IMPLEMENTS)
626+
&& $finalTokens[$lastNotEmptyToken]['code'] === T_CLASS
627+
) {
628+
$preserveKeyword = true;
629+
}
630+
631+
// `namespace\` should be preserved
632+
if ($token[0] === T_NAMESPACE) {
633+
for ($i = ($stackPtr + 1); $i < $numTokens; $i++) {
634+
if (is_array($tokens[$i]) === false) {
635+
break;
636+
}
637+
638+
if (isset(Util\Tokens::$emptyTokens[$tokens[$i][0]]) === true) {
639+
continue;
640+
}
641+
642+
if ($tokens[$i][0] === T_NS_SEPARATOR) {
643+
$preserveKeyword = true;
644+
}
628645

629-
// `namespace\` should be preserved
630-
if ($token[0] === T_NAMESPACE) {
631-
for ($i = ($stackPtr + 1); $i < $numTokens; $i++) {
632-
if (is_array($tokens[$i]) === false) {
633646
break;
634647
}
648+
}
649+
}//end if
650+
651+
if ($finalTokens[$lastNotEmptyToken]['content'] === '&') {
652+
$preserveKeyword = true;
635653

636-
if (isset(Util\Tokens::$emptyTokens[$tokens[$i][0]]) === true) {
654+
for ($i = ($lastNotEmptyToken - 1); $i >= 0; $i--) {
655+
if (isset(Util\Tokens::$emptyTokens[$finalTokens[$i]['code']]) === true) {
637656
continue;
638657
}
639658

640-
if ($tokens[$i][0] === T_NS_SEPARATOR) {
641-
$preserveKeyword = true;
659+
if ($finalTokens[$i]['code'] === T_FUNCTION) {
660+
$preserveKeyword = false;
642661
}
643662

644663
break;

tests/Core/Tokenizer/ContextSensitiveKeywordsTest.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,3 +217,4 @@ class Foo extends /* testNamespaceInNameIsKeyword */ namespace\Exception
217217
{}
218218

219219
function /* testKeywordAfterFunctionShouldBeString */ eval() {}
220+
function /* testKeywordAfterFunctionByRefShouldBeString */ &switch() {}

tests/Core/Tokenizer/ContextSensitiveKeywordsTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ public function dataStrings()
124124
['/* testNamespaceNameIsString3 */'],
125125

126126
['/* testKeywordAfterFunctionShouldBeString */'],
127+
['/* testKeywordAfterFunctionByRefShouldBeString */'],
127128
];
128129

129130
}//end dataStrings()

0 commit comments

Comments
 (0)