Skip to content

Added getter and setter of QuestionHelper to SymfonyStyle #17136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/Symfony/Component/Console/Style/SymfonyStyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Helper\Helper;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Helper\SymfonyQuestionHelper;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -355,6 +356,22 @@ public function write($messages, $newline = false, $type = self::OUTPUT_NORMAL)
$this->bufferedOutput->write($this->reduceBuffer($messages), $newline, $type);
}

/**
* @return QuestionHelper|null
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally dislike the idea of creating a getter which can return either an object or null. It will introduce additional complexity above - the need of checking if the given result is an instance of QuestionHelper

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just create a SymfonyQuestionHelper in __construct(),
so getQuestionHelp() can always return an instance of QuestionHelper

for example:

diff --git a/Style/SymfonyStyle.php b/Style/SymfonyStyle.php
index 47d7d12..3402918 100644
--- a/Style/SymfonyStyle.php
+++ b/Style/SymfonyStyle.php
@@ -47,6 +47,7 @@ class SymfonyStyle extends OutputStyle
     public function __construct(InputInterface $input, OutputInterface $output)
     {
         $this->input = $input;
+        $this->questionHelper = new SymfonyQuestionHelper();
         $this->bufferedOutput = new BufferedOutput($output->getVerbosity(), false, clone $output->getFormatter());
         // Windows cmd wraps lines as soon as the terminal width is reached, whether there are following chars or not.
         $this->lineLength = min($this->getTerminalWidth() - (int) (DIRECTORY_SEPARATOR === '\\'), self::MAX_LINE_LENGTH);
@@ -323,10 +324,6 @@ class SymfonyStyle extends OutputStyle
             $this->autoPrependBlock();
         }

-        if (!$this->questionHelper) {
-            $this->questionHelper = new SymfonyQuestionHelper();
-        }
-
         $answer = $this->questionHelper->ask($this->input, $this, $question);

         if ($this->input->isInteractive()) {
@@ -356,6 +353,14 @@ class SymfonyStyle extends OutputStyle
     }

     /**
+     * @return QuestionHelper
+     */
+    public function getQuestionHelper()
+    {
+        return $this->questionHelper;
+    }
+
+    /**
      * {@inheritdoc}
      */
     public function newLine($count = 1)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't feel like creating a SymfonyQuestionHelper object in the constructor is a good idea. There are many use cases where it's not used at all. Still, getQuestionHelper use case is very unclear for me, could you explain your point of view a little bit more please?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i want to test the action of interactively input:

  1. creating a SymfonyQuestionHelper object in the constructor for making code simpler.
  2. use getQuestionHelper() to get the SymfonyQuestionHelper object for test interactively input with mock via setInputStream()

*/
public function getQuestionHelper()
{
return $this->questionHelper;
}

/**
* @param QuestionHelper $questionHelper
*/
public function setQuestionHelper(QuestionHelper $questionHelper)
{
$this->questionHelper = $questionHelper;
}

/**
* {@inheritdoc}
*/
Expand Down