Skip to content

Commit 44f79f4

Browse files
committed
Fix mis-identification of 'readonly' keyword
1 parent ed8e00d commit 44f79f4

File tree

2 files changed

+32
-32
lines changed

2 files changed

+32
-32
lines changed

src/Tokenizers/PHP.php

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,16 @@ protected function tokenize($string)
566566
$lastNotEmptyToken = ($newStackPtr - 1);
567567
}
568568

569+
// Get the next non-empty token.
570+
$nextNonEmptyToken = null;
571+
for ($i = ($stackPtr + 1); $i < $numTokens; $i++) {
572+
if (is_array($tokens[$i]) === false
573+
|| isset(Util\Tokens::$emptyTokens[$tokens[$i][0]]) === false) {
574+
$nextNonEmptyToken = $i;
575+
break;
576+
}
577+
}
578+
569579
/*
570580
If we are using \r\n newline characters, the \r and \n are sometimes
571581
split over two tokens. This normally occurs after comments. We need
@@ -602,14 +612,17 @@ protected function tokenize($string)
602612
}
603613

604614
/*
605-
Tokenize context sensitive keyword as string when it should be string.
615+
Tokenize context-sensitive keyword as string when it should be string.
606616
*/
607617

608618
if ($tokenIsArray === true
609619
&& isset(Util\Tokens::$contextSensitiveKeywords[$token[0]]) === true
610620
&& (isset($this->tstringContexts[$finalTokens[$lastNotEmptyToken]['code']]) === true
611-
|| $finalTokens[$lastNotEmptyToken]['content'] === '&')
621+
|| $finalTokens[$lastNotEmptyToken]['content'] === '&'
622+
|| (isset($nextNonEmptyToken) === true && $tokens[$nextNonEmptyToken] === '('))
612623
) {
624+
$preserveKeyword = true;
625+
613626
if (isset($this->tstringContexts[$finalTokens[$lastNotEmptyToken]['code']]) === true) {
614627
$preserveKeyword = false;
615628

@@ -649,8 +662,6 @@ protected function tokenize($string)
649662
}//end if
650663

651664
if ($finalTokens[$lastNotEmptyToken]['content'] === '&') {
652-
$preserveKeyword = true;
653-
654665
for ($i = ($lastNotEmptyToken - 1); $i >= 0; $i--) {
655666
if (isset(Util\Tokens::$emptyTokens[$finalTokens[$i]['code']]) === true) {
656667
continue;
@@ -664,6 +675,13 @@ protected function tokenize($string)
664675
}
665676
}
666677

678+
if (isset($nextNonEmptyToken) === true
679+
&& $tokens[$nextNonEmptyToken] === '('
680+
&& $token[0] === T_READONLY
681+
) {
682+
$preserveKeyword = false;
683+
}
684+
667685
if ($preserveKeyword === false) {
668686
if (PHP_CODESNIFFER_VERBOSITY > 1) {
669687
$type = Util\Tokens::tokenName($token[0]);
@@ -1012,18 +1030,9 @@ protected function tokenize($string)
10121030
&& $token[0] === T_STRING
10131031
&& strtolower($token[1]) === 'enum'
10141032
) {
1015-
// Get the next non-empty token.
1016-
for ($i = ($stackPtr + 1); $i < $numTokens; $i++) {
1017-
if (is_array($tokens[$i]) === false
1018-
|| isset(Util\Tokens::$emptyTokens[$tokens[$i][0]]) === false
1019-
) {
1020-
break;
1021-
}
1022-
}
1023-
1024-
if (isset($tokens[$i]) === true
1025-
&& is_array($tokens[$i]) === true
1026-
&& $tokens[$i][0] === T_STRING
1033+
if (isset($tokens[$nextNonEmptyToken]) === true
1034+
&& is_array($tokens[$nextNonEmptyToken]) === true
1035+
&& $tokens[$nextNonEmptyToken][0] === T_STRING
10271036
) {
10281037
// Modify $tokens directly so we can use it later when converting enum "case".
10291038
$tokens[$stackPtr][0] = T_ENUM;
@@ -1230,18 +1239,9 @@ protected function tokenize($string)
12301239
&& ($token[0] === T_STRING
12311240
|| preg_match('`^[a-zA-Z_\x80-\xff]`', $token[1]) === 1)
12321241
) {
1233-
// Get the next non-empty token.
1234-
for ($i = ($stackPtr + 1); $i < $numTokens; $i++) {
1235-
if (is_array($tokens[$i]) === false
1236-
|| isset(Util\Tokens::$emptyTokens[$tokens[$i][0]]) === false
1237-
) {
1238-
break;
1239-
}
1240-
}
1241-
1242-
if (isset($tokens[$i]) === true
1243-
&& is_array($tokens[$i]) === false
1244-
&& $tokens[$i] === ':'
1242+
if (isset($tokens[$nextNonEmptyToken]) === true
1243+
&& is_array($tokens[$nextNonEmptyToken]) === false
1244+
&& $tokens[$nextNonEmptyToken] === ':'
12451245
) {
12461246
// Get the previous non-empty token.
12471247
for ($j = ($stackPtr - 1); $j > 0; $j--) {

tests/Core/Tokenizer/BackfillReadonlyTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,6 @@ public function dataReadonly()
147147
'/* testReadonlyPropertyInAnonymousClass */',
148148
'readonly',
149149
],
150-
[
151-
'/* testReadonlyUsedAsFunctionCallWithSpaceBetweenKeywordAndParens */',
152-
'readonly',
153-
],
154150
[
155151
'/* testParseErrorLiveCoding */',
156152
'readonly',
@@ -224,6 +220,10 @@ public function dataNotReadonly()
224220
'/* testReadonlyAsFunctionCall */',
225221
'readonly',
226222
],
223+
[
224+
'/* testReadonlyUsedAsFunctionCallWithSpaceBetweenKeywordAndParens */',
225+
'readonly',
226+
],
227227
[
228228
'/* testClassConstantFetchWithReadonlyAsConstantName */',
229229
'READONLY',

0 commit comments

Comments
 (0)