Skip to content

Commit 4338337

Browse files
bug symfony#46264 [Console] Better required argument check in InputArgument (jnoordsij)
This PR was squashed before being merged into the 4.4 branch. Discussion ---------- [Console] Better required argument check in InputArgument Use better check for required arguments in InputArgument when setting default value, to correctly account for combining multiple mode options | Q | A | ------------- | --- | Branch? | 4.4 <!-- see below --> | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | none <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT | Doc PR | - <!-- required for new features --> <!-- Replace this notice by a short README for your feature/bugfix. This will help reviewers and should be a good start for the documentation. Additionally (see https://symfony.com/releases): - Always add tests and ensure they pass. - Bug fixes must be submitted against the lowest maintained branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too.) - Features and deprecations must be submitted against the latest branch. - Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry - Never break backward compatibility (see https://symfony.com/bc). --> In the current implementation, when combining multiple argument modes, e.g. `REQUIRED` and `IS_ARRAY`, it is possible to also pass a default value, while this should raise an exception. This fix uses the `isRequired` method (which does a bitwise comparison) instead of a full equality match, to always reject a default value if the argument is required. Note: I am not sure if this is considered a bugfix or something that could be considered breaking backwards compatibility, as some configurations (e.g. combining the `REQUIRED` and `IS_ARRAY` modes and still passing a default value) that previously worked will break due to this. Commits ------- 89dd2bb [Console] Better required argument check in InputArgument
2 parents b368ce1 + 89dd2bb commit 4338337

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public function isArray()
9292
*/
9393
public function setDefault($default = null)
9494
{
95-
if (self::REQUIRED === $this->mode && null !== $default) {
95+
if ($this->isRequired() && null !== $default) {
9696
throw new LogicException('Cannot set a default value except for InputArgument::OPTIONAL mode.');
9797
}
9898

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,14 @@ public function testSetDefaultWithRequiredArgument()
8888
$argument->setDefault('default');
8989
}
9090

91+
public function testSetDefaultWithRequiredArrayArgument()
92+
{
93+
$this->expectException(\LogicException::class);
94+
$this->expectExceptionMessage('Cannot set a default value except for InputArgument::OPTIONAL mode.');
95+
$argument = new InputArgument('foo', InputArgument::REQUIRED | InputArgument::IS_ARRAY);
96+
$argument->setDefault([]);
97+
}
98+
9199
public function testSetDefaultWithArrayArgument()
92100
{
93101
$this->expectException(\LogicException::class);

0 commit comments

Comments
 (0)