8000 [Console] Add docblocks and unit tests to QuestionHelper · symfony/symfony@336bba2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 336bba2

Browse files
committed
[Console] Add docblocks and unit tests to QuestionHelper
1 parent c413f89 commit 336bba2

File tree

6 files changed

+483
-49
lines changed

6 files changed

+483
-49
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Console\Descriptor\TextDescriptor;
1515
use Symfony\Component\Console\Descriptor\XmlDescriptor;
16+
use Symfony\Component\Console\Helper\QuestionHelper;
1617
use Symfony\Component\Console\Input\InputInterface;
1718
use Symfony\Component\Console\Input\ArgvInput;
1819
use Symfony\Component\Console\Input\ArrayInput;
@@ -968,6 +969,7 @@ protected function getDefaultHelperSet()
968969
new DialogHelper(),
969970
new ProgressHelper(),
970971
new TableHelper(),
972+
new QuestionHelper(),
971973
));
972974
}
973975

src/Symfony/Component/Console/Helper/QuestionHelper.php

Lines changed: 65 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111

1212
namespace Symfony\Component\Console\Helper;
1313

14-
use Symfony\Component\Console\Helper\Helper;
14+
use Symfony\Component\Console\Input\InputInterface;
1515
use Symfony\Component\Console\Output\OutputInterface;
1616
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
17-
use Symfony\Component\Console\Dialog\Question;
18-
use Symfony\Component\Console\Dialog\ChoiceQuestion;
17+
use Symfony\Component\Console\Question\Question;
18+
use Symfony\Component\Console\Question\ChoiceQuestion;
1919

