Skip to content

Commit 0a9ff6e

Browse files
committed
Start adding basic branch coverage requirements
1 parent 6e6a9ee commit 0a9ff6e

File tree

4 files changed

+59
-3
lines changed

4 files changed

+59
-3
lines changed

src/CodeCoverage.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ final class CodeCoverage
134134
*/
135135
private $report;
136136

137+
/**
138+
* @var bool
139+
*/
140+
private $determineBranchCoverage;
141+
137142
/**
138143
* @throws RuntimeException
139144
*/
@@ -457,6 +462,23 @@ public function setUnintentionallyCoveredSubclassesWhitelist(array $whitelist):
457462
$this->unintentionallyCoveredSubclassesWhitelist = $whitelist;
458463
}
459464

465+
/**
466+
* Specify whether branch coverage should be processed, if the chosen driver supports branch coverage
467+
* Branch coverage is only supported for the Xdebug driver, with an xdebug version of >= 2.3.2
468+
*/
469+
public function setDetermineBranchCoverage(bool $flag): void
470+
{
471+
if ($flag) {
472+
if ($this->driver instanceof Xdebug && \version_compare(\phpversion('xdebug'), '2.3.2', '>=')) {
473+
$this->determineBranchCoverage = $flag;
474+
} else {
475+
throw new RuntimeException('Branch coverage requires Xdebug version 2.3.2 or newer');
476+
}
477+
} else {
478+
$this->determineBranchCoverage = false;
479+
}
480+
}
481+
460482
/**
461483
* Determine the priority for a line
462484
*

src/Driver/Driver.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,9 @@ public function start(bool $determineUnusedAndDead = true): void;
4444
* Stop collection of code coverage information.
4545
*/
4646
public function stop(): array;
47+
48+
/**
49+
* Specify that branch coverage should be included with collected code coverage information.
50+
*/
51+
public function setDetermineBranchCoverage(bool $flag): void;
4752
}

src/Driver/PHPDBG.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ public function stop(): array
7676
return $this->detectExecutedLines($fetchedLines, $dbgData);
7777
}
7878

79+
/**
80+
* Specify that branch coverage should be included with collected code coverage information.
81+
*/
82+
public function setDetermineBranchCoverage(bool $flag): void
83+
{
84+
throw new RuntimeException('Branch coverage is not supported in PHPDBG');
85+
}
86+
7987
/**
8088
* Convert phpdbg based data into the format CodeCoverage expects
8189
*/

src/Driver/Xdebug.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ final class Xdebug implements Driver
2929
*/
3030
private $filter;
3131

32+
/**
33+
* @var bool
34+
*/
35+
private $determineBranchCoverage = false;
36+
3237
/**
3338
* @throws RuntimeException
3439
*/
@@ -54,11 +59,16 @@ public function __construct(Filter $filter = null)
5459
*/
5560
public function start(bool $determineUnusedAndDead = true): void
5661
{
62+
$flag = 0;
63+
5764
if ($determineUnusedAndDead) {
58-
\xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE);
59-
} else {
60-
\xdebug_start_code_coverage();
65+
$flag = XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE;
66+
}
67+
68+
if ($this->determineBranchCoverage) {
69+
$flag |= XDEBUG_CC_BRANCH_CHECK;
6170
}
71+
\xdebug_start_code_coverage($flag);
6272
}
6373

6474
/**
@@ -73,6 +83,17 @@ public function stop(): array
7383
return $this->cleanup($data);
7484
}
7585

86+
/**
87+
* Specify that branch coverage should be included with collected code coverage information.
88+
*/
89+
public function setDetermineBranchCoverage(bool $flag): void
90+
{
91+
if ($flag && \version_compare(\phpversion('xdebug'), '2.3.2', '<')) {
92+
throw new RuntimeException('Branch coverage requires Xdebug 2.3.2 or newer');
93+
}
94+
$this->determineBranchCoverage = $flag;
95+
}
96+
7697
private function cleanup(array $data): array
7798
{
7899
foreach (\array_keys($data) as $file) {

0 commit comments

Comments
 (0)