From 9bddbbd7edf14291df2592eafc9b4bcd885052d0 Mon Sep 17 00:00:00 2001 From: marbul Date: Wed, 3 Mar 2021 18:38:02 +0100 Subject: [PATCH] #40302 improve exception message when required argument is added after an optional one --- .../Console/Input/InputDefinition.php | 22 +++++++++---------- .../Tests/Input/InputDefinitionTest.php | 6 ++--- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/Symfony/Component/Console/Input/InputDefinition.php b/src/Symfony/Component/Console/Input/InputDefinition.php index 2fcf777127..f8f8d250fa 100644 --- a/src/Symfony/Component/Console/Input/InputDefinition.php +++ b/src/Symfony/Component/Console/Input/InputDefinition.php @@ -30,8 +30,8 @@ class InputDefinition { private $arguments; private $requiredCount; - private $hasAnArrayArgument = false; - private $hasOptional; + private $lastArrayArgument; + private $lastOptionalArgument; private $options; private $negations; private $shortcuts; @@ -72,8 +72,8 @@ class InputDefinition { $this->arguments = []; $this->requiredCount = 0; - $this->hasOptional = false; - $this->hasAnArrayArgument = false; + $this->lastOptionalArgument = null; + $this->lastArrayArgument = null; $this->addArguments($arguments); } @@ -100,22 +100,22 @@ class InputDefinition throw new LogicException(sprintf('An argument with name "%s" already exists.', $argument->getName())); } - if ($this->hasAnArrayArgument) { - throw new LogicException('Cannot add an argument after an array argument.'); + if (null !== $this->lastArrayArgument) { + throw new LogicException(sprintf('Cannot add a required argument "%s" after an array argument "%s".', $argument->getName(), $this->lastArrayArgument->getName())); } - if ($argument->isRequired() && $this->hasOptional) { - throw new LogicException('Cannot add a required argument after an optional one.'); + if ($argument->isRequired() && null !== $this->lastOptionalArgument) { + throw new LogicException(sprintf('Cannot add a required argument "%s" after an optional one "%s".', $argument->getName(), $this->lastOptionalArgument->getName())); } if ($argument->isArray()) { - $this->hasAnArrayArgument = true; + $this->lastArrayArgument = $argument; } if ($argument->isRequired()) { ++$this->requiredCount; } else { - $this->hasOptional = true; + $this->lastOptionalArgument = $argument; } $this->arguments[$argument->getName()] = $argument; @@ -172,7 +172,7 @@ class InputDefinition */ public function getArgumentCount() { - return $this->hasAnArrayArgument ? \PHP_INT_MAX : \count($this->arguments); + return null !== $this->lastArrayArgument ? \PHP_INT_MAX : \count($this->arguments); } /** diff --git a/src/Symfony/Component/Console/Tests/Input/InputDefinitionTest.php b/src/Symfony/Component/Console/Tests/Input/InputDefinitionTest.php index fdc64c22c8..11c6ae165e 100644 --- a/src/Symfony/Component/Console/Tests/Input/InputDefinitionTest.php +++ b/src/Symfony/Component/Console/Tests/Input/InputDefinitionTest.php @@ -101,7 +101,7 @@ class InputDefinitionTest extends TestCase public function testArrayArgumentHasToBeLast() { $this->expectException(\LogicException::class); - $this->expectExceptionMessage('Cannot add an argument after an array argument.'); + $this->expectExceptionMessage('Cannot add a required argument "anotherbar" after an array argument "fooarray".'); $this->initializeArguments(); $definition = new InputDefinition(); @@ -111,9 +111,9 @@ class InputDefinitionTest extends TestCase public function testRequiredArgumentCannotFollowAnOptionalOne() { - $this->expectException(\LogicException::class); - $this->expectExceptionMessage('Cannot add a required argument after an optional one.'); $this->initializeArguments(); + $this->expectException(\LogicException::class); + $this->expectExceptionMessage(sprintf('Cannot add a required argument "%s" after an optional one "%s".', $this->foo2->getName(), $this->foo->getName())); $definition = new InputDefinition(); $definition->addArgument($this->foo);