Calculate the real coverage based on an existing php-code-coverage report

dev-master 2014-03-13 13:14 UTC


Build Status Code Coverage Scrutinizer Quality Score License Latest Stable Version

What is "real" coverage?

Given you have a simple class

class SomeClass
    public function someFunction()
        $instance = "important message";    // this line is important
        $a = "This code";
        $b = "is completely";
        $c = "usesless";
        $instance .= "!!!";                 // this one is important too
        if (true) {
            $f = "and has";
            $g = "100% coverage";
        $c .= "!";
        return $instance;                   // and this one is important

And a pretty useless test for this class, that leaves most of the behavior untested

class SomeClassTest extends \PHPUnit_Framework_TestCase
    public function testThisTestIsStupid()
        $sut = new SomeClass();
        $instance = $sut->someFunction();

        $this->assertEquals("important message!!!", $instance);

But nevertheless, the test produces 100% coverage for this class

When you run php-real-coverage on this project

Then you will know, what lines are actually tested

In this example, only line 8, 12 and 18 are neccessary to make the test pass.


Add it to your composer.json

"require-dev": {
    "julianseeger/php-real-coverage": "*"

Generate a coverage-report with phpunit

./vendor/bin/phpunit --coverage-php coverage.php

And let php-real-coverage test the quality of your coverage

./vendor/bin/php-real-coverage coverage.php

Roadmap to Version 0.1-Beta

  • add proxy-autoloader
  • handle non-composer autoloader (wrap existing autoloaders)
  • keep original method signature in the polymorphic proxies (strict support)
  • allow non-namespaced classes
  • support for magic methods
  • rewrite the prototype of RealCoverageRun (the "main" method)
  • pass appropriate arguments to phpunit
  • support bootstrapping (like "--bootstrap" in phpunit)

Roadmap to Version 1.0

  • integrate symfony/console
  • run only those tests that cover the modified lines (huge speedup)
  • add hooks into main algorithm to allow listeners/printers/etc
  • support all default phpunit coverage writers (html, text, clover, php)
  • review and restructure the architecture


  • only works with phpunit

looking forward to extend it for other frameworks, given there is an audience for it

  • no support for phpunit 4

as soon as phpunit 4 is stable, I will integrate support for phpunit 3 and 4 simultanuously

  • maybe you will run into problems when you abuse reflections or dynamic loading in your project, so php-real-coverage probably won't work for doctrine, etc. But it is basically meant for straight-forward test-driven projects

  • no support for hhvm

I'm currently lacking any experience with hhvm, but as it seems to support phpunit I'm looking forward to change this.


Execute the tests

composer install --prefer-dist --dev

Make your changes tested and in PSR-2

Execute the the tests again

And make your Pull Request

PS: The build will fail if the testcoverage falls below 100%