Completed
Branch master (06cb84)
by Tomáš
06:00
created

RunCommand::configure()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 3
Bugs 1 Features 0
Metric Value
dl 0
loc 25
ccs 0
cts 23
cp 0
rs 8.8571
c 3
b 1
f 0
cc 1
eloc 18
nc 1
nop 0
crap 2
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\PHP7_CodeSniffer\Console\Command;
9
10
use Symfony\Component\Console\Command\Command;
11
use Symfony\Component\Console\Input\InputArgument;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Input\InputOption;
14
use Symfony\Component\Console\Output\OutputInterface;
15
use Symplify\PHP7_CodeSniffer\Console\ExitCode;
16
use Symplify\PHP7_CodeSniffer\Console\Style\CodeSnifferStyle;
17
use Symplify\PHP7_CodeSniffer\ErrorDataCollector;
18
use Symplify\PHP7_CodeSniffer\Php7CodeSniffer;
19
use Throwable;
20
21
final class RunCommand extends Command
22
{
23
    /**
24
     * @var CodeSnifferStyle
25
     */
26
    private $codeSnifferStyle;
27
28
    /**
29
     * @var ErrorDataCollector
30
     */
31
    private $reportCollector;
32
33
    /**
34
     * @var Php7CodeSniffer
35
     */
36
    private $php7CodeSniffer;
37
38
    public function __construct(
39
        CodeSnifferStyle $codeSnifferStyle,
40
        ErrorDataCollector $reportCollector,
41
        Php7CodeSniffer $php7CodeSniffer
42
    ) {
43
        $this->codeSnifferStyle = $codeSnifferStyle;
44
        $this->reportCollector = $reportCollector;
45
        $this->php7CodeSniffer = $php7CodeSniffer;
46
47
        parent::__construct();
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    protected function configure()
54
    {
55
        $this->setName('run');
56
        $this->setDescription('Checks code against coding standard.');
57
58
        $this->addArgument(
59
            'source',
60
            InputArgument::REQUIRED | InputArgument::IS_ARRAY,
61
            'Files or directories to process.'
62
        );
63
        $this->addOption('fix', null, null, 'Fix all fixable errors.');
64
65
        $this->addOption(
66
            'standards',
67
            null,
68
            InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
69
            'List of coding standards to use.'
70
        );
71
        $this->addOption(
72
            'sniffs',
73
            null,
74
            InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
75
            'List of sniff codes to use.'
76
        );
77
    }
78
79
    protected function execute(InputInterface $input, OutputInterface $output)
80
    {
81
        try {
82
            $this->php7CodeSniffer->registerSniffs(
83
                $input->getOption('standards'),
84
                $input->getOption('sniffs')
85
            );
86
            $this->php7CodeSniffer->runForSource(
87
                $input->getArgument('source'),
88
                $input->getOption('fix')
89
            );
90
91
            // 2. print found errors to the output
92
            if ($this->reportCollector->getErrorCount()) {
93
                if ($input->getOption('fix')) {
94
                    $this->printUnfixedErrors();
95
                } else {
96
                    $this->printErrors();
97
                    $this->printFixingNote();
98
                }
99
100
                return ExitCode::ERROR;
101
            }
102
103
            $this->codeSnifferStyle->success(
104
                'Great job! Your code is completely fine. Take a break and look around you.'
105
            );
106
107
            return ExitCode::SUCCESS;
108
        } catch (Throwable $throwable) {
109
            $this->codeSnifferStyle->error($throwable->getMessage());
110
111
            return ExitCode::ERROR;
112
        }
113
    }
114
115
    private function printErrors()
116
    {
117
        $this->codeSnifferStyle->writeErrorReports($this->reportCollector->getErrorMessages());
118
        $this->codeSnifferStyle->error(sprintf(
119
            '%d errors were found.',
120
            $this->reportCollector->getErrorCount()
121
        ));
122
    }
123
124
    private function printFixingNote()
125
    {
126
        if ($fixableCount = $this->reportCollector->getFixableErrorCount()) {
127
            $howMany = $fixableCount;
128
            if ($fixableCount === $this->reportCollector->getErrorCount()) {
129
                $howMany = 'all';
130
            }
131
132
            $this->codeSnifferStyle->success(sprintf(
133
                'Good news is, we can fix %s of them for you. Just add "--fix".',
134
                $howMany
135
            ));
136
        }
137
    }
138
139
    private function printUnfixedErrors()
140
    {
141
        $this->codeSnifferStyle->writeErrorReports(
142
            $this->reportCollector->getUnfixableErrorMessages()
143
        );
144
145
        if ($this->reportCollector->getFixableErrorCount()) {
146
            $this->codeSnifferStyle->success(sprintf(
147
                'Congrats! %d errors were fixed.',
148
                $this->reportCollector->getFixableErrorCount()
149
            ));
150
        }
151
152
        if ($this->reportCollector->getUnfixableErrorCount()) {
153
            $this->codeSnifferStyle->error(sprintf(
154
                '%d errors could not be fixed. You have to do it manually.',
155
                $this->reportCollector->getUnfixableErrorCount()
156
            ));
157
        }
158
    }
159
}
160