Code coverage analysis can let you know which line (or section) of the script is being executed when requested.
Related settings
xdebug.coverage_enable
Type: boolean, Default value: 1, starts with
Xdebug >= 2.2
If set to 0, Xdebug will not set up built-in structures to allow code analysis. This can speed up xdebug faster, but code coverage analysis will not work.
Returns a Boolean value to see if code coverage analysis has started.
Example:
<?<span>php </span><span>var_dump</span><span>(xdebug_code_coverage_started()); xdebug_start_code_coverage(); </span><span>var_dump</span><span>(xdebug_code_coverage_started()); </span>?>
Returns:
bool(false)
bool(true)
array xdebug_get_code_coverage()
Returns a structure containing which line of the script (including referenced files) is being executed. The following shows an example of code coverage specifying a certain file:
Example:
<?<span>php xdebug_start_code_coverage(); </span><span>function</span> a(<span>$a</span><span>) { </span><span>echo</span> <span>$a</span> * 2.5<span>; } </span><span>function</span> b(<span>$count</span><span>) { </span><span>for</span> (<span>$i</span> = 0; <span>$i</span> < <span>$count</span>; <span>$i</span>++<span>) { a(</span><span>$i</span> + 0.17<span>); } } b(</span>6<span>); b(</span>10<span>); </span><span>var_dump</span><span>(xdebug_get_code_coverage()); </span>?>
Returns:
array
'/home/httpd/html/test/xdebug/docs/xdebug_get_code_coverage.php' =>
array
5 => int 1
6 => int 1
7 => int 1
9 => int 1
10 => int 1
11 => int 1
12 => int 1
13 => int 1
15 => int 1
16 => int 1
18 => int 1
void xdebug_start_code_coverage( [int options] )
Start code coverage analysis
This function starts collecting code coverage information. This information is formed by a two-dimensional array. The first-dimensional index is the execution file name and the second-dimensional index is the line number. The element value indicates whether the line was executed or it has unreachable lines.
Value returned for each row:
The -1 value will only be returned when XDEBUG_CC_UNUSED is turned on, and the -2 value will be returned only when XDEBUG_CC_UNUSED and XDEBUG_CC_DEAD_CODE are turned on at the same time.
The function has two options with enumeration values:
XDEBUG_CC_UNUSED
Enable code detection and indicate which lines of code are executable. Without this item, the returned array only contains the actual executed line numbers.
XDEBUG_CC_DEAD_CODE
Start additional analysis to show which code can be executed.
If these options are enabled, code coverage analysis will be significantly slowed down.
The following examples show how to use the options:
Example:
<?<span>php xdebug_start_code_coverage( XDEBUG_CC_UNUSED </span>|<span> XDEBUG_CC_DEAD_CODE ); </span>?>
void xdebug_stop_code_coverage( [int cleanup=true] )
Stop code coverage analysis
This function stops collecting information and the information will be cleared in memory. If you pass the "false" parameter, the code coverage information will not be cleared and you can use xdebug_start_code_coverage() again to resume information collection.