Completed
Pull Request — 5.3 (#2650)
by Jeroen
21:04 queued 15:00
created

CommandAssistant::setInput()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
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;
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
            ),
154
            $defaultValue
155
        );
156
157
        return $this->getQuestionHelper()->ask(
158
            $this->input,
159
            $this->output,
160
            $confirmationQuestion
161
        );
162
    }
163
164 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...
165
    {
166
        $askQuestion = new Question(
167
            $this->questionHelper->getQuestion($question, $default),
168
            $default
169
        );
170
        $askQuestion->setAutocompleterValues($autoComplete);
0 ignored issues
show
Bug introduced by
It seems like $autoComplete defined by parameter $autoComplete on line 164 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...
171
172
        return $this->getQuestionHelper()->ask(
173
            $this->input,
174
            $this->output,
175
            $askQuestion
176
        );
177
    }
178
179
    public function askSelect(
180
        $question,
181
        $choices,
182
        $default = null,
183
        $multiSelect = false,
184
        $errorMessage = 'Value "%s" is invalid'
185
    ) {
186
        $bundleQuestion = new ChoiceQuestion(
187
            $this->getQuestionHelper()->getQuestion($question, $default),
188
            $choices
189
        );
190
        $bundleQuestion->setErrorMessage($errorMessage);
191
        $bundleQuestion->setMultiselect($multiSelect);
192
        if ($multiSelect) {
193
            $toReturn = array();
194
            foreach ($this->getQuestionHelper()->ask(
195
                $this->input,
196
                $this->output,
197
                $bundleQuestion
198
            ) as $each) {
199
                array_push(
200
                    $toReturn,
201
                    array_search($each, $bundleQuestion->getChoices())
202
                );
203
            }
204
205
            return $toReturn;
206
        } else {
207
            $value = $this->getQuestionHelper()->ask(
208
                $this->input,
209
                $this->output,
210
                $bundleQuestion
211
            );
212
213
            return array_search($value, $bundleQuestion->getChoices());
214
        }
215
    }
216
217
    public function setOption($name, $value)
218
    {
219
        $this->input->setOption($name, $value);
220
    }
221
222
    public function hasOption($name)
223
    {
224
        return $this->input->hasOption($name);
225
    }
226
227
    public function getOption($name)
228
    {
229
        return $this->input->getOption($name);
230
    }
231
232
    public function isInteractive()
233
    {
234
        return $this->input->isInteractive();
235
    }
236
237
    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...
238
    {
239
        return $this->input->hasOption($option) ? $this->input->getOption(
240
            $option
241
        ) : $default;
242
    }
243
}
244