Skip to content

Commit b54ec8f

Browse files
Initial work on flag to control Xdebug branch coverage
1 parent 9021391 commit b54ec8f

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

src/CodeCoverage.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,14 @@ class CodeCoverage
134134
*
135135
* @param Driver $driver
136136
* @param Filter $filter
137+
* @param bool $branchCoverage
137138
*
138139
* @throws RuntimeException
139140
*/
140-
public function __construct(Driver $driver = null, Filter $filter = null)
141+
public function __construct(Driver $driver = null, Filter $filter = null, $branchCoverage = false)
141142
{
142143
if ($driver === null) {
143-
$driver = $this->selectDriver();
144+
$driver = $this->selectDriver($branchCoverage);
144145
}
145146

146147
if ($filter === null) {
@@ -1020,11 +1021,13 @@ private function getAllowedLines(array $linesToBeCovered, array $linesToBeUsed)
10201021
}
10211022

10221023
/**
1024+
* @param bool $branchCoverage
1025+
*
10231026
* @return Driver
10241027
*
10251028
* @throws RuntimeException
10261029
*/
1027-
private function selectDriver()
1030+
private function selectDriver($branchCoverage = false)
10281031
{
10291032
$runtime = new Runtime;
10301033

@@ -1037,7 +1040,7 @@ private function selectDriver()
10371040
} elseif ($runtime->isPHPDBG()) {
10381041
return new PHPDBG;
10391042
} else {
1040-
return new Xdebug;
1043+
return new Xdebug($branchCoverage);
10411044
}
10421045
}
10431046

src/Driver/Xdebug.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace SebastianBergmann\CodeCoverage\Driver;
1212

13+
use SebastianBergmann\CodeCoverage\InvalidArgumentException;
1314
use SebastianBergmann\CodeCoverage\RuntimeException;
1415

1516
/**
@@ -27,9 +28,17 @@ class Xdebug implements Driver
2728
private $cacheNumLines = [];
2829

2930
/**
31+
* @var bool
32+
*/
33+
private $branchCoverage = false;
34+
35+
/**
36+
* @param bool $branchCoverage
37+
*
38+
* @throws InvalidArgumentException
3039
* @throws RuntimeException
3140
*/
32-
public function __construct()
41+
public function __construct($branchCoverage = false)
3342
{
3443
if (!extension_loaded('xdebug')) {
3544
throw new RuntimeException('This driver requires Xdebug');
@@ -38,6 +47,16 @@ public function __construct()
3847
if (version_compare(phpversion('xdebug'), '2.2.1', '>=') && !ini_get('xdebug.coverage_enable')) {
3948
throw new RuntimeException('xdebug.coverage_enable=On has to be set in php.ini');
4049
}
50+
51+
if (!is_bool($branchCoverage)) {
52+
throw InvalidArgumentException::create(1, 'boolean');
53+
}
54+
55+
if ($branchCoverage && !defined('XDEBUG_CC_BRANCH_CHECK')) {
56+
throw new RuntimeException('Xdebug 2.3 is required for branch coverage');
57+
}
58+
59+
$this->branchCoverage = $branchCoverage;
4160
}
4261

4362
/**
@@ -47,6 +66,10 @@ public function start()
4766
{
4867
$options = XDEBUG_CC_DEAD_CODE | XDEBUG_CC_UNUSED;
4968

69+
if ($this->branchCoverage) {
70+
$options |= XDEBUG_CC_BRANCH_CHECK;
71+
}
72+
5073
xdebug_start_code_coverage($options);
5174
}
5275

0 commit comments

Comments
 (0)