Skip to content

Commit 22e353c

Browse files
authored
Merge pull request #1222 from PHPCSStandards/phpcs-4.0/feature/modernize-use-class-constants-for-constant-arrays-1
Modernize: use class constant for constant array [1]
2 parents c356178 + 2be11eb commit 22e353c

File tree

2 files changed

+52
-45
lines changed

2 files changed

+52
-45
lines changed

src/Config.php

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,30 @@ class Config
122122
'no-cache' => 'cache',
123123
];
124124

125+
/**
126+
* A list of valid generators.
127+
*
128+
* @var array<string, string> Keys are the lowercase version of the generator name, while values
129+
* are the name of the associated PHP generator class.
130+
*/
131+
private const VALID_GENERATORS = [
132+
'text' => 'Text',
133+
'html' => 'HTML',
134+
'markdown' => 'Markdown',
135+
];
136+
137+
/**
138+
* The default configuration file names supported by PHPCS.
139+
*
140+
* @var array<string> The supported file names in order of precedence (highest first).
141+
*/
142+
private const CONFIG_FILENAMES = [
143+
'.phpcs.xml',
144+
'phpcs.xml',
145+
'.phpcs.xml.dist',
146+
'phpcs.xml.dist',
147+
];
148+
125149
/**
126150
* An array of settings that PHPCS and PHPCBF accept.
127151
*
@@ -191,21 +215,6 @@ class Config
191215
*/
192216
private $cliArgs = [];
193217

194-
/**
195-
* A list of valid generators.
196-
*
197-
* {@internal Once support for PHP < 5.6 is dropped, this property should be refactored into a
198-
* class constant.}
199-
*
200-
* @var array<string, string> Keys are the lowercase version of the generator name, while values
201-
* are the associated PHP generator class.
202-
*/
203-
private $validGenerators = [
204-
'text' => 'Text',
205-
'html' => 'HTML',
206-
'markdown' => 'Markdown',
207-
];
208-
209218
/**
210219
* Command line values that the user has supplied directly.
211220
*
@@ -429,15 +438,8 @@ public function __construct(array $cliArgs=[], $dieOnUnknownArg=true)
429438
// Look for a default ruleset in the current directory or higher.
430439
$currentDir = getcwd();
431440

432-
$defaultFiles = [
433-
'.phpcs.xml',
434-
'phpcs.xml',
435-
'.phpcs.xml.dist',
436-
'phpcs.xml.dist',
437-
];
438-
439441
do {
440-
foreach ($defaultFiles as $defaultFilename) {
442+
foreach (self::CONFIG_FILENAMES as $defaultFilename) {
441443
$default = $currentDir.DIRECTORY_SEPARATOR.$defaultFilename;
442444
if (is_file($default) === true) {
443445
$this->standards = [$default];
@@ -1250,8 +1252,8 @@ public function processLongArgument($arg, $pos)
12501252
$generatorName = substr($arg, 10);
12511253
$lowerCaseGeneratorName = strtolower($generatorName);
12521254

1253-
if (isset($this->validGenerators[$lowerCaseGeneratorName]) === false) {
1254-
$validOptions = implode(', ', $this->validGenerators);
1255+
if (isset(self::VALID_GENERATORS[$lowerCaseGeneratorName]) === false) {
1256+
$validOptions = implode(', ', self::VALID_GENERATORS);
12551257
$validOptions = substr_replace($validOptions, ' and', strrpos($validOptions, ','), 1);
12561258
$error = sprintf(
12571259
'ERROR: "%s" is not a valid generator. The following generators are supported: %s.'.PHP_EOL.PHP_EOL,
@@ -1262,7 +1264,7 @@ public function processLongArgument($arg, $pos)
12621264
throw new DeepExitException($error, ExitCode::PROCESS_ERROR);
12631265
}
12641266

1265-
$this->generator = $this->validGenerators[$lowerCaseGeneratorName];
1267+
$this->generator = self::VALID_GENERATORS[$lowerCaseGeneratorName];
12661268
$this->overriddenDefaults['generator'] = true;
12671269
} else if (substr($arg, 0, 9) === 'encoding=') {
12681270
if (isset($this->overriddenDefaults['encoding']) === true) {

src/Tokenizers/Tokenizer.php

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,29 @@
1818
abstract class Tokenizer
1919
{
2020

21+
/**
22+
* List of tokens which may contain tab characters.
23+
*
24+
* @var array<int|string, true>
25+
*/
26+
private const TOKENS_WITH_TABS = [
27+
T_WHITESPACE => true,
28+
T_COMMENT => true,
29+
T_DOC_COMMENT => true,
30+
T_DOC_COMMENT_WHITESPACE => true,
31+
T_DOC_COMMENT_STRING => true,
32+
T_CONSTANT_ENCAPSED_STRING => true,
33+
T_DOUBLE_QUOTED_STRING => true,
34+
T_START_HEREDOC => true,
35+
T_START_NOWDOC => true,
36+
T_HEREDOC => true,
37+
T_NOWDOC => true,
38+
T_END_HEREDOC => true,
39+
T_END_NOWDOC => true,
40+
T_INLINE_HTML => true,
41+
T_YIELD_FROM => true,
42+
];
43+
2144
/**
2245
* The config data for the run.
2346
*
@@ -188,24 +211,6 @@ private function createPositionMap()
188211
$encoding = $this->config->encoding;
189212
$tabWidth = $this->config->tabWidth;
190213

191-
$tokensWithTabs = [
192-
T_WHITESPACE => true,
193-
T_COMMENT => true,
194-
T_DOC_COMMENT => true,
195-
T_DOC_COMMENT_WHITESPACE => true,
196-
T_DOC_COMMENT_STRING => true,
197-
T_CONSTANT_ENCAPSED_STRING => true,
198-
T_DOUBLE_QUOTED_STRING => true,
199-
T_START_HEREDOC => true,
200-
T_START_NOWDOC => true,
201-
T_HEREDOC => true,
202-
T_NOWDOC => true,
203-
T_END_HEREDOC => true,
204-
T_END_NOWDOC => true,
205-
T_INLINE_HTML => true,
206-
T_YIELD_FROM => true,
207-
];
208-
209214
$this->numTokens = count($this->tokens);
210215
for ($i = 0; $i < $this->numTokens; $i++) {
211216
$this->tokens[$i]['line'] = $lineNumber;
@@ -216,7 +221,7 @@ private function createPositionMap()
216221
$length = $this->knownLengths[$this->tokens[$i]['code']];
217222
$currColumn += $length;
218223
} else if ($tabWidth === 0
219-
|| isset($tokensWithTabs[$this->tokens[$i]['code']]) === false
224+
|| isset(self::TOKENS_WITH_TABS[$this->tokens[$i]['code']]) === false
220225
|| strpos($this->tokens[$i]['content'], "\t") === false
221226
) {
222227
// There are no tabs in this content, or we aren't replacing them.

0 commit comments

Comments
 (0)