Skip to content

Commit 6134834

Browse files
committed
Restore backward compatibility
Enable pathCoverage by default only when supported
1 parent 905fe15 commit 6134834

File tree

3 files changed

+60
-48
lines changed

3 files changed

+60
-48
lines changed

src/CodeCoverage.php

+28-22
Original file line numberDiff line numberDiff line change
@@ -91,18 +91,20 @@ class PHP_CodeCoverage
9191
*
9292
* @param PHP_CodeCoverage_Driver $driver
9393
* @param PHP_CodeCoverage_Filter $filter
94-
* @param bool $pathCoverage
94+
* @param null|bool $pathCoverage `null` enables path coverage if supported.
9595
* @throws PHP_CodeCoverage_InvalidArgumentException
9696
*/
97-
public function __construct(PHP_CodeCoverage_Driver $driver = null, PHP_CodeCoverage_Filter $filter = null, $pathCoverage = true)
97+
public function __construct(PHP_CodeCoverage_Driver $driver = null, PHP_CodeCoverage_Filter $filter = null, $pathCoverage = null)
9898
{
99-
if (!is_bool($pathCoverage)) {
99+
if ($pathCoverage === null) {
100+
$pathCoverage = version_compare(phpversion('xdebug'), '2.3.2', '>=');
101+
} elseif (!is_bool($pathCoverage)) {
100102
throw PHP_CodeCoverage_InvalidArgumentException::create(
101103
3,
102104
'boolean'
103105
);
104106
}
105-
107+
$pathCoverage = false;
106108
if ($driver === null) {
107109
$driver = $this->selectDriver($pathCoverage);
108110
}
@@ -340,18 +342,20 @@ public function append(array $data, $id = null, $append = true, $linesToBeCovere
340342
}
341343
}
342344

343-
foreach ($fileData['functions'] as $function => $functionCoverage) {
344-
foreach ($functionCoverage['branches'] as $branch => $branchCoverage) {
345-
if ($branchCoverage['hit'] === 1){
346-
$this->data[$file]['branches'][$function][$branch]['hit'] = 1;
347-
if (!in_array($id, $this->data[$file]['branches'][$function][$branch]['tests'])) {
348-
$this->data[$file]['branches'][$function][$branch]['tests'][] = $id;
345+
if ($this->pathCoverage) {
346+
foreach ($fileData['functions'] as $function => $functionCoverage) {
347+
foreach ($functionCoverage['branches'] as $branch => $branchCoverage) {
348+
if ($branchCoverage['hit'] === 1) {
349+
$this->data[$file]['branches'][$function][$branch]['hit'] = 1;
350+
if (!in_array($id, $this->data[$file]['branches'][$function][$branch]['tests'])) {
351+
$this->data[$file]['branches'][$function][$branch]['tests'][] = $id;
352+
}
349353
}
350354
}
351-
}
352-
foreach ($functionCoverage['paths'] as $path => $pathCoverage) {
353-
if ($pathCoverage['hit'] === 1 && $this->data[$file]['paths'][$function][$path]['hit'] === 0){
354-
$this->data[$file]['paths'][$function][$path]['hit'] = 1;
355+
foreach ($functionCoverage['paths'] as $path => $pathCoverage) {
356+
if ($pathCoverage['hit'] === 1 && $this->data[$file]['paths'][$function][$path]['hit'] === 0) {
357+
$this->data[$file]['paths'][$function][$path]['hit'] = 1;
358+
}
355359
}
356360
}
357361
}
@@ -380,15 +384,17 @@ public function merge(PHP_CodeCoverage $that)
380384

381385
foreach ($fileData['lines'] as $line => $data) {
382386
if ($data !== null) {
383-
if (!isset($this->data[$file]['lines'][$line])) {
384-
$this->data[$file]['lines'][$line] = $data;
385-
} else {
386-
if ($data['pathCovered']) {
387-
$this->data[$file]['lines'][$line]['pathCovered'] = $data['pathCovered'];
387+
if ($this->pathCoverage) {
388+
if (!isset($this->data[$file]['lines'][$line])) {
389+
$this->data[$file]['lines'][$line] = $data;
390+
} else {
391+
if ($data['pathCovered']) {
392+
$this->data[$file]['lines'][$line]['pathCovered'] = $data['pathCovered'];
393+
}
394+
$this->data[$file]['lines'][$line]['tests'] = array_unique(
395+
array_merge($this->data[$file]['lines'][$line]['tests'], $data['tests'])
396+
);
388397
}
389-
$this->data[$file]['lines'][$line]['tests'] = array_unique(
390-
array_merge($this->data[$file]['lines'][$line]['tests'], $data['tests'])
391-
);
392398
}
393399
}
394400
}

src/CodeCoverage/Driver/Xdebug.php

+11-7
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,14 @@ class PHP_CodeCoverage_Driver_Xdebug implements PHP_CodeCoverage_Driver
2424
/**
2525
* @param bool $pathCoverage
2626
*/
27-
public function __construct($pathCoverage = true)
27+
public function __construct($pathCoverage = false)
2828
{
29-
if (!extension_loaded('xdebug') ||
30-
version_compare(phpversion('xdebug'), '2.3.2', '<')) {
31-
throw new PHP_CodeCoverage_RuntimeException(
32-
'This driver requires Xdebug 2.3.2 (or newer)'
33-
);
29+
if (!extension_loaded('xdebug')) {
30+
throw new PHP_CodeCoverage_RuntimeException('This driver requires Xdebug');
3431
}
3532

36-
if (!ini_get('xdebug.coverage_enable')) {
33+
if (version_compare(phpversion('xdebug'), '2.2.0-dev', '>=') &&
34+
!ini_get('xdebug.coverage_enable')) {
3735
throw new PHP_CodeCoverage_RuntimeException(
3836
'xdebug.coverage_enable=On has to be set in php.ini'
3937
);
@@ -42,6 +40,12 @@ public function __construct($pathCoverage = true)
4240
$this->flags = XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE;
4341

4442
if ($pathCoverage) {
43+
if (version_compare(phpversion('xdebug'), '2.3.2', '<')) {
44+
throw new PHP_CodeCoverage_RuntimeException(
45+
'Path coverage requires Xdebug 2.3.2 (or newer)'
46+
);
47+
}
48+
4549
$this->flags |= XDEBUG_CC_BRANCH_CHECK;
4650
}
4751
}

src/CodeCoverage/Report/Node/File.php

+21-19
Original file line numberDiff line numberDiff line change
@@ -704,29 +704,31 @@ function ($carry, $value) {
704704
protected function calcAndApplyClassAggregate(&$classOrTrait, $classOrTraitName)
705705
{
706706
foreach ($classOrTrait['methods'] as &$method) {
707-
if ($method['methodName'] === 'anonymous function') {
708-
// Locate index
709-
$methodCoveragePath = $method['methodName'];
710-
foreach ($this->coverageData['branches'] as $index => $branch) {
711-
if ($method['startLine'] === $branch[0]['line_start']) {
712-
$methodCoveragePath = $index;
707+
if (isset($this->coverageData['branches'])) {
708+
if ($method['methodName'] === 'anonymous function') {
709+
// Locate index
710+
$methodCoveragePath = $method['methodName'];
711+
foreach ($this->coverageData['branches'] as $index => $branch) {
712+
if ($method['startLine'] === $branch[0]['line_start']) {
713+
$methodCoveragePath = $index;
714+
}
713715
}
714-
}
715716

716-
} else {
717-
$methodCoveragePath = $classOrTraitName . '->' . $method['methodName'];
718-
}
719-
if (isset($this->coverageData['paths'][$methodCoveragePath])) {
720-
$methodPaths = $this->coverageData['paths'][$methodCoveragePath];
721-
$this->calcPathsAggregate($methodPaths, $numExecutablePaths, $numExecutedPaths);
717+
} else {
718+
$methodCoveragePath = $classOrTraitName . '->' . $method['methodName'];
719+
}
720+
if (isset($this->coverageData['paths'][$methodCoveragePath])) {
721+
$methodPaths = $this->coverageData['paths'][$methodCoveragePath];
722+
$this->calcPathsAggregate($methodPaths, $numExecutablePaths, $numExecutedPaths);
722723

723-
$method['executablePaths'] = $numExecutablePaths;
724-
$classOrTrait['executablePaths'] += $numExecutablePaths;
725-
$this->numExecutablePaths += $numExecutablePaths;
724+
$method['executablePaths'] = $numExecutablePaths;
725+
$classOrTrait['executablePaths'] += $numExecutablePaths;
726+
$this->numExecutablePaths += $numExecutablePaths;
726727

727-
$method['executedPaths'] = $numExecutedPaths;
728-
$classOrTrait['executedPaths'] += $numExecutedPaths;
729-
$this->numExecutedPaths += $numExecutedPaths;
728+
$method['executedPaths'] = $numExecutedPaths;
729+
$classOrTrait['executedPaths'] += $numExecutedPaths;
730+
$this->numExecutedPaths += $numExecutedPaths;
731+
}
730732
}
731733
if ($method['executableLines'] > 0) {
732734
$method['coverage'] = ($method['executedLines'] / $method['executableLines']) * 100;

0 commit comments

Comments
 (0)