Skip to content
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
11 changes: 3 additions & 8 deletions src/Symfony/Component/Console/Attribute/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,6 @@ public static function tryFrom(\ReflectionParameter $parameter): ?self
throw new LogicException(\sprintf('The option parameter "$%s" must not be nullable when it has a default boolean value.', $name));
}

if ('string' === $self->typeName && null === $self->default) {
throw new LogicException(\sprintf('The option parameter "$%s" must not have a default of null.', $name));
}

if ('array' === $self->typeName && $self->allowNull) {
throw new LogicException(\sprintf('The option parameter "$%s" must not be nullable.', $name));
}
Expand All @@ -97,11 +93,10 @@ public static function tryFrom(\ReflectionParameter $parameter): ?self
if (false !== $self->default) {
$self->mode |= InputOption::VALUE_NEGATABLE;
}
} elseif ('array' === $self->typeName) {
$self->mode = InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY;
} else {
$self->mode = $self->allowNull ? InputOption::VALUE_OPTIONAL : InputOption::VALUE_REQUIRED;
if ('array' === $self->typeName) {
$self->mode |= InputOption::VALUE_IS_ARRAY;
}
$self->mode = $self->allowNull && null !== $self->default ? InputOption::VALUE_OPTIONAL : InputOption::VALUE_REQUIRED;
}

if (\is_array($self->suggestedValues) && !\is_callable($self->suggestedValues) && 2 === \count($self->suggestedValues) && ($instance = $parameter->getDeclaringFunction()->getClosureThis()) && $instance::class === $self->suggestedValues[0] && \is_callable([$instance, $self->suggestedValues[1]])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ public function testCommandInputOptionDefinition()
$timeoutInputOption = $command->getDefinition()->getOption('idle');
self::assertSame('idle', $timeoutInputOption->getName());
self::assertNull($timeoutInputOption->getShortcut());
self::assertTrue($timeoutInputOption->isValueOptional());
self::assertTrue($timeoutInputOption->isValueRequired());
self::assertFalse($timeoutInputOption->isValueOptional());
Copy link
Member Author

Choose a reason for hiding this comment

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

Note this behaviour change - but I think it's ok - VALUE_OPTIONAL didn't make sense here imo.

self::assertFalse($timeoutInputOption->isNegatable());
self::assertNull($timeoutInputOption->getDefault());

Expand Down Expand Up @@ -265,11 +266,13 @@ public function testNonBinaryInputOptions(array $parameters, array $expected)
#[Option] ?string $b = '',
#[Option] array $c = [],
#[Option] array $d = ['a', 'b'],
#[Option] ?string $e = null,
) use ($expected): int {
$this->assertSame($expected[0], $a);
$this->assertSame($expected[1], $b);
$this->assertSame($expected[2], $c);
$this->assertSame($expected[3], $d);
$this->assertSame($expected[4], $e);

return 0;
});
Expand All @@ -279,9 +282,9 @@ public function testNonBinaryInputOptions(array $parameters, array $expected)

public static function provideNonBinaryInputOptions(): \Generator
{
yield 'defaults' => [[], ['', '', [], ['a', 'b']]];
yield 'with-value' => [['--a' => 'x', '--b' => 'y', '--c' => ['z'], '--d' => ['c', 'd']], ['x', 'y', ['z'], ['c', 'd']]];
yield 'without-value' => [['--b' => null], ['', null, [], ['a', 'b']]];
yield 'defaults' => [[], ['', '', [], ['a', 'b'], null]];
yield 'with-value' => [['--a' => 'x', '--b' => 'y', '--c' => ['z'], '--d' => ['c', 'd'], '--e' => 'a'], ['x', 'y', ['z'], ['c', 'd'], 'a']];
yield 'without-value' => [['--b' => null], ['', null, [], ['a', 'b'], null]];
}

/**
Expand Down Expand Up @@ -312,10 +315,6 @@ function (#[Option] ?bool $a = true) {},
function (#[Option] ?bool $a = false) {},
'The option parameter "$a" must not be nullable when it has a default boolean value.',
];
yield 'nullable-string' => [
function (#[Option] ?string $a = null) {},
'The option parameter "$a" must not have a default of null.',
];
yield 'nullable-array' => [
function (#[Option] ?array $a = null) {},
'The option parameter "$a" must not be nullable.',
Expand Down
Loading