explaingit

sebastianbergmann/php-code-coverage

8,927PHPAudience · developerComplexity · 2/5Setup · easy

TLDR

A PHP library that tracks which lines of your code are executed during tests, helping you find untested parts of your codebase. Most developers use it through PHPUnit, but it can also run standalone.

Mindmap

mindmap
  root((php-code-coverage))
    What it does
      Tracks tested lines
      Finds untested code
      Generates reports
    Report formats
      OpenClover XML
      HTML output
    Usage
      Via PHPUnit
      Standalone library
    Setup
      Composer install
      Dev dependency only
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Add code coverage reporting to your PHPUnit test suite to see which functions are never tested.

USE CASE 2

Integrate coverage data into a CI pipeline via OpenClover XML output for quality dashboards.

USE CASE 3

Build a custom test runner that collects and processes PHP code coverage without using PHPUnit.

USE CASE 4

Generate coverage reports from previously saved data to track coverage trends over time.

Tech stack

PHPComposerPHPUnit

Getting it running

Difficulty · easy Time to first run · 30min

In plain English

php-code-coverage is a PHP library that measures which lines of your code actually run when your tests execute. This is called code coverage: the idea is to see which parts of your codebase are being tested and which parts are being missed. Once you know that, you can decide where to add more tests. The library handles three steps: collecting the raw data as tests run, processing it, and then producing reports. The reports can be generated in several formats. The README shows an example producing an OpenClover XML file, which is a format that many continuous integration tools and code quality dashboards can read. In practice, most PHP developers encounter this library indirectly through PHPUnit, which is the standard testing framework for PHP. PHPUnit uses php-code-coverage under the hood when you run tests with coverage reporting turned on. The library is also available as a standalone dependency for developers who want to integrate coverage tracking into their own tools or custom test setups. Installing it requires Composer, the standard PHP package manager, and it is typically added as a development-only dependency since coverage reporting is not needed in production. The README is brief and focused, covering installation and two code examples: one for collecting coverage during a live test run, and one for loading previously saved coverage data and generating a report from it. The repository is maintained by Sebastian Bergmann, who also created PHPUnit, and it lives under the phpunit namespace on Packagist.

Copy-paste prompts

Prompt 1
I'm using php-code-coverage standalone (not through PHPUnit). Show me how to collect coverage for a single PHP file and generate an OpenClover XML report.
Prompt 2
My PHPUnit coverage report shows low coverage on a specific class. Give me a step-by-step guide to write tests that cover the missing lines.
Prompt 3
Show me the minimal PHP code to start collecting coverage, stop collecting, and export a report using the php-code-coverage library directly.
Prompt 4
How do I load a previously saved php-code-coverage data file and generate an HTML report from it?
Open on GitHub → Explain another repo

← sebastianbergmann on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.