Skip to content

Instantly share code, notes, and snippets.

@wanghuida
Created December 21, 2012 06:06
Show Gist options
  • Save wanghuida/4350970 to your computer and use it in GitHub Desktop.
Save wanghuida/4350970 to your computer and use it in GitHub Desktop.
generate xdebug coverage
<?php
$xdebug_coverage_log_file_prefix = "/tmp/xdebug/v2-coverageLog-";
xdebug_start_code_coverage();
register_shutdown_function('coverageLog');
$xdebug_coverage_log_file = $xdebug_coverage_log_file_prefix . date('Ymd') . '.log';
function coverageLog() {
global $xdebug_coverage_log_file;
$logs = xdebug_get_code_coverage();
$mem = new Memcache();
$mem->connect('127.0.0.1', 11211) or die ("Could not connect memcache");
$uri = $mem->get($_SERVER['REQUEST_URI']);
if( empty($uri) ) {
$log_pre = "#{$_SERVER['REQUEST_URI']}\n";
$tmp = array();
foreach($logs as $filepath => $coverageLines) {
$tmp[] = "\t$filepath|" . implode(',', xdebug_merge_next(array_keys($coverageLines)));
}
sort($tmp);
file_put_contents($xdebug_coverage_log_file, $log_pre . join("\n", $tmp) . "\n", FILE_APPEND | LOCK_EX);
$mem->set($_SERVER['REQUEST_URI'], 1, false, 86400);
}
$mem->close();
}
function xdebug_merge_next($array) {
$rt = array();
for($i = 0; $i < count($array); $i++) {
$current = $array[$i];
$rt[] = $current;
$next = $current;
if(($next + 1) == $array[$i + 1]) {
for($j = $i + 1; $j <= count($array); $j++) {
$next += 1;
if($next != $array[$j]) {
$i = $j - 1;
break;
}
}
$rt[] = '-';
$rt[] = $next - 1;
}
}
return $rt;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment