Skip to content

Commit b0b97bc

Browse files
committed
Start adding basic branch coverage requirements
1 parent e0ec326 commit b0b97bc

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
@@ -133,6 +133,11 @@ final class CodeCoverage
133133
*/
134134
private $report;
135135

136+
/**
137+
* @var bool
138+
*/
139+
private $determineBranchCoverage;
140+
136141
/**
137142
* @throws RuntimeException
138143
*/
@@ -458,6 +463,23 @@ public function setUnintentionallyCoveredSubclassesWhitelist(array $whitelist):
458463
$this->unintentionallyCoveredSubclassesWhitelist = $whitelist;
459464
}
460465

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

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
@@ -23,6 +23,11 @@ final class Xdebug implements Driver
2323
*/
2424
private $cacheNumLines = [];
2525

26+
/**
27+
* @var bool
28+
*/
29+
private $determineBranchCoverage = false;
30+
2631
/**
2732
* @throws RuntimeException
2833
*/
@@ -42,11 +47,16 @@ public function __construct()
4247
*/
4348
public function start(bool $determineUnusedAndDead = true): void
4449
{
50+
$flag = 0;
51+
4552
if ($determineUnusedAndDead) {
46-
\xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE);
47-
} else {
48-
\xdebug_start_code_coverage();
53+
$flag = XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE;
54+
}
55+
56+
if ($this->determineBranchCoverage) {
57+
$flag |= XDEBUG_CC_BRANCH_CHECK;
4958
}
59+
\xdebug_start_code_coverage($flag);
5060
}
5161

5262
/**
@@ -61,6 +71,17 @@ public function stop(): array
6171
return $this->cleanup($data);
6272
}
6373

74+
/**
75+
* Specify that branch coverage should be included with collected code coverage information.
76+
*/
77+
public function setDetermineBranchCoverage(bool $flag): void
78+
{
79+
if ($flag && \version_compare(\phpversion('xdebug'), '2.3.2', '<')) {
80+
throw new RuntimeException('Branch coverage requires Xdebug 2.3.2 or newer');
81+
}
82+
$this->determineBranchCoverage = $flag;
83+
}
84+
6485
private function cleanup(array $data): array
6586
{
6687
foreach (\array_keys($data) as $file) {

0 commit comments

Comments
 (0)