Converts PHPUnit's Clover XML coverage report into compact JSON for LLM analysis.
Generate a Clover report, then convert it:
vendor/bin/phpunit --coverage-clover=coverage/clover.xml --coverage-filter src
php scripts/clover-to-json.php coverage/clover.xml > coverage/coverage-summary.jsonJSON with three sections:
generated– Unix timestamptotals– Project-wide coveragefiles– Per-file breakdown with uncovered line numbers
Example:
{
"generated": "1763041588",
"totals": {
"statements": 45,
"covered_statements": 27,
"coverage_percent": 60
},
"files": [
{
"file": "/path/to/src/functions.php",
"statements": 45,
"covered_statements": 27,
"coverage_percent": 60,
"uncovered_lines": [38, 46, 65, 66, 67]
}
]
}Show files below 80% coverage:
php scripts/clover-to-json.php coverage/clover.xml --min-percent 80Show the 5 lowest-coverage files:
php scripts/clover-to-json.php coverage/clover.xml --top 5Combine filters:
php scripts/clover-to-json.php coverage/clover.xml --top 10 --min-percent 90Paste the JSON into your LLM conversation to get targeted suggestions:
"Here's my code coverage report. Which untested lines pose the highest risk?"
Or ask specific questions:
"Lines 65-76 in functions.php are untested. What test cases should I add?"
The compact format keeps token usage low while giving the LLM everything it needs.
The script:
- Parses the Clover XML file
- Extracts project totals from
<project><metrics> - Collects per-file metrics from
<file><metrics> - Finds uncovered lines via XPath query for
<line type="stmt" count="0"> - Sorts files by coverage (lowest first)
- Applies optional filters
- Outputs JSON to stdout
- PHP 8.0+
- SimpleXML extension (bundled with PHP)
- A Clover XML report from PHPUnit
The script exits with specific codes:
0- Success2- File not readable3- XML parsing failed
Errors go to stderr, so you can safely redirect stdout to a file.