Skip to content

[Console] Better error handling when misuse of ArgvInput with arrays #54147

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Symfony/Component/Console/Input/ArgvInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ public function __construct(?array $argv = null, ?InputDefinition $definition =
{
$argv = $argv ?? $_SERVER['argv'] ?? [];

if (array_filter($argv, 'is_array')) {
throw new RuntimeException('Argument values expected to be all scalars, got array.');
}
Comment on lines +50 to +52
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Catching arrays feels a bit arbitrary. What about objects? Resources?

Should we instead check for values that cannot be cast to string?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like this (I'm not sure for the Stringable):

Suggested change
if (array_filter($argv, 'is_array')) {
throw new RuntimeException('Argument values expected to be all scalars, got array.');
}
foreach($argv as $arg) {
if (!is_scalar($arg) && !$arg instanceof \Stringable) {
throw new RuntimeException(sprintf('Argument values expected to be all scalars, got "%s".', get_debug_type($arg)));
}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GromNaN Applied your suggestion in the new PR #54576 targeting 7.1
Thank you


// strip the application name
array_shift($argv);

Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Console/Tests/Input/ArgvInputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,11 @@ public static function provideInvalidInput()
new InputDefinition([new InputArgument('name', InputArgument::REQUIRED)]),
'Too many arguments, expected arguments "name".',
],
[
['cli.php', ['array']],
new InputDefinition(),
'Argument values expected to be all scalars, got array.',
],
];
}

Expand Down