Skip to content

Commit 9fd9b01

Browse files
committed
PHP 8.1: Enum case name should be always tokenized as T_STRING
1 parent f4115be commit 9fd9b01

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

src/Tokenizers/PHP.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,7 @@ class PHP extends Tokenizer
477477
T_INTERFACE => true,
478478
T_TRAIT => true,
479479
T_ENUM => true,
480+
T_ENUM_CASE => true,
480481
T_EXTENDS => true,
481482
T_IMPLEMENTS => true,
482483
T_ATTRIBUTE => true,

tests/Core/Tokenizer/EnumCaseTest.inc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,20 @@ switch ($x) {
7676
enum Bar {}
7777
break;
7878
}
79+
80+
enum Foo: string {
81+
/* testKeywordAsEnumCaseNameShouldBeString1 */
82+
case INTERFACE = 'interface';
83+
/* testKeywordAsEnumCaseNameShouldBeString2 */
84+
case TRAIT = 'trait';
85+
/* testKeywordAsEnumCaseNameShouldBeString3 */
86+
case ENUM = 'enum';
87+
/* testKeywordAsEnumCaseNameShouldBeString4 */
88+
case FUNCTION = 'function';
89+
/* testKeywordAsEnumCaseNameShouldBeString5 */
90+
case FALSE = 'false';
91+
/* testKeywordAsEnumCaseNameShouldBeString6 */
92+
case DEFAULT = 'default';
93+
/* testKeywordAsEnumCaseNameShouldBeString7 */
94+
case ARRAY = 'array';
95+
}

tests/Core/Tokenizer/EnumCaseTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,45 @@ public function dataNotEnumCases()
113113
}//end dataNotEnumCases()
114114

115115

116+
/**
117+
* Test that "case" that is not enum case is still tokenized as `T_CASE`.
118+
*
119+
* @param string $testMarker The comment which prefaces the target token in the test file.
120+
*
121+
* @dataProvider dataKeywordAsEnumCaseNameShouldBeString
122+
* @covers PHP_CodeSniffer\Tokenizers\PHP::tokenize
123+
*
124+
* @return void
125+
*/
126+
public function testKeywordAsEnumCaseNameShouldBeString($testMarker)
127+
{
128+
$tokens = self::$phpcsFile->getTokens();
129+
130+
$enumCaseName = $this->getTargetToken($testMarker, [T_STRING, T_INTERFACE, T_TRAIT, T_ENUM, T_FUNCTION, T_FALSE, T_DEFAULT, T_ARRAY]);
131+
132+
$this->assertSame(T_STRING, $tokens[$enumCaseName]['code']);
133+
$this->assertSame('T_STRING', $tokens[$enumCaseName]['type']);
134+
135+
}//end testKeywordAsEnumCaseNameShouldBeString()
136+
137+
138+
/**
139+
* Data provider.
140+
*
141+
* @see testKeywordAsEnumCaseNameShouldBeString()
142+
*
143+
* @return array
144+
*/
145+
public function dataKeywordAsEnumCaseNameShouldBeString()
146+
{
147+
return [
148+
['/* testKeywordAsEnumCaseNameShouldBeString1 */'],
149+
['/* testKeywordAsEnumCaseNameShouldBeString2 */'],
150+
['/* testKeywordAsEnumCaseNameShouldBeString3 */'],
151+
['/* testKeywordAsEnumCaseNameShouldBeString4 */'],
152+
];
153+
154+
}//end dataKeywordAsEnumCaseNameShouldBeString()
155+
156+
116157
}//end class

0 commit comments

Comments
 (0)