Completed
Push — master ( d2992a...8fe16f )
by Michał
06:47
created

Style::errorMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 9
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 9
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 1
crap 1
1
<?php
2
3
namespace CloverReporter\Console;
4
5
use Symfony\Component\Console\Style\SymfonyStyle;
6
use Symfony\Component\Console\Command\Command;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
class Style extends SymfonyStyle
11
{
12
    /**
13
     * @var \Symfony\Component\Console\Helper\FormatterHelper
14
     */
15
    protected $formatter;
16
17
    /**
18
     * @var int
19
     */
20
    protected $align = 20;
21
22
    /**
23
     * Style constructor.
24
     *
25
     * @param InputInterface $input
26
     * @param OutputInterface $output
27
     * @param Command $command
28
     */
29 7
    public function __construct(InputInterface $input, OutputInterface $output, Command $command)
30
    {
31 7
        $this->formatter = $command->getHelper('formatter');
32
33 7
        parent::__construct($input, $output);
34 7
    }
35
36
    /**
37
     * @param int $align
38
     * @return $this
39
     */
40
    public function setAlign($align)
41
    {
42
        $this->align = $align;
43
44
        return $this;
45
    }
46
47
    /**
48
     * @return int
49
     */
50
    public function getAlign()
51
    {
52
        return $this->align;
53
    }
54
55
    /**
56
     * @param string $section
57
     * @param string $message
58
     * @param string $style
59
     * @throws \InvalidArgumentException
60
     * @return $this
61
     */
62 7
    public function formatSection($section, $message, $style = 'info')
63
    {
64 7
        $this->writeln(
65 7
            $this->formatter->formatSection(
66 7
                $section,
67 7
                $message,
68
                $style
69 7
            )
70 7
        );
71
72 7
        return $this;
73
    }
74
75
    /**
76
     * @param string|array $messages
77
     * @param string $style
78
     * @param bool $large
79
     * @throws \InvalidArgumentException
80
     * @return $this
81
     */
82
    public function formatBlock($messages, $style, $large = false)
83
    {
84
        $this->writeln(
85
            $this->formatter->formatBlock(
86
                $messages,
87
                $style,
88
                $large
89
            )
90
        );
91
92
        return $this;
93
    }
94
95
    /**
96
     * @param array $message
97
     * @throws \InvalidArgumentException
98
     * @return $this
99
     */
100
    public function errorLine(array $message)
101
    {
102
        $this->writeln(
103
            $this->formatBlock($message, 'error')
0 ignored issues
show
Documentation introduced by
$this->formatBlock($message, 'error') is of type this<CloverReporter\Console\Style>, but the function expects a string|array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
104
        );
105
106
        return $this;
107
    }
108
109
    /**
110
     * @param string|int $strLength
111
     * @param int $align
112
     * @return string
113
     */
114 5
    public function align($strLength, $align)
115
    {
116 5
        if (is_string($strLength)) {
117 2
            $strLength = mb_strlen($strLength);
118 2
        }
119
120 5
        $newAlign = ' ';
121 5
        $spaces = $align - $strLength;
122
123 5
        for ($i = 1; $i <= $spaces; $i++) {
124 5
            $newAlign .= ' ';
125 5
        }
126
127 5
        return $newAlign;
128
    }
129
130
    /**
131
     * @param $message
132
     * @return $this
133
     * @throws \InvalidArgumentException
134
     */
135 View Code Duplication
    public function okMessage($message)
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...
136
    {
137
        $alignment = $this->align(4, $this->align);
138
        $this->write('<info>[OK]</info>');
139
        $this->write($alignment);
140
        $this->writeln($message);
141
142
        return $this;
143
    }
144
145
    /**
146
     * @param $message
147
     * @return $this
148
     * @throws \InvalidArgumentException
149
     */
150 1 View Code Duplication
    public function errorMessage($message)
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...
151
    {
152 1
        $alignment = $this->align(7, $this->align);
153 1
        $this->write('<fg=red>[ERROR]</>');
154 1
        $this->write($alignment);
155 1
        $this->writeln($message);
156
157 1
        return $this;
158
    }
159
160
    /**
161
     * @param $message
162
     * @return $this
163
     * @throws \InvalidArgumentException
164
     */
165 View Code Duplication
    public function warningMessage($message)
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...
166
    {
167
        $alignment = $this->align(9, $this->align);
168
        $this->write('<comment>[WARNING]</comment>');
169
        $this->write($alignment);
170
        $this->writeln($message);
171
172
        return $this;
173
    }
174
175
    /**
176
     * @param string $message
177
     * @return $this
178
     * @throws \InvalidArgumentException
179
     */
180
    public function note($message)
181
    {
182
        return $this->genericBlock($message, 'blue', 'note');
183
    }
184
185
    /**
186
     * @param string $message
187
     * @return $this
188
     * @throws \InvalidArgumentException
189
     */
190
    public function caution($message)
191
    {
192
        return $this->genericBlock($message, 'magenta', 'caution');
193
    }
194
195
    /**
196
     * @param string $message
197
     * @return $this
198
     * @throws \InvalidArgumentException
199
     */
200
    public function success($message)
201
    {
202
        return $this->genericBlock($message, 'green', 'success');
203
    }
204
205
    /**
206
     * @param string $message
207
     * @return $this
208
     * @throws \InvalidArgumentException
209
     */
210
    public function warning($message)
211
    {
212
        return $this->genericBlock($message, 'yellow', 'warning');
213
    }
214
215
    /**
216
     * @param string $message
217
     * @return $this
218
     * @throws \InvalidArgumentException
219
     */
220
    public function error($message)
221
    {
222
        return $this->genericBlock($message, 'red', 'error');
223
    }
224
225
    /**
226
     * @param string $message
227
     * @param string $background
228
     * @param string $type
229
     * @param int $length
230
     * @return $this
231
     * @throws \InvalidArgumentException
232
     */
233
    public function genericBlock($message, $background, $type, $length = 100)
234
    {
235
        $type = strtoupper($type);
236
        $alignment = $this->align(0, $length);
237
        $alignmentMessage = $this->align($message, $length - (mb_strlen($type) + 5));
238
239
        $this->writeln("<bg=$background>$alignment</>");
240
        $this->writeln("<fg=white;bg=$background>  [$type] $message$alignmentMessage</>");
241
        $this->writeln("<bg=$background>$alignment</>");
242
        $this->newLine();
243
244
        return $this;
245
    }
246
247
    /**
248
     * @param int $lineNumber
249
     * @param string $line
250
     * @param string $coverage
251
     * @throws \InvalidArgumentException
252
     * @return $this
253
     */
254 2
    public function formatUncoveredLine($lineNumber, $line, $coverage = '')
255
    {
256 2
        $endAlign = $this->align($line, 120);
257 2
        $this->writeln(
258 2
            "<comment>$lineNumber</comment>:"
259 2
            . ($coverage === '' ? $coverage : "<fg=red>$coverage</>:")
260 2
            . $this->align(mb_strlen($lineNumber), 6)
261 2
            . "<error>$line$endAlign</error>"
262 2
        );
263
264 2
        return $this;
265
    }
266
267
    /**
268
     * @param float $coverage
269
     * @param string $namespace
270
     * @return $this
271
     * @throws \InvalidArgumentException
272
     */
273 5
    public function formatCoverage($coverage, $namespace)
274
    {
275 5
        $coverage = round($coverage, 3);
276
277 5
        $align = $this->align(mb_strlen($coverage), 10);
278
279 5
        $this->write('  - ');
280
281 5
        $this->write($this->formatCoveragePercent($coverage));
282
283 5
        $this->write('%');
284 5
        $this->write($align);
285
286 5
        $this->writeln($namespace);
287
288 5
        return $this;
289
    }
290
291
    /**
292
     * @param int $lineNumber
293
     * @param int|string $lineCoverage
294
     * @param string $line
295
     * @throws \InvalidArgumentException
296
     * @return $this
297
     */
298 1
    public function formatCoveredLine($lineNumber, $lineCoverage, $line)
299
    {
300 1
        $lineColor = 'info';
301 1
        if ($lineCoverage === '-') {
302 1
            $lineColor = 'comment';
303 1
        }
304
305 1
        $endAlign = $this->align($line, 120);
306 1
        $this->writeln(
307 1
            "<comment>$lineNumber</comment>:"
308 1
            . "<$lineColor>$lineCoverage</$lineColor>:"
309 1
            . $this->align(mb_strlen($lineNumber), 6)
310 1
            . "<$lineColor>$line$endAlign</$lineColor>"
311 1
        );
312
313 1
        return $this;
314
    }
315
316
    /**
317
     * @param float $coverage
318
     * @return string
319
     */
320 6
    public function formatCoveragePercent($coverage)
321
    {
322 6
        switch (true) {
323 6
            case $coverage < 40:
324 4
                return "<fg=red>$coverage</>";
325
326 6
            case $coverage < 90:
327 5
                return "<comment>$coverage</comment>";
328
329 5
            default:
330 5
                return "<info>$coverage</info>";
331 5
        }
332
    }
333
}
334