Skip to content

Commit aaef070

Browse files
committed
bug #54576 [Console] Better error handling when misuse of ArgvInput with arrays (symfonyaml)
This PR was squashed before being merged into the 7.2 branch. Discussion ---------- [Console] Better error handling when misuse of `ArgvInput` with arrays | Q | A | ------------- | --- | Branch? | 7.1 | Bug fix? | no | New feature? | no | Deprecations? | no | Issues | Fix #53836 | License | MIT ### Issue When we don't use `ArgvInput` correclty, and use array in $argv values, it returns different PHP fatal errors. See all details and how to reproduce it in the issue symfony/symfony#53836 ### Solution In this PR - Add some DX with an exception explaining the problem, to avoid PHP fatal errors - Add tests** _____ Note : Old PR #54147 was targeting 5.4, see [this comment](symfony/symfony#54147 (comment)) for more details Commits ------- 6f64cf4f80 [Console] Better error handling when misuse of `ArgvInput` with arrays
2 parents 9b008f2 + a1b3df8 commit aaef070

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

Input/ArgvInput.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ public function __construct(?array $argv = null, ?InputDefinition $definition =
4949
{
5050
$argv ??= $_SERVER['argv'] ?? [];
5151

52+
foreach ($argv as $arg) {
53+
if (!\is_scalar($arg) && !$arg instanceof \Stringable) {
54+
throw new RuntimeException(sprintf('Argument values expected to be all scalars, got "%s".', get_debug_type($arg)));
55+
}
56+
}
57+
5258
// strip the application name
5359
array_shift($argv);
5460

Tests/Input/ArgvInputTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,11 @@ public static function provideInvalidInput(): array
324324
new InputDefinition([new InputArgument('name', InputArgument::REQUIRED)]),
325325
'Too many arguments, expected arguments "name".',
326326
],
327+
[
328+
['cli.php', ['array']],
329+
new InputDefinition(),
330+
'Argument values expected to be all scalars, got "array".',
331+
],
327332
];
328333
}
329334

0 commit comments

Comments
 (0)