Skip to content

[Console] Improve #[Argument]/#[Option] exception messages #60447

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

Merged
merged 1 commit into from
May 24, 2025
Merged
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
[Console] Improve #[Argument]/#[Option] exception messages
  • Loading branch information
kbond authored and fabpot committed May 24, 2025
commit c9d7c63cbe5ef82a34a5ced5720f97a8cc43feb1
11 changes: 9 additions & 2 deletions src/Symfony/Component/Console/Attribute/Argument.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Argument
private string|bool|int|float|array|null $default = null;
private array|\Closure $suggestedValues;
private ?int $mode = null;
private string $function = '';

/**
* Represents a console command <argument> definition.
Expand All @@ -52,17 +53,23 @@ public static function tryFrom(\ReflectionParameter $parameter): ?self
return null;
}

if (($function = $parameter->getDeclaringFunction()) instanceof \ReflectionMethod) {
$self->function = $function->class.'::'.$function->name;
} else {
$self->function = $function->name;
}

$type = $parameter->getType();
$name = $parameter->getName();

if (!$type instanceof \ReflectionNamedType) {
throw new LogicException(\sprintf('The parameter "$%s" must have a named type. Untyped, Union or Intersection types are not supported for command arguments.', $name));
throw new LogicException(\sprintf('The parameter "$%s" of "%s()" must have a named type. Untyped, Union or Intersection types are not supported for command arguments.', $name, $self->function));
}

$parameterTypeName = $type->getName();

if (!\in_array($parameterTypeName, self::ALLOWED_TYPES, true)) {
throw new LogicException(\sprintf('The type "%s" of parameter "$%s" is not supported as a command argument. Only "%s" types are allowed.', $parameterTypeName, $name, implode('", "', self::ALLOWED_TYPES)));
throw new LogicException(\sprintf('The type "%s" on parameter "$%s" of "%s()" is not supported as a command argument. Only "%s" types are allowed.', $parameterTypeName, $name, $self->function, implode('", "', self::ALLOWED_TYPES)));
}

if (!$self->name) {
Expand Down
21 changes: 14 additions & 7 deletions src/Symfony/Component/Console/Attribute/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Option
private ?int $mode = null;
private string $typeName = '';
private bool $allowNull = false;
private string $function = '';

/**
* Represents a console command --option definition.
Expand Down Expand Up @@ -57,11 +58,17 @@ public static function tryFrom(\ReflectionParameter $parameter): ?self
return null;
}

if (($function = $parameter->getDeclaringFunction()) instanceof \ReflectionMethod) {
$self->function = $function->class.'::'.$function->name;
} else {
$self->function = $function->name;
}

$name = $parameter->getName();
$type = $parameter->getType();

if (!$parameter->isDefaultValueAvailable()) {
throw new LogicException(\sprintf('The option parameter "$%s" must declare a default value.', $name));
throw new LogicException(\sprintf('The option parameter "$%s" of "%s()" must declare a default value.', $name, $self->function));
}

if (!$self->name) {
Expand All @@ -76,21 +83,21 @@ public static function tryFrom(\ReflectionParameter $parameter): ?self
}

if (!$type instanceof \ReflectionNamedType) {
throw new LogicException(\sprintf('The parameter "$%s" must have a named type. Untyped or Intersection types are not supported for command options.', $name));
throw new LogicException(\sprintf('The parameter "$%s" of "%s()" must have a named type. Untyped or Intersection types are not supported for command options.', $name, $self->function));
}

$self->typeName = $type->getName();

if (!\in_array($self->typeName, self::ALLOWED_TYPES, true)) {
throw new LogicException(\sprintf('The type "%s" of parameter "$%s" is not supported as a command option. Only "%s" types are allowed.', $self->typeName, $name, implode('", "', self::ALLOWED_TYPES)));
throw new LogicException(\sprintf('The type "%s" on parameter "$%s" of "%s()" is not supported as a command option. Only "%s" types are allowed.', $self->typeName, $name, $self->function, implode('", "', self::ALLOWED_TYPES)));
}

if ('bool' === $self->typeName && $self->allowNull && \in_array($self->default, [true, false], true)) {
throw new LogicException(\sprintf('The option parameter "$%s" must not be nullable when it has a default boolean value.', $name));
throw new LogicException(\sprintf('The option parameter "$%s" of "%s()" must not be nullable when it has a default boolean value.', $name, $self->function));
}

if ($self->allowNull && null !== $self->default) {
throw new LogicException(\sprintf('The option parameter "$%s" must either be not-nullable or have a default of null.', $name));
throw new LogicException(\sprintf('The option parameter "$%s" of "%s()" must either be not-nullable or have a default of null.', $name, $self->function));
}

if ('bool' === $self->typeName) {
Expand Down Expand Up @@ -160,11 +167,11 @@ private function handleUnion(\ReflectionUnionType $type): self
$this->typeName = implode('|', array_filter($types));

if (!\in_array($this->typeName, self::ALLOWED_UNION_TYPES, true)) {
throw new LogicException(\sprintf('The union type for parameter "$%s" is not supported as a command option. Only "%s" types are allowed.', $this->name, implode('", "', self::ALLOWED_UNION_TYPES)));
throw new LogicException(\sprintf('The union type for parameter "$%s" of "%s()" is not supported as a command option. Only "%s" types are allowed.', $this->name, $this->function, implode('", "', self::ALLOWED_UNION_TYPES)));
}

if (false !== $this->default) {
throw new LogicException(\sprintf('The option parameter "$%s" must have a default value of false.', $this->name));
throw new LogicException(\sprintf('The option parameter "$%s" of "%s()" must have a default value of false.', $this->name, $this->function));
}

$this->mode = InputOption::VALUE_OPTIONAL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ public function testInvalidArgumentType()
$command->setCode(function (#[Argument] object $any) {});

$this->expectException(LogicException::class);
$this->expectExceptionMessage('The type "object" of parameter "$any" is not supported as a command argument. Only "string", "bool", "int", "float", "array" types are allowed.');

$command->getDefinition();
}
Expand All @@ -149,7 +148,6 @@ public function testInvalidOptionType()
$command->setCode(function (#[Option] ?object $any = null) {});

$this->expectException(LogicException::class);
$this->expectExceptionMessage('The type "object" of parameter "$any" is not supported as a command option. Only "string", "bool", "int", "float", "array" types are allowed.');

$command->getDefinition();
}
Expand Down Expand Up @@ -322,54 +320,44 @@ public static function provideNonBinaryInputOptions(): \Generator
/**
* @dataProvider provideInvalidOptionDefinitions
*/
public function testInvalidOptionDefinition(callable $code, string $expectedMessage)
public function testInvalidOptionDefinition(callable $code)
{
$command = new Command('foo');
$command->setCode($code);

$this->expectException(LogicException::class);
$this->expectExceptionMessage($expectedMessage);

$command->getDefinition();
}

