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 f37bbbd

Browse filesBrowse files
committed
[Console] Escape default value and question in SymfonyStyle::ask()
1 parent b28cd81 commit f37bbbd
Copy full SHA for f37bbbd

File tree

Expand file treeCollapse file tree

2 files changed

+36
-23
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+36
-23
lines changed

‎src/Symfony/Component/Console/Helper/SymfonyQuestionHelper.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Helper/SymfonyQuestionHelper.php
+17-23Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\Console\Question\ConfirmationQuestion;
1818
use Symfony\Component\Console\Question\Question;
1919
use Symfony\Component\Console\Style\SymfonyStyle;
20+
use Symfony\Component\Console\Formatter\OutputFormatter;
2021

2122
/**
2223
* Symfony Style Guide compliant question helper.
@@ -52,42 +53,35 @@ public function ask(InputInterface $input, OutputInterface $output, Question $qu
5253
*/
5354
protected function writePrompt(OutputInterface $output, Question $question)
5455
{
55-
$text = $question->getQuestion();
56+
$text = OutputFormatter::escape($question->getQuestion());
5657
$default = $question->getDefault();
5758

58-
switch (true) {
59-
case null === $default:
60-
$text = sprintf(' <info>%s</info>:', $text);
61-
62-
break;
63-
64-
case $question instanceof ConfirmationQuestion:
65-
$text = sprintf(' <info>%s (yes/no)</info> [<comment>%s</comment>]:', $text, $default ? 'yes' : 'no');
66-
67-
break;
59+
if ($question instanceof ConfirmationQuestion) {
60+
$text = sprintf(' <info>%s (yes/no)</info>', $text);
61+
} else {
62+
$text = sprintf(' <info>%s</info>', $text);
63+
}
6864

69-
case $question instanceof ChoiceQuestion && $question->isMultiselect():
65+
if ($question instanceof ConfirmationQuestion) {
66+
$default = $default ? 'yes' : 'no';
67+
} elseif (null !== $default) {
68+
if ($question instanceof ChoiceQuestion && $question->isMultiselect()) {
7069
$choices = $question->getChoices();
7170
$default = explode(',', $default);
7271

7372
foreach ($default as $key => $value) {
7473
$default[$key] = $choices[trim($value)];
7574
}
7675

77-
$text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, implode(', ', $default));
78-
79-
break;
80-
81-
case $question instanceof ChoiceQuestion:
76+
$default = implode(', ', $default);
77+
} elseif ($question instanceof ChoiceQuestion && null !== $default) {
8278
$choices = $question->getChoices();
83-
$text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, $choices[$default]);
84-
85-
break;
86-
87-
default:
88-
$text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, $default);
79+
$default = $choices[$default];
80+
}
8981
}
9082

83+
$text .= null === $default ? ':' : sprintf(' [<comment>%s</comment>]:', OutputFormatter::escape($default));
84+
9185
$output->writeln($text);
9286

9387
if ($question instanceof ChoiceQuestion) {

‎src/Symfony/Component/Console/Tests/Helper/SymfonyQuestionHelperTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Tests/Helper/SymfonyQuestionHelperTest.php
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Symfony\Component\Console\Helper\HelperSet;
77
use Symfony\Component\Console\Helper\SymfonyQuestionHelper;
88
use Symfony\Component\Console\Output\StreamOutput;
9+
use Symfony\Component\Console\Question\Question;
910
use Symfony\Component\Console\Question\ChoiceQuestion;
1011

1112
/**
@@ -73,6 +74,24 @@ public function testAskChoice()
7374
$this->assertOutputContains('What is your favorite superhero? [Superman, Batman]', $output);
7475
}
7576

77+
public function testAskEscapeDefaultValue()
78+
{
79+
$helper = new SymfonyQuestionHelper();
80+
$helper->setInputStream($this->getInputStream('\\'));
81+
$helper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), new Question('Can I have a backslash?', '\\'));
82+
83+
$this->assertOutputContains('Can I have a backslash? [\]', $output);
84+
}
85+
86+
public function testAskEscapeLabel()
87+
{
88+
$helper = new SymfonyQuestionHelper();
89+
$helper->setInputStream($this->getInputStream('sure'));
90+
$helper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), new Question('Do you want a \?'));
91+
92+
$this->assertOutputContains('Do you want a \?', $output);
93+
}
94+
7695
protected function getInputStream($input)
7796
{
7897
$stream = fopen('php://memory', 'r+', false);

0 commit comments

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