Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e532750

Browse filesBrowse files
committed
feature #59602 [Console] #[Option] rules & restrictions (kbond)
This PR was merged into the 7.3 branch. Discussion ---------- [Console] `#[Option]` rules & restrictions | Q | A | ------------- | --- | Branch? | 7.3 | Bug fix? | no | New feature? | no | Deprecations? | no | Issues | n/a | License | MIT 1. Adds tests for the rules described in [this table](#59602 (comment)) 2. Adds exceptions/tests for the restrictions described in [this table](#59602 (comment)) Commits ------- 41a5462 [Console] `#[Option]` rules & restrictions
2 parents 00a39e7 + 41a5462 commit e532750
Copy full SHA for e532750

File tree

2 files changed

+50
-9
lines changed
Filter options

2 files changed

+50
-9
lines changed

‎src/Symfony/Component/Console/Attribute/Option.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Attribute/Option.php
+12Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,18 @@ public static function tryFrom(\ReflectionParameter $parameter): ?self
8080
$self->default = $parameter->getDefaultValue();
8181
$self->allowNull = $parameter->allowsNull();
8282

83+
if ('bool' === $self->typeName && $self->allowNull && \in_array($self->default, [true, false], true)) {
84+
throw new LogicException(\sprintf('The option parameter "$%s" must not be nullable when it has a default boolean value.', $name));
85+
}
86+
87+
if ('string' === $self->typeName && null === $self->default) {
88+
throw new LogicException(\sprintf('The option parameter "$%s" must not have a default of null.', $name));
89+
}
90+
91+
if ('array' === $self->typeName && $self->allowNull) {
92+
throw new LogicException(\sprintf('The option parameter "$%s" must not be nullable.', $name));
93+
}
94+
8395
if ('bool' === $self->typeName) {
8496
$self->mode = InputOption::VALUE_NONE;
8597
if (false !== $self->default) {

‎src/Symfony/Component/Console/Tests/Command/InvokableCommandTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Tests/Command/InvokableCommandTest.php
+38-9Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -261,13 +261,15 @@ public function testNonBinaryInputOptions(array $parameters, array $expected)
261261
{
262262
$command = new Command('foo');
263263
$command->setCode(function (
264-
#[Option] ?string $a = null,
265-
#[Option] ?string $b = 'b',
266-
#[Option] ?array $c = [],
264+
#[Option] string $a = '',
265+
#[Option] ?string $b = '',
266+
#[Option] array $c = [],
267+
#[Option] array $d = ['a', 'b'],
267268
) use ($expected): int {
268269
$this->assertSame($expected[0], $a);
269270
$this->assertSame($expected[1], $b);
270271
$this->assertSame($expected[2], $c);
272+
$this->assertSame($expected[3], $d);
271273

272274
return 0;
273275
});
@@ -277,22 +279,49 @@ public function testNonBinaryInputOptions(array $parameters, array $expected)
277279

278280
public static function provideNonBinaryInputOptions(): \Generator
279281
{
280-
yield 'defaults' => [[], [null, 'b', []]];
281-
yield 'with-value' => [['--a' => 'x', '--b' => 'y', '--c' => ['z']], ['x', 'y', ['z']]];
282-
yield 'without-value' => [['--a' => null, '--b' => null, '--c' => null], [null, null, null]];
282+
yield 'defaults' => [[], ['', '', [], ['a', 'b']]];
283+
yield 'with-value' => [['--a' => 'x', '--b' => 'y', '--c' => ['z'], '--d' => ['c', 'd']], ['x', 'y', ['z'], ['c', 'd']]];
284+
yield 'without-value' => [['--b' => null], ['', null, [], ['a', 'b']]];
283285
}
284286

285-
public function testInvalidOptionDefinition()
287+
/**
288+
* @dataProvider provideInvalidOptionDefinitions
289+
*/
290+
public function testInvalidOptionDefinition(callable $code, string $expectedMessage)
286291
{
287292
$command = new Command('foo');
288-
$command->setCode(function (#[Option] string $a) {});
293+
$command->setCode($code);
289294

290295
$this->expectException(LogicException::class);
291-
$this->expectExceptionMessage('The option parameter "$a" must declare a default value.');
296+
$this->expectExceptionMessage($expectedMessage);
292297

293298
$command->getDefinition();
294299
}
295300

301+
public static function provideInvalidOptionDefinitions(): \Generator
302+
{
303+
yield 'no-default' => [
304+
function (#[Option] string $a) {},
305+
'The option parameter "$a" must declare a default value.',
306+
];
307+
yield 'nullable-bool-default-true' => [
308+
function (#[Option] ?bool $a = true) {},
309+
'The option parameter "$a" must not be nullable when it has a default boolean value.',
310+
];
311+
yield 'nullable-bool-default-false' => [
312+
function (#[Option] ?bool $a = false) {},
313+
'The option parameter "$a" must not be nullable when it has a default boolean value.',
314+
];
315+
yield 'nullable-string' => [
316+
function (#[Option] ?string $a = null) {},
317+
'The option parameter "$a" must not have a default of null.',
318+
];
319+
yield 'nullable-array' => [
320+
function (#[Option] ?array $a = null) {},
321+
'The option parameter "$a" must not be nullable.',
322+
];
323+
}
324+
296325
public function testInvalidRequiredValueOptionEvenWithDefault()
297326
{
298327
$command = new Command('foo');

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.