8000 minor #40322 [Console] improve exception message when required argume… · symfony/symfony@c487a56 · GitHub
[go: up one dir, main page]

Skip to content

Commit c487a56

Browse files
committed
minor #40322 [Console] improve exception message when required argument is added after an optional one (marbul)
This PR was merged into the 5.3-dev branch. Discussion ---------- [Console] improve exception message when required argument is added after an optional one | Q | A | ------------- | --- | Branch? | 5.x <!-- see below --> | Bug fix? | no | New feature? | wouldn't say so, rather DX improvement <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | Fix #40302 <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT | Doc PR | n/a Hello, this is my first contribution to Symfony Framework. It's time to pull my weight. About the issue: I did improve an error message to include passed argument's name and the latest optional argument's name. ![Screenshot at 2021-02-26 23-08-10](https://user-images.githubusercontent.com/79662742/109361609-8f011e80-7889-11eb-8700-cbbd388c0109.png) An author also mentioned "But which command?", however this is shown twice as we can see on the screenshot above so I skipped that. Commits ------- 9bddbbd #40302 improve exception message when required argument is added after an optional one
2 parents 7754bed + 9bddbbd commit c487a56

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)
< 2A5E /turbo-frame>
0