Skip to content

Instantly share code, notes, and snippets.

@satooshi
Last active November 27, 2018 02:30
Show Gist options
  • Save satooshi/4958909 to your computer and use it in GitHub Desktop.
Save satooshi/4958909 to your computer and use it in GitHub Desktop.
Parse checkstyle.xml generated by PHP_CodeSniffer (phpcs --report=checkstyle --report-file=checkstyle.xml src) and print violation messages. If you want to color messages at severity, get ColorCLI class (see https://gist.github.com/satooshi/4750401).
<?php
function run($path)
{
$xml = simplexml_load_file($path);
foreach ($xml->file as $file) {
echo sprintf("file: %s", $file['name']) . PHP_EOL;
foreach ($file->error as $violation) {
echo " " . printMessage($violation) . PHP_EOL;
echo sprintf(
" severity: %s rule: %s at line %s column %s",
$violation['severity'],
$violation['source'],
$violation['line'],
$violation['column']
),
PHP_EOL;
}
}
return 0;
}
function printMessage($violation)
{
$str = $violation['message'];
if (!class_exists('ColorCLI')) {
return $str;
}
$severity = $violation['severity'];
if ($severity == 'error') {
return ColorCLI::red($str);
} elseif ($severity == 'warning') {
return ColorCLI::yellow($str);
}
return ColorCLI::cyan($str);
}
function checkFile($xmlFileName)
{
$root = realpath(__DIR__ . "/..");
$path = realpath("$root/build/logs/$xmlFileName");
if ($path === false || !file_exists($path)) {
return "Not found $xmlFileName";
}
return run($path);
}
$colorCli = realpath(__DIR__ . '/ColorCLI.php');
if (file_exists($colorCli)) {
include_once $colorCli;
}
define('NORMAL_PRIORITY', 3);
$result = array(
checkFile("checkstyle.xml"),
checkFile("checkstyle-apigen.xml"),
);
foreach ($result as $value) {
if (is_string($value)) {
echo $value, PHP_EOL;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment