Skip to content

Commit bb43b55

Browse files
authored
Merge pull request #1228 from PHPCSStandards/phpcs-4.0/feature/modernize-use-class-constants-for-constant-arrays-5
Modernize: use class constant for constant array [5]
2 parents 7dc700b + e9c4732 commit bb43b55

File tree

7 files changed

+79
-65
lines changed

7 files changed

+79
-65
lines changed

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

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

18+
/**
19+
* Required tags in correct order.
20+
*
21+
* @var array<string, true>
22+
*/
23+
private const REQUIRED_TAGS = [
24+
'@package' => true,
25+
'@subpackage' => true,
26+
'@author' => true,
27+
'@copyright' => true,
28+
];
29+
1830

1931
/**
2032
* Returns an array of tokens this test wants to listen for.
@@ -123,18 +135,10 @@ public function process(File $phpcsFile, $stackPtr)
123135
$phpcsFile->addError($error, $commentEnd, 'SpacingAfterComment');
124136
}
125137

126-
// Required tags in correct order.
127-
$required = [
128-
'@package' => true,
129-
'@subpackage' => true,
130-
'@author' => true,
131-
'@copyright' => true,
132-
];
133-
134138
$foundTags = [];
135139
foreach ($tokens[$commentStart]['comment_tags'] as $tag) {
136140
$name = $tokens[$tag]['content'];
137-
$isRequired = isset($required[$name]);
141+
$isRequired = isset(self::REQUIRED_TAGS[$name]);
138142

139143
if ($isRequired === true && in_array($name, $foundTags, true) === true) {
140144
$error = 'Only one %s tag is allowed in a file comment';
@@ -185,7 +189,7 @@ public function process(File $phpcsFile, $stackPtr)
185189

186190
// Check if the tags are in the correct position.
187191
$pos = 0;
188-
foreach ($required as $tag => $true) {
192+
foreach (self::REQUIRED_TAGS as $tag => $true) {
189193
if (in_array($tag, $foundTags, true) === false) {
190194
$error = 'Missing %s tag in file comment';
191195
$data = [$tag];

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

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

19+
/**
20+
* Characters which are accepted to end a sentence.
21+
*
22+
* @var array<string, string>
23+
*/
24+
private const VALID_SENTENCE_END_CHARS = [
25+
'full-stops' => '.',
26+
'exclamation marks' => '!',
27+
'or question marks' => '?',
28+
];
29+
1930

