Passed
Push — master ( ae8014...667440 )
by butschster
08:34
created

HelpersTrait::question()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Console\Traits;
6
7
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
8
use Symfony\Component\Console\Helper\Table;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Component\Console\Question\ChoiceQuestion;
12
use Symfony\Component\Console\Question\Question;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Question\Question was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Symfony\Component\Console\Style\SymfonyStyle;
14
15
/**
16
 * Trait expect command to set $output and $input scopes.
17
 */
18
trait HelpersTrait
19
{
20
    /**
21
     * OutputInterface is the interface implemented by all Output classes. Only exists when command
22
     * are being executed.
23
     * @var SymfonyStyle|null
24
     */
25
    protected ?OutputInterface $output = null;
26
27
    /**
28
     * InputInterface is the interface implemented by all input classes. Only exists when command
29
     * are being executed.
30
     */
31
    protected ?InputInterface $input = null;
32
33
    /**
34
     * Check if verbosity level of output is higher or equal to VERBOSITY_VERBOSE.
35
     */
36 14
    protected function isVerbose(): bool
37
    {
38 14
        return $this->output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE;
0 ignored issues
show
Bug introduced by
The method getVerbosity() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

38
        return $this->output->/** @scrutinizer ignore-call */ getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE;

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
39
    }
40
41
    /**
42
     * Determine if the input option is present.
43
     */
44 33
    protected function hasOption(string $name): bool
45
    {
46 33
        return $this->input->hasOption($name);
0 ignored issues
show
Bug introduced by
The method hasOption() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

46
        return $this->input->/** @scrutinizer ignore-call */ hasOption($name);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
47
    }
48
49
    /**
50
     * Input option.
51
     */
52 51
    protected function option(string $name): mixed
53
    {
54 51
        return $this->input->getOption($name);
55
    }
56
57
    /**
58
     * Determine if the input argument is present.
59
     */
60
    protected function hasArgument(string $name): bool
61
    {
62
        return $this->input->hasArgument($name);
63
    }
64
65
    /**
66
     * Input argument.
67
     */
68 53
    protected function argument(string $name): mixed
69
    {
70 53
        return $this->input->getArgument($name);
71
    }
72
73
    /**
74
     * Asks for confirmation.
75
     */
76 2
    protected function confirm(string $question, bool $default = false): bool
77
    {
78 2
        return $this->output->confirm($question, $default);
0 ignored issues
show
Bug introduced by
The method confirm() does not exist on Symfony\Component\Console\Output\OutputInterface. It seems like you code against a sub-type of Symfony\Component\Console\Output\OutputInterface such as Symfony\Component\Console\Style\OutputStyle. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

78
        return $this->output->/** @scrutinizer ignore-call */ confirm($question, $default);
Loading history...
79
    }
80
81
    /**
82
     * Asks a question.
83
     */
84
    protected function ask(string $question, string $default = null): mixed
85
    {
86
        return $this->output->ask($question, $default);
0 ignored issues
show
Bug introduced by
The method ask() does not exist on Symfony\Component\Console\Output\OutputInterface. It seems like you code against a sub-type of Symfony\Component\Console\Output\OutputInterface such as Symfony\Component\Console\Style\OutputStyle. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

86
        return $this->output->/** @scrutinizer ignore-call */ ask($question, $default);
Loading history...
87
    }
88
89
    /**
90
     * Asks a multiple choice question.
91
     */
92
    protected function choiceQuestion(
93
        string $question,
94
        array $choices,
95
        mixed $default = null,
96
        int $attempts = null,
97
        bool $multiselect = false
98
    ): mixed {
99
        $question = new ChoiceQuestion($question, $choices, $default);
100
101
        $question->setMaxAttempts($attempts)->setMultiselect($multiselect);
102
103
        return $this->output->askQuestion($question);
0 ignored issues
show
Bug introduced by
The method askQuestion() does not exist on Symfony\Component\Console\Output\OutputInterface. It seems like you code against a sub-type of Symfony\Component\Console\Output\OutputInterface such as Symfony\Component\Console\Style\SymfonyStyle. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

103
        return $this->output->/** @scrutinizer ignore-call */ askQuestion($question);
Loading history...
104
    }
105
106
    /**
107
     * Prompt the user for input but hide the answer from the console.
108
     */
109
    protected function secret(string $question, bool $fallback = true): mixed
110
    {
111
        $question = new Question($question);
112
113
        $question->setHidden(true)->setHiddenFallback($fallback);
114
115
        return $this->output->askQuestion($question);
116
    }
117
118
    /**
119
     * Write a string as information output.
120
     */
121 19
    protected function info(string $string): void
122
    {
123 19
        $this->line($string, 'info');
124
    }
125
126
    /**
127
     * Write a string as comment output.
128
     */
129 4
    protected function comment(string $string): void
130
    {
131 4
        $this->line($string, 'comment');
132
    }
133
134
    /**
135
     * Write a string as question output.
136
     */
137
    protected function question(string $string): void
138
    {
139
        $this->line($string, 'question');
140
    }
141
142
    /**
143
     * Write a string as error output.
144
     */
145 9
    protected function error(string $string): void
146
    {
147 9
        $this->line($string, 'error');
148
    }
149
150
    /**
151
     * Write a string as warning output.
152
     */
153
    protected function warning(string $string): void
154
    {
155
        if (!$this->output->getFormatter()->hasStyle('warning')) {
156
            $style = new OutputFormatterStyle('yellow');
157
            $this->output->getFormatter()->setStyle('warning', $style);
158
        }
159
160
        $this->line($string, 'warning');
161
    }
162
163
    /**
164
     * Write a string in an alert box.
165
     */
166 2
    protected function alert(string $string): void
167
    {
168 2
        $length = \mb_strlen(\strip_tags($string)) + 12;
169 2
        $stringLines = explode("\n", wordwrap($string, 300));
170
171 2
        $this->comment(\str_repeat('*', $length));
172 2
        foreach ($stringLines as $line) {
173 2
            $this->comment('*     ' . $line . '     *');
174
        }
175 2
        $this->comment(\str_repeat('*', $length));
176
177 2
        $this->newLine();
178
    }
179
180
    /**
181
     * Write a string as standard output.
182
     */
183 24
    protected function line(string $string, string $style = null): void
184
    {
185 24
        $styled = $style ? "<$style>$string</$style>" : $string;
186
187 24
        $this->writeln(
188 24
            messages: $styled
189 24
        );
190
    }
191
192
    /**
193
     * Write a blank line.
194
     */
195 21
    protected function newLine(int $count = 1): void
196
    {
197 21
        $this->output->newLine($count);
0 ignored issues
show
Bug introduced by
The method newLine() does not exist on Symfony\Component\Console\Output\OutputInterface. It seems like you code against a sub-type of Symfony\Component\Console\Output\OutputInterface such as Symfony\Component\Console\Style\OutputStyle. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

197
        $this->output->/** @scrutinizer ignore-call */ 
198
                       newLine($count);
Loading history...
198
    }
199
200
    /**
201
     * Identical to write function but provides ability to format message. Does not add new line.
202
     */
203 19
    protected function sprintf(string $format, mixed ...$args): void
204
    {
205 19
        $this->write(
206 19
            messages: \sprintf($format, ...$args),
207 19
            newline: false
208 19
        );
209
    }
210
211
    /**
212
     * Writes a message to the output.
213
     *
214
     * @param string|iterable $messages The message as an array of lines or a single string
215
     * @param bool $newline Whether to add a newline
216
     *
217
     * @throws \InvalidArgumentException When unknown output type is given
218
     */
219 34
    protected function write(string|iterable $messages, bool $newline = false): void
220
    {
221 34
        $this->output->write(messages: $messages, newline: $newline);
222
    }
223
224
    /**
225
     * Writes a message to the output and adds a newline at the end.
226
     *
227
     * @param string|iterable<mixed, string> $messages The message as an array of lines of a single string
228
     *
229
     * @throws \InvalidArgumentException When unknown output type is given
230
     */
231 55
    protected function writeln(string|iterable $messages): void
232
    {
233 55
        $this->output->writeln(messages: $messages);
234
    }
235
236
    /**
237
     * Table helper instance with configured header and pre-defined set of rows.
238
     */
239 20
    protected function table(array $headers, array $rows = [], string $style = 'default'): Table
240
    {
241 20
        $table = new Table($this->output);
242
243 20
        return $table->setHeaders($headers)->setRows($rows)->setStyle($style);
244
    }
245
}
246