Backend Development 5 min read

Implementing Incremental Code Coverage for PHP Projects Using Xdebug, Git, and php-code-coverage

This article explains how to set up incremental code coverage for PHP applications by installing Xdebug, leveraging Git diff to identify changed files, and extending php-code-coverage to generate reports that highlight coverage gaps in newly added code, thereby improving test efficiency and reducing manual effort.

360 Quality & Efficiency
360 Quality & Efficiency
360 Quality & Efficiency
Implementing Incremental Code Coverage for PHP Projects Using Xdebug, Git, and php-code-coverage

Code coverage is a useful metric for assessing test completeness; while 100% coverage does not guarantee bug‑free software, higher coverage generally correlates with fewer defects. The author describes a project where code volume grew rapidly and manual testing proved insufficient, prompting the adoption of automated, incremental coverage analysis.

Implementation Overview

The solution relies on three main components: xdebug for collecting raw coverage data, git to extract the incremental code changes, and php-code-coverage (a PHPUnit library) modified to merge the incremental information and produce a combined report.

Environment Dependencies

Xdebug – a PHP extension that gathers line‑level execution data.

Git – used to obtain the diff between the current commit and the previous tag.

php-code-coverage (version 6.0.7) – extended to accept the incremental diff and generate coverage reports in XML/HTML.

Step 1: Install Xdebug

Download the appropriate binary from the official site (e.g., http://xdebug.org.cn/download.php ), place the .dll (Windows) or compile the .so (Linux) and enable it in php.ini with xdebug.mode=coverage . Verify installation with php -m – the output should list Xdebug.

Step 2: Collect Coverage Data

Configure auto_prepend_file in php.ini to load a bootstrap script that starts coverage when a specific trigger (e.g., a cookie named XDEBUG_COVERAGE_START ) is present. In Nginx, check the cookie and forward the trigger via fastcgi_param to PHP.

Step 3: Obtain Incremental Code Information

Use the command git diff $(git describe --tags --abbrev=0 $commit_id) $commit_id to list changes between the latest tag and the current commit. Parse the diff with regular expressions to produce a file list compatible with the coverage format.

Step 4: Generate the Coverage Report

Extend php-code-coverage to ingest the incremental file list from step 3, merge it with the raw Xdebug data, and output a report that highlights coverage for the newly added code.

The article concludes with screenshots of the setup and a brief promotion of the Qtest testing team, emphasizing the practical benefits of incremental coverage for reducing manual testing effort.

code coverageautomationtestinggitPHPxdebug
360 Quality & Efficiency
Written by

360 Quality & Efficiency

360 Quality & Efficiency focuses on seamlessly integrating quality and efficiency in R&D, sharing 360’s internal best practices with industry peers to foster collaboration among Chinese enterprises and drive greater efficiency value.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.