Completed
Push — master ( f7eb77...bdcf7d )
by Tomáš
03:24
created

RunCommand   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 10
Bugs 3 Features 4
Metric Value
wmc 6
lcom 2
cbo 7
dl 0
loc 95
ccs 0
cts 59
cp 0
rs 10
c 10
b 3
f 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A configure() 0 14 1
B execute() 0 30 3
A addArrayOption() 0 9 1
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\Console\Output\InfoMessagePrinter;
18
use Symplify\PHP7_CodeSniffer\Php7CodeSniffer;
19
use Symplify\PHP7_CodeSniffer\Php7CodeSnifferCommand;
20
use Symplify\PHP7_CodeSniffer\Report\ErrorDataCollector;
21
use Throwable;
22
23
final class RunCommand extends Command
24
{
25
    /**
26
     * @var CodeSnifferStyle
27
     */
28
    private $codeSnifferStyle;
29
30
    /**
31
     * @var ErrorDataCollector
32
     */
33
    private $errorDataCollector;
34
35
    /**
36
     * @var Php7CodeSniffer
37
     */
38
    private $php7CodeSniffer;
39
40
    /**
41
     * @var InfoMessagePrinter
42
     */
43
    private $infoMessagePrinter;
44
45
    public function __construct(
46
        CodeSnifferStyle $codeSnifferStyle,
47
        ErrorDataCollector $reportCollector,
48
        Php7CodeSniffer $php7CodeSniffer,
49
        InfoMessagePrinter $infoMessagePrinter
50
    ) {
51
        $this->codeSnifferStyle = $codeSnifferStyle;
52
        $this->errorDataCollector = $reportCollector;
53
        $this->php7CodeSniffer = $php7CodeSniffer;
54
        $this->infoMessagePrinter = $infoMessagePrinter;
55
56
        parent::__construct();
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    protected function configure()
63
    {
64
        $this->setName('run');
65
        $this->setDescription('Checks code against coding standard.');
66
        $this->addArgument(
67
            'source',
68
            InputArgument::REQUIRED | InputArgument::IS_ARRAY,
69
            'Files or directories to process.'
70
        );
71
        $this->addOption('fix', null, null, 'Fix all fixable errors.');
72
        $this->addArrayOption('standards', 'List of coding standards to use.');
73
        $this->addArrayOption('sniffs', 'List of sniff codes to use.');
74
        $this->addArrayOption('exclude-sniffs', 'List of sniff codes to be excluded.');
75
    }
76
77
    protected function execute(InputInterface $input, OutputInterface $output)
78
    {
79
        try {
80
            $php7CodeSnifferCommand = new Php7CodeSnifferCommand(
81
                $input->getArgument('source'),
82
                $input->getOption('standards'),
83
                $input->getOption('sniffs'),
84
                $input->getOption('exclude-sniffs'),
85
                $input->getOption('fix')
86
            );
87
88
            $this->php7CodeSniffer->runCommand($php7CodeSnifferCommand);
89
90
            if ($this->errorDataCollector->getErrorCount()) {
91
                $this->infoMessagePrinter->printFoundErrorsStatus($input->getOption('fix'));
92
93
                return ExitCode::ERROR;
94
            }
95
96
            $this->codeSnifferStyle->success(
97
                'Great job! Your code is completely fine. Take a break and look around you.'
98
            );
99
100
            return ExitCode::SUCCESS;
101
        } catch (Throwable $throwable) {
102
            $this->codeSnifferStyle->error($throwable->getMessage());
103
104
            return ExitCode::ERROR;
105
        }
106
    }
107
108
    private function addArrayOption(string $name, string $description)
109
    {
110
        $this->addOption(
111
            $name,
112
            null,
113
            InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
114
            $description
115
        );
116
    }
117
}
118