Passed
Pull Request — development (#671)
by Nick
06:47
created

CodeSnifferCommand   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 67
rs 10
c 0
b 0
f 0
wmc 6
lcom 2
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 13 1
B execute() 0 24 5
1
<?php
2
/***************************************************************************
3
 * for license information see LICENSE.md
4
 ***************************************************************************/
5
6
namespace Oc\Command;
7
8
use Symfony\Component\Console\Exception\InvalidArgumentException;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Symfony\Component\Process\Exception\LogicException;
13
use Symfony\Component\Process\Exception\RuntimeException;
14
use Symfony\Component\Process\Process;
15
16
/**
17
 * Class CodeSnifferCommand
18
 *
19
 * @package Oc\Command
20
 */
21
class CodeSnifferCommand extends AbstractCommand
22
{
23
    const COMMAND_NAME = 'code:sniff';
24
25
    const OPTION_DRY_RUN = 'dry';
26
    const OPTION_FIX = 'fix';
27
    const OPTION_XML = 'xml';
28
    const OPTION_DIR = 'dir';
29
30
    /**
31
     * Configures the command.
32
     *
33
     * @return void
34
     *
35
     * @throws InvalidArgumentException
36
     */
37
    protected function configure()
38
    {
39
        parent::configure();
40
41
        $this
42
            ->setName(self::COMMAND_NAME)
43
            ->setDescription('Wrapper for phpcs');
44
45
        $this->addOption(self::OPTION_DRY_RUN, 't', InputOption::VALUE_NONE, 'Dry run the command');
46
        $this->addOption(self::OPTION_FIX, 'f', InputOption::VALUE_NONE, 'Fix errors that can be fixed automatically');
47
        $this->addOption(self::OPTION_XML, 'x', InputOption::VALUE_NONE, 'Write to checkstyle xml file');
48
        $this->addOption(self::OPTION_DIR, 'd', InputOption::VALUE_REQUIRED, 'Specify directory or file to check');
49
    }
50
51
    /**
52
     * Executes the command.
53
     *
54
     * @param InputInterface $input
55
     * @param OutputInterface $output
56
     *
57
     * @return int|null
58
     *
59
     * @throws RuntimeException
60
     * @throws LogicException
61
     * @throws InvalidArgumentException
62
     */
63
    protected function execute(InputInterface $input, OutputInterface $output)
64
    {
65
        $command = $input->getOption(self::OPTION_FIX) ? 'phpcbf' : 'phpcs';
66
        $cmd = 'vendor/bin/' . ($command) . ' -n -p --colors -s --standard=../tests/ruleset.xml';
67
        if ($input->getOption(self::OPTION_DIR)) {
68
            $cmd .= ' ' . $input->getOption(self::OPTION_DIR);
69
        }
70
        if ($input->getOption(self::OPTION_XML)) {
71
            $cmd .= ' --report=checkstyle --report-file=../tests/cs-data';
72
        }
73
74
        if ($input->getOption(self::OPTION_DRY_RUN)) {
75
            $output->writeln($cmd);
76
77
            return self::CODE_SUCCESS;
78
        }
79
80
        $process = new Process($cmd, $this->rootPath, null, null, 9600);
81
        $process->run(function ($type, $buffer) {
82
            echo $buffer;
83
        });
84
85
        return $process->getExitCode();
86
    }
87
}
88