Sniffer   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 0
cbo 0
dl 0
loc 65
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A sniffFiles() 0 13 3
B sniffFile() 0 30 4
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 string $extensions
13
     *
14
     * @return array
15
     */
16
    public static function sniffFiles(array $files, $standards = '', $extensions = '')
17
    {
18
        $results = [];
19
        foreach ($files as $file) {
20
            $sniff_result = self::sniffFile($file, $standards, $extensions);
21
            if (is_null($sniff_result)) {
22
                continue;
23
            }
24
            $results[$file] = $sniff_result->$file;
25
        }
26
27
        return $results;
28
    }
29
30
    /**
31
     * Run PHPCS in a single file.
32
     *
33
     * @param $file
34
     * @param string $standards
35
     * @param string $extensions
36
     *
37
     * @return string|null
38
     */
39
    private static function sniffFile($file, $standards = '', $extensions = '')
40
    {
41
        $command = COMPOSER_VENDOR_DIR . '/bin/phpcs';
42
43
        if (!empty($standards)) {
44
            $command .= " --standard=$standards";
45
        }
46
47
        // @fixme: handle extensions properly (ex: the code is trying to sniff a .json file)
48
        if (!empty($extensions)) {
49
            $command .= " --extensions=$extensions";
50
        }
51
52
        $command .= ' --report=json';
53
54
        $command .= " $file";
55
56
        $command = escapeshellcmd($command);
57
58
        // Run phpcs and grab the output.
59
        exec($command, $output);
60
61
        if (is_null($output)) {
62
            return null;
63
        }
64
65
        $result = json_decode(reset($output));
66
67
        return $result->files;
68
    }
69
}
70