2013-05-13 11:23:37 +02:00
|
|
|
<?php
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
if (php_sapi_name() != 'cli') {
|
|
|
|
die("This script must be called from the command line\n");
|
2013-05-13 11:23:37 +02:00
|
|
|
}
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
if (!empty($_SERVER['argv'][1])) {
|
|
|
|
$path = $_SERVER['argv'][1];
|
2013-05-13 11:23:37 +02:00
|
|
|
} else {
|
2016-12-16 05:34:21 +01:00
|
|
|
die("Usage: php {$_SERVER['argv'][0]} <file>\n");
|
2013-05-13 11:23:37 +02:00
|
|
|
}
|
|
|
|
|
2020-04-20 09:07:53 +02:00
|
|
|
$result = ['comments' => []];
|
2013-05-13 11:23:37 +02:00
|
|
|
|
2013-08-21 06:57:57 +02:00
|
|
|
$extension = pathinfo($path, PATHINFO_EXTENSION);
|
2013-05-13 11:23:37 +02:00
|
|
|
|
2013-08-21 06:57:57 +02:00
|
|
|
// Whitelist of extensions to check (default phpcs list)
|
2020-04-20 09:07:53 +02:00
|
|
|
if (in_array($extension, ['php', 'js', 'inc', 'css'])) {
|
2016-12-16 05:34:21 +01:00
|
|
|
// Run each sniff
|
2013-05-13 11:23:37 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
// phpcs --encoding=utf-8 --standard=framework/tests/phpcs/tabs.xml
|
|
|
|
run_sniff('tabs.xml', $path, $result);
|
2013-05-13 11:23:37 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
// phpcs --encoding=utf-8 --tab-width=4 --standard=framework/tests/phpcs/ruleset.xml
|
|
|
|
run_sniff('ruleset.xml', $path, $result, '--tab-width=4');
|
2013-08-21 06:57:57 +02:00
|
|
|
}
|
2013-05-13 11:23:37 +02:00
|
|
|
echo json_encode($result);
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
function run_sniff($standard, $path, array &$result, $extraFlags = '')
|
|
|
|
{
|
|
|
|
$sniffPath = escapeshellarg(__DIR__ . '/phpcs/' . $standard);
|
|
|
|
$checkPath = escapeshellarg($path);
|
|
|
|
|
|
|
|
exec("phpcs --encoding=utf-8 $extraFlags --standard=$sniffPath --report=xml $checkPath", $output);
|
|
|
|
|
|
|
|
// We can't check the return code as it's non-zero if the sniff finds an error
|
|
|
|
if ($output) {
|
|
|
|
$xml = implode("\n", $output);
|
|
|
|
$xml = simplexml_load_string($xml);
|
|
|
|
$errors = $xml->xpath('/phpcs/file/error');
|
|
|
|
if ($errors) {
|
|
|
|
$sanePath = str_replace('/', '_', $path);
|
|
|
|
foreach ($errors as $error) {
|
|
|
|
$attributes = $error->attributes();
|
2020-04-20 09:07:53 +02:00
|
|
|
$result['comments'][] = [
|
2016-12-16 05:34:21 +01:00
|
|
|
'line' => (int)strval($attributes->line),
|
|
|
|
'id' => $standard . '-' . $sanePath . '-' . $attributes->line . '-' . $attributes->column,
|
|
|
|
'message' => strval($error)
|
2020-04-20 09:07:53 +02:00
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-05-13 11:23:37 +02:00
|
|
|
}
|