Skip to content

Instantly share code, notes, and snippets.

@satooshi
Last active November 27, 2018 02:30
Show Gist options
  • Save satooshi/4958593 to your computer and use it in GitHub Desktop.
Save satooshi/4958593 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/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->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