Skip to content

Instantly share code, notes, and snippets.

@jleonardolemos
Created August 28, 2020 21:17
Show Gist options
  • Save jleonardolemos/c4b880de724d61f105bc539110231c06 to your computer and use it in GitHub Desktop.
Save jleonardolemos/c4b880de724d61f105bc539110231c06 to your computer and use it in GitHub Desktop.
php metrics checker over json output
#!/usr/bin/env php
<?php
/**
* check for metrics on jenkins
* pass -d param with the path of phpmetrics output json
*/
function getHigherMetric($metrics, $value)
{
$mappedMetrics = array_map(function ($metric) use ($value) {
return $metric[$value];
}, array_filter($metrics, function ($metric) use ($value) {
return isset($metric[$value]);
}));
sort($mappedMetrics);
return array_pop($mappedMetrics);
}
function getAvarageMetric($metrics, $value)
{
$mappedMetrics = array_map(function ($metric) use ($value) {
return $metric[$value];
}, array_filter($metrics, function ($metric) use ($value) {
return isset($metric[$value]);
}));
return number_format(
array_sum($mappedMetrics) / count($mappedMetrics),
3,
'.',
''
);
}
$options = getopt("d:");
if (!isset($options['d'])) {
echo "path not provided" . PHP_EOL;
exit(1);
}
$path = realpath(__DIR__ . '/' . $options['d']);
if (!file_exists($path)) {
echo "file not found" . PHP_EOL;
exit(1);
}
$metrics = json_decode(
file_get_contents($options['d']),
true
);
if (getHigherMetric($metrics, 'lcom') > 16) {
echo "Max LCOM greater than 16" . PHP_EOL;
exit(1);
}
if (getAvarageMetric($metrics, 'lcom') > 1.789) {
echo "Average LCOM greater than 1.789" . PHP_EOL;
exit(1);
}
if (getHigherMetric($metrics, 'lloc') > 210) {
echo "Max Logical Lines of Code in class greater than 210" . PHP_EOL;
exit(1);
}
// class ciclomatic complexity
if (getHigherMetric($metrics, 'ccn') > 21) {
echo "Max Class Cyclomatic Complexity greater than 21" . PHP_EOL;
exit(1);
}
// class ciclomatic complexity
if (getHigherMetric($metrics, 'ccnMethodMax') > 14) {
echo "Max Method Cyclomatic Complexity greater than 14" . PHP_EOL;
exit(1);
}
if (getAvarageMetric($metrics, 'ccn') > 2.747) {
echo "Average Class Cyclomatic Complexity greater than 2.747" . PHP_EOL;
exit(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment