|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
if (php_sapi_name() != 'cli') { |
|
4
|
|
|
die("This script must be called from the command line\n"); |
|
5
|
|
|
} |
|
6
|
|
|
|
|
7
|
|
|
if (!empty($_SERVER['argv'][1])) { |
|
8
|
|
|
$path = $_SERVER['argv'][1]; |
|
9
|
|
|
} else { |
|
10
|
|
|
die("Usage: php {$_SERVER['argv'][0]} <file>\n"); |
|
11
|
|
|
} |
|
12
|
|
|
|
|
13
|
|
|
$result = array('comments' => array()); |
|
14
|
|
|
|
|
15
|
|
|
$extension = pathinfo($path, PATHINFO_EXTENSION); |
|
16
|
|
|
|
|
17
|
|
|
// Whitelist of extensions to check (default phpcs list) |
|
18
|
|
|
if (in_array($extension, array('php', 'js', 'inc', 'css'))) { |
|
19
|
|
|
// Run each sniff |
|
20
|
|
|
|
|
21
|
|
|
// phpcs --encoding=utf-8 --standard=framework/tests/phpcs/tabs.xml |
|
22
|
|
|
run_sniff('tabs.xml', $path, $result); |
|
23
|
|
|
|
|
24
|
|
|
// phpcs --encoding=utf-8 --tab-width=4 --standard=framework/tests/phpcs/ruleset.xml |
|
25
|
|
|
run_sniff('ruleset.xml', $path, $result, '--tab-width=4'); |
|
26
|
|
|
} |
|
27
|
|
|
echo json_encode($result); |
|
28
|
|
|
|
|
29
|
|
|
function run_sniff($standard, $path, array &$result, $extraFlags = '') |
|
30
|
|
|
{ |
|
31
|
|
|
$sniffPath = escapeshellarg(__DIR__ . '/phpcs/' . $standard); |
|
32
|
|
|
$checkPath = escapeshellarg($path); |
|
33
|
|
|
|
|
34
|
|
|
exec("phpcs --encoding=utf-8 $extraFlags --standard=$sniffPath --report=xml $checkPath", $output); |
|
35
|
|
|
|
|
36
|
|
|
// We can't check the return code as it's non-zero if the sniff finds an error |
|
37
|
|
|
if ($output) { |
|
38
|
|
|
$xml = implode("\n", $output); |
|
39
|
|
|
$xml = simplexml_load_string($xml); |
|
40
|
|
|
$errors = $xml->xpath('/phpcs/file/error'); |
|
41
|
|
|
if ($errors) { |
|
|
|
|
|
|
42
|
|
|
$sanePath = str_replace('/', '_', $path); |
|
43
|
|
|
foreach ($errors as $error) { |
|
44
|
|
|
$attributes = $error->attributes(); |
|
45
|
|
|
$result['comments'][] = array( |
|
46
|
|
|
'line' => (int)strval($attributes->line), |
|
47
|
|
|
'id' => $standard . '-' . $sanePath . '-' . $attributes->line . '-' . $attributes->column, |
|
48
|
|
|
'message' => strval($error) |
|
49
|
|
|
); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.