Skip to content

Commit 7dc700b

Browse files
authored
Merge pull request #1227 from PHPCSStandards/phpcs-4.0/feature/modernize-use-class-constants-for-constant-arrays-4
Modernize: use class constants for constant arrays [4]
2 parents 95daae9 + 500e653 commit 7dc700b

File tree

7 files changed

+102
-75
lines changed

7 files changed

+102
-75
lines changed

src/Standards/PEAR/Sniffs/WhiteSpace/ObjectOperatorIndentSniff.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@
1515
class ObjectOperatorIndentSniff implements Sniff
1616
{
1717

18+
/**
19+
* Tokens to listen for.
20+
*
21+
* @var array<int|string>
22+
*/
23+
private const TARGET_TOKENS = [
24+
T_OBJECT_OPERATOR,
25+
T_NULLSAFE_OBJECT_OPERATOR,
26+
];
27+
1828
/**
1929
* The number of spaces code should be indented.
2030
*
@@ -29,16 +39,6 @@ class ObjectOperatorIndentSniff implements Sniff
2939
*/
3040
public $multilevel = false;
3141

32-
/**
33-
* Tokens to listen for.
34-
*
35-
* @var array
36-
*/
37-
private $targets = [
38-
T_OBJECT_OPERATOR,
39-
T_NULLSAFE_OBJECT_OPERATOR,
40-
];
41-
4242

4343
/**
4444
* Returns an array of tokens this test wants to listen for.
@@ -47,7 +47,7 @@ class ObjectOperatorIndentSniff implements Sniff
4747
*/
4848
public function register()
4949
{
50-
return $this->targets;
50+
return self::TARGET_TOKENS;
5151

5252
}//end register()
5353

@@ -67,14 +67,14 @@ public function process(File $phpcsFile, $stackPtr)
6767

6868
// Make sure this is the first object operator in a chain of them.
6969
$start = $phpcsFile->findStartOfStatement($stackPtr);
70-
$prev = $phpcsFile->findPrevious($this->targets, ($stackPtr - 1), $start);
70+
$prev = $phpcsFile->findPrevious(self::TARGET_TOKENS, ($stackPtr - 1), $start);
7171
if ($prev !== false) {
7272
return;
7373
}
7474

7575
// Make sure this is a chained call.
7676
$end = $phpcsFile->findEndOfStatement($stackPtr);
77-
$next = $phpcsFile->findNext($this->targets, ($stackPtr + 1), $end);
77+
$next = $phpcsFile->findNext(self::TARGET_TOKENS, ($stackPtr + 1), $end);
7878
if ($next === false) {
7979
// Not a chained call.
8080
return;
@@ -189,7 +189,7 @@ public function process(File $phpcsFile, $stackPtr)
189189
}//end if
190190

191191
$next = $phpcsFile->findNext(
192-
$this->targets,
192+
self::TARGET_TOKENS,
193193
($next + 1),
194194
$end
195195
);

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

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,30 @@
1616
class SideEffectsSniff implements Sniff
1717
{
1818

19+
/**
20+
* Tokens which represent symbols in the context of this sniff.
21+
*
22+
* @var array<int|string, int|string>
23+
*/
24+
private const SYMBOL_TOKENS = [
25+
T_CLASS => T_CLASS,
26+
T_INTERFACE => T_INTERFACE,
27+
T_TRAIT => T_TRAIT,
28+
T_ENUM => T_ENUM,
29+
T_FUNCTION => T_FUNCTION,
30+
];
31+
32+
/**
33+
* Condition tokens within which symbols may be defined.
34+
*
35+
* @var array<int|string, int|string>
36+
*/
37+
private const CONDITION_TOKENS = [
38+
T_IF => T_IF,
39+
T_ELSE => T_ELSE,
40+
T_ELSEIF => T_ELSEIF,
41+
];
42+
1943

2044
/**
2145
* Returns an array of tokens this test wants to listen for.
@@ -78,20 +102,6 @@ public function process(File $phpcsFile, $stackPtr)
78102
*/
79103
private function searchForConflict($phpcsFile, $start, $end, $tokens)
80104
{
81-
$symbols = [
82-
T_CLASS => T_CLASS,
83-
T_INTERFACE => T_INTERFACE,
84-
T_TRAIT => T_TRAIT,
85-
T_ENUM => T_ENUM,
86-
T_FUNCTION => T_FUNCTION,
87-
];
88-
89-
$conditions = [
90-
T_IF => T_IF,
91-
T_ELSE => T_ELSE,
92-
T_ELSEIF => T_ELSEIF,
93-
];
94-
95105
$checkAnnotations = $phpcsFile->config->annotations;
96106

97107
$firstSymbol = null;
@@ -192,7 +202,7 @@ private function searchForConflict($phpcsFile, $start, $end, $tokens)
192202
}
193203

194204
// Detect and skip over symbols.
195-
if (isset($symbols[$tokens[$i]['code']]) === true
205+
if (isset(self::SYMBOL_TOKENS[$tokens[$i]['code']]) === true
196206
&& isset($tokens[$i]['scope_closer']) === true
197207
) {
198208
if ($firstSymbol === null) {
@@ -251,7 +261,7 @@ private function searchForConflict($phpcsFile, $start, $end, $tokens)
251261
// Conditional statements are allowed in symbol files as long as the
252262
// contents is only a symbol definition. So don't count these as effects
253263
// in this case.
254-
if (isset($conditions[$tokens[$i]['code']]) === true) {
264+
if (isset(self::CONDITION_TOKENS[$tokens[$i]['code']]) === true) {
255265
if (isset($tokens[$i]['scope_opener']) === false) {
256266
// Probably an "else if", so just ignore.
257267
continue;

src/Standards/PSR12/Sniffs/ControlStructures/BooleanOperatorPlacementSniff.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@
1515
class BooleanOperatorPlacementSniff implements Sniff
1616
{
1717

18+
/**
19+
* Boolean operator tokens.
20+
*
21+
* @var array<int|string>
22+
*/
23+
private const BOOLEAN_OPERATORS = [
24+
T_BOOLEAN_AND,
25+
T_BOOLEAN_OR,
26+
];
27+
1828
/**
1929
* Used to restrict the placement of the boolean operator.
2030
*
@@ -70,11 +80,6 @@ public function process(File $phpcsFile, $stackPtr)
7080
return;
7181
}
7282

73-
$find = [
74-
T_BOOLEAN_AND,
75-
T_BOOLEAN_OR,
76-
];
77-
7883
if ($this->allowOnly === 'first' || $this->allowOnly === 'last') {
7984
$position = $this->allowOnly;
8085
} else {
@@ -86,7 +91,7 @@ public function process(File $phpcsFile, $stackPtr)
8691
$operators = [];
8792

8893
do {
89-
$operator = $phpcsFile->findNext($find, ($operator + 1), $parenCloser);
94+
$operator = $phpcsFile->findNext(self::BOOLEAN_OPERATORS, ($operator + 1), $parenCloser);
9095
if ($operator === false) {
9196
break;
9297
}

src/Standards/PSR12/Sniffs/Functions/NullableTypeDeclarationSniff.php

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,25 @@
1111

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

1516
class NullableTypeDeclarationSniff implements Sniff
1617
{
1718

1819
/**
1920
* An array of valid tokens after `T_NULLABLE` occurrences.
2021
*
21-
* @var array
22+
* @var array<int|string, int|string>
2223
*/
23-
private $validTokens = [
24-
T_STRING => true,
25-
T_NAME_QUALIFIED => true,
26-
T_NAME_FULLY_QUALIFIED => true,
27-
T_NAME_RELATIVE => true,
28-
T_CALLABLE => true,
29-
T_SELF => true,
30-
T_PARENT => true,
31-
T_STATIC => true,
32-
T_NULL => true,
33-
T_FALSE => true,
34-
T_TRUE => true,
35-
];
24+
private const VALID_TOKENS = (Tokens::NAME_TOKENS + [
25+
T_CALLABLE => T_CALLABLE,
26+
T_SELF => T_SELF,
27+
T_PARENT => T_PARENT,
28+
T_STATIC => T_STATIC,
29+
T_NULL => T_NULL,
30+
T_FALSE => T_FALSE,
31+
T_TRUE => T_TRUE,
32+
]);
3633

3734

3835
/**
@@ -66,7 +63,7 @@ public function process(File $phpcsFile, $stackPtr)
6663

6764
$tokens = $phpcsFile->getTokens();
6865
$nextNonEmptyCode = $tokens[$nextNonEmptyPtr]['code'];
69-
$validTokenFound = isset($this->validTokens[$nextNonEmptyCode]);
66+
$validTokenFound = isset(self::VALID_TOKENS[$nextNonEmptyCode]);
7067

7168
if ($validTokenFound === true && $nextNonEmptyPtr === ($stackPtr + 1)) {
7269
// Valid structure.

src/Standards/PSR12/Sniffs/Properties/ConstantVisibilitySniff.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@
1616
class ConstantVisibilitySniff implements Sniff
1717
{
1818

19+
/**
20+
* Visibility tokens which are valid for class constants.
21+
*
22+
* @var array<int, int>
23+
*/
24+
private const VALID_VISIBILITY = [
25+
T_PRIVATE => T_PRIVATE,
26+
T_PUBLIC => T_PUBLIC,
27+
T_PROTECTED => T_PROTECTED,
28+
];
29+
1930

2031
/**
2132
* Returns an array of tokens this test wants to listen for.
@@ -50,14 +61,8 @@ public function process(File $phpcsFile, $stackPtr)
5061
$ignore = Tokens::EMPTY_TOKENS;
5162
$ignore[] = T_FINAL;
5263

53-
$validVisibility = [
54-
T_PRIVATE => T_PRIVATE,
55-
T_PUBLIC => T_PUBLIC,
56-
T_PROTECTED => T_PROTECTED,
57-
];
58-
5964
$prev = $phpcsFile->findPrevious($ignore, ($stackPtr - 1), null, true);
60-
if (isset($validVisibility[$tokens[$prev]['code']]) === true) {
65+
if (isset(self::VALID_VISIBILITY[$tokens[$prev]['code']]) === true) {
6166
return;
6267
}
6368

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@
1616
class ClassDeclarationSniff extends PEARClassDeclarationSniff
1717
{
1818

19+
/**
20+
* Modifier keywords which can be used in class declarations.
21+
*
22+
* @var array<int|string, int|string>
23+
*/
24+
private const CLASS_MODIFIERS = [
25+
T_ABSTRACT => T_ABSTRACT,
26+
T_FINAL => T_FINAL,
27+
T_READONLY => T_READONLY,
28+
];
29+
1930
/**
2031
* The number of spaces code should be indented.
2132
*
@@ -65,16 +76,10 @@ public function processOpen(File $phpcsFile, $stackPtr)
6576
$stackPtrType = strtolower($tokens[$stackPtr]['content']);
6677

6778
// Check alignment of the keyword and braces.
68-
$classModifiers = [
69-
T_ABSTRACT => T_ABSTRACT,
70-
T_FINAL => T_FINAL,
71-
T_READONLY => T_READONLY,
72-
];
73-
7479
$prevNonSpace = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
7580
$prevNonEmpty = $phpcsFile->findPrevious(Tokens::EMPTY_TOKENS, ($stackPtr - 1), null, true);
7681

77-
if (isset($classModifiers[$tokens[$prevNonEmpty]['code']]) === true) {
82+
if (isset(self::CLASS_MODIFIERS[$tokens[$prevNonEmpty]['code']]) === true) {
7883
$spaces = 0;
7984
$errorCode = 'SpaceBeforeKeyword';
8085
if ($tokens[$prevNonEmpty]['line'] !== $tokens[$stackPtr]['line']) {

src/Standards/PSR2/Sniffs/ControlStructures/SwitchDeclarationSniff.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,20 @@
1616
class SwitchDeclarationSniff implements Sniff
1717
{
1818

19+
/**
20+
* Tokens which can terminate a "case".
21+
*
22+
* @var array<int|string, int|string>
23+
*/
24+
private const CASE_TERMINATING_TOKENS = [
25+
T_RETURN => T_RETURN,
26+
T_BREAK => T_BREAK,
27+
T_CONTINUE => T_CONTINUE,
28+
T_THROW => T_THROW,
29+
T_EXIT => T_EXIT,
30+
T_GOTO => T_GOTO,
31+
];
32+
1933
/**
2034
* The number of spaces code should be indented.
2135
*
@@ -382,17 +396,8 @@ private function findNestedTerminator($phpcsFile, $stackPtr, $end)
382396
} else if ($tokens[$lastToken]['code'] === T_SEMICOLON) {
383397
// We found the last statement of the CASE. Now we want to
384398
// check whether it is a terminating one.
385-
$terminators = [
386-
T_RETURN => T_RETURN,
387-
T_BREAK => T_BREAK,
388-
T_CONTINUE => T_CONTINUE,
389-
T_THROW => T_THROW,
390-
T_EXIT => T_EXIT,
391-
T_GOTO => T_GOTO,
392-
];
393-
394399
$terminator = $phpcsFile->findStartOfStatement(($lastToken - 1));
395-
if (isset($terminators[$tokens[$terminator]['code']]) === true) {
400+
if (isset(self::CASE_TERMINATING_TOKENS[$tokens[$terminator]['code']]) === true) {
396401
return $terminator;
397402
}
398403
}//end if

0 commit comments

Comments
 (0)