Files
ohmj2/api/coverage-report.php
T

65 lines
1.9 KiB
PHP

<?php
// Dev-only: merges the Xdebug dumps produced by coverage-prepend.php and
// prints per-file line coverage. Usage (from the repo root):
//
// SHOW_MISSING=1 php api/coverage-report.php \
// api/index.php api/router.php api/lib/Auth.php api/lib/ScoreScanner.php
$dir = sys_get_temp_dir() . '/ohmj_cov';
$files = glob($dir . '/cov_*.ser') ?: [];
$merged = [];
foreach ($files as $f) {
$data = @unserialize(file_get_contents($f));
if (!is_array($data)) {
continue;
}
foreach ($data as $file => $lines) {
foreach ($lines as $line => $status) {
if (!isset($merged[$file][$line]) || $status === 1) {
$merged[$file][$line] = ($status === 1 || ($merged[$file][$line] ?? -1) === 1) ? 1 : $status;
}
}
}
}
$targets = array_slice($argv, 1);
if (!$targets) {
$targets = array_keys($merged);
}
$grandTotal = 0;
$grandHit = 0;
foreach ($targets as $target) {
$target = realpath($target) ?: $target;
$lines = $merged[$target] ?? [];
$exec = [];
$hit = [];
foreach ($lines as $line => $status) {
if ($status === -2) {
continue; // dead code
}
$exec[$line] = true;
if ($status === 1) {
$hit[$line] = true;
}
}
$total = count($exec);
$covered = count($hit);
$grandTotal += $total;
$grandHit += $covered;
$pct = $total > 0 ? round($covered * 100 / $total, 1) : 0.0;
$missing = array_diff(array_keys($exec), array_keys($hit));
sort($missing);
printf("%6.1f%% %3d/%-3d %s\n", $pct, $covered, $total, str_replace(getcwd() . '/', '', $target));
if ($missing && getenv('SHOW_MISSING')) {
echo ' manquantes: ' . implode(', ', $missing) . "\n";
}
}
if ($grandTotal > 0) {
printf("\nTOTAL: %d/%d (%.1f%%)\n", $grandHit, $grandTotal, $grandHit * 100 / $grandTotal);
}
echo 'dumps: ' . count($files) . "\n";