Completed
Push — master ( 3c5828...866487 )
by Dorian
02:10
created

CodingStandardCommand::runJsHint()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 19
rs 9.4285
cc 2
eloc 11
nc 2
nop 0
1
<?php
2
3
namespace Application\Commands;
4
5
use Symfony\Component\Console\Input\InputInterface;
6
use Symfony\Component\Console\Input\InputOption;
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Symfony\Component\Process\Process;
9
10
/**
11
 * Coding Standard Command
12
 */
13
class CodingStandardCommand extends Command
14
{
15
    /**
16
     * {@inheritDoc}
17
     */
18
    protected function configure()
19
    {
20
        $this->setName('cs')
21
            ->setDescription('Run coding standard inspection tools.')
22
            ->addOption(
23
                'skip',
24
                null,
25
                InputOption::VALUE_REQUIRED,
26
                'Skip some inspectors (input as a CSV list). '.
27
                'Could be any of the following value: '.implode(', ', array_keys($this->getInspectors())).'.'
28
            );
29
    }
30
31
    /**
32
     * {@inheritDoc}
33
     */
34
    protected function execute(InputInterface $input, OutputInterface $output)
35
    {
36
        $inspectorsCallbacks = $this->getInspectors();
37
        $skippedInspectors = explode(',', $this->input->getOption('skip'));
38
39
        $exitCodes = array();
40
        foreach ($inspectorsCallbacks as $id => $callback) {
41
            if (in_array($id, $skippedInspectors, true)) {
42
                if (OutputInterface::VERBOSITY_DEBUG <= $this->input->getOption('verbose')) {
43
                    $this->outputSection('Debug', 'Inspector "'.$id.'" has been skipped.', 'comment', false, true);
44
                }
45
                continue;
46
            }
47
48
            $process = call_user_func($callback);
49
            if (!($process instanceof Process)) {
50
                throw new \UnexpectedValueException('The return value of an inspector callback must be a Process.');
51
            }
52
53
            $exitCodes[] = $process->getExitCode();
54
        }
55
56
        if (!empty($exitCodes) && array(0) !== array_unique($exitCodes)) {
57
            throw new \UnexpectedValueException('There are validation errors in the code. Please, fix them.');
58
        }
59
    }
60
61
    /**
62
     * @return array
63
     */
64
    protected function getInspectors()
65
    {
66
        return array(
67
            'phpcs' => array($this, 'runPhpCS'),
68
            'jshint' => array($this, 'runJsHint'),
69
        );
70
    }
71
72
    /**
73
     * Runs PHPCS inspection tool
74
     */
75
    protected function runPhpCS()
76
    {
77
        $root = $this->getApplication()->getApplicationRootDir();
78
        $phpcsExecutable = $root.'/bin/phpcs';
79
80
        // Ensure the binaries needed are available
81
        if (!file_exists($phpcsExecutable)) {
82
            throw new \UnexpectedValueException(
83
                'The file "'.$phpcsExecutable.'" could not be found. Have you installed the project\'s vendors ?'
84
            );
85
        }
86
87
        // Run PHPCS
88
        $this->outputBlock('PHP Code Sniffer validation');
89
90
        return $this->runProcess(
91
            $phpcsExecutable.' '.$root.'/src --extensions=php --standard=PSR2 -p',
92
            true,
93
            60 * 5,
94
            false
95
        );
96
    }
97
98
    /**
99
     * Runs jsHint inspection tool
100
     */
101
    protected function runJsHint()
102
    {
103
        $root = $this->getApplication()->getApplicationRootDir();
104
105
        $this->outputBlock('JS Hint validation');
106
        $jsHint = $this->runProcess(
107
            'jshint . --config="'.$root.'/.jshintrc" --verbose',
108
            true,
109
            60,
110
            false
111
        );
112
113
        // Add an output for happy jsHint
114
        if (null === $jsHint->getOutput()) {
115
            $this->output->writeln('No error reported.');
116
        }
117
118
        return $jsHint;
119
    }
120
}
121