Skip to content

Commit e455518

Browse files
committed
Tokens: add CLASS_MODIFIERS and PROPERTY_MODIFIERS constants
Several core sniffs and many custom sniffs rely heavily on the Tokens class to simplify code, particularly as new features (like readonly) are added to PHP. This change adds class and property modifier constants and implements those constants in sniffs that were already hardcoding those collections of values. Semantically, these constants also do a better job of communicating what is being searched for. In one or two cases, I decided to continue including SCOPE_MODIFIERS even though all of the tokens are currently part of the PROPERTY_MODIFIERS constant. I do not know what the future holds, so I do not want to assume that will not change in the future. There should not be any changes in behavior.
1 parent 44c338e commit e455518

14 files changed

Lines changed: 50 additions & 80 deletions

File tree

src/Files/File.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1909,15 +1909,7 @@ public function getMemberProperties(int $stackPtr)
19091909
}
19101910
}
19111911

1912-
$valid = [
1913-
T_STATIC => T_STATIC,
1914-
T_VAR => T_VAR,
1915-
T_READONLY => T_READONLY,
1916-
T_FINAL => T_FINAL,
1917-
T_ABSTRACT => T_ABSTRACT,
1918-
];
1919-
1920-
$valid += Tokens::SCOPE_MODIFIERS;
1912+
$valid = Tokens::PROPERTY_MODIFIERS;
19211913
$valid += Tokens::EMPTY_TOKENS;
19221914

19231915
$scope = 'public';
@@ -2068,13 +2060,12 @@ public function getClassProperties(int $stackPtr)
20682060
}
20692061

20702062
$valid = [
2071-
T_FINAL => T_FINAL,
2072-
T_ABSTRACT => T_ABSTRACT,
2073-
T_READONLY => T_READONLY,
20742063
T_WHITESPACE => T_WHITESPACE,
20752064
T_COMMENT => T_COMMENT,
20762065
];
20772066