public static function provideInvalidOptionDefinitions(): \Generator
{
yield 'no-default' => [
function (#[Option] string $a) {},
'The option parameter "$a" must declare a default value.',
function (#[Option] string $a) {}
];
yield 'nullable-bool-default-true' => [
function (#[Option] ?bool $a = true) {},
'The option parameter "$a" must not be nullable when it has a default boolean value.',
function (#[Option] ?bool $a = true) {}
];
yield 'nullable-bool-default-false' => [
function (#[Option] ?bool $a = false) {},
'The option parameter "$a" must not be nullable when it has a default boolean value.',
function (#[Option] ?bool $a = false) {}
];
yield 'invalid-union-type' => [
function (#[Option] array|bool $a = false) {},
'The union type for parameter "$a" is not supported as a command option. Only "bool|string", "bool|int", "bool|float" types are allowed.',
function (#[Option] array|bool $a = false) {}
];
yield 'union-type-cannot-allow-null' => [
function (#[Option] string|bool|null $a = null) {},
'The union type for parameter "$a" is not supported as a command option. Only "bool|string", "bool|int", "bool|float" types are allowed.',
];
yield 'union-type-default-true' => [
function (#[Option] string|bool $a = true) {},
'The option parameter "$a" must have a default value of false.',
];
yield 'union-type-default-string' => [
function (#[Option] string|bool $a = 'foo') {},
'The option parameter "$a" must have a default value of false.',
];
yield 'nullable-string-not-null-default' => [
function (#[Option] ?string $a = 'foo') {},
'The option parameter "$a" must either be not-nullable or have a default of null.',
];
yield 'nullable-array-not-null-default' => [
function (#[Option] ?array $a = []) {},
'The option parameter "$a" must either be not-nullable or have a default of null.',
];
}

Expand Down