InfoMessagePrinter::hasSomeErrorMessages()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 10
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 0
crap 12
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Symplify
7
 * Copyright (c) 2016 Tomas Votruba (http://tomasvotruba.cz).
8
 */
9
10
namespace Symplify\MultiCodingStandard\Console\Output;
11
12
use Symfony\Component\Console\Formatter\OutputFormatter;
13
use Symfony\Component\Console\Output\Output;
14
use Symplify\MultiCodingStandard\PhpCsFixer\Report\DiffDataCollector;
15
use Symplify\PHP7_CodeSniffer\Console\Output\InfoMessagePrinter as PhpCodeSnifferInfoMessagePrinter;
16
use Symplify\PHP7_CodeSniffer\Report\ErrorDataCollector;
17
18
final class InfoMessagePrinter
19
{
20
    /**
21
     * @var ErrorDataCollector
22
     */
23
    private $errorDataCollector;
24
25
    /**
26
     * @var DiffDataCollector
27
     */
28
    private $diffDataCollector;
29
30
    /**
31
     * @var PhpCodeSnifferInfoMessagePrinter
32
     */
33
    private $phpCodeSnifferInfoMessagePrinter;
34
35
    /**
36
     * @var Output
37
     */
38
    private $output;
39
40
    public function __construct(
41
        ErrorDataCollector $errorDataCollector,
42
        DiffDataCollector $diffDataCollector,
43
        PhpCodeSnifferInfoMessagePrinter $phpCodeSnifferInfoMessagePrinter,
44
        Output $output
45
    ) {
46
        $this->errorDataCollector = $errorDataCollector;
47
        $this->diffDataCollector = $diffDataCollector;
48
        $this->phpCodeSnifferInfoMessagePrinter = $phpCodeSnifferInfoMessagePrinter;
49
        $this->output = $output;
50
    }
51
52
    public function hasSomeErrorMessages() : bool
53
    {
54
        if ($this->errorDataCollector->getErrorCount()) {
55
            return true;
56
        }
57
58
        if (count($this->diffDataCollector->getDiffs())) {
59
            return true;
60
        }
61
62
        return false;
63
    }
64
65
    public function printFoundErrorsStatus(bool $isFixer)
66
    {
67
        // code sniffer
68
        $this->phpCodeSnifferInfoMessagePrinter->printFoundErrorsStatus($isFixer);
69
70
        // php-cs-fixer
71
        $diffs = $this->diffDataCollector->getDiffs();
72
        $this->printDiffs($diffs);
73
    }
74
75
    /**
76
     * Used original code from PHP-CS-FIXER/CS/Console/FixCommand.php
77
     * https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/185c1758aadc942956e84accf1b24be9e2609718/Symfony/CS/Console/Command/FixCommand.php#L431-L493.
78
     */
79
    private function printDiffs(array $diffs)
80
    {
81
        $fixerDetailLine = ' (<comment>%s</comment>)';
82
83
        $i = 0;
84
        foreach ($diffs as $file => $fixResult) {
85
            $this->output->write(sprintf('%4d) %s', $i++, $file));
86
87
            if ($fixerDetailLine) {
88
                $this->output->write(sprintf($fixerDetailLine, implode(', ', $fixResult['appliedFixers'])));
89
            }
90
91
            $this->output->writeln('');
92
            $this->output->writeln('<comment>      ---------- begin diff ----------</comment>');
93
94
            $diff = implode(
95
                PHP_EOL,
96
                array_map(
97
                    function ($string) {
98
                        $string = preg_replace('/^(\+){3}/', '<info>+++</info>', $string);
99
                        $string = preg_replace('/^(\+){1}/', '<info>+</info>', $string);
100
                        $string = preg_replace('/^(\-){3}/', '<error>---</error>', $string);
101
                        $string = preg_replace('/^(\-){1}/', '<error>-</error>', $string);
102
                        $string = str_repeat(' ', 6).$string;
103
104
                        return $string;
105
                    },
106
                    explode(PHP_EOL, OutputFormatter::escape($fixResult['diff']))
107
                )
108
            );
109
110
            $this->output->writeln($diff);
111
112
            $this->output->writeln('<comment>      ---------- end diff ----------</comment>');
113
        }
114
115
        $this->output->writeln('');
116
    }
117
}
118