Skip to content

Commit 267336f

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

File tree

3 files changed

+55
-54
lines changed

3 files changed

+55
-54
lines changed

src/CodeCoverage.php

+23-28
Original file line numberDiff line numberDiff line change
@@ -81,22 +81,19 @@ class PHP_CodeCoverage
8181
*/
8282
private $tests = [];
8383

84-
/**
85-
* @var bool
86-
*/
87-
private $pathCoverage;
88-
8984
/**
9085
* Constructor.
9186
*
9287
* @param PHP_CodeCoverage_Driver $driver
9388
* @param PHP_CodeCoverage_Filter $filter
94-
* @param bool $pathCoverage
89+
* @param null|bool $pathCoverage `null` enables path coverage if supported.
9590
* @throws PHP_CodeCoverage_InvalidArgumentException
9691
*/
97-
public function __construct(PHP_CodeCoverage_Driver $driver = null, PHP_CodeCoverage_Filter $filter = null, $pathCoverage = true)
92+
public function __construct(PHP_CodeCoverage_Driver $driver = null, PHP_CodeCoverage_Filter $filter = null, $pathCoverage = null)
9893
{
99-
if (!is_bool($pathCoverage)) {
94+
if ($pathCoverage === null) {
95+
$pathCoverage = version_compare(phpversion('xdebug'), '2.3.2', '>=');
96+
} elseif (!is_bool($pathCoverage)) {
10097
throw PHP_CodeCoverage_InvalidArgumentException::create(
10198
3,
10299
'boolean'
@@ -113,7 +110,6 @@ public function __construct(PHP_CodeCoverage_Driver $driver = null, PHP_CodeCove
113110

114111
$this->driver = $driver;
115112
$this->filter = $filter;
116-
$this->pathCoverage = $pathCoverage;
117113
}
118114

119115
/**
@@ -600,7 +596,11 @@ private function initializeFilesThatAreSeenTheFirstTime(array $data)
600596
continue;
601597
}
602598

603-
$this->data[$file] = ['lines' => []];
599+
$this->data[$file] = [
600+
'lines' => [],
601+
'branches' =>[],
602+
'paths' => [],
603+
];
604604

605605
foreach ($fileData['lines'] as $lineNumber => $flag) {
606606
if ($flag === PHP_CodeCoverage_Driver::LINE_NOT_EXECUTABLE) {
@@ -613,26 +613,21 @@ private function initializeFilesThatAreSeenTheFirstTime(array $data)
613613
}
614614
}
615615

616-
if ($this->pathCoverage) {
617-
$this->data[$file]['branches'] = [];
618-
$this->data[$file]['paths'] = [];
616+
foreach ($fileData['functions'] as $functionName => $functionData) {
617+
$this->data[$file]['branches'][$functionName] = [];
618+
$this->data[$file]['paths'][$functionName] = $functionData['paths'];
619619

620-
foreach ($fileData['functions'] as $functionName => $functionData) {
621-
$this->data[$file]['branches'][$functionName] = [];
622-
$this->data[$file]['paths'][$functionName] = $functionData['paths'];
623-
624-
foreach ($functionData['branches'] as $index => $branch) {
625-
$this->data[$file]['branches'][$functionName][$index] = [
626-
'hit' => $branch['hit'],
627-
'line_start' => $branch['line_start'],
628-
'line_end' => $branch['line_end'],
629-
'tests' => []
630-
];
620+
foreach ($functionData['branches'] as $index => $branch) {
621+
$this->data[$file]['branches'][$functionName][$index] = [
622+
'hit' => $branch['hit'],
623+
'line_start' => $branch['line_start'],
624+
'line_end' => $branch['line_end'],
625+
'tests' => []
626+
];
631627

632-
for ($i = $branch['line_start']; $i < $branch['line_end']; $i++) {
633-
if (isset($this->data[$file]['lines'][$i])) {
634-
$this->data[$file]['lines'][$i]['pathCovered'] = (bool) $branch['hit'];
635-
}
628+
for ($i = $branch['line_start']; $i < $branch['line_end']; $i++) {
629+
if (isset($this->data[$file]['lines'][$i])) {
630+
$this->data[$file]['lines'][$i]['pathCovered'] = (bool) $branch['hit'];
636631
}
637632
}
638633
}

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)