Last active
November 27, 2018 02:30
-
-
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).
This file contains hidden or 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->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