diff --git a/src/Config.php b/src/Config.php index d8e0d19e12..84fcfed139 100644 --- a/src/Config.php +++ b/src/Config.php @@ -122,6 +122,30 @@ class Config 'no-cache' => 'cache', ]; + /** + * A list of valid generators. + * + * @var array Keys are the lowercase version of the generator name, while values + * are the name of the associated PHP generator class. + */ + private const VALID_GENERATORS = [ + 'text' => 'Text', + 'html' => 'HTML', + 'markdown' => 'Markdown', + ]; + + /** + * The default configuration file names supported by PHPCS. + * + * @var array The supported file names in order of precedence (highest first). + */ + private const CONFIG_FILENAMES = [ + '.phpcs.xml', + 'phpcs.xml', + '.phpcs.xml.dist', + 'phpcs.xml.dist', + ]; + /** * An array of settings that PHPCS and PHPCBF accept. * @@ -191,21 +215,6 @@ class Config */ private $cliArgs = []; - /** - * A list of valid generators. - * - * {@internal Once support for PHP < 5.6 is dropped, this property should be refactored into a - * class constant.} - * - * @var array Keys are the lowercase version of the generator name, while values - * are the associated PHP generator class. - */ - private $validGenerators = [ - 'text' => 'Text', - 'html' => 'HTML', - 'markdown' => 'Markdown', - ]; - /** * Command line values that the user has supplied directly. * @@ -429,15 +438,8 @@ public function __construct(array $cliArgs=[], $dieOnUnknownArg=true) // Look for a default ruleset in the current directory or higher. $currentDir = getcwd(); - $defaultFiles = [ - '.phpcs.xml', - 'phpcs.xml', - '.phpcs.xml.dist', - 'phpcs.xml.dist', - ]; - do { - foreach ($defaultFiles as $defaultFilename) { + foreach (self::CONFIG_FILENAMES as $defaultFilename) { $default = $currentDir.DIRECTORY_SEPARATOR.$defaultFilename; if (is_file($default) === true) { $this->standards = [$default]; @@ -1250,8 +1252,8 @@ public function processLongArgument($arg, $pos) $generatorName = substr($arg, 10); $lowerCaseGeneratorName = strtolower($generatorName); - if (isset($this->validGenerators[$lowerCaseGeneratorName]) === false) { - $validOptions = implode(', ', $this->validGenerators); + if (isset(self::VALID_GENERATORS[$lowerCaseGeneratorName]) === false) { + $validOptions = implode(', ', self::VALID_GENERATORS); $validOptions = substr_replace($validOptions, ' and', strrpos($validOptions, ','), 1); $error = sprintf( '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) throw new DeepExitException($error, ExitCode::PROCESS_ERROR); } - $this->generator = $this->validGenerators[$lowerCaseGeneratorName]; + $this->generator = self::VALID_GENERATORS[$lowerCaseGeneratorName]; $this->overriddenDefaults['generator'] = true; } else if (substr($arg, 0, 9) === 'encoding=') { if (isset($this->overriddenDefaults['encoding']) === true) { diff --git a/src/Tokenizers/Tokenizer.php b/src/Tokenizers/Tokenizer.php index 4c7feae960..373e9b456e 100644 --- a/src/Tokenizers/Tokenizer.php +++ b/src/Tokenizers/Tokenizer.php @@ -18,6 +18,29 @@ abstract class Tokenizer { + /** + * List of tokens which may contain tab characters. + * + * @var array + */ + private const TOKENS_WITH_TABS = [ + T_WHITESPACE => true, + T_COMMENT => true, + T_DOC_COMMENT => true, + T_DOC_COMMENT_WHITESPACE => true, + T_DOC_COMMENT_STRING => true, + T_CONSTANT_ENCAPSED_STRING => true, + T_DOUBLE_QUOTED_STRING => true, + T_START_HEREDOC => true, + T_START_NOWDOC => true, + T_HEREDOC => true, + T_NOWDOC => true, + T_END_HEREDOC => true, + T_END_NOWDOC => true, + T_INLINE_HTML => true, + T_YIELD_FROM => true, + ]; + /** * The config data for the run. * @@ -188,24 +211,6 @@ private function createPositionMap() $encoding = $this->config->encoding; $tabWidth = $this->config->tabWidth; - $tokensWithTabs = [ - T_WHITESPACE => true, - T_COMMENT => true, - T_DOC_COMMENT => true, - T_DOC_COMMENT_WHITESPACE => true, - T_DOC_COMMENT_STRING => true, - T_CONSTANT_ENCAPSED_STRING => true, - T_DOUBLE_QUOTED_STRING => true, - T_START_HEREDOC => true, - T_START_NOWDOC => true, - T_HEREDOC => true, - T_NOWDOC => true, - T_END_HEREDOC => true, - T_END_NOWDOC => true, - T_INLINE_HTML => true, - T_YIELD_FROM => true, - ]; - $this->numTokens = count($this->tokens); for ($i = 0; $i < $this->numTokens; $i++) { $this->tokens[$i]['line'] = $lineNumber; @@ -216,7 +221,7 @@ private function createPositionMap() $length = $this->knownLengths[$this->tokens[$i]['code']]; $currColumn += $length; } else if ($tabWidth === 0 - || isset($tokensWithTabs[$this->tokens[$i]['code']]) === false + || isset(self::TOKENS_WITH_TABS[$this->tokens[$i]['code']]) === false || strpos($this->tokens[$i]['content'], "\t") === false ) { // There are no tabs in this content, or we aren't replacing them.