php-code-coverage: Executable code not shown as executed/executable

Moved here from #411 (where more than one issue was discussed).

Environment

  • Linux
  • PHP 7.1.0RC6
  • Xdebug 2.5.0RC1
  • PHPUnit 5.6.7
  • php-code-coverage 4.0.3

Steps to reproduce

git clone https://github.com/ramsey/uuid-doctrine.git
cd uuid-doctrine
git checkout abbcbe56b023319dea7a72177333089cf47f5a69
composer install
./vendor/bin/phpunit

Actual result

e9f95398-d168-11e5-901a-a8b81661e3a2

Expected result

Lines 50-52 of the uuid-doctrine/src/UuidBinaryType.php files should not be white.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 5
  • Comments: 30 (17 by maintainers)

Commits related to this issue

Most upvoted comments

FYI I see xdebug 2.5.1 was released today and includes the patch for this issue https://xdebug.org/updates.php#x_2_5_1

It does! Please add an xdebug ticket with the relevant info!

Ok, after some digging I have been able to build a small script that reproduces the problem in XDebug and it is completely independent of the phpunit code. Here it is:

// test.php
xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE);

include 'TestClass2.php';
include 'TestClass.php';
$Test1 = new TestClass();

var_dump(xdebug_get_code_coverage());
// TestClass.php
class TestClass extends TestClass2
{
    public function test()
    {
        $a = 5*5;
        $b = $a*2;
        return $b;
    }
}
// TestClass2.php
class TestClass2
{
    public function test()
    {
        $a = 5*5;
        $b = $a*2;
        return $b;
    }
}

As you can see TestClass extends TestClass2. When I remove extends TestClass2, the bug does not occur. Also, when I put the line include 'TestClass2.php' directly in TestClass.php, the bug also does not occur. So it has to do with the fact that the class is included in a top level file, but extended from in another file. The bug does occur in PHP7, but not in PHP5.

This shows an XDebug related issue, therefore I will start an issue thread on their bugtracker.

Fast work there! Thank you so much, I’ll compile and try out the master branch now. Kudos to JoepRoebroek for figuring the cause. Happy New Year!

Thanks @lordelph 👍

Thanks for investigating!

Ok, I investigated the code some more and concluded that my initial addition should already be done by the function initializeFilesThatAreSeenTheFirstTime. However this function is flawed when it is called multiple times and is presented with the same file, but where each file contains different lines in its array.

Therefore, the following fix also works:

 foreach ($data as $file => $lines) {
-    if ($this->filter->isFile($file) && !isset($this->data[$file])) {
+    if ($this->filter->isFile($file)) {
+        if(!isset($this->data[$file])) {
               $this->data[$file] = [];
+       }
        foreach ($lines as $k => $v) {
+           if (!isset($this->data[$file][$k])) {
                $this->data[$file][$k] = $v == -2 ? null : [];
+           }
        }
    }
}

Let me know what you think. I tried phpunit with #!/usr/bin/env php5 aswell, to see what the difference is. And with PHP5, the inital call to xdebug_get_code_coverage returns an array with all lines containing executable code, whereas in PHP7 this does not happen. Therefore, I think the above fix is sufficient.