Completed
Push — master ( 3b5f38...c5ceed )
by Tomáš
04:59
created

CheckCommand::printFixingNote()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 3
Bugs 1 Features 0
Metric Value
dl 0
loc 14
ccs 0
cts 13
cp 0
rs 9.4285
c 3
b 1
f 0
cc 3
eloc 8
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\PHP7_CodeSniffer\Console\Command;
9
10
use Exception;
11
use Symfony\Component\Console\Command\Command;
12
use Symfony\Component\Console\Input\InputArgument;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Input\InputOption;
15
use Symfony\Component\Console\Output\OutputInterface;
16
use Symplify\PHP7_CodeSniffer\Console\Progress\ShowProgress;
17
use Symplify\PHP7_CodeSniffer\Console\Style\CodeSnifferStyle;
18
use Symplify\PHP7_CodeSniffer\ErrorDataCollector;
19
use Symplify\PHP7_CodeSniffer\File\SourceFilesProvider;
20
use Symplify\PHP7_CodeSniffer\Php7CodeSniffer;
21
use Symplify\PHP7_CodeSniffer\Ruleset;
22
23
final class CheckCommand extends Command
24
{
25
    /**
26
     * @var SourceFilesProvider
27
     */
28
    private $sourceFilesProvider;
29
30
    /**
31
     * @var CodeSnifferStyle
32
     */
33
    private $codeSnifferStyle;
34
35
    /**
36
     * @var ErrorDataCollector
37
     */
38
    private $reportCollector;
39
40
    /**
41
     * @var Ruleset
42
     */
43
    private $ruleset;
44
45
    /**
46
     * @var ShowProgress
47
     */
48
    private $showProgress;
49
50
    /**
51
     * @var Php7CodeSniffer
52
     */
53
    private $php7CodeSniffer;
54
55
    public function __construct(
56
        SourceFilesProvider $sourceFilesProvider,
57
        CodeSnifferStyle $codeSnifferStyle,
58
        ErrorDataCollector $reportCollector,
59
        Ruleset $ruleset,
60
        ShowProgress $showProgress,
61
        Php7CodeSniffer $php7CodeSniffer
62
    ) {
63
        $this->sourceFilesProvider = $sourceFilesProvider;
64
        $this->codeSnifferStyle = $codeSnifferStyle;
65
        $this->reportCollector = $reportCollector;
66
        $this->ruleset = $ruleset;
67
        $this->showProgress = $showProgress;
68
        $this->php7CodeSniffer = $php7CodeSniffer;
69
70
        parent::__construct();
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    protected function configure()
77
    {
78
        $this->setName('check');
79
        $this->setDescription('Checks code against coding standard.');
80
        $this->addArgument('source', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'One or more files or directories to process');
81
        $this->addOption('standards', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'The name(s) of the coding standard to use', ['PSR2']);
82
        $this->addOption('sniffs', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'List of sniff codes to use.');
83
        $this->addOption('fix', null, InputOption::VALUE_NONE, 'Fix all fixable errors.');
84
    }
85
86
    protected function execute(InputInterface $input, OutputInterface $output)
87
    {
88
        try {
89
            // 0. setup
90
            $this->ruleset->createSniffList();
91
92
            // 1. get files
93
            $files = $this->sourceFilesProvider->getFiles();
94
            $this->showProgress->init(count($files));
95
96
            // 2. run it
97
            $this->php7CodeSniffer->runForFiles($files);
98
99
            // 3. finish it
100
            $this->showProgress->finish();
101
            $this->codeSnifferStyle->newLine();
102
103
            // 2. print found errors to the output
104
            if ($this->reportCollector->getErrorCount()) {
105
                $this->printErrors();
106
                $this->printFixingNote();
107
108
                return 1;
109
            }
110
111
            $this->codeSnifferStyle->success('Great job! Your code is completely fine.');
112
113
            return 0;
114
        } catch (Exception $exception) {
115
            $this->codeSnifferStyle->error($exception->getMessage());
116
117
            return 0;
118
        }
119
    }
120
121
    private function printErrors()
122
    {
123
        $this->codeSnifferStyle->writeErrorReports($this->reportCollector->getErrorMessages());
124
        $this->codeSnifferStyle->error(sprintf(
125
            '%d errors were found.',
126
            $this->reportCollector->getErrorCount()
127
        ));
128
    }
129
130
    private function printFixingNote()
131
    {
132
        if ($fixableCount = $this->reportCollector->getFixableErrorCount()) {
133
            $howMany = $fixableCount;
134
            if ($fixableCount === $this->reportCollector->getErrorCount()) {
135
                $howMany = 'all';
136
            }
137
138
            $this->codeSnifferStyle->success(sprintf(
139
                'We can fix %s of them automatically.',
140
                $howMany
141
            ));
142
        }
143
    }
144
}
145