Completed
Pull Request — master (#10)
by Tomáš
07:32
created

RunCommand::createRunApplicationCommandFromInput()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
crap 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\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\Report\ErrorDataCollector;
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 ErrorDataCollector
41
     */
42
    private $errorDataCollector;
43
44
    public function __construct(
45
        Application $application,
46
        StyleInterface $style,
47
        MultiCsFileLoader $multiCsFileLoader,
48
        ErrorDataCollector $errorDataCollector
49
    ) {
50
        parent::__construct();
51
52
        $this->application = $application;
53
        $this->style = $style;
54
        $this->multiCsFileLoader = $multiCsFileLoader;
55
        $this->errorDataCollector = $errorDataCollector;
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
            dump($this->errorDataCollector->getErrorCount());
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
            $this->style->error($throwable->getMessage());
91
92
            return ExitCode::ERROR;
93
        }
94
    }
95
96
    private function createRunApplicationCommandFromInput(InputInterface $input) : RunApplicationCommand
97
    {
98
        return new RunApplicationCommand(
99
            $input->getArgument('source'),
100
            $input->getOption('fix'),
101
            $this->multiCsFileLoader->load()
102
        );
103
    }
104
}
105