Handle duplicate negatable options
authorJérémy Derussé <jeremy@derusse.com>
Tue, 5 Jan 2021 11:54:42 +0000 (12:54 +0100)
committerJérémy Derussé <jeremy@derusse.com>
Tue, 5 Jan 2021 11:54:42 +0000 (12:54 +0100)
src/Symfony/Component/Console/Input/InputDefinition.php
src/Symfony/Component/Console/Tests/Input/InputDefinitionTest.php

index 56ae718..2fcf777 100644 (file)
@@ -233,6 +233,9 @@ class InputDefinition
         if (isset($this->options[$option->getName()]) && !$option->equals($this->options[$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()) {
             foreach (explode('|', $option->getShortcut()) as $shortcut) {
index 4b8f78a..d7046f7 100644 (file)
@@ -242,6 +242,26 @@ class InputDefinitionTest extends TestCase
         $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()
     {
         $this->expectException('LogicException');