RunCommand   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 2
cbo 7
dl 0
loc 83
ccs 0
cts 51
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A configure() 0 7 1
B execute() 0 29 4
A createRunApplicationCommandFromInput() 0 8 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Symplify
7
 * Copyright (c) 2016 Tomas Votruba (http://tomasvotruba.cz).
8
 */
9
10
namespace Symplify\MultiCodingStandard\Console\Command;
11
12
use Symfony\Component\Console\Command\Command;
13
use Symfony\Component\Console\Input\InputArgument;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Output\OutputInterface;
16
use Symfony\Component\Console\Style\StyleInterface;
17
use Symplify\MultiCodingStandard\Application\Application;
18
use Symplify\MultiCodingStandard\Application\Command\RunApplicationCommand;
19
use Symplify\MultiCodingStandard\Configuration\MultiCsFileLoader;
20
use Symplify\MultiCodingStandard\Console\Output\InfoMessagePrinter;
21
use Symplify\PHP7_CodeSniffer\Console\ExitCode;
22
use Throwable;
23
24
final class RunCommand extends Command
25
{
26
    /**
27
     * @var StyleInterface
28
     */
29
    private $style;
30
31
    /**
32
     * @var Application
33
     */
34
    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...
35
36
    /**
37
     * @var MultiCsFileLoader
38
     */
39
    private $multiCsFileLoader;
40
41
    /**
42
     * @var InfoMessagePrinter
43
     */
44
    private $infoMessagePrinter;
45
46
    public function __construct(
47
        Application $application,
48
        StyleInterface $style,
49
        MultiCsFileLoader $multiCsFileLoader,
50
        InfoMessagePrinter $infoMessagePrinter
51
    ) {
52
        parent::__construct();
53
54
        $this->application = $application;
55
        $this->style = $style;
56
        $this->multiCsFileLoader = $multiCsFileLoader;
57
        $this->infoMessagePrinter = $infoMessagePrinter;
58
    }
59
60
    protected function configure()
61
    {
62
        $this->setName('run');
63
        $this->addArgument('source', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'The path(s) to be checked.');
64
        $this->addOption('fix', null, null, 'Fix found violations.');
65
        $this->setDescription('Check coding standard in one or more directories.');
66
    }
67
68
    protected function execute(InputInterface $input, OutputInterface $output)
69
    {
70
        try {
71
            $this->application->runCommand(
72
                $this->createRunApplicationCommandFromInput($input)
73
            );
74
75
            if ($this->infoMessagePrinter->hasSomeErrorMessages()) {
76
                $this->infoMessagePrinter->printFoundErrorsStatus($input->getOption('fix'));
77
78
                return ExitCode::ERROR;
79
            }
80
81
            $this->style->success(
82
                sprintf(
83
                    'Sources "%s" were checked!',
84
                    implode(',', $input->getArgument('source'))
85
                )
86
            );
87
88
            return ExitCode::SUCCESS;
89
        } catch (Throwable $throwable) {
90
            if ($throwable->getMessage()) {
91
                $this->style->error($throwable->getMessage());
92
            }
93
94
            return ExitCode::ERROR;
95
        }
96
    }
97
98
    private function createRunApplicationCommandFromInput(InputInterface $input) : RunApplicationCommand
99
    {
100
        return new RunApplicationCommand(
101
            $input->getArgument('source'),
102
            $input->getOption('fix'),
103
            $this->multiCsFileLoader->load()
104
        );
105
    }
106
}
107