Completed
Push — master ( afadea...e6376f )
by Tomáš
03:06 queued 03:01
created

RunCommand::execute()   B

Complexity

Conditions 4
Paths 10

Size

Total Lines 36
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 36
ccs 0
cts 24
cp 0
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 16
nc 10
nop 2
crap 20
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\MultiCodingStandard\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\Output\OutputInterface;
14
use Symfony\Component\Console\Style\StyleInterface;
15
use Symplify\MultiCodingStandard\Application\Application;
16
use Symplify\MultiCodingStandard\Application\Command\RunApplicationCommand;
17
use Symplify\MultiCodingStandard\Configuration\MultiCsFileLoader;
18
use Symplify\MultiCodingStandard\Console\Output\InfoMessagePrinter;
19
use Symplify\PHP7_CodeSniffer\Console\ExitCode;
20
use Throwable;
21
22
final class RunCommand extends Command
23
{
24
    /**
25
     * @var StyleInterface
26
     */
27
    private $style;
28
29
    /**
30
     * @var Application
31
     */
32
    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...
33
34
    /**
35
     * @var MultiCsFileLoader
36
     */
37
    private $multiCsFileLoader;
38
39
    /**
40
     * @var InfoMessagePrinter
41
     */
42
    private $infoMessagePrinter;
43
44
    public function __construct(
45
        Application $application,
46
        StyleInterface $style,
47
        MultiCsFileLoader $multiCsFileLoader,
48
        InfoMessagePrinter $infoMessagePrinter
49
    ) {
50
        parent::__construct();
51
52
        $this->application = $application;
53
        $this->style = $style;
54
        $this->multiCsFileLoader = $multiCsFileLoader;
55
        $this->infoMessagePrinter = $infoMessagePrinter;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    protected function configure()
62
    {
63
        $this->setName('run');
64
        $this->addArgument('source', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'The path(s) to be checked.');
65
        $this->addOption('fix', null, null, 'Fix found violations.');
66
        $this->setDescription('Check coding standard in one or more directories.');
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    protected function execute(InputInterface $input, OutputInterface $output)
73
    {
74
        try {
75
            $this->application->runCommand(
76
                $this->createRunApplicationCommandFromInput($input)
77
            );
78
79
            if ($this->infoMessagePrinter->hasSomeErrorMessages()) {
80
                $this->infoMessagePrinter->printFoundErrorsStatus($input->getOption('fix'));
81
82
//                $this->style->error(
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
83
//                    sprintf(
84
//                        'We found some errors in "%s".',
85
//                        implode(',', $input->getArgument('source'))
86
//                    )
87
//                );
88
89
                return ExitCode::ERROR;
90
            }
91
92
            $this->style->success(
93
                sprintf(
94
                    'Sources "%s" were checked!',
95
                    implode(',', $input->getArgument('source'))
96
                )
97
            );
98
99
            return ExitCode::SUCCESS;
100
        } catch (Throwable $throwable) {
101
            if ($throwable->getMessage()) {
102
                $this->style->error($throwable->getMessage());
103
            }
104
105
            return ExitCode::ERROR;
106
        }
107
    }
108
109
    private function createRunApplicationCommandFromInput(InputInterface $input) : RunApplicationCommand
110
    {
111
        return new RunApplicationCommand(
112
            $input->getArgument('source'),
113
            $input->getOption('fix'),
114
            $this->multiCsFileLoader->load()
115
        );
116
    }
117
}
118