diff --git a/src/Config.php b/src/Config.php index 8a94edf22c..cfbae896b9 100644 --- a/src/Config.php +++ b/src/Config.php @@ -198,6 +198,23 @@ class Config */ private static $executablePaths = []; + /** + * A list of valid generators. + * + * - Keys: lowercase version of the generator name. + * - Values: name of the generator PHP class. + * + * Note: once support for PHP < 5.6 is dropped, this property should be refactored into a class + * constant. + * + * @var array + */ + private static $validGenerators = [ + 'text' => 'Text', + 'html' => 'HTML', + 'markdown' => 'Markdown', + ]; + /** * Get the value of an inaccessible property. @@ -1233,20 +1250,21 @@ public function processLongArgument($arg, $pos) break; } - $generatorName = substr($arg, 10); - $validGenerators = [ - 'Text', - 'HTML', - 'Markdown', - ]; - - if (in_array($generatorName, $validGenerators, true) === false) { - $error = 'ERROR: "'.$generatorName.'" is not a valid generator. Valid options are: Text, HTML, and Markdown.'.PHP_EOL.PHP_EOL; - $error .= $this->printShortUsage(true); + $generatorName = substr($arg, 10); + $lowerCaseGeneratorName = strtolower($generatorName); + + if (isset(self::$validGenerators[$lowerCaseGeneratorName]) === false) { + $validOptions = implode(', ', array_values(self::$validGenerators)); + $error = sprintf( + 'ERROR: "%s" is not a valid generator. Valid options are: %s.'.PHP_EOL.PHP_EOL, + $generatorName, + $validOptions + ); + $error .= $this->printShortUsage(true); throw new DeepExitException($error, 3); } - $this->generator = $generatorName; + $this->generator = self::$validGenerators[$lowerCaseGeneratorName]; self::$overriddenDefaults['generator'] = true; } else if (substr($arg, 0, 9) === 'encoding=') { if (isset(self::$overriddenDefaults['encoding']) === true) { diff --git a/tests/Core/Config/GeneratorArgTest.php b/tests/Core/Config/GeneratorArgTest.php index 389d3e4af4..72949409e9 100644 --- a/tests/Core/Config/GeneratorArgTest.php +++ b/tests/Core/Config/GeneratorArgTest.php @@ -23,17 +23,18 @@ final class GeneratorArgTest extends TestCase /** * Ensure that the generator property is set when the parameter is passed a valid value. * - * @param string $generatorName Generator name. + * @param string $argumentValue Generator name passed in the command line. + * @param string $expectedPropertyValue Expected value of the generator property. * * @dataProvider dataValidGeneratorNames * * @return void */ - public function testValidGenerators($generatorName) + public function testValidGenerators($argumentValue, $expectedPropertyValue) { - $config = new ConfigDouble(["--generator=$generatorName"]); + $config = new ConfigDouble(["--generator=$argumentValue"]); - $this->assertSame($generatorName, $config->generator); + $this->assertSame($expectedPropertyValue, $config->generator); }//end testValidGenerators() @@ -48,9 +49,30 @@ public function testValidGenerators($generatorName) public static function dataValidGeneratorNames() { return [ - ['Text'], - ['HTML'], - ['Markdown'], + [ + 'Text', + 'Text', + ], + [ + 'HTML', + 'HTML', + ], + [ + 'Markdown', + 'Markdown', + ], + [ + 'TEXT', + 'Text', + ], + [ + 'tEXt', + 'Text', + ], + [ + 'html', + 'HTML', + ], ]; }//end dataValidGeneratorNames() @@ -88,7 +110,7 @@ public function testOnlySetOnce() public function testInvalidGenerator($generatorName) { $exception = 'PHP_CodeSniffer\Exceptions\DeepExitException'; - $message = 'ERROR: "'.$generatorName.'" is not a valid generator. Valid options are: Text, HTML, and Markdown.'; + $message = 'ERROR: "'.$generatorName.'" is not a valid generator. Valid options are: Text, HTML, Markdown.'; if (method_exists($this, 'expectException') === true) { // PHPUnit 5+. @@ -116,7 +138,6 @@ public static function dataInvalidGeneratorNames() return [ ['InvalidGenerator'], ['Text,HTML'], - ['TEXT'], [''], ];