bug #39723 [Console] Handle duplicate negatable options (jderusse)

This PR was merged into the 5.3-dev branch.

Discussion
----------

[Console] Handle duplicate negatable options

| Q             | A
| ------------- | ---
| Branch?       | 5.x
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | -
| License       | MIT
| Doc PR        | -

Throws an LogicException when adding a "standard" option "--no-foo" along side a negatable "--foo"

Commits
-------

2954c9a238 Handle duplicate negatable options
This commit is contained in:
Fabien Potencier 2021-01-05 13:16:40 +01:00
commit eb3aa168f1
2 changed files with 23 additions and 0 deletions

View File

@ -233,6 +233,9 @@ class InputDefinition
if (isset($this->options[$option->getName()]) && !$option->equals($this->options[$option->getName()])) { if (isset($this->options[$option->getName()]) && !$option->equals($this->options[$option->getName()])) {
throw new LogicException(sprintf('An option named "%s" already exists.', $option->getName())); throw new LogicException(sprintf('An option named "%s" already exists.', $option->getName()));
} }
if (isset($this->negations[$option->getName()])) {
throw new LogicException(sprintf('An option named "%s" already exists.', $option->getName()));
}
if ($option->getShortcut()) { if ($option->getShortcut()) {
foreach (explode('|', $option->getShortcut()) as $shortcut) { foreach (explode('|', $option->getShortcut()) as $shortcut) {

View File

@ -242,6 +242,26 @@ class InputDefinitionTest extends TestCase
$definition->addOption($this->foo2); $definition->addOption($this->foo2);
} }
public function testAddDuplicateNetgatedOption()
{
$this->expectException('LogicException');
$this->expectExceptionMessage('An option named "no-foo" already exists.');
$definition = new InputDefinition();
$definition->addOption(new InputOption('no-foo'));
$definition->addOption(new InputOption('foo', null, InputOption::VALUE_NEGATABLE));
}
public function testAddDuplicateNetgatedReverseOption()
{
$this->expectException('LogicException');
$this->expectExceptionMessage('An option named "no-foo" already exists.');
$definition = new InputDefinition();
$definition->addOption(new InputOption('foo', null, InputOption::VALUE_NEGATABLE));
$definition->addOption(new InputOption('no-foo'));
}
public function testAddDuplicateShortcutOption() public function testAddDuplicateShortcutOption()
{ {
$this->expectException('LogicException'); $this->expectException('LogicException');