Completed
Push — master ( afadea...e6376f )
by Tomáš
03:06 queued 03:01
created

InfoMessagePrinter::hasSomeErrorMessages()   A

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