Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Juliette <[email protected]>
  • Loading branch information
rodrigoprimo and jrfnl committed Dec 20, 2024
1 parent 6bb661d commit 541e2ef
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 43 deletions.
46 changes: 23 additions & 23 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,21 @@ 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<string, string> 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.
*
Expand Down Expand Up @@ -198,23 +213,6 @@ 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<string, string>
*/
private static $validGenerators = [
'text' => 'Text',
'html' => 'HTML',
'markdown' => 'Markdown',
];


/**
* Get the value of an inaccessible property.
Expand Down Expand Up @@ -1253,18 +1251,20 @@ public function processLongArgument($arg, $pos)
$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,
if (isset($this->validGenerators[$lowerCaseGeneratorName]) === false) {
$lastOption = array_pop($this->validGenerators);
$validOptions = implode(', ', $this->validGenerators);
$validOptions .= ' and '.$lastOption;
$error = sprintf(
'ERROR: "%s" is not a valid generator. The following generators are supported: %s.'.PHP_EOL.PHP_EOL,
$generatorName,
$validOptions
);
$error .= $this->printShortUsage(true);
$error .= $this->printShortUsage(true);
throw new DeepExitException($error, 3);
}

$this->generator = self::$validGenerators[$lowerCaseGeneratorName];
$this->generator = $this->validGenerators[$lowerCaseGeneratorName];
self::$overriddenDefaults['generator'] = true;
} else if (substr($arg, 0, 9) === 'encoding=') {
if (isset(self::$overriddenDefaults['encoding']) === true) {
Expand Down
40 changes: 20 additions & 20 deletions tests/Core/Config/GeneratorArgTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class GeneratorArgTest extends TestCase
/**
* Ensure that the generator property is set when the parameter is passed a valid value.
*
* @param string $argumentValue Generator name passed in the command line.
* @param string $argumentValue Generator name passed on the command line.
* @param string $expectedPropertyValue Expected value of the generator property.
*
* @dataProvider dataValidGeneratorNames
Expand All @@ -49,29 +49,29 @@ public function testValidGenerators($argumentValue, $expectedPropertyValue)
public static function dataValidGeneratorNames()
{
return [
[
'Text',
'Text',
'Text generator passed' => [
'argumentValue' => 'Text',
'expectedPropertyValue' => 'Text',
],
[
'HTML',
'HTML',
'HTML generator passed' => [
'argumentValue' => 'HTML',
'expectedPropertyValue' => 'HTML',
],
[
'Markdown',
'Markdown',
'Markdown generator passed' => [
'argumentValue' => 'Markdown',
'expectedPropertyValue' => 'Markdown',
],
[
'TEXT',
'Text',
'Uppercase Text generator passed' => [
'argumentValue' => 'TEXT',
'expectedPropertyValue' => 'Text',
],
[
'tEXt',
'Text',
'Mixed case Text generator passed' => [
'argumentValue' => 'tEXt',
'expectedPropertyValue' => 'Text',
],
[
'html',
'HTML',
'Lowercase HTML generator passed' => [
'argumentValue' => 'html',
'expectedPropertyValue' => 'HTML',
],
];

Expand Down Expand Up @@ -110,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, Markdown.';
$message = 'ERROR: "'.$generatorName.'" is not a valid generator. The following generators are supported: Text, HTML and Markdown.';

if (method_exists($this, 'expectException') === true) {
// PHPUnit 5+.
Expand Down

0 comments on commit 541e2ef

Please sign in to comment.