2067+
$valid += Tokens::CLASS_MODIFIERS;
2068+
20782069
$isAbstract = false;
20792070
$isFinal = false;
20802071
$isReadonly = false;

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

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,8 @@ public function register()
5757
{
5858
$targets = self::TARGET_TOKENS;
5959

60-
// Register scope modifiers to filter out property type declarations.
61-
$targets += Tokens::SCOPE_MODIFIERS;
62-
$targets[] = T_VAR;
63-
$targets[] = T_STATIC;
64-
$targets[] = T_READONLY;
65-
$targets[] = T_FINAL;
66-
$targets[] = T_ABSTRACT;
60+
// Register property modifiers to filter out property type declarations.
61+
$targets += Tokens::PROPERTY_MODIFIERS;
6762

6863
// Register function keywords to filter out param/return type declarations.
6964
$targets[] = T_FUNCTION;
@@ -119,13 +114,7 @@ public function process(File $phpcsFile, int $stackPtr)
119114
* declarations, in which case, it is correct to skip over them.
120115
*/
121116

122-
if (isset(Tokens::SCOPE_MODIFIERS[$tokens[$stackPtr]['code']]) === true
123-
|| $tokens[$stackPtr]['code'] === T_VAR
124-
|| $tokens[$stackPtr]['code'] === T_STATIC
125-
|| $tokens[$stackPtr]['code'] === T_READONLY
126-
|| $tokens[$stackPtr]['code'] === T_FINAL
127-
|| $tokens[$stackPtr]['code'] === T_ABSTRACT
128-
) {
117+
if (isset(Tokens::PROPERTY_MODIFIERS[$tokens[$stackPtr]['code']]) === true) {
129118
$skipOver = (Tokens::EMPTY_TOKENS + self::PROPERTY_TYPE_TOKENS);
130119
$skipTo = $phpcsFile->findNext($skipOver, ($stackPtr + 1), null, true);
131120
if ($skipTo !== false) {

src/Standards/PEAR/Sniffs/Commenting/ClassCommentSniff.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace PHP_CodeSniffer\Standards\PEAR\Sniffs\Commenting;
1212

1313
use PHP_CodeSniffer\Files\File;
14+
use PHP_CodeSniffer\Util\Tokens;
1415

1516
class ClassCommentSniff extends FileCommentSniff
1617
{
@@ -47,12 +48,8 @@ public function process(File $phpcsFile, int $stackPtr)
4748
$type = strtolower($tokens[$stackPtr]['content']);
4849
$errorData = [$type];
4950

50-
$find = [
51-
T_ABSTRACT => T_ABSTRACT,
52-
T_FINAL => T_FINAL,
53-
T_READONLY => T_READONLY,
54-
T_WHITESPACE => T_WHITESPACE,
55-
];
51+
$find = [T_WHITESPACE => T_WHITESPACE];
52+
$find += Tokens::CLASS_MODIFIERS;
5653

5754
for ($commentEnd = ($stackPtr - 1); $commentEnd >= 0; $commentEnd--) {
5855
if (isset($find[$tokens[$commentEnd]['code']]) === true) {

src/Standards/PSR1/Sniffs/Files/SideEffectsSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ private function searchForConflict(File $phpcsFile, int $start, int $end, array
181181

182182
// Ignore function/class prefixes.
183183
if (isset(Tokens::METHOD_MODIFIERS[$tokens[$i]['code']]) === true
184-
|| $tokens[$i]['code'] === T_READONLY
184+
|| isset(Tokens::CLASS_MODIFIERS[$tokens[$i]['code']]) === true
185185
) {
186186
continue;
187187
}

src/Standards/PSR12/Sniffs/Files/FileHeaderSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ public function getHeaderLines(File $phpcsFile, int $stackPtr)
187187

188188
if (isset($commentOpeners[$tokens[$docToken]['code']]) === false
189189
&& isset(Tokens::METHOD_MODIFIERS[$tokens[$docToken]['code']]) === false
190-
&& $tokens[$docToken]['code'] !== T_READONLY
190+
&& isset(Tokens::CLASS_MODIFIERS[$tokens[$docToken]['code']]) === false
191191
) {
192192
// Check for an @var annotation.
193193
$annotation = false;

src/Standards/PSR2/Sniffs/Classes/ClassDeclarationSniff.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,6 @@
1717
class ClassDeclarationSniff extends PEARClassDeclarationSniff
1818
{
1919

20-
/**
21-
* Modifier keywords which can be used in class declarations.
22-
*
23-
* @var array<int|string, int|string>
24-
*/
25-
private const CLASS_MODIFIERS = [
26-
T_ABSTRACT => T_ABSTRACT,
27-
T_FINAL => T_FINAL,
28-
T_READONLY => T_READONLY,
29-
];
30-
3120
/**
3221
* The number of spaces code should be indented.
3322
*
@@ -79,7 +68,7 @@ public function processOpen(File $phpcsFile, int $stackPtr)
7968
$prevNonSpace = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
8069
$prevNonEmpty = $phpcsFile->findPrevious(Tokens::EMPTY_TOKENS, ($stackPtr - 1), null, true);
8170

82-
if (isset(self::CLASS_MODIFIERS[$tokens[$prevNonEmpty]['code']]) === true) {
71+
if (isset(Tokens::CLASS_MODIFIERS[$tokens[$prevNonEmpty]['code']]) === true) {
8372
$spaces = 0;
8473
$errorCode = 'SpaceBeforeKeyword';
8574
if ($tokens[$prevNonEmpty]['line'] !== $tokens[$stackPtr]['line']) {

src/Standards/PSR2/Sniffs/Classes/PropertyDeclarationSniff.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,8 @@ protected function processMemberVar(File $phpcsFile, int $stackPtr)
5757
// Detect multiple properties defined at the same time. Throw an error
5858
// for this, but also only process the first property in the list so we don't
5959
// repeat errors.
60-
$find = Tokens::SCOPE_MODIFIERS;
60+
$find = Tokens::PROPERTY_MODIFIERS;
6161
$find[] = T_VARIABLE;
62-
$find[] = T_VAR;
63-
$find[] = T_READONLY;
64-
$find[] = T_FINAL;
65-
$find[] = T_ABSTRACT;
6662
$find[] = T_SEMICOLON;
6763
$find[] = T_OPEN_CURLY_BRACKET;
6864

src/Standards/Squiz/Sniffs/Classes/ClassDeclarationSniff.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,7 @@ public function processOpen(File $phpcsFile, int $stackPtr)
6363
$blankSpace = substr($prevContent, strpos($prevContent, $phpcsFile->eolChar));
6464
$spaces = strlen($blankSpace);
6565

66-
if ($tokens[($stackPtr - 2)]['code'] !== T_ABSTRACT
67-
&& $tokens[($stackPtr - 2)]['code'] !== T_FINAL
68-
&& $tokens[($stackPtr - 2)]['code'] !== T_READONLY
69-
) {
66+
if (isset(Tokens::CLASS_MODIFIERS[$tokens[($stackPtr - 2)]['code']]) === false) {
7067
if ($spaces !== 0) {
7168
$type = strtolower($tokens[$stackPtr]['content']);
7269
$error = 'Expected 0 spaces before %s keyword; %s found';

src/Standards/Squiz/Sniffs/Classes/LowercaseClassKeywordsSniff.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,9 @@ class LowercaseClassKeywordsSniff implements Sniff
2626
public function register()
2727
{
2828
$targets = Tokens::OO_SCOPE_TOKENS;
29+
$targets += Tokens::CLASS_MODIFIERS;
2930
$targets[] = T_EXTENDS;
3031
$targets[] = T_IMPLEMENTS;
31-
$targets[] = T_ABSTRACT;
32-
$targets[] = T_FINAL;
33-
$targets[] = T_READONLY;
3432
$targets[] = T_VAR;
3533
$targets[] = T_CONST;
3634

src/Standards/Squiz/Sniffs/Commenting/ClassCommentSniff.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
use PHP_CodeSniffer\Files\File;
2222
use PHP_CodeSniffer\Sniffs\Sniff;
23+
use PHP_CodeSniffer\Util\Tokens;
2324

2425
class ClassCommentSniff implements Sniff
2526
{
@@ -48,12 +49,9 @@ public function register()
4849
public function process(File $phpcsFile, int $stackPtr)
4950
{
5051
$tokens = $phpcsFile->getTokens();
51-
$find = [
52-
T_ABSTRACT => T_ABSTRACT,
53-
T_FINAL => T_FINAL,
54-
T_READONLY => T_READONLY,
55-
T_WHITESPACE => T_WHITESPACE,
56-
];
52+
53+
$find = [T_WHITESPACE => T_WHITESPACE];
54+
$find += Tokens::CLASS_MODIFIERS;
5755

5856
$previousContent = null;
5957
for ($commentEnd = ($stackPtr - 1); $commentEnd >= 0; $commentEnd--) {

0 commit comments

Comments
 (0)