2020
/**
21-
* The Question class provides helpers to interact with the user.
21+
* The QuestionHelper class provides helpers to interact with the user.
2222
*
2323
* @author Fabien Potencier <fabien@symfony.com>
2424
*/
@@ -36,22 +36,27 @@ public function __construct()
3636
/**
3737
* Asks a question to the user.
3838
*
39-
* @param OutputInterface $output An Output instance
39+
* @param InputInterface $input An InputInterface instance
40+
* @param OutputInterface $output An OutputInterface instance
4041
* @param Question $question The question to ask
4142
*
4243
* @return string The user answer
4344
*
4445
* @throws \RuntimeException If there is no data to read in the input stream
4546
*/
46-
public function ask(OutputInterface $output, Question $question)
47+
public function ask(InputInterface $input, OutputInterface $output, Question $question)
4748
{
48-
$that = $this;
49+
if (!$input->isInteractive()) {
50+
return $question->getDefault();
51+
}
4952

5053
if (!$question->getValidator()) {
51-
return $that->doAsk($output, $question);
54+
return $this->doAsk($output, $question);
5255
}
5356

54-
$interviewer = function() use ($output, $question, $that) {
57+
$that = $this;
58+
59+
$interviewer = function () use ($output, $question, $that) {
5560
return $that->doAsk($output, $question);
5661
};
5762

@@ -64,23 +69,48 @@ public function ask(OutputInterface $output, Question $question)
6469
* This is mainly useful for testing purpose.
6570
*
6671
* @param resource $stream The input stream
72+
*
73+
* @throws \InvalidArgumentException In case the stream is not a resource
6774
*/
6875
public function setInputStream($stream)
6976
{
77+
if (!is_resource($stream)) {
78+
throw new \InvalidArgumentException('Input stream must be a valid resource.');
79+
}
80+
7081
$this->inputStream = $stream;
7182
}
7283

7384
/**
7485
* Returns the helper's input stream
7586
*
76-
* @return F438 string
87+
* @return resource
7788
*/
7889
public function getInputStream()
7990
{
8091
return $this->inputStream;
8192
}
8293

83-
private function doAsk($output, $question)
94+
/**
95+
* {@inheritdoc}
96+
*/
97+
public function getName()
98+
{
99+
return 'question';
100+
}
101+
102+
/**
103+
* Asks the question to the user.
104+
*
105+
* @param OutputInterface $output
106+
* @param Question $question
107+
*
108+
* @return bool|mixed|null|string
109+
*
110+
* @throws \Exception
111+
* @throws \RuntimeException
112+
*/
113+
private function doAsk(OutputInterface $output, Question $question)
84114
{
85115
$message = $question->getQuestion();
86116
if ($question instanceof ChoiceQuestion) {
@@ -98,12 +128,12 @@ private function doAsk($output, $question)
98128

99129
$output->write($message);
100130

101-
$autocomplete = $question->getAutocompleter();
131+
$autocomplete = $question->getAutocompleterValues();
102132
if (null === $autocomplete || !$this->hasSttyAvailable()) {
103133
$ret = false;
104134
if ($question->isHidden()) {
105135
try {
106-
$ret = trim($this->askHiddenResponse($output, $question));
136+
$ret = trim($this->getHiddenResponse($output));
107137
} catch (\RuntimeException $e) {
108138
if (!$question->isHiddenFallback()) {
109139
throw $e;
@@ -119,7 +149,7 @@ private function doAsk($output, $question)
119149
$ret = trim($ret);
120150
}
121151
} else {
122-
$ret = $this->autocomplete($output, $question);
152+
$ret = trim($this->autocomplete($output, $question));
123153
}
124154

125155
$ret = strlen($ret) > 0 ? $ret : $question->getDefault();
@@ -131,9 +161,17 @@ private function doAsk($output, $question)
131161
return $ret;
132162
}
133163

164+
/**
165+
* Autocompletes a question.
166+
*
167+
* @param OutputInterface $output
168+
* @param Question $question
169+
*
170+
* @return string
171+
*/
134172
private function autocomplete(OutputInterface $output, Question $question)
135173
{
136-
$autocomplete = $question->getAutocompleter();
174+
$autocomplete = $question->getAutocompleterValues();
137175
$ret = '';
138176

139177
$i = 0;
@@ -241,16 +279,15 @@ private function autocomplete(OutputInterface $output, Question $question)
241279
}
242280

243281
/**
244-
* Asks a question to the user, the response is hidden
282+
* Gets a hidden response from user.
245283
*
246284
* @param OutputInterface $output An Output instance
247-
* @param string|array $question The question
248285
*
249286
* @return string The answer
250287
*
251-
* @throws \RuntimeException In case the fallback is deactivated and the response can not be hidden
288+
* @throws \RuntimeException In case the fallback is deactivated and the response cannot be hidden
252289
*/
253-
private function askHiddenResponse(OutputInterface $output, Question $question)
290+
private function F438 getHiddenResponse(OutputInterface $output)
254291
{
255292
if (defined('PHP_WINDOWS_VERSION_BUILD')) {
256293
$exe = __DIR__.'/../Resources/bin/hiddeninput.exe';
@@ -298,7 +335,7 @@ private function askHiddenResponse(OutputInterface $output, Question $question)
298335
return $value;
299336
}
300337

301-
throw new \RuntimeException('Unable to hide the response');
338+
throw new \RuntimeException('Unable to hide the response.');
302339
}
303340

304341
/**
@@ -315,8 +352,8 @@ private function askHiddenResponse(OutputInterface $output, Question $question)
315352
private function validateAttempts($interviewer, OutputInterface $output, Question $question)
316353
{
317354
$error = null;
318-
$attempts = $question->getMaxAttemps();
319-
while (false === $attempts || $attempts--) {
355+
$attempts = $question->getMaxAttempts();
356+
while (null === $attempts || $attempts--) {
320357
if (null !== $error) {
321358
$output->writeln($this->getHelperSet()->get('formatter')->formatBlock($error->getMessage(), 'error'));
322359
}
@@ -331,7 +368,7 @@ private function validateAttempts($interviewer, OutputInterface $output, Questio
331368
}
332369

333370
/**
334-
* Return a valid unix shell
371+
* Returns a valid unix shell.
335372
*
336373
* @return string|Boolean The valid shell name, false in case no valid shell is found
337374
*/
@@ -357,6 +394,11 @@ private function getShell()
357394
return self::$shell;
358395
}
359396

397+
/**
398+
* Returns whether Stty is available or not.
399+
*
400+
* @return Boolean
401+
*/
360402
private function hasSttyAvailable()
361403
{
362404
if (null !== self::$stty) {
@@ -367,12 +409,4 @@ private function hasSttyAvailable()
367409

368410
return self::$stty = $exitcode === 0;
369411
}
370-
371-
/**
372-
* {@inheritDoc}
373-
*/
374-
public function getName()
375-
{
376-
return 'question';
377-
}
378412
}

src/Symfony/Component/Console/Question/ChoiceQuestion.php

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,32 +29,75 @@ public function __construct($question, array $choices, $default = null)
2929

3030
$this->choices = $choices;
3131
$this->setValidator($this->getDefaultValidator());
32-
$this->setAutocompleter(array_keys($choices));
32+
$this->setAutocompleterValues(array_keys($choices));
3333
}
3434

35+
/**
36+
* Returns available choices.
37+
*
38+
* @return array
39+
*/
3540
public function getChoices()
3641
{
3742
return $this->choices;
3843
}
3944

45+
/**
46+
* Sets multiselect option.
47+
*
48+
* When multiselect is set to true, multiple choices can be answered.
49+
*
50+
* @param Boolean $multiselect
51+
*
52+
* @return ChoiceQuestion The current instance
53+
*/
4054
public function setMultiselect($multiselect)
4155
{
4256
$this->multiselect = $multiselect;
57+
$this->setValidator($this->getDefaultValidator());
58+
59+
return $this;
4360
}
4461

62+
/**
63+
* Gets the prompt for choices.
64+
*
65+
* @return string
66+
*/
4567
public function getPrompt()
4668
{
4769
return $this->prompt;
4870
}
4971

72+
/**
73+
* Sets the prompt for choices.
74+
*
75+
* @param string $prompt
76+
*
77+
* @return ChoiceQuestion The current instance
78+
*/
5079
public function setPrompt($prompt)
5180
{
5281
$this->prompt = $prompt;
82+
83+
return $this;
5384
}
5485

86+
/**
87+
* Sets the error message for invalid values.
88+
*
89+
* The error message has a string placeholder (%s) for the invalid value.
90+
*
91+
* @param string $errorMessage
92+
*
93+
* @return ChoiceQuestion The current instance
94+
*/
5595
public function setErrorMessage($errorMessage)
5696
{
5797
$this->errorMessage = $errorMessage;
98+
$this->setValidator($this->getDefaultValidator());
99+
100+
return $this;
58101
}
59102

60103
private function getDefaultValidator()
@@ -82,14 +125,14 @@ private function getDefaultValidator()
82125
if (empty($choices[$value])) {
83126
throw new \InvalidArgumentException(sprintf($errorMessage, $value));
84127
}
85-
array_push($multiselectChoices, $value);
128+
array_push($multiselectChoices, $choices[$value]);
86129
}
87130

88131
if ($multiselect) {
89132
return $multiselectChoices;
90133
}
91134

92-
return $selected;
135+
return $choices[$selected];
93136
};
94137
}
95138
}

src/Symfony/Component/Console/Question/ConfirmationQuestion.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
*/
1919
class ConfirmationQuestion extends Question
2020
{
21-
public function __construct($question, $default = false)
21+
public function __construct($question, $default = true)
2222
{
23-
parent::__construct($question, $default);
23+
parent::__construct($question, (Boolean) $default);
2424

2525
$this->setNormalizer($this->getDefaultNormalizer());
2626
}
@@ -35,10 +35,10 @@ private function getDefaultNormalizer()
3535
}
3636

3737
if (false === $default) {
38-
return $answer && 'y' == strtolower($answer[0]);
38+
return $answer && 'y' === strtolower($answer[0]);
3939
}
4040

41-
return !$answer || 'y' == strtolower($answer[0]);
41+
return !$answer || 'y' === strtolower($answer[0]);
4242
};
4343
}
4444
}

0 commit comments

Comments
 (0)
0