Last active
November 11, 2015 19:02
-
-
Save albertpark/ef9ed4553192ecd5ea44 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/albertpark/985d39b759f356cdbbeb).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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