Completed
Push — master ( b453a3...b1e9d6 )
by Ruud
40:05 queued 27:14
created

CommandAssistant::writeLine()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Kunstmaan\GeneratorBundle\Helper;
4
5
use Sensio\Bundle\GeneratorBundle\Command\Helper\QuestionHelper;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Symfony\Component\Console\Question\ChoiceQuestion;
9
use Symfony\Component\Console\Question\ConfirmationQuestion;
10
use Symfony\Component\Console\Question\Question;
11
use Symfony\Component\HttpKernel\Kernel;
12
13
class CommandAssistant
14
{
15
    /**
16
     * @var InputInterface
17
     */
18
    private $input;
19
20
    /**
21
     * @var OutputInterface
22
     */
23
    private $output;
24
25
    /**
26
     * @var QuestionHelper
27
     */
28
    private $questionHelper;
29
30
    /**
31
     * @var Kernel
32
     */
33
    private $kernel;
34
35
    /**
36
     * @param $input InputInterface
37
     */
38
    public function setInput(InputInterface $input)
39
    {
40
        $this->input = $input;
41
    }
42
43
    /**
44
     * @return OutputInterface
45
     */
46
    public function getOutput()
47
    {
48
        return $this->output;
49
    }
50
51
    /**
52
     * @param $output OutputInterface
53
     */
54 1
    public function setOutput(OutputInterface $output)
55
    {
56 1
        $this->output = $output;
57 1
    }
58
59
    /**
60
     * @param $questionHelper QuestionHelper
61
     */
62
    public function setQuestionHelper(QuestionHelper $questionHelper)
63
    {
64
        $this->questionHelper = $questionHelper;
65
    }
66
67
    /**
68
     * @return Kernel
69
     */
70
    public function getKernel()
71
    {
72
        return $this->kernel;
73
    }
74
75
    /**
76
     * @param $kernel Kernel
77
     */
78
    public function setKernel(Kernel $kernel)
79
    {
80
        $this->kernel = $kernel;
81
    }
82
83
    public function writeSection($text, $style = 'bg=blue;fg=white')
84
    {
85
        $this->getQuestionHelper()->writeSection($this->output, $text, $style);
86
    }
87
88
    /**
89
     * @return Questionhelper
0 ignored issues
show
Documentation introduced by
Should the return type not be QuestionHelper?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
90
     */
91
    private function getQuestionHelper()
92
    {
93
        return $this->questionHelper;
94
    }
95
96 1
    public function writeLine($text, $type = OutputInterface::OUTPUT_NORMAL)
97
    {
98 1
        $this->output->writeln($text, $type);
99 1
    }
100
101
    public function write(
102
        $text,
103
        $newLine = false,
104
        $type = OutputInterface::OUTPUT_NORMAL
105
    ) {
106
        $this->output->write($text, $newLine, $type);
107
    }
108
109 View Code Duplication
    public function writeError($message, $exit = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
110
    {
111
        $this->output->writeln(
112
            $this->getQuestionHelper()
113
                ->getHelperSet()
114
                ->get('formatter')
115
                ->formatBlock($message, 'error')
116
        );
117
        if ($exit) {
118
            exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method writeError() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
119
        }
120
    }
121
122 View Code Duplication
    public function askAndValidate(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
123
        $question,
124
        $validator,
125
        $defaultValue = null,
126
        array $autoComplete = null
127
    ) {
128
        $validationQuestion = new Question(
129
            $this->getQuestionHelper()->getQuestion($question, $defaultValue),
130
            $defaultValue
131
        );
132
        $validationQuestion->setAutocompleterValues($autoComplete);
0 ignored issues
show
Bug introduced by
It seems like $autoComplete defined by parameter $autoComplete on line 126 can also be of type array; however, Symfony\Component\Consol...etAutocompleterValues() does only seem to accept object<Symfony\Component...Question\iterable>|null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
133
        $validationQuestion->setValidator($validator);
134
135
        return $this->getQuestionHelper()->ask(
136
            $this->input,
137
            $this->output,
138
            $validationQuestion
139
        );
140
    }
141
142
    public function askConfirmation(
143
        $question,
144
        $defaultString,
145
        $separator = '?',
146
        $defaultValue = true
147
    ) {
148
        $confirmationQuestion = new ConfirmationQuestion(
149
            $this->getQuestionHelper()->getQuestion(
150
                $question,
151
                $defaultString,
152
                $separator
153
            ), $defaultValue
154
        );
155
156
        return $this->getQuestionHelper()->ask(
157
            $this->input,
158
            $this->output,
159
            $confirmationQuestion
160
        );
161
    }
162
163 View Code Duplication
    public function ask($question, $default = null, array $autoComplete = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
164
    {
165
        $askQuestion = new Question(
166
            $this->questionHelper->getQuestion($question, $default), $default
167
        );
168
        $askQuestion->setAutocompleterValues($autoComplete);
0 ignored issues
show
Bug introduced by
It seems like $autoComplete defined by parameter $autoComplete on line 163 can also be of type array; however, Symfony\Component\Consol...etAutocompleterValues() does only seem to accept object<Symfony\Component...Question\iterable>|null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
169
170
        return $this->getQuestionHelper()->ask(
171
            $this->input,
172
            $this->output,
173
            $askQuestion
174
        );
175
    }
176
177
    public function askSelect(
178
        $question,
179
        $choices,
180
        $default = null,
181
        $multiSelect = false,
182
        $errorMessage = 'Value "%s" is invalid'
183
    ) {
184
        $bundleQuestion = new ChoiceQuestion(
185
            $this->getQuestionHelper()->getQuestion($question, $default),
186
            $choices
187
        );
188
        $bundleQuestion->setErrorMessage($errorMessage);
189
        $bundleQuestion->setMultiselect($multiSelect);
190
        if ($multiSelect) {
191
            $toReturn = array();
192
            foreach ($this->getQuestionHelper()->ask(
193
                $this->input,
194
                $this->output,
195
                $bundleQuestion
196
            ) as $each) {
197
                array_push(
198
                    $toReturn,
199
                    array_search($each, $bundleQuestion->getChoices())
200
                );
201
            }
202
203
            return $toReturn;
204
        } else {
205
            $value = $this->getQuestionHelper()->ask(
206
                $this->input,
207
                $this->output,
208
                $bundleQuestion
209
            );
210
211
            return array_search($value, $bundleQuestion->getChoices());
212
        }
213
    }
214
215
    public function setOption($name, $value)
216
    {
217
        $this->input->setOption($name, $value);
218
    }
219
220
    public function hasOption($name)
221
    {
222
        return $this->input->hasOption($name);
223
    }
224
225
    public function getOption($name)
226
    {
227
        return $this->input->getOption($name);
228
    }
229
230
    public function isInteractive()
231
    {
232
        return $this->input->isInteractive();
233
    }
234
235
    public function getOptionOrDefault($option, $default = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
236
    {
237
        return $this->input->hasOption($option) ? $this->input->getOption(
238
            $option
239
        ) : $default;
240
    }
241
}
242