Skip to content

Instantly share code, notes, and snippets.

@albertpark
Last active November 11, 2015 19:00
Show Gist options
  • Save albertpark/f68a8e2def7e37ea6bc3 to your computer and use it in GitHub Desktop.
Save albertpark/f68a8e2def7e37ea6bc3 to your computer and use it in GitHub Desktop.
Parse pmd.xml generated by PHPMD (phpmd src xml pmd.xml) and print violation messages. If you want to color messages at priority, get ColorCLI class (see https://gist.github.com/albertpark/985d39b759f356cdbbeb).
<?php
function run($path) {
$xml = simplexml_load_file($path);
foreach ($xml->file as $file) {
echo sprintf("file: %s", $file['name']) . PHP_EOL;
foreach ($file->violation as $violation) {
echo " " . printMessage($violation) . PHP_EOL;
echo sprintf(
" priority: %s rule: %s:%s at line %s - %s"
, $violation['priority']
, $violation['ruleset']
, $violation['rule']
, $violation['beginline']
, $violation['endline']
)
, PHP_EOL;
}
}
return 0;
}
function isHighPriority($priority) {
// red
return $priority < NORMAL_PRIORITY;
}
function isNormatPriority($priority) {
// yellow
return $priority == NORMAL_PRIORITY;
}
function isLowPriority($priority) {
return $priority > NORMAL_PRIORITY;
}
function printMessage($violation) {
$str = formatMessage($violation);
if (!class_exists('ColorCLI')) {
return $str;
}
$priority = $violation['priority'];
if (isHighPriority($priority)) {
return ColorCLI::red($str);
} elseif (isNormatPriority($priority)) {
return ColorCLI::yellow($str);
}
return ColorCLI::cyan($str);
}
function formatMessage($violation) {
return trim($violation);
}
$colorCli = realpath(__DIR__ . '/ColorCLI.php');
if (file_exists($colorCli)) {
include_once $colorCli;
}
$xmlFileName = "pmd.xml";
$root = realpath(__DIR__ . "/..");
$path = realpath("$root/build/logs/$xmlFileName");
if ($path === false || !file_exists($path)) {
die("Not found $xmlFileName");
}
define('NORMAL_PRIORITY', 3);
return run($path);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment