Skip to content

Commit bf17a5b

Browse files
committed
PHP 8.1 | Generic/LowerCaseKeyword: simplify registered tokens + add enum support
This is a PR to simplify maintenance of this sniff. Since PR 3484, the `Tokens` class contains a `$contextSensitiveKeywords` property which largely overlaps with the list of tokens registered for this sniff. All tokens in that list should always be included in the targets for this sniff anyway, so we may as well leverage the new token array. Includes adding the new custom `T_ENUM_CASE` token to the list of additional tokens to take into account. Includes unit test.
1 parent 2596a15 commit bf17a5b

File tree

4 files changed

+23
-79
lines changed

4 files changed

+23
-79
lines changed

src/Standards/Generic/Sniffs/PHP/LowerCaseKeywordSniff.php

Lines changed: 16 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111

1212
use PHP_CodeSniffer\Files\File;
1313
use PHP_CodeSniffer\Sniffs\Sniff;
14-
use PHP_CodeSniffer\Util;
14+
use PHP_CodeSniffer\Util\Common;
15+
use PHP_CodeSniffer\Util\Tokens;
1516

1617
class LowerCaseKeywordSniff implements Sniff
1718
{
@@ -24,83 +25,21 @@ class LowerCaseKeywordSniff implements Sniff
2425
*/
2526
public function register()
2627
{
27-
return [
28-
T_ABSTRACT,
29-
T_ARRAY,
30-
T_AS,
31-
T_BREAK,
32-
T_CALLABLE,
33-
T_CASE,
34-
T_CATCH,
35-
T_CLASS,
36-
T_CLONE,
37-
T_CLOSURE,
38-
T_CONST,
39-
T_CONTINUE,
40-
T_DECLARE,
41-
T_DEFAULT,
42-
T_DO,
43-
T_ECHO,
44-
T_ELSE,
45-
T_ELSEIF,
46-
T_EMPTY,
47-
T_ENDDECLARE,
48-
T_ENDFOR,
49-
T_ENDFOREACH,
50-
T_ENDIF,
51-
T_ENDSWITCH,
52-
T_ENDWHILE,
53-
T_ENUM,
54-
T_EVAL,
55-
T_EXIT,
56-
T_EXTENDS,
57-
T_FINAL,
58-
T_FINALLY,
59-
T_FN,
60-
T_FOR,
61-
T_FOREACH,
62-
T_FUNCTION,
63-
T_GLOBAL,
64-
T_GOTO,
65-
T_IF,
66-
T_IMPLEMENTS,
67-
T_INCLUDE,
68-
T_INCLUDE_ONCE,
69-
T_INSTANCEOF,
70-
T_INSTEADOF,
71-
T_INTERFACE,
72-
T_ISSET,
73-
T_LIST,
74-
T_LOGICAL_AND,
75-
T_LOGICAL_OR,
76-
T_LOGICAL_XOR,
77-
T_MATCH,
78-
T_MATCH_DEFAULT,
79-
T_NAMESPACE,
80-
T_NEW,
81-
T_PARENT,
82-
T_PRINT,
83-
T_PRIVATE,
84-
T_PROTECTED,
85-
T_PUBLIC,
86-
T_READONLY,
87-
T_REQUIRE,
88-
T_REQUIRE_ONCE,
89-
T_RETURN,
90-
T_SELF,
91-
T_STATIC,
92-
T_SWITCH,
93-
T_THROW,
94-
T_TRAIT,
95-
T_TRY,
96-
T_UNSET,
97-
T_USE,
98-
T_VAR,
99-
T_WHILE,
100-
T_YIELD,
101-
T_YIELD_FROM,
28+
$targets = Tokens::$contextSensitiveKeywords;
29+
$targets += [
30+
T_CLOSURE => T_CLOSURE,
31+
T_EMPTY => T_EMPTY,
32+
T_ENUM_CASE => T_ENUM_CASE,
33+
T_EVAL => T_EVAL,
34+
T_ISSET => T_ISSET,
35+
T_MATCH_DEFAULT => T_MATCH_DEFAULT,
36+
T_PARENT => T_PARENT,
37+
T_SELF => T_SELF,
38+
T_UNSET => T_UNSET,
10239
];
10340

41+
return $targets;
42+
10443
}//end register()
10544

10645

@@ -124,7 +63,7 @@ public function process(File $phpcsFile, $stackPtr)
12463
$phpcsFile->recordMetric($stackPtr, 'PHP keyword case', 'mixed');
12564
}
12665

127-
$messageKeyword = Util\Common::prepareForOutput($keyword);
66+
$messageKeyword = Common::prepareForOutput($keyword);
12867

12968
$error = 'PHP keywords must be lowercase; expected "%s" but found "%s"';
13069
$data = [

src/Standards/Generic/Tests/PHP/LowerCaseKeywordUnitTest.inc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ class Reading {
3939
Public READOnly int $var;
4040
}
4141

42-
EnuM Enum {
42+
EnuM ENUM: string
43+
{
44+
Case HEARTS;
4345
}
4446

4547
__HALT_COMPILER(); // An exception due to phar support.

src/Standards/Generic/Tests/PHP/LowerCaseKeywordUnitTest.inc.fixed

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ class Reading {
3939
public readonly int $var;
4040
}
4141

42-
enum Enum {
42+
enum ENUM: string
43+
{
44+
case HEARTS;
4345
}
4446

4547
__HALT_COMPILER(); // An exception due to phar support.

src/Standards/Generic/Tests/PHP/LowerCaseKeywordUnitTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public function getErrorList()
4242
35 => 1,
4343
39 => 2,
4444
42 => 1,
45+
44 => 1,
4546
];
4647

4748
}//end getErrorList()

0 commit comments

Comments
 (0)