View Issue Details

IDProjectCategoryView StatusLast Update
0001917XdebugDocumentationpublic2020-12-18 14:52
ReporterDeadApe Assigned Toderick  
PrioritynormalSeverityminorReproducibilityalways
Status closedResolutionfixed 
Product Version3.0.1 
Target Version3.0dev 
Summary0001917: Document that code coverage flags only affect new function scopes
DescriptionI stumbled upon a behaviour of the code coverage component that I consider to be a bug OR insufficiently documented. It is so obvious that I almost can not believe it really is a bug, but at the same time I have no other explanation for it.

I have the following code sample:
<?php
    xdebug_start_code_coverage(XDEBUG_CC_UNUSED);
    echo "Hello world";
    if (isset($foo)) {
        echo "Foo Bar";
    }
    var_dump(xdebug_get_code_coverage());


What I expect: Code coverage to return a 1 for the executed lines 3, 4 and 7 and a -1 for an unexecuted line 5.
  array(3) {
    [3]=>
    int(1)
    [4]=>
    int(1)
    [5]=>
    int(-1)
    [7]=>
    int(1)
  }


Actual result: Line 5 is missing in the result.
  array(3) {
    [3]=>
    int(1)
    [4]=>
    int(1)
    [7]=>
    int(1)
  }


Additionally, I'd expect line 6 to appear in the result array with value -2 when passing the XDEBUG_CC_DEAD_CODE flag. This is not the case either.
Steps To ReproduceExecute the given PHP snippet
Additional Informationphp -v
PHP 7.4.13 (cli) (built: Nov 24 2020 12:43:32) ( ZTS Visual C++ 2017 x64 )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Xdebug v3.0.1, Copyright (c) 2002-2020, by Derick Rethans

(prebuilt PHP binary from php.net; I was able to reproduce the same behaviour on a macOS 10.15.6 using PHP 7.4.9 and XDebug 2.9.6)

php.ini xdebug settings: Untouched, except for
xdebug.mode=coverage
TagsNo tags attached.
Operating SystemWindows 10.0.19041.685
PHP Version7.4.10-7.4.19

Activities

derick

2020-12-17 15:08

administrator   ~0005592

Hi!

Thanks for your report. This is expected behaviour that is undocumented. The algorithm that analyses code for unused and/or dead code only runs for functions, methods, and included scripts that are being executed *after* the call to <code>xdebug_start_code_coverage()</code> is made. Because you are inlining your code in the same script, the analysis for the script has not been run. If you would move your code into a function, or into a file that you include, you will see that it will correctly detect the unused lines and/or dead code.

However, this is not documented at https://xdebug.org/docs/all_functions#xdebug_start_code_coverage so I'm leaving this open and I've reclassified it as a documentation bug.

cheers,
Derick

DeadApe

2020-12-18 13:14

reporter   ~0005596

Hi Derick,

thanks a lot for your explanation. Are you interested in me supplying a pull request to add this to the docs in https://github.com/xdebug/xdebug.org/blob/master/html/docs/include/functions/xdebug_start_code_coverage or is it something you would like to do on your own?

Cheers,
David

derick

2020-12-18 14:52

administrator   ~0005600

Hi,

I saw your offer for a PR to document this after I had already pushed it, so there is no need for it. Thanks for offering it though!

cheers,
Derick

Issue History

Date Modified Username Field Change
2020-12-16 02:16 DeadApe New Issue
2020-12-17 15:08 derick Category Code Coverage => Documentation
2020-12-17 15:08 derick Target Version => 3.0dev
2020-12-17 15:08 derick Note Added: 0005592
2020-12-17 15:09 derick Status new => confirmed
2020-12-18 13:14 DeadApe Note Added: 0005596
2020-12-18 14:52 derick Summary Code Coverage return is missing out unused line even with XDEBUG_CC_UNUSED flag => Document that code coverage flags only affect new function scopes
2020-12-18 14:52 derick Assigned To => derick
2020-12-18 14:52 derick Status confirmed => closed
2020-12-18 14:52 derick Resolution open => fixed
2020-12-18 14:52 derick Note Added: 0005600