From db10d0385fdc03e4ba0ff1cccbbdb957b81a78be Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Mon, 12 May 2025 15:44:05 -0400 Subject: [PATCH] [Console] `#[Option] ?string $opt = null` as `VALUE_REQUIRED` --- .../Component/Console/Attribute/Option.php | 11 +++-------- .../Tests/Command/InvokableCommandTest.php | 15 +++++++-------- 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/src/Symfony/Component/Console/Attribute/Option.php b/src/Symfony/Component/Console/Attribute/Option.php index 4aea4831e9ac6..39a4990c2a135 100644 --- a/src/Symfony/Component/Console/Attribute/Option.php +++ b/src/Symfony/Component/Console/Attribute/Option.php @@ -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)); } @@ -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]])) { diff --git a/src/Symfony/Component/Console/Tests/Command/InvokableCommandTest.php b/src/Symfony/Component/Console/Tests/Command/InvokableCommandTest.php index 88f1b78701e0a..953d0a0c401ba 100644 --- a/src/Symfony/Component/Console/Tests/Command/InvokableCommandTest.php +++ b/src/Symfony/Component/Console/Tests/Command/InvokableCommandTest.php @@ -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()); self::assertFalse($timeoutInputOption->isNegatable()); self::assertNull($timeoutInputOption->getDefault()); @@ -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; }); @@ -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]]; } /** @@ -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.', pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy