Skip to content

Commit 531a731

Browse files
committed
Fixed bug #1218 : CASE conditions using class constants named NAMESPACE/INTERFACE/TRAIT etc are incorrectly tokenized
1 parent aeeade9 commit 531a731

File tree

4 files changed

+45
-19
lines changed

4 files changed

+45
-19
lines changed

CodeSniffer/Standards/PSR2/Tests/ControlStructures/SwitchDeclarationUnitTest.inc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,13 @@ switch($foo)
144144
break;
145145
}
146146
}
147+
148+
switch ($foo) {
149+
case Foo::INTERFACE:
150+
return self::INTERFACE;
151+
case Foo::NAMESPACE:
152+
return self::MODULE;
153+
case Foo::TRAIT:
154+
case Foo::ARRAY:
155+
return self::VALUE;
156+
}

CodeSniffer/Standards/PSR2/Tests/ControlStructures/SwitchDeclarationUnitTest.inc.fixed

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,13 @@ switch($foo)
147147
break;
148148
}
149149
}
150+
151+
switch ($foo) {
152+
case Foo::INTERFACE:
153+
return self::INTERFACE;
154+
case Foo::NAMESPACE:
155+
return self::MODULE;
156+
case Foo::TRAIT:
157+
case Foo::ARRAY:
158+
return self::VALUE;
159+
}

CodeSniffer/Tokenizers/PHP.php

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,30 @@ public function tokenizeString($string, $eolChar='\n')
678678
continue;
679679
}
680680

681+
/*
682+
Tokens after a double colon may be look like scope openers,
683+
such as when writing code like Foo::NAMESAPCE, but they are
684+
only ever variables or strings.
685+
*/
686+
687+
if ($stackPtr > 1
688+
&& $tokens[($stackPtr - 1)][0] === T_PAAMAYIM_NEKUDOTAYIM
689+
&& $tokenIsArray === true
690+
&& $token[0] !== T_STRING
691+
&& $token[0] !== T_VARIABLE
692+
&& $token[0] !== T_DOLLAR
693+
&& isset(PHP_CodeSniffer_Tokens::$emptyTokens[$token[0]]) === false
694+
) {
695+
$newToken = array();
696+
$newToken['code'] = T_STRING;
697+
$newToken['type'] = 'T_STRING';
698+
$newToken['content'] = $token[1];
699+
$finalTokens[$newStackPtr] = $newToken;
700+
701+
$newStackPtr++;
702+
continue;
703+
}
704+
681705
/*
682706
Before PHP 7, the <=> operator was tokenized as
683707
T_IS_SMALLER_OR_EQUAL followed by T_GREATER_THAN.
@@ -1269,25 +1293,6 @@ public function processAdditional(&$tokens, $eolChar)
12691293
echo "\t* token $x on line $line changed from $type to T_STRING".PHP_EOL;
12701294
}
12711295

1272-
$tokens[$x]['code'] = T_STRING;
1273-
$tokens[$x]['type'] = 'T_STRING';
1274-
}
1275-
} else if ($tokens[$i]['code'] === T_PAAMAYIM_NEKUDOTAYIM) {
1276-
// Context sensitive keywords support.
1277-
for ($x = ($i + 1); $i < $numTokens; $x++) {
1278-
if (isset(PHP_CodeSniffer_Tokens::$emptyTokens[$tokens[$x]['code']]) === false) {
1279-
// Non-whitespace content.
1280-
break;
1281-
}
1282-
}
1283-
1284-
if (in_array($tokens[$x]['code'], array(T_STRING, T_VARIABLE, T_DOLLAR), true) === false) {
1285-
if (PHP_CODESNIFFER_VERBOSITY > 1) {
1286-
$line = $tokens[$x]['line'];
1287-
$type = $tokens[$x]['type'];
1288-
echo "\t* token $x on line $line changed from $type to T_STRING".PHP_EOL;
1289-
}
1290-
12911296
$tokens[$x]['code'] = T_STRING;
12921297
$tokens[$x]['type'] = 'T_STRING';
12931298
}

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
5353
- Fixed bug #1190 : phpcbf on if/else with trailing comment generates erroneous code
5454
- Fixed bug #1191 : Javascript sniffer fails with function called "Function"
5555
- Fixed bug #1203 : Inconsistent behavior of PHP_CodeSniffer_File::findEndOfStatement
56+
- Fixed bug #1218 : CASE conditions using class constants named NAMESPACE/INTERFACE/TRAIT etc are incorrectly tokenized
5657
</notes>
5758
<contents>
5859
<dir name="/">

0 commit comments

Comments
 (0)