E57B #40302 improve exception message when required argument is added afte… · symfony/symfony@9bddbbd · GitHub
[go: up one dir, main page]

Skip to content

Commit 9bddbbd

Browse files
author
marbul
committed
#40302 improve exception message when required argument is added after an optional one
1 parent f0e076a commit 9bddbbd

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

src/Symfony/Component/Console/Input/InputDefinition.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ class InputDefinition
3030
{
3131
private $arguments;
3232
private $requiredCount;
33-
private $hasAnArrayArgument = false;
34-
private $hasOptional;
33+
private $lastArrayArgument;
34+
private $lastOptionalArgument;
3535
private $options;
3636
private $negations;
3737
private $shortcuts;
@@ -72,8 +72,8 @@ public function setArguments(array $arguments = [])
7272
{
7373
$this->arguments = [];
7474
$this->requiredCount = 0;
75-
$this->hasOptional = false;
76-
$this->hasAnArrayArgument = false;
75+
$this->lastOptionalArgument = null;
76+
$this->lastArrayArgument = null;
7777
$this->addArguments($arguments);
7878
}
7979

@@ -100,22 +100,22 @@ public function addArgument(InputArgument $argument)
100100
throw new LogicException(sprintf('An argument with name "%s" already exists.', $argument->getName()));
101101
}
102102

103-
if ($this->hasAnArrayArgument) {
104-
throw new LogicException('Cannot add an argument after an array argument.');
103+
if (null !== $this->lastArrayArgument) {
104+
throw new LogicException(sprintf('Cannot add a required argument "%s" after an array argument "%s".', $argument->getName(), $this->lastArrayArgument->getName()));
105105
}
106106

107-
if ($argument->isRequired() && $this->hasOptional) {
108-
throw new LogicException('Cannot add a required argument after an optional one.');
107+
if ($argument->isRequired() && null !== $this->lastOptionalArgument) {
108+
throw new LogicException(sprintf('Cannot add a required argument "%s" after an optional one "%s".', $argument->getName(), $this->lastOptionalArgument->getName()));
109109
}
110110

111111
if ($argument->isArray()) {
112-
$this->hasAnArrayArgument = true;
112+
$this->lastArrayArgument = $argument;
113113
}
114114

115115
if ($argument->isRequired()) {
116116
++$this->requiredCount;
117117
} else {
118-
$this->hasOptional = true;
118+
$this->lastOptionalArgument = $argument;
119119
}
120120

121121
$this->arguments[$argument->getName()] = $argument;
@@ -172,7 +172,7 @@ public function getArguments()
172172
*/
173173
public function getArgumentCount()
174174
{
175-
return $this->hasAnArrayArgument ? \PHP_INT_MAX : \count($this->arguments);
175+
return null !== $this->lastArrayArgument ? \PHP_INT_MAX : \count($this->arguments);
176176
}
177177

178178
/**

src/Symfony/Component/Console/Tests/Input/InputDefinitionTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public function testArgumentsMustHaveDifferentNames()
101101
public function testArrayArgumentHasToBeLast()
102102
{
103103
$this->expectException(\LogicException::class);
104-
$this->expectExceptionMessage('Cannot add an argument after an array argument.');
104+
$this->expectExceptionMessage('Cannot add a required argument "anotherbar" after an array argument "fooarray".');
105105
$this->initializeArguments();
106106

107107
$definition = new InputDefinition();
@@ -111,9 +111,9 @@ public function testArrayArgumentHasToBeLast()
111111

112112
public function testRequiredArgumentCannotFollowAnOptionalOne()
113113
{
114-
$this->expectException(\LogicException::class);
115-
$this->expectExceptionMessage('Cannot add a required argument after an optional one.');
116114
$this->initializeArguments();
115+
$this->expectException(\LogicException::class);
116+
$this->expectExceptionMessage(sprintf('Cannot add a required argument "%s" after an optional one "%s".', $this->foo2->getName(), $this->foo->getName()));
117117

118118
$definition = new InputDefinition();
119119
$definition->addArgument($this->foo);

0 commit comments

Comments
 (0)
0