Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config: display user-friendly message when invalid generator is passed #771

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
jrfnl marked this conversation as resolved.
Show resolved Hide resolved
*
* Note: once support for PHP < 5.6 is dropped, this property should be refactored into a class
* constant.
rodrigoprimo marked this conversation as resolved.
Show resolved Hide resolved
*
* @var array<string, string>
*/
private static $validGenerators = [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason for making this property static ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made this property static because I see its value as common for all instances of this class and not something that will change depending on the instance of the class. Do you see other reasons not to make it static?

Anyway, I had to change it to an object property to be able to manipulate its value when generating the error message (copying the value of the property before manipulating it seemed too much). See #771 (comment)

'text' => 'Text',
'html' => 'HTML',
'markdown' => 'Markdown',
];


/**
* Get the value of an inaccessible property.
Expand Down Expand Up @@ -1233,7 +1250,21 @@ public function processLongArgument($arg, $pos)
break;
}

$this->generator = substr($arg, 10);
$generatorName = substr($arg, 10);
$lowerCaseGeneratorName = strtolower($generatorName);

if (isset(self::$validGenerators[$lowerCaseGeneratorName]) === false) {
$validOptions = implode(', ', array_values(self::$validGenerators));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the array_values() really needed here ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also wondering if for readability, the last comma should be replaced with " and " ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the array_values() really needed here ?

Ops, no. It was added by mistake.

Also wondering if for readability, the last comma should be replaced with " and " ?

Yes, I think it is slightly better, and I used it in the original version. But then, when I moved to store the list of valid generators in a class property and used implode() to get the string with the name of the generators, I removed "and", assuming that the text is good enough without it, still correct and the code is slightly simpler.

As you are asking, I'm assuming you prefer the version with the "and," so I went ahead and added it. I'm not super happy with my approach using array_pop(), but I couldn't think of anything better. Please let me know if you have another suggestion.

Using array_pop() meant that I had to change Config::$validGenerators to an object property instead of a class property which is related to another comment you left.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, but this is supposed to be an unchangeable array (to become a class constant later on as discussed in previous comments) and the use of array_pop() explicitly changes the array (by reference) and would block the future change to make this a class constant. See: https://3v4l.org/QrM61

While the change of the property value would not currently be problematic (as a second --generator=... argument is ignored anyway), it does make the code less stable, so I'd like to see an alternative solution for this.

$error = sprintf(
'ERROR: "%s" is not a valid generator. Valid options are: %s.'.PHP_EOL.PHP_EOL,
rodrigoprimo marked this conversation as resolved.
Show resolved Hide resolved
$generatorName,
$validOptions
);
$error .= $this->printShortUsage(true);
throw new DeepExitException($error, 3);
}

$this->generator = self::$validGenerators[$lowerCaseGeneratorName];
self::$overriddenDefaults['generator'] = true;
} else if (substr($arg, 0, 9) === 'encoding=') {
if (isset(self::$overriddenDefaults['encoding']) === true) {
Expand Down
94 changes: 81 additions & 13 deletions tests/Core/Config/GeneratorArgTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,37 +23,59 @@ 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.
rodrigoprimo marked this conversation as resolved.
Show resolved Hide resolved
* @param string $expectedPropertyValue Expected value of the generator property.
*
* @dataProvider dataGeneratorNames
* @dataProvider dataValidGeneratorNames
*
* @return void
*/
public function testGenerators($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 testGenerators()
}//end testValidGenerators()


/**
* Data provider for testGenerators().
* Data provider for testValidGenerators().
*
* @see self::testGenerators()
* @see self::testValidGenerators()
*
* @return array<int, array<string>>
*/
public static function dataGeneratorNames()
public static function dataValidGeneratorNames()
{
return [
jrfnl marked this conversation as resolved.
Show resolved Hide resolved
['Text'],
['HTML'],
['Markdown'],
[
'Text',
'Text',
],
[
'HTML',
'HTML',
],
[
'Markdown',
'Markdown',
],
[
'TEXT',
'Text',
],
[
'tEXt',
'Text',
],
[
'html',
'HTML',
],
];

}//end dataGeneratorNames()
}//end dataValidGeneratorNames()


/**
Expand All @@ -76,4 +98,50 @@ public function testOnlySetOnce()
}//end testOnlySetOnce()


/**
* Ensure that an exception is thrown for an invalid generator.
*
* @param string $generatorName Generator name.
*
* @dataProvider dataInvalidGeneratorNames
*
* @return void
*/
public function testInvalidGenerator($generatorName)
{
$exception = 'PHP_CodeSniffer\Exceptions\DeepExitException';
$message = 'ERROR: "'.$generatorName.'" is not a valid generator. Valid options are: Text, HTML, Markdown.';

if (method_exists($this, 'expectException') === true) {
// PHPUnit 5+.
$this->expectException($exception);
$this->expectExceptionMessage($message);
} else {
// PHPUnit 4.
$this->setExpectedException($exception, $message);
}

new ConfigDouble(["--generator={$generatorName}"]);

}//end testInvalidGenerator()


/**
* Data provider for testInvalidGenerator().
*
* @see self::testInvalidGenerator()
*
* @return array<int, array<string>>
*/
public static function dataInvalidGeneratorNames()
{
return [
['InvalidGenerator'],
['Text,HTML'],
[''],
];

}//end dataInvalidGeneratorNames()


}//end class
Loading