Passed
Pull Request — master (#346)
by Mirko
08:32
created

CodeSnifferCommand::execute()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 14
c 1
b 0
f 0
nc 16
nop 2
dl 0
loc 24
rs 8.5125
1
<?php
2
/***************************************************************************
3
 *  For license information see doc/license.txt
4
 *
5
 *  Unicode Reminder メモ
6
 ***************************************************************************/
7
8
namespace AppBundle\Command;
9
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Input\InputOption;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use Symfony\Component\Process\Process;
14
15
class CodeSnifferCommand extends AbstractCommand
16
{
17
    const COMMAND_NAME = 'code:sniff';
18
19
    const OPTION_DRY_RUN = 'dry';
20
    const OPTION_FIX = 'fix';
21
    const OPTION_XML = 'xml';
22
    const OPTION_DIR = 'dir';
23
24
    /**
25
     * @return void
26
     */
27
    protected function configure()
28
    {
29
        parent::configure();
30
31
        $this
32
            ->setName(self::COMMAND_NAME)
33
            ->setDescription('Wrapper for phpcs');
34
35
        $this->addOption(self::OPTION_DRY_RUN, 't', InputOption::VALUE_NONE, 'Dry run the command');
36
        $this->addOption(self::OPTION_FIX, 'f', InputOption::VALUE_NONE, 'Fix errors that can be fixed automatically');
37
        $this->addOption(self::OPTION_XML, 'x', InputOption::VALUE_NONE, 'Write to checkstyle xml file');
38
        $this->addOption(self::OPTION_DIR, 'd', InputOption::VALUE_REQUIRED, 'Specify directory or file to check');
39
    }
40
41
    /**
42
     * @param \Symfony\Component\Console\Input\InputInterface $input
43
     * @param \Symfony\Component\Console\Output\OutputInterface $output
44
     *
45
     * @return int|null
46
     */
47
    protected function execute(InputInterface $input, OutputInterface $output)
48
    {
49
        $command = $input->getOption(self::OPTION_FIX) ? 'phpcbf' : 'phpcs';
50
        $cmd = 'vendor/bin/' . ($command) . ' -s --standard=../tests/ruleset.xml';
51
        if ($input->getOption(self::OPTION_DIR)) {
52
            $cmd .= ' ' . $input->getOption(self::OPTION_DIR);
53
        }
54
        if ($input->getOption(self::OPTION_XML)) {
55
            $cmd .= ' --report=checkstyle --report-file=../tests/cs-data';
56
        }
57
58
        if ($input->getOption(self::OPTION_DRY_RUN)) {
59
            $output->writeln($cmd);
60
61
            return self::CODE_SUCCESS;
62
        }
63
64
        $process = new Process($cmd, $this->rootPath, null, null, 9600);
65
        $process->run(function ($type, $buffer) {
66
            echo $buffer;
67
        });
68
69
        return $process->getExitCode();
70
    }
71
}
72