8000 Implements Option/ArgumentInput::complete() for openess · symfony/symfony@4f9c779 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4f9c779

Browse files
committed
Implements Option/ArgumentInput::complete() for openess
1 parent 373785a commit 4f9c779

File tree

6 files changed

+85
-44
lines changed

6 files changed

+85
-44
lines changed

src/Symfony/Component/Console/Command/Command.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,9 +321,9 @@ public function complete(CompletionInput $input, CompletionSuggestions $suggesti
321321
{
322322
$definition = $this->getDefinition();
323323
if (CompletionInput::TYPE_OPTION_VALUE === $input->getCompletionType() && $definition->hasOption($input->getCompletionName())) {
324-
$suggestions->suggestValues($definition->getOption($input->getCompletionName())->getSuggestedValues($input));
324+
$definition->getOption($input->getCompletionName())->complete($input, $suggestions);
325325
} elseif (CompletionInput::TYPE_ARGUMENT_VALUE === $input->getCompletionType() && $definition->hasArgument($input->getCompletionName())) {
326-
$suggestions->suggestValues($definition->getArgument($input->getCompletionName())->getSuggestedValues($input));
326+
$definition->getArgument($input->getCompletionName())->complete($input, $suggestions);
327327
}
328328
}
329329

src/Symfony/Component/Console/Input/InputArgument.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Console\Input;
1313

1414
use Symfony\Component\Console\Completion\CompletionInput;
15+
use Symfony\Component\Console\Completion\CompletionSuggestions;
1516
use Symfony\Component\Console\Exception\InvalidArgumentException;
1617
use Symfony\Component\Console\Exception\LogicException;
1718

@@ -115,17 +116,25 @@ public function getDefault(): string|bool|int|float|array|null
115116
return $this->default;
116117
}
117118

119+
public function hasCompletion(): bool
120+
{
121+
return [] !== $this->suggestedValues;
122+
}
123+
118124
/**
119-
* Returns suggested values for input completion.
125+
* Adds suggestions to $suggestions for the current completion input.
126+
*
127+
* @see Command::complete()
120128
*/
121-
public function getSuggestedValues(CompletionInput $input): array
129+
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
122130
{
123131
$values = $this->suggestedValues;
124132
if ($values instanceof \Closure && !\is_array($values = $values($input))) {
125-
throw new LogicException(sprintf('Closure for argument "%s" must return an array. Got "%s".', $this->name, get_debug_type($values)));
133+
throw new LogicException(sprintf('Closure for argument "%s" must return an array or null. Got "%s".', $this->name, get_debug_type($values)));
134+
}
135+
if ($values) {
136+
$suggestions->suggestValues($values);
126137
}
127-
128-
return $values;
129138
}
130139

131140
/**

src/Symfony/Component/Console/Input/InputOption.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111

1212
namespace Symfony\Component\Console\Input;
1313

14+
use Symfony\Component\Console\Command\Command;
1415
use Symfony\Component\Console\Completion\CompletionInput;
16+
use Symfony\Component\Console\Completion\CompletionSuggestions;
1517
use Symfony\Component\Console\Exception\InvalidArgumentException;
1618
use Symfony\Component\Console\Exception\LogicException;
1719

@@ -201,24 +203,32 @@ public function getDefault(): string|bool|int|float|array|null
201203
}
202204

203205
/**
204-
* Returns suggested values for input completion.
206+
* Returns the description text.
205207
*/
206-
public function getSuggestedValues(CompletionInput $input): array
208+
public function getDescription(): string
207209
{
208-
$values = $this->suggestedValues;
209-
if ($values instanceof \Closure && !\is_array($values = $values($input))) {
210-
throw new LogicException(sprintf('Closure for option "%s" must return an array. Got "%s".', $this->name, get_debug_type($values)));
211-
}
210+
return $this->description;
211+
}
212212

213-
return $values;
213+
public function hasCompletion(): bool
214+
{
215+
return [] !== $this->suggestedValues;
214216
}
215217

