Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Console] Fix issue with reserved keyword "command" as argument name #54795

Open
wants to merge 5 commits into
base: 7.2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/Console/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

* Add `ArgvInput::getRawTokens()`
* Throws `InvalidArgumentException` if an argument was created with the reserved keyword `command` as name and is not the `command name` argument.

7.0
---
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Console/Command/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ public function getNativeDefinition(): InputDefinition
*
* @return $this
*
* @throws InvalidArgumentException When argument mode is not valid
* @throws InvalidArgumentException When argument name is 'command' or mode is not valid
flkasper marked this conversation as resolved.
Show resolved Hide resolved
*/
public function addArgument(string $name, ?int $mode = null, string $description = '', mixed $default = null, array|\Closure $suggestedValues = []): static
{
Expand Down
5 changes: 4 additions & 1 deletion src/Symfony/Component/Console/Input/InputArgument.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class InputArgument
* @param string|bool|int|float|array|null $default The default value (for self::OPTIONAL mode only)
* @param array|\Closure(CompletionInput,CompletionSuggestions):list<string|Suggestion> $suggestedValues The values used for input completion
*
* @throws InvalidArgumentException When argument mode is not valid
* @throws InvalidArgumentException When argument name is 'command' or mode is not valid
flkasper marked this conversation as resolved.
Show resolved Hide resolved
*/
public function __construct(
private string $name,
Expand All @@ -59,6 +59,9 @@ public function __construct(
string|bool|int|float|array|null $default = null,
private \Closure|array $suggestedValues = [],
) {
if ('command' === $name && 'The command to execute' !== $description) {
throw new InvalidArgumentException('Reserved keyword "command" was used as argument name.');
flkasper marked this conversation as resolved.
Show resolved Hide resolved
}
if (null === $mode) {
$mode = self::OPTIONAL;
} elseif ($mode >= (self::IS_ARRAY << 1) || $mode < 1) {
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Console/Tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1303,7 +1303,7 @@ public function testAddingAlreadySetDefinitionElementData($def)
public static function getAddingAlreadySetDefinitionElementData(): array
{
return [
[new InputArgument('command', InputArgument::REQUIRED)],
[new InputArgument('command', InputArgument::REQUIRED, 'The command to execute')],
[new InputOption('quiet', '', InputOption::VALUE_NONE)],
[new InputOption('query', 'q', InputOption::VALUE_NONE)],
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ public static function provideInvalidInput(): array
],
[
['cli.php', 'acme:foo', 'bar'],
new InputDefinition([new InputArgument('command', InputArgument::REQUIRED)]),
new InputDefinition([new InputArgument('command', InputArgument::REQUIRED, 'The command to execute')]),
'No arguments expected for "acme:foo" command, got "bar"',
],
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Console\Completion\CompletionInput;
use Symfony\Component\Console\Completion\CompletionSuggestions;
use Symfony\Component\Console\Completion\Suggestion;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Exception\LogicException;
use Symfony\Component\Console\Input\InputArgument;

Expand All @@ -26,6 +27,13 @@ public function testConstructor()
$this->assertEquals('foo', $argument->getName(), '__construct() takes a name as its first argument');
}

public function testReservedArgumentName()
{
self::expectException(InvalidArgumentException::class);
self::expectExceptionMessage('Reserved keyword "command" was used as argument name.');
new InputArgument('command');
}

public function testModes()
{
$argument = new InputArgument('foo');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\Output;
use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Console\Question\Question;
Expand All @@ -30,7 +31,7 @@ class CommandTesterTest extends TestCase
protected function setUp(): void
{
$this->command = new Command('foo');
$this->command->addArgument('command');
$this->command->addArgument('command', InputArgument::REQUIRED, 'The command to execute');
$this->command->addArgument('foo');
$this->command->setCode(function ($input, $output) { $output->writeln('foo'); });

Expand Down Expand Up @@ -231,7 +232,7 @@ public function testSymfonyStyleCommandWithInputs()
public function testErrorOutput()
{
$command = new Command('foo');
$command->addArgument('command');
$command->addArgument('command', InputArgument::REQUIRED, 'The command to execute');
$command->addArgument('foo');
$command->setCode(function ($input, $output) {
$output->getErrorOutput()->write('foo');
Expand Down