2031
/**
2132
* Returns an array of tokens this test wants to listen for.
@@ -227,17 +238,11 @@ public function process(File $phpcsFile, $stackPtr)
227238
// Only check the end of comment character if the start of the comment
228239
// is a letter, indicating that the comment is just standard text.
229240
if (preg_match('/^\p{L}/u', $commentText) === 1) {
230-
$commentCloser = $commentText[(strlen($commentText) - 1)];
231-
$acceptedClosers = [
232-
'full-stops' => '.',
233-
'exclamation marks' => '!',
234-
'or question marks' => '?',
235-
];
236-
237-
if (in_array($commentCloser, $acceptedClosers, true) === false) {
241+
$commentCloser = $commentText[(strlen($commentText) - 1)];
242+
if (in_array($commentCloser, self::VALID_SENTENCE_END_CHARS, true) === false) {
238243
$error = 'Inline comments must end in %s';
239244
$ender = '';
240-
foreach ($acceptedClosers as $closerName => $symbol) {
245+
foreach (self::VALID_SENTENCE_END_CHARS as $closerName => $symbol) {
241246
$ender .= ' '.$closerName.',';
242247
}
243248

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,19 @@ class LongConditionClosingCommentSniff implements Sniff
1616
{
1717

1818
/**
19-
* The openers that we are interested in.
19+
* The condition openers that we are interested in.
2020
*
21-
* @var array<int|string>
21+
* @var array<int|string, int|string>
2222
*/
23-
private static $openers = [
24-
T_SWITCH,
25-
T_IF,
26-
T_FOR,
27-
T_FOREACH,
28-
T_WHILE,
29-
T_TRY,
30-
T_CASE,
31-
T_MATCH,
23+
private const CONDITION_OPENERS = [
24+
T_SWITCH => T_SWITCH,
25+
T_IF => T_IF,
26+
T_FOR => T_FOR,
27+
T_FOREACH => T_FOREACH,
28+
T_WHILE => T_WHILE,
29+
T_TRY => T_TRY,
30+
T_CASE => T_CASE,
31+
T_MATCH => T_MATCH,
3232
];
3333

3434
/**
@@ -84,7 +84,7 @@ public function process(File $phpcsFile, $stackPtr)
8484
$endBrace = $tokens[$stackPtr];
8585

8686
// We are only interested in some code blocks.
87-
if (in_array($startCondition['code'], self::$openers, true) === false) {
87+
if (isset(self::CONDITION_OPENERS[$startCondition['code']]) === false) {
8888
return;
8989
}
9090

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ class PostStatementCommentSniff implements Sniff
2121
* If post statement comments are found within the condition
2222
* parenthesis of these structures, leave them alone.
2323
*
24-
* @var array
24+
* @var array<int|string, true>
2525
*/
26-
private $controlStructureExceptions = [
26+
private const CONTROL_STRUCTURE_EXCEPTIONS = [
2727
T_IF => true,
2828
T_ELSEIF => true,
2929
T_SWITCH => true,
@@ -92,7 +92,7 @@ public function process(File $phpcsFile, $stackPtr)
9292
$nestedParens = $tokens[$stackPtr]['nested_parenthesis'];
9393
foreach ($nestedParens as $open => $close) {
9494
if (isset($tokens[$open]['parenthesis_owner']) === true
95-
&& isset($this->controlStructureExceptions[$tokens[$tokens[$open]['parenthesis_owner']]['code']]) === true
95+
&& isset(self::CONTROL_STRUCTURE_EXCEPTIONS[$tokens[$tokens[$open]['parenthesis_owner']]['code']]) === true
9696
) {
9797
return;
9898
}

src/Standards/Squiz/Sniffs/Operators/ComparisonOperatorUsageSniff.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ class ComparisonOperatorUsageSniff implements Sniff
1919
/**
2020
* A list of valid comparison operators.
2121
*
22-
* @var array
22+
* @var array<int|string, true>
2323
*/
24-
private static $validOps = [
24+
private const VALID_OPERATIONS = [
2525
T_IS_IDENTICAL => true,
2626
T_IS_NOT_IDENTICAL => true,
2727
T_LESS_THAN => true,
@@ -36,7 +36,7 @@ class ComparisonOperatorUsageSniff implements Sniff
3636
*
3737
* @var array<int|string, string>
3838
*/
39-
private static $invalidOps = [
39+
private const INVALID_OPERATIONS = [
4040
T_IS_EQUAL => '===',
4141
T_IS_NOT_EQUAL => '!==',
4242
T_BOOLEAN_NOT => '=== FALSE',
@@ -149,15 +149,15 @@ public function process(File $phpcsFile, $stackPtr)
149149

150150
for ($i = $start; $i <= $end; $i++) {
151151
$type = $tokens[$i]['code'];
152-
if (isset(self::$invalidOps[$type]) === true) {
152+
if (isset(self::INVALID_OPERATIONS[$type]) === true) {
153153
$error = 'Operator %s prohibited; use %s instead';
154154
$data = [
155155
$tokens[$i]['content'],
156-
self::$invalidOps[$type],
156+
self::INVALID_OPERATIONS[$type],
157157
];
158158
$phpcsFile->addError($error, $i, 'NotAllowed', $data);
159159
$foundOps++;
160-
} else if (isset(self::$validOps[$type]) === true) {
160+
} else if (isset(self::VALID_OPERATIONS[$type]) === true) {
161161
$foundOps++;
162162
}
163163

src/Standards/Squiz/Sniffs/PHP/NonExecutableCodeSniff.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ class NonExecutableCodeSniff implements Sniff
2525
* `throw` can be used as an expression since PHP 8.0.
2626
* {@link https://wiki.php.net/rfc/throw_expression}
2727
*
28-
* @var array
28+
* @var array<int, int>
2929
*/
30-
private $expressionTokens = [
30+
private const EXPRESSION_TOKENS = [
3131
T_EXIT => T_EXIT,
3232
T_THROW => T_THROW,
3333
];
@@ -68,7 +68,7 @@ public function process(File $phpcsFile, $stackPtr)
6868
$prev = $phpcsFile->findPrevious(Tokens::EMPTY_TOKENS, ($stackPtr - 1), null, true);
6969

7070
// Tokens which can be used in inline expressions need special handling.
71-
if (isset($this->expressionTokens[$tokens[$stackPtr]['code']]) === true) {
71+
if (isset(self::EXPRESSION_TOKENS[$tokens[$stackPtr]['code']]) === true) {
7272
// If this token is preceded by a logical operator, it only relates to one line
7373
// and should be ignored. For example: fopen() or die().
7474
// Note: There is one exception: throw expressions can not be used with xor.

src/Standards/Squiz/Sniffs/Strings/DoubleQuoteUsageSniff.php

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,32 @@
1515
class DoubleQuoteUsageSniff implements Sniff
1616
{
1717

18+
/**
19+
* Escape chars which are supported in double quoted strings, but not in single quoted strings.
20+
*
21+
* @var array<string>
22+
*/
23+
private const ESCAPE_CHARS = [
24+
'\0',
25+
'\1',
26+
'\2',
27+
'\3',
28+
'\4',
29+
'\5',
30+
'\6',
31+
'\7',
32+
'\n',
33+
'\r',
34+
'\f',
35+
'\t',
36+
'\v',
37+
'\x',
38+
'\b',
39+
'\e',
40+
'\u',
41+
'\'',
42+
];
43+
1844

1945
/**
2046
* Returns an array of tokens this test wants to listen for.
@@ -91,28 +117,7 @@ public function process(File $phpcsFile, $stackPtr)
91117
return $skipTo;
92118
}//end if
93119

94-
$allowedChars = [
95-
'\0',
96-
'\1',
97-
'\2',
98-
'\3',
99-
'\4',
100-
'\5',
101-
'\6',
102-
'\7',
103-
'\n',
104-
'\r',
105-
'\f',
106-
'\t',
107-
'\v',
108-
'\x',
109-
'\b',
110-
'\e',
111-
'\u',
112-
'\'',
113-
];
114-
115-
foreach ($allowedChars as $testChar) {
120+
foreach (self::ESCAPE_CHARS as $testChar) {
116121
if (strpos($workingString, $testChar) !== false) {
117122
return $skipTo;
118123
}

0 commit comments

Comments
 (0)