Passed
Branch ignore-files (1e3f4a)
by Natan
02:30
created

Sniffer::sniffFiles()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
rs 9.2
cc 4
eloc 9
nc 5
nop 3
1
<?php
2
3
namespace SnifferReport\Service;
4
5
abstract class Sniffer
6
{
7
    /**
8
     * Run PHPCS on given files.
9
     *
10
     * @param array $files
11
     * @param string $standards
12
     * @param array $options
13
     *
14
     * @return array
15
     */
16
    public static function sniffFiles(array $files, $standards, array $options)
17
    {
18
        $results = [];
19
        foreach ($files as $file) {
20
            if (!is_null($options)) {
21
                // @fixme: check if file fits include/exclude criteria before sniffing.
22
            }
23
            $sniff_result = self::sniffFile($file, $standards);
24
            if (is_null($sniff_result)) {
25
                continue;
26
            }
27
            $results[$file] = $sniff_result->$file;
28
        }
29
30
        return $results;
31
    }
32
33
    /**
34
     * Run PHPCS in a single file.
35
     *
36
     * @param string $file
37
     * @param string $standards
38
     *
39
     * @return string|null
40
     */
41
    private static function sniffFile($file, $standards)
42
    {
43
        $command = COMPOSER_VENDOR_DIR . '/bin/phpcs';
44
45
        $command .= " --standard={$standards}";
46
47
        $command .= ' --report=json';
48
49
        $command .= " $file";
50
51
        $command = escapeshellcmd($command);
52
53
        // Run phpcs and grab the output.
54
        exec($command, $output);
55
56
        if (is_null($output)) {
57
            return null;
58
        }
59
60
        $result = json_decode(reset($output));
61
62
        return $result->files;
63
    }
64
}
65