Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does Xdebug in PHP return?

This code with PHP/Xdebug (using Xdebug 2.2.1 and PHP 5.4 on Windows 7 x64, also I have added a line counter for readability):

1: xdebug_start_code_coverage();
2:
3: for ($ii = 0; $ii < 3; ++$ii)
4:    $x = time();
5:
6: if (false)
7:    echo "Never executed.";
8:
9: echo var_dump(xdebug_get_code_coverage());

..I get this output (I have modified some values to make it more readable):

array (size=1)
  '..\testpage.php' => 
    array (size=4)
      3 => int 1
      4 => int 1
      7 => int 1
      9 => int 1

The documentation over at Xdebug's site says:

"The value in the elements represents the total number of execution units on this line have been executed."

Source: http://www.xdebug.com/docs/code_coverage

So apparently the output is wrong. Line 3 and 4 should have been executed three times whereas Xdebug said 1 respectively. Line 6 should have been executed once, Xdebug didn't have a saying at all. Line 7 should definitely not have been executed, yet Xdebug said it did execute once. It should be said that the output stays the same with or without curly braces.

Over at this url: http://xdebug.org/archives/xdebug-general/0377.html

.. Derick Rethans is caught saying (7 years ago!) that somehow the documentation is wrong (and still it is), that Xdebug only returns -1, 0 or 1. However, as my example shows, Xdebug return 1 straight through and the rigged counter seems to make his selection arbitrary. Even if Xdebug did return -1, 0 or 1, I wouldn't know what those values says.

So, anyone of you elite coders have an idea?? And if there's seriously something wrong with Xdebug here, does that mean I can't trust any other applications and plugins used for profiling and code coverage that in their turn use Xdebug? I'm thinking Phing and PHPUnit which seems to be a common marriage.

Also if you feel like elaborating some on this issue; if Xdebug is faulty and as such, all dependent applications, what do you use for code coverage reports in PHP?

EDIT: The output listed above using the same code example, is unchanged if I send arguments XDEBUG_CC_UNUSED or XDEBUG_CC_DEAD_CODE as arguments to xdebug_start_code_coverage. I'm beginning to think that Xdebug doesn't work for code coverage at all, not on my system.

like image 841
Martin Andersson Avatar asked Sep 22 '12 22:09

Martin Andersson


1 Answers

I know there is an issue when using conditionals with indentation syntax.

XDebug only knows about "statements" and doesn't actually understand "lines" even though that's how it is displayed and talked about.

This:

1  <?php
2
3  xdebug_start_code_coverage();
4
5  for ($ii = 0; $ii < 3; ++$ii) {
6     $x = time();
7  }
8 
9  if (false) {
10    echo "Never executed.";
11 }
12 
13 echo var_dump(xdebug_get_code_coverage());

produces:

array(1) {
  ["./test.php"]=>
  array(5) {
    [5]  => int(1)
    [6]  => int(1)
    [7]  => int(1)
    [9]  => int(1)
    [13] => int(1)
  }
}

For more information, see: Edge Cases in the PHPUnit manual

and other SO questions:

  • Why does PHPUnit show some close-curly-braces as not being covered?
  • Reaching 100% Code Coverage with PHPUnit
  • code coverage of xDebug and PHPUnit says 100%, in fact it is not
like image 162
willoller Avatar answered Oct 15 '22 19:10

willoller