diff --git a/src/Config.php b/src/Config.php index 28d64835e5..e060e41c1f 100644 --- a/src/Config.php +++ b/src/Config.php @@ -1141,21 +1141,24 @@ public function processLongArgument($arg, $pos) break; } - $extensions = explode(',', substr($arg, 11)); - $newExtensions = []; - foreach ($extensions as $ext) { - $slash = strpos($ext, '/'); - if ($slash !== false) { - // They specified the tokenizer too. - list($ext, $tokenizer) = explode('/', $ext); - $newExtensions[$ext] = strtoupper($tokenizer); - continue; - } + $extensionsString = substr($arg, 11); + $newExtensions = []; + if (empty($extensionsString) === false) { + $extensions = explode(',', $extensionsString); + foreach ($extensions as $ext) { + $slash = strpos($ext, '/'); + if ($slash !== false) { + // They specified the tokenizer too. + list($ext, $tokenizer) = explode('/', $ext); + $newExtensions[$ext] = strtoupper($tokenizer); + continue; + } - if (isset($this->extensions[$ext]) === true) { - $newExtensions[$ext] = $this->extensions[$ext]; - } else { - $newExtensions[$ext] = 'PHP'; + if (isset($this->extensions[$ext]) === true) { + $newExtensions[$ext] = $this->extensions[$ext]; + } else { + $newExtensions[$ext] = 'PHP'; + } } } diff --git a/tests/Core/Config/ExtensionsArgTest.php b/tests/Core/Config/ExtensionsArgTest.php new file mode 100644 index 0000000000..aa5fb6b810 --- /dev/null +++ b/tests/Core/Config/ExtensionsArgTest.php @@ -0,0 +1,128 @@ +assertSame($expected, $config->extensions); + + }//end testValidExtensions() + + + /** + * Data provider. + * + * @see self::testValidExtensions() + * + * @return array> + */ + public static function dataValidExtensions() + { + return [ + // Passing an empty extensions list is not useful, as it will result in no files being scanned, + // but that's the responsibility of the user. + 'Empty extensions list' => [ + 'passedValue' => '', + 'expected' => [], + ], + 'Single extension passed: php' => [ + 'passedValue' => 'php', + 'expected' => [ + 'php' => 'PHP', + ], + ], + // This would cause PHPCS to scan python files as PHP, which will probably cause very weird scan results, + // but that's the responsibility of the user. + 'Single extension passed: py' => [ + 'passedValue' => 'py', + 'expected' => [ + 'py' => 'PHP', + ], + ], + // This would likely result in a problem when PHPCS can't find a "PY" tokenizer class, + // but that's not our concern at this moment. Support for non-PHP tokenizers is being dropped soon anyway. + 'Single extension passed with language: py/py' => [ + 'passedValue' => 'py/py', + 'expected' => [ + 'py' => 'PY', + ], + ], + 'Multiple extensions passed: php,js,css' => [ + 'passedValue' => 'php,js,css', + 'expected' => [ + 'php' => 'PHP', + 'js' => 'JS', + 'css' => 'CSS', + ], + ], + 'Multiple extensions passed, some with language: php,inc/php,phpt/php,js' => [ + 'passedValue' => 'php,inc/php,phpt/php,js', + 'expected' => [ + 'php' => 'PHP', + 'inc' => 'PHP', + 'phpt' => 'PHP', + 'js' => 'JS', + ], + ], + 'File extensions are set case sensitively (and filtering is case sensitive too)' => [ + 'passedValue' => 'PHP,php', + 'expected' => [ + 'PHP' => 'PHP', + 'php' => 'PHP', + ], + ], + ]; + + }//end dataValidExtensions() + + + /** + * Ensure that only the first argument is processed and others are ignored. + * + * @return void + */ + public function testOnlySetOnce() + { + $config = new ConfigDouble( + [ + '--extensions=php', + '--extensions=inc,module', + ] + ); + + $this->assertSame(['php' => 'PHP'], $config->extensions); + + }//end testOnlySetOnce() + + +}//end class