216218
/**
217-
* Returns the description text.
219+
* Adds suggestions to $suggestions for the current completion input.
220+
*
221+
* @see Command::complete()
218222
*/
219-
public function getDescription(): string
223+
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
220224
{
221-
return $this->description;
225+
$values = $this->suggestedValues;
226+
if ($values instanceof \Closure && !\is_array($values = $values($input))) {
227+
throw new LogicException(sprintf('Closure for option "%s" must return an array or null. Got "%s".', $this->name, get_debug_type($values)));
228+
}
229+
if ($values) {
230+
$suggestions->suggestValues($values);
231+
}
222232
}
223233

224234
/**

src/Symfony/Component/Console/Tests/Command/CommandTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use Symfony\Component\Console\Application;
1717
use Symfony\Component\Console\Attribute\AsCommand;
1818
use Symfony\Component\Console\Command\Command;
19-
use Symfony\Component\Console\Completion\CompletionInput;
2019
use Symfony\Component\Console\Exception\InvalidOptionException;
2120
use Symfony\Component\Console\Helper\FormatterHelper;
2221
use Symfony\Component\Console\Input\InputArgument;
@@ -96,7 +95,7 @@ public function testAddArgumentFull()
9695
$argument = $command->getDefinition()->getArgument('foo');
9796
$this->assertSame('Description', $argument->getDescription());
9897
$this->assertSame('default', $argument->getDefault());
99-
$this->assertSame(['a', 'b'], $argument->getSuggestedValues(new CompletionInput()));
98+
$this->assertTrue($argument->hasCompletion());
10099
}
101100

102101
public function testAddOption()
@@ -115,7 +114,7 @@ public function testAddOptionFull()
115114
$this->assertSame('f', $option->getShortcut());
116115
$this->assertSame('Description', $option->getDescription());
117116
$this->assertSame('default', $option->getDefault());
118-
$this->assertSame(['a', 'b'], $option->getSuggestedValues(new CompletionInput()));
117+
$this->assertTrue($option->hasCompletion());
119118
}
120119

121120
public function testSetHidden()

src/Symfony/Component/Console/Tests/Input/InputArgumentTest.php

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Console\Completion\CompletionInput;
16+
use Symfony\Component\Console\Completion\CompletionSuggestions;
17+
use Symfony\Component\Console\Completion\Suggestion;
1618
use Symfony\Component\Console\Exception\LogicException;
1719
use Symfony\Component\Console\Input\InputArgument;
1820

@@ -98,22 +100,32 @@ public function testSetDefaultWithArrayArgument()
98100
$argument->setDefault('default');
99101
}
100102

101-
public function testGetSuggestedValues()
103+
public function testCompleteArray()
102104
{
103105
$values = ['foo', 'bar'];
104-
$option = new InputArgument('foo', null, '', null, $values);
105-
$this->assertSame($values, $option->getSuggestedValues(new CompletionInput()));
106+
$argument = new InputArgument('foo', null, '', null, $values);
107+
$this->assertTrue($argument->hasCompletion());
108+
$suggestions = new CompletionSuggestions();
109+
$argument->complete(new CompletionInput(), $suggestions);
110+
$this->assertSame($values, array_map(fn (Suggestion $suggestion) => $suggestion->getValue(), $suggestions->getValueSuggestions()));
111+
}
106112

107-
$option = new InputArgument('foo', null, '', null, fn (CompletionInput $input): array => $values);
108-
$this->assertSame($values, $option->getSuggestedValues(new CompletionInput()));
113+
public function testCompleteClosure()
114+
{
115+
$values = ['foo', 'bar'];
116+
$argument = new InputArgument('foo', null, '', null, fn (CompletionInput $input): array => $values);
117+
$this->assertTrue($argument->hasCompletion());
118+
$suggestions = new CompletionSuggestions();
119+
$argument->complete(new CompletionInput(), $suggestions);
120+
$this->assertSame($values, array_map(fn (Suggestion $suggestion) => $suggestion->getValue(), $suggestions->getValueSuggestions()));
109121
}
110122

111-
public function testGetSuggestedValuesClosureReturnIncorrectType()
123+
public function testCompleteClosureReturnIncorrectType()
112124
{
113125
$this->expectException(LogicException::class);
114-
$this->expectExceptionMessage('Closure for argument "foo" must return an array. Got "string".');
126+
$this->expectExceptionMessage('Closure for argument "foo" must return an array or null. Got "string".');
115127

116-
$option = new InputArgument('foo', InputArgument::OPTIONAL, '', null, fn (CompletionInput $input) => 'invalid');
117-
$option->getSuggestedValues(new CompletionInput());
128+
$argument = new InputArgument('foo', InputArgument::OPTIONAL, '', null, fn (CompletionInput $input) => 'invalid');
129+
$argument->complete(new CompletionInput(), new CompletionSuggestions());
118130
}
119131
}

src/Symfony/Component/Console/Tests/Input/InputOptionTest.php

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Console\Completion\CompletionInput;
16+
use Symfony\Component\Console\Completion\CompletionSuggestions;
17+
use Symfony\Component\Console\Completion\Suggestion;
1618
use Symfony\Component\Console\Exception\LogicException;
1719
use Symfony\Component\Console\Input\InputOption;
1820

@@ -197,31 +199,40 @@ public function testEquals()
197199
$this->assertFalse($option->equals($option2));
198200
}
199201

200-
public function testGetSuggestedValues()
202+
public function testSuggestedValuesErrorIfNoValue()
201203
{
202-
$values = ['foo', 'bar'];
203-
$option = new InputOption('foo', null, InputOption::VALUE_OPTIONAL, '', null, $values);
204-
$this->assertSame($values, $option->getSuggestedValues(new CompletionInput()));
204+
$this->expectException(LogicException::class);
205+
$this->expectExceptionMessage('Cannot set suggested values if the option does not accept a value.');
205206

206-
$option = new InputOption('foo', null, InputOption::VALUE_OPTIONAL, '', null, fn (CompletionInput $input): array => $values);
207-
$this->assertSame($values, $option->getSuggestedValues(new CompletionInput()));
207+
new InputOption('foo', null, InputOption::VALUE_NONE, '', null, ['foo']);
208208
}
209209

210-
public function testGetSuggestedValuesClosureReturnIncorrectType()
210+
public function testCompleteArray()
211211
{
212-
$this->expectException(LogicException::class);
213-
$this->expectExceptionMessage('Closure for option "foo" must return an array. Got "string".');
212+
$values = ['foo', 'bar'];
213+
$option = new InputOption('foo', null, InputOption::VALUE_OPTIONAL, '', null, $values);
214+
$this->assertTrue($option->hasCompletion());
215+
$suggestions = new CompletionSuggestions();
216+
$option->complete(new CompletionInput(), $suggestions);
217+
$this->assertSame($values, array_map(fn (Suggestion $suggestion) => $suggestion->getValue(), $suggestions->getValueSuggestions()));
218+
}
214219

215-
$option = new InputOption('foo', null, InputOption::VALUE_OPTIONAL, '', null, fn (CompletionInput $input) => 'invalid');
216-
$option->getSuggestedValues(new CompletionInput());
220+
public function testCompleteClosure()
221+
{
222+
$values = ['foo', 'bar'];
223+
$option = new InputOption('foo', null, InputOption::VALUE_OPTIONAL, '', null, fn (CompletionInput $input): array => $values);
224+
$this->assertTrue($option->hasCompletion());
225+
$suggestions = new CompletionSuggestions();
226+
$option->complete(new CompletionInput(), $suggestions);
227+
$this->assertSame($values, array_map(fn (Suggestion $suggestion) => $suggestion->getValue(), $suggestions->getValueSuggestions()));
217228
}
218229

219-
public function testSuggestedValuesErrorIfNoValue()
230+
public function testCompleteClosureReturnIncorrectType()
220231
{
221232
$this->expectException(LogicException::class);
222-
$this->expectExceptionMessage('Cannot set suggested values if the option does not accept a value.');
233+
$this->expectExceptionMessage('Closure for option "foo" must return an array or null. Got "string".');
223234

224-
$option = new InputOption('foo', null, InputOption::VALUE_NONE, '', null, ['foo']);
225-
$option->getSuggestedValues(new CompletionInput());
235+
$option = new InputOption('foo', null, InputOption::VALUE_OPTIONAL, '', null, fn (CompletionInput $input) => 'invalid');
236+
$option->complete(new CompletionInput(), new CompletionSuggestions());
226237
}
227238
}

0 commit comments

Comments
 (0)
0