Generate Code Coverage with native PHP Debugger (no xdebug required)

Sometimes when using brand new versions of PHP or on servers where you are unable to install extensions, it might be useful to generate test coverage without needing to use xdebug. That's where PHPs native debugger comes in: phpdbg. It's part of the default PHP installation and basically available everywhere you have access to PHP.

Tip
If you are only generating coverage (for example in CI environments), phpdbg might well be faster than using xdebug. !!!

Using PHPDBG

To generate coverage with phpdbg use it as PHP executable instead of your normal PHP binary. For example, instead of /usr/bin/php vendor/bin/phpunit ... run /usr/bin/phpdbg -qrr vendor/bin/phpunit (-q disables the welcome banner, -rr runs the script and exits the debugger afterwards).

Example: Generate Coverage for the TYPO3 Core

To have a small example of how to use phpdbg to generate coverage and to have some numbers to compare, I'm running the 10.035 Unit Tests of the TYPO3 Core including generation of coverage in xml and HTML formats. The first run is done with xdebug disabled using only phpdbg, the second run is using xdebug to achieve the same result:

Generate Coverage with PHPDBG

The phpdbg coverage was generated using the following command (using PHP 7.4 as base version):

phpdbg7.4 -d memory_limit=1024M -qrr vendor/phpunit/phpunit/phpunit \
-c phpunit.xml --verbose \
--log-junit var/log/junit/phpunit.xml \
--coverage-clover var/log/junit/cover \
--coverage-html var/log/coverage-phpdbg

Resulting in:

Generated by php-code-coverage 9.1.3 using PHPDBG 7.4.3 and PHPUnit 9.3.5 at Sun Aug 16 13:05:10 UTC 2020.
Time: 02:39.950, Memory: 570.01 MB
Generating code coverage report in Clover XML format ... done [00:25.196]
Generating code coverage report in HTML format ... done [00:12.011]

Comparison to XDebug

The xdebug coverage was generated using the following command (using PHP 7.4 as base version):

/bin/php7.4 -dxdebug.coverage_enable=1 vendor/phpunit/phpunit/phpunit \
-c phpunit.xml --verbose \
--log-junit var/log/junit/phpunit.xml \
--coverage-clover var/log/junit/cover \
--coverage-html var/log/coverage

Resulting in

Generated by php-code-coverage 9.1.3 using PHP 7.4.3 with Xdebug 2.9.2 and PHPUnit 9.3.5 at Sun Aug 16 12:59:50 UTC 2020.
Time: 03:04.453, Memory: 528.25 MB
Generating code coverage report in Clover XML format ... done [00:32.479]
Generating code coverage report in HTML format ... done [00:15.872]

Conclusion

As you can see, generating code coverage with phpdbg is fairly easy - and in case of the TYPO3 Core Unit Tests about 25% faster than using xdebug while using slightly more memory.

However, it depends on the use case and the amount of tests you are running. I still prefer using xdebug for my local systems for its superior debugging capabilities and usually use phpdbg in environments where I do not have xdebug available or where I only generate coverage (in CI mostly).

Give it a try
Try it yourself and see what works better for your use cases.