Skip to content

Commit d04c9bc

Browse files
authored
Merge pull request #1224 from PHPCSStandards/phpcs-4.0/feature/modernize-use-class-constants-for-constant-arrays-3
Modernize: use class constant for constant array [3]
2 parents 91cad8a + f8f9296 commit d04c9bc

9 files changed

+105
-97
lines changed

src/Standards/Generic/Sniffs/CodeAnalysis/UnusedFunctionParameterSniff.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,15 @@
2323
class UnusedFunctionParameterSniff implements Sniff
2424
{
2525

26-
/**
27-
* The list of class type hints which will be ignored.
28-
*
29-
* @var array
30-
*/
31-
public $ignoreTypeHints = [];
32-
3326
/**
3427
* A list of all PHP magic methods with fixed method signatures.
3528
*
3629
* Note: `__construct()` and `__invoke()` are excluded on purpose
3730
* as their method signature is not fixed.
3831
*
39-
* @var array
32+
* @var array<string, true>
4033
*/
41-
private $magicMethods = [
34+
private const MAGIC_METHODS = [
4235
'__destruct' => true,
4336
'__call' => true,
4437
'__callstatic' => true,
@@ -56,6 +49,13 @@ class UnusedFunctionParameterSniff implements Sniff
5649
'__debuginfo' => true,
5750
];
5851

52+
/**
53+
* The list of class type hints which will be ignored.
54+
*
55+
* @var array
56+
*/
57+
public $ignoreTypeHints = [];
58+
5959

6060
/**
6161
* Returns an array of tokens this test wants to listen for.
@@ -101,7 +101,7 @@ public function process(File $phpcsFile, $stackPtr)
101101
// Check for magic methods and ignore these as the method signature cannot be changed.
102102
$methodName = $phpcsFile->getDeclarationName($stackPtr);
103103
$methodNameLc = strtolower($methodName);
104-
if (isset($this->magicMethods[$methodNameLc]) === true) {
104+
if (isset(self::MAGIC_METHODS[$methodNameLc]) === true) {
105105
return;
106106
}
107107

src/Standards/Generic/Sniffs/CodeAnalysis/UselessOverridingMethodSniff.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class UselessOverridingMethodSniff implements Sniff
3333
*
3434
* @var array<int|string, bool> Keys are the token constants, value is irrelevant.
3535
*/
36-
private $validOOScopes = [
36+
private const VALID_OO_SCOPES = [
3737
T_CLASS => true,
3838
T_ANON_CLASS => true,
3939
T_TRAIT => true,
@@ -74,8 +74,8 @@ public function process(File $phpcsFile, $stackPtr)
7474
$conditions = $token['conditions'];
7575
$lastCondition = end($conditions);
7676

77-
// Skip functions that are not a method part of a class, anon class or trait.
78-
if (isset($this->validOOScopes[$lastCondition]) === false) {
77+
// Skip functions that are not a method, i.e. part of a class, anon class or trait.
78+
if (isset(self::VALID_OO_SCOPES[$lastCondition]) === false) {
7979
return;
8080
}
8181

src/Standards/Generic/Sniffs/Metrics/CyclomaticComplexitySniff.php

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,27 @@
2020
class CyclomaticComplexitySniff implements Sniff
2121
{
2222

23+
/**
24+
* Predicate nodes for PHP.
25+
*
26+
* @var array<int|string, true>
27+
*/
28+
private const PREDICATE_NODES = [
29+
T_CASE => true,
30+
T_DEFAULT => true,
31+
T_CATCH => true,
32+
T_IF => true,
33+
T_FOR => true,
34+
T_FOREACH => true,
35+
T_WHILE => true,
36+
T_ELSEIF => true,
37+
T_INLINE_THEN => true,
38+
T_COALESCE => true,
39+
T_COALESCE_EQUAL => true,
40+
T_MATCH_ARROW => true,
41+
T_NULLSAFE_OBJECT_OPERATOR => true,
42+
];
43+
2344
/**
2445
* A complexity higher than this value will throw a warning.
2546
*
@@ -69,28 +90,11 @@ public function process(File $phpcsFile, $stackPtr)
6990
$start = $tokens[$stackPtr]['scope_opener'];
7091
$end = $tokens[$stackPtr]['scope_closer'];
7192

72-
// Predicate nodes for PHP.
73-
$find = [
74-
T_CASE => true,
75-
T_DEFAULT => true,
76-
T_CATCH => true,
77-
T_IF => true,
78-
T_FOR => true,
79-
T_FOREACH => true,
80-
T_WHILE => true,
81-
T_ELSEIF => true,
82-
T_INLINE_THEN => true,
83-
T_COALESCE => true,
84-
T_COALESCE_EQUAL => true,
85-
T_MATCH_ARROW => true,
86-
T_NULLSAFE_OBJECT_OPERATOR => true,
87-
];
88-
8993
$complexity = 1;
9094

9195
// Iterate from start to end and count predicate nodes.
9296
for ($i = ($start + 1); $i < $end; $i++) {
93-
if (isset($find[$tokens[$i]['code']]) === true) {
97+
if (isset(self::PREDICATE_NODES[$tokens[$i]['code']]) === true) {
9498
$complexity++;
9599
}
96100
}

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ class LowerCaseConstantSniff implements Sniff
1919
/**
2020
* The tokens this sniff is targetting.
2121
*
22-
* @var array
22+
* @var array<int|string, int|string>
2323
*/
24-
private $targets = [
24+
private const TARGET_TOKENS = [
2525
T_TRUE => T_TRUE,
2626
T_FALSE => T_FALSE,
2727
T_NULL => T_NULL,
@@ -32,23 +32,19 @@ class LowerCaseConstantSniff implements Sniff
3232
*
3333
* @var array<int|string, int|string>
3434
*/
35-
private $propertyTypeTokens = [
35+
private const PROPERTY_TYPE_TOKENS = (Tokens::NAME_TOKENS + [
3636
T_CALLABLE => T_CALLABLE,
3737
T_SELF => T_SELF,
3838
T_PARENT => T_PARENT,
3939
T_FALSE => T_FALSE,
4040
T_TRUE => T_TRUE,
4141
T_NULL => T_NULL,
42-
T_STRING => T_STRING,
43-
T_NAME_QUALIFIED => T_NAME_QUALIFIED,
44-
T_NAME_FULLY_QUALIFIED => T_NAME_FULLY_QUALIFIED,
45-
T_NAME_RELATIVE => T_NAME_RELATIVE,
4642
T_TYPE_UNION => T_TYPE_UNION,
4743
T_TYPE_INTERSECTION => T_TYPE_INTERSECTION,
4844
T_TYPE_OPEN_PARENTHESIS => T_TYPE_OPEN_PARENTHESIS,
4945
T_TYPE_CLOSE_PARENTHESIS => T_TYPE_CLOSE_PARENTHESIS,
5046
T_NULLABLE => T_NULLABLE,
51-
];
47+
]);
5248

5349

5450
/**
@@ -58,7 +54,7 @@ class LowerCaseConstantSniff implements Sniff
5854
*/
5955
public function register()
6056
{
61-
$targets = $this->targets;
57+
$targets = self::TARGET_TOKENS;
6258

6359
// Register scope modifiers to filter out property type declarations.
6460
$targets += Tokens::SCOPE_MODIFIERS;
@@ -130,7 +126,7 @@ public function process(File $phpcsFile, $stackPtr)
130126
|| $tokens[$stackPtr]['code'] === T_FINAL
131127
|| $tokens[$stackPtr]['code'] === T_ABSTRACT
132128
) {
133-
$skipOver = (Tokens::EMPTY_TOKENS + $this->propertyTypeTokens);
129+
$skipOver = (Tokens::EMPTY_TOKENS + self::PROPERTY_TYPE_TOKENS);
134130
$skipTo = $phpcsFile->findNext($skipOver, ($stackPtr + 1), null, true);
135131
if ($skipTo !== false) {
136132
return $skipTo;
@@ -161,7 +157,7 @@ public function process(File $phpcsFile, $stackPtr)
161157
}
162158

163159
// Do a quick check if any of the targets exist in the declaration.
164-
$found = $phpcsFile->findNext($this->targets, $tokens[$stackPtr]['parenthesis_opener'], $end);
160+
$found = $phpcsFile->findNext(self::TARGET_TOKENS, $tokens[$stackPtr]['parenthesis_opener'], $end);
165161
if ($found === false) {
166162
// Skip forward, no need to examine these tokens again.
167163
return $end;
@@ -180,7 +176,7 @@ public function process(File $phpcsFile, $stackPtr)
180176
}
181177

182178
for ($i = $param['default_token']; $i < $paramEnd; $i++) {
183-
if (isset($this->targets[$tokens[$i]['code']]) === true) {
179+
if (isset(self::TARGET_TOKENS[$tokens[$i]['code']]) === true) {
184180
$this->processConstant($phpcsFile, $i);
185181
}
186182
}

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ class LowerCaseTypeSniff implements Sniff
2020
/**
2121
* Native types supported by PHP.
2222
*
23-
* @var array
23+
* @var array<string, true>
2424
*/
25-
private $phpTypes = [
25+
private const PHP_TYPES = [
2626
'self' => true,
2727
'parent' => true,
2828
'array' => true,
@@ -146,7 +146,7 @@ public function process(File $phpcsFile, $stackPtr)
146146
);
147147
} else {
148148
$type = $tokens[$startOfType]['content'];
149-
if (isset($this->phpTypes[strtolower($type)]) === true) {
149+
if (isset(self::PHP_TYPES[strtolower($type)]) === true) {
150150
$this->processType($phpcsFile, $startOfType, $type, $error, $errorCode);
151151
}
152152
}
@@ -182,7 +182,7 @@ public function process(File $phpcsFile, $stackPtr)
182182
$error,
183183
$errorCode
184184
);
185-
} else if (isset($this->phpTypes[strtolower($type)]) === true) {
185+
} else if (isset(self::PHP_TYPES[strtolower($type)]) === true) {
186186
$this->processType($phpcsFile, $props['type_token'], $type, $error, $errorCode);
187187
}
188188
}
@@ -213,7 +213,7 @@ public function process(File $phpcsFile, $stackPtr)
213213
$error,
214214
$errorCode
215215
);
216-
} else if (isset($this->phpTypes[strtolower($returnType)]) === true) {
216+
} else if (isset(self::PHP_TYPES[strtolower($returnType)]) === true) {
217217
$this->processType($phpcsFile, $props['return_type_token'], $returnType, $error, $errorCode);
218218
}
219219
}
@@ -244,7 +244,7 @@ public function process(File $phpcsFile, $stackPtr)
244244
$error,
245245
$errorCode
246246
);
247-
} else if (isset($this->phpTypes[strtolower($typeHint)]) === true) {
247+
} else if (isset(self::PHP_TYPES[strtolower($typeHint)]) === true) {
248248
$this->processType($phpcsFile, $param['type_hint_token'], $typeHint, $error, $errorCode);
249249
}
250250
}
@@ -285,7 +285,7 @@ protected function processUnionType(File $phpcsFile, $typeDeclStart, $typeDeclEn
285285
) {
286286
if ($typeTokenCount === 1
287287
&& $type !== ''
288-
&& isset($this->phpTypes[strtolower($type)]) === true
288+
&& isset(self::PHP_TYPES[strtolower($type)]) === true
289289
) {
290290
$this->processType($phpcsFile, $typeStart, $type, $error, $errorCode);
291291
}
@@ -309,7 +309,7 @@ protected function processUnionType(File $phpcsFile, $typeDeclStart, $typeDeclEn
309309
// Handle type at end of type string.
310310
if ($typeTokenCount === 1
311311
&& $type !== ''
312-
&& isset($this->phpTypes[strtolower($type)]) === true
312+
&& isset(self::PHP_TYPES[strtolower($type)]) === true
313313
) {
314314
$this->processType($phpcsFile, $typeStart, $type, $error, $errorCode);
315315
}

src/Standards/Generic/Sniffs/Strings/UnnecessaryHeredocSniff.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class UnnecessaryHeredocSniff implements Sniff
2020
*
2121
* @var array<string>
2222
*/
23-
private $escapeChars = [
23+
private const ESCAPE_CHARS = [
2424
// Octal sequences.
2525
'\0',
2626
'\1',
@@ -111,7 +111,7 @@ public function process(File $phpcsFile, $stackPtr)
111111
$phpcsFile->recordMetric($stackPtr, 'Heredoc contains interpolation or expression', 'no');
112112

113113
// Check for escape sequences which aren't supported in nowdocs.
114-
foreach ($this->escapeChars as $testChar) {
114+
foreach (self::ESCAPE_CHARS as $testChar) {
115115
if (strpos($body, $testChar) !== false) {
116116
return;
117117
}

src/Standards/Generic/Sniffs/WhiteSpace/ArbitraryParenthesesSpacingSniff.php

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,24 @@
1919
class ArbitraryParenthesesSpacingSniff implements Sniff
2020
{
2121

22+
/**
23+
* Tokens which when they precede an open parenthesis indicate
24+
* that this is a type of structure this sniff should ignore.
25+
*
26+
* @var array<int|string, int|string>
27+
*/
28+
private const IGNORE_TOKENS = (Tokens::FUNCTION_NAME_TOKENS + [
29+
T_VARIABLE => T_VARIABLE,
30+
T_CLOSE_PARENTHESIS => T_CLOSE_PARENTHESIS,
31+
T_CLOSE_CURLY_BRACKET => T_CLOSE_CURLY_BRACKET,
32+
T_CLOSE_SQUARE_BRACKET => T_CLOSE_SQUARE_BRACKET,
33+
T_CLOSE_SHORT_ARRAY => T_CLOSE_SHORT_ARRAY,
34+
T_THROW => T_THROW,
35+
T_YIELD => T_YIELD,
36+
T_YIELD_FROM => T_YIELD_FROM,
37+
T_CLONE => T_CLONE,
38+
]);
39+
2240
/**
2341
* The number of spaces desired on the inside of the parentheses.
2442
*
@@ -33,14 +51,6 @@ class ArbitraryParenthesesSpacingSniff implements Sniff
3351
*/
3452
public $ignoreNewlines = false;
3553

36-
/**
37-
* Tokens which when they precede an open parenthesis indicate
38-
* that this is a type of structure this sniff should ignore.
39-
*
40-
* @var array
41-
*/
42-
private $ignoreTokens = [];
43-
4454

4555
/**
4656
* Returns an array of tokens this test wants to listen for.
@@ -49,19 +59,6 @@ class ArbitraryParenthesesSpacingSniff implements Sniff
4959
*/
5060
public function register()
5161
{
52-
$this->ignoreTokens = Tokens::FUNCTION_NAME_TOKENS;
53-
54-
$this->ignoreTokens[T_VARIABLE] = T_VARIABLE;
55-
$this->ignoreTokens[T_CLOSE_PARENTHESIS] = T_CLOSE_PARENTHESIS;
56-
$this->ignoreTokens[T_CLOSE_CURLY_BRACKET] = T_CLOSE_CURLY_BRACKET;
57-
$this->ignoreTokens[T_CLOSE_SQUARE_BRACKET] = T_CLOSE_SQUARE_BRACKET;
58-
$this->ignoreTokens[T_CLOSE_SHORT_ARRAY] = T_CLOSE_SHORT_ARRAY;
59-
60-
$this->ignoreTokens[T_THROW] = T_THROW;
61-
$this->ignoreTokens[T_YIELD] = T_YIELD;
62-
$this->ignoreTokens[T_YIELD_FROM] = T_YIELD_FROM;
63-
$this->ignoreTokens[T_CLONE] = T_CLONE;
64-
6562
return [
6663
T_OPEN_PARENTHESIS,
6764
T_CLOSE_PARENTHESIS,
@@ -101,7 +98,7 @@ public function process(File $phpcsFile, $stackPtr)
10198

10299
$preOpener = $phpcsFile->findPrevious(Tokens::EMPTY_TOKENS, ($opener - 1), null, true);
103100
if ($preOpener !== false
104-
&& isset($this->ignoreTokens[$tokens[$preOpener]['code']]) === true
101+
&& isset(self::IGNORE_TOKENS[$tokens[$preOpener]['code']]) === true
105102
&& ($tokens[$preOpener]['code'] !== T_CLOSE_CURLY_BRACKET
106103
|| isset($tokens[$preOpener]['scope_condition']) === false )
107104
) {

src/Standards/Generic/Sniffs/WhiteSpace/DisallowSpaceIndentSniff.php

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

18+
/**
19+
* Tokens which can include indentation.
20+
*
21+
* @var array<int|string, true>
22+
*/
23+
private const TOKENS_CONTAINING_INDENT = [
24+
T_WHITESPACE => true,
25+
T_INLINE_HTML => true,
26+
T_DOC_COMMENT_WHITESPACE => true,
27+
T_COMMENT => true,
28+
T_END_HEREDOC => true,
29+
T_END_NOWDOC => true,
30+
];
31+
1832
/**
1933
* The --tab-width CLI value that is being used.
2034
*
@@ -62,20 +76,11 @@ public function process(File $phpcsFile, $stackPtr)
6276
}
6377
}
6478

65-
$checkTokens = [
66-
T_WHITESPACE => true,
67-
T_INLINE_HTML => true,
68-
T_DOC_COMMENT_WHITESPACE => true,
69-
T_COMMENT => true,
70-
T_END_HEREDOC => true,
71-
T_END_NOWDOC => true,
72-
];
73-
7479
$eolLen = strlen($phpcsFile->eolChar);
7580

7681
$tokens = $phpcsFile->getTokens();
7782
for ($i = 0; $i < $phpcsFile->numTokens; $i++) {
78-
if ($tokens[$i]['column'] !== 1 || isset($checkTokens[$tokens[$i]['code']]) === false) {
83+
if ($tokens[$i]['column'] !== 1 || isset(self::TOKENS_CONTAINING_INDENT[$tokens[$i]['code']]) === false) {
7984
continue;
8085
}
8186

0 commit comments

Comments
 (0)