Completed
Push — master ( 1442eb...64bec4 )
by Tomáš
11:51
created

RunCommand::execute()   A

Complexity

Conditions 3
Paths 6

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 10
Bugs 2 Features 4
Metric Value
c 10
b 2
f 4
dl 0
loc 22
rs 9.2
cc 3
eloc 12
nc 6
nop 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 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\Application\Application;
17
use Symplify\PHP7_CodeSniffer\Application\Command\RunApplicationCommand;
18
use Symplify\PHP7_CodeSniffer\Console\ExitCode;
19
use Symplify\PHP7_CodeSniffer\Console\Style\CodeSnifferStyle;
20
use Symplify\PHP7_CodeSniffer\Console\Output\InfoMessagePrinter;
21
use Symplify\PHP7_CodeSniffer\Report\ErrorDataCollector;
22
use Throwable;
23
24
final class RunCommand extends Command
25
{
26
    /**
27
     * @var CodeSnifferStyle
28
     */
29
    private $codeSnifferStyle;
30
31
    /**
32
     * @var ErrorDataCollector
33
     */
34
    private $errorDataCollector;
35
36
    /**
37
     * @var Application
38
     */
39
    private $application;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
40
41
    /**
42
     * @var InfoMessagePrinter
43
     */
44
    private $infoMessagePrinter;
45
46
    public function __construct(
47
        CodeSnifferStyle $codeSnifferStyle,
48
        ErrorDataCollector $reportCollector,
49
        Application $application,
50
        InfoMessagePrinter $infoMessagePrinter
51
    ) {
52
        $this->codeSnifferStyle = $codeSnifferStyle;
53
        $this->errorDataCollector = $reportCollector;
54
        $this->application = $application;
55
        $this->infoMessagePrinter = $infoMessagePrinter;
56
57
        parent::__construct();
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    protected function configure()
64
    {
65
        $this->setName('run');
66
        $this->setDescription('Checks code against coding standard.');
67
        $this->addArgument(
68
            'source',
69
            InputArgument::REQUIRED | InputArgument::IS_ARRAY,
70
            'Files or directories to process.'
71
        );
72
        $this->addOption('fix', null, null, 'Fix all fixable errors.');
73
        $this->addArrayOption('standards', 'List of coding standards to use.');
74
        $this->addArrayOption('sniffs', 'List of sniff codes to use.');
75
        $this->addArrayOption('exclude-sniffs', 'List of sniff codes to be excluded.');
76
    }
77
78
    protected function execute(InputInterface $input, OutputInterface $output)
79
    {
80
        try {
81
            $this->application->runCommand($this->createCommandFromInput($input));
82
83
            if ($this->errorDataCollector->getErrorCount()) {
84
                $this->infoMessagePrinter->printFoundErrorsStatus($input->getOption('fix'));
85
86
                return ExitCode::ERROR;
87
            }
88
89
            $this->codeSnifferStyle->success(
90
                'Great job! Your code is completely fine. Take a break and look around you.'
91
            );
92
93
            return ExitCode::SUCCESS;
94
        } catch (Throwable $throwable) {
95
            $this->codeSnifferStyle->error($throwable->getMessage());
96
97
            return ExitCode::ERROR;
98
        }
99
    }
100
101
    private function addArrayOption(string $name, string $description)
102
    {
103
        $this->addOption($name, null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, $description);
104
    }
105
106
    private function createCommandFromInput(InputInterface $input) : RunApplicationCommand
107
    {
108
        return new RunApplicationCommand(
109
            $input->getArgument('source'),
110
            $input->getOption('standards'),
111
            $input->getOption('sniffs'),
112
            $input->getOption('exclude-sniffs'),
113
            $input->getOption('fix')
114
        );
115
    }
116
}
117