Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit06c2ece

Browse files
committed
[Console]#[Option] rules & restrictions
1 parent7c43418 commit06c2ece

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,14 @@ 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+
thrownewLogicException(\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+
thrownewLogicException(\sprintf('The option parameter "$%s" must not have a default of null.',$name));
89+
}
90+
8391
if ('bool' ===$self->typeName) {
8492
$self->mode = InputOption::VALUE_NONE;
8593
if (false !==$self->default) {

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

Lines changed: 34 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 =newCommand('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
return0;
273275
});
@@ -277,22 +279,45 @@ public function testNonBinaryInputOptions(array $parameters, array $expected)
277279

278280
publicstaticfunctionprovideNonBinaryInputOptions():\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-
publicfunctiontestInvalidOptionDefinition()
287+
/**
288+
* @dataProvider provideInvalidOptionDefinitions
289+
*/
290+
publicfunctiontestInvalidOptionDefinition(callable$code,string$expectedMessage)
286291
{
287292
$command =newCommand('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+
publicstaticfunctionprovideInvalidOptionDefinitions():\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+
}
320+
296321
publicfunctiontestInvalidRequiredValueOptionEvenWithDefault()
297322
{
298323
$command =newCommand('foo');

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp