Skip to content

Commit 6bb661d

Browse files
committed
Ensures the generator CLI parameter is handled case-insensitively
1 parent 3b6c8c6 commit 6bb661d

File tree

2 files changed

+59
-20
lines changed

2 files changed

+59
-20
lines changed

src/Config.php

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,23 @@ class Config
198198
*/
199199
private static $executablePaths = [];
200200

201+
/**
202+
* A list of valid generators.
203+
*
204+
* - Keys: lowercase version of the generator name.
205+
* - Values: name of the generator PHP class.
206+
*
207+
* Note: once support for PHP < 5.6 is dropped, this property should be refactored into a class
208+
* constant.
209+
*
210+
* @var array<string, string>
211+
*/
212+
private static $validGenerators = [
213+
'text' => 'Text',
214+
'html' => 'HTML',
215+
'markdown' => 'Markdown',
216+
];
217+
201218

202219
/**
203220
* Get the value of an inaccessible property.
@@ -1233,20 +1250,21 @@ public function processLongArgument($arg, $pos)
12331250
break;
12341251
}
12351252

1236-
$generatorName = substr($arg, 10);
1237-
$validGenerators = [
1238-
'Text',
1239-
'HTML',
1240-
'Markdown',
1241-
];
1242-
1243-
if (in_array($generatorName, $validGenerators, true) === false) {
1244-
$error = 'ERROR: "'.$generatorName.'" is not a valid generator. Valid options are: Text, HTML, and Markdown.'.PHP_EOL.PHP_EOL;
1245-
$error .= $this->printShortUsage(true);
1253+
$generatorName = substr($arg, 10);
1254+
$lowerCaseGeneratorName = strtolower($generatorName);
1255+
1256+
if (isset(self::$validGenerators[$lowerCaseGeneratorName]) === false) {
1257+
$validOptions = implode(', ', array_values(self::$validGenerators));
1258+
$error = sprintf(
1259+
'ERROR: "%s" is not a valid generator. Valid options are: %s.'.PHP_EOL.PHP_EOL,
1260+
$generatorName,
1261+
$validOptions
1262+
);
1263+
$error .= $this->printShortUsage(true);
12461264
throw new DeepExitException($error, 3);
12471265
}
12481266

1249-
$this->generator = $generatorName;
1267+
$this->generator = self::$validGenerators[$lowerCaseGeneratorName];
12501268
self::$overriddenDefaults['generator'] = true;
12511269
} else if (substr($arg, 0, 9) === 'encoding=') {
12521270
if (isset(self::$overriddenDefaults['encoding']) === true) {

tests/Core/Config/GeneratorArgTest.php

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,18 @@ final class GeneratorArgTest extends TestCase
2323
/**
2424
* Ensure that the generator property is set when the parameter is passed a valid value.
2525
*
26-
* @param string $generatorName Generator name.
26+
* @param string $argumentValue Generator name passed in the command line.
27+
* @param string $expectedPropertyValue Expected value of the generator property.
2728
*
2829
* @dataProvider dataValidGeneratorNames
2930
*
3031
* @return void
3132
*/
32-
public function testValidGenerators($generatorName)
33+
public function testValidGenerators($argumentValue, $expectedPropertyValue)
3334
{
34-
$config = new ConfigDouble(["--generator=$generatorName"]);
35+
$config = new ConfigDouble(["--generator=$argumentValue"]);
3536

36-
$this->assertSame($generatorName, $config->generator);
37+
$this->assertSame($expectedPropertyValue, $config->generator);
3738

3839
}//end testValidGenerators()
3940

@@ -48,9 +49,30 @@ public function testValidGenerators($generatorName)
4849
public static function dataValidGeneratorNames()
4950
{
5051
return [
51-
['Text'],
52-
['HTML'],
53-
['Markdown'],
52+
[
53+
'Text',
54+
'Text',
55+
],
56+
[
57+
'HTML',
58+
'HTML',
59+
],
60+
[
61+
'Markdown',
62+
'Markdown',
63+
],
64+
[
65+
'TEXT',
66+
'Text',
67+
],
68+
[
69+
'tEXt',
70+
'Text',
71+
],
72+
[
73+
'html',
74+
'HTML',
75+
],
5476
];
5577

5678
}//end dataValidGeneratorNames()
@@ -88,7 +110,7 @@ public function testOnlySetOnce()
88110
public function testInvalidGenerator($generatorName)
89111
{
90112
$exception = 'PHP_CodeSniffer\Exceptions\DeepExitException';
91-
$message = 'ERROR: "'.$generatorName.'" is not a valid generator. Valid options are: Text, HTML, and Markdown.';
113+
$message = 'ERROR: "'.$generatorName.'" is not a valid generator. Valid options are: Text, HTML, Markdown.';
92114

93115
if (method_exists($this, 'expectException') === true) {
94116
// PHPUnit 5+.
@@ -116,7 +138,6 @@ public static function dataInvalidGeneratorNames()
116138
return [
117139
['InvalidGenerator'],
118140
['Text,HTML'],
119-
['TEXT'],
120141
[''],
121142
];
122143

0 commit comments

Comments
 (0)