Skip to content

Commit 1b4794c

Browse files
dvdougsebastianbergmann
authored andcommitted
Store captured branch/path function data
1 parent 9952b35 commit 1b4794c

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

src/RawCodeCoverageData.php

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,43 @@ final class RawCodeCoverageData
2121
*/
2222
private $lineCoverage = [];
2323

24+
/**
25+
* @var array
26+
*
27+
* @see https://xdebug.org/docs/code_coverage for format
28+
*/
29+
private $functionCoverage = [];
30+
2431
public static function fromXdebugWithoutPathCoverage(array $rawCoverage): self
2532
{
26-
return new self($rawCoverage);
33+
return new self($rawCoverage, []);
2734
}
2835

2936
public static function fromXdebugWithPathCoverage(array $rawCoverage): self
3037
{
31-
$lineCoverage = [];
38+
$lineCoverage = $functionCoverage = [];
3239

3340
foreach ($rawCoverage as $file => $fileCoverageData) {
34-
$lineCoverage[$file] = $fileCoverageData['lines'];
41+
if (isset($fileCoverageData['functions'])) {
42+
$lineCoverage[$file] = $fileCoverageData['lines'];
43+
$functionCoverage[$file] = $fileCoverageData['functions'];
44+
} else { // not every file has functions, Xdebug outputs just line data for these
45+
$lineCoverage[$file] = $fileCoverageData;
46+
}
3547
}
3648

37-
return new self($lineCoverage);
49+
return new self($lineCoverage, $functionCoverage);
3850
}
3951

40-
private function __construct(array $lineCoverage)
52+
private function __construct(array $lineCoverage, array $functionCoverage)
4153
{
42-
$this->lineCoverage = $lineCoverage;
54+
$this->lineCoverage = $lineCoverage;
55+
$this->functionCoverage = $functionCoverage;
4356
}
4457

4558
public function clear(): void
4659
{
47-
$this->lineCoverage = [];
60+
$this->lineCoverage = $this->functionCoverage = [];
4861
}
4962

5063
public function getLineCoverage(): array
@@ -54,7 +67,7 @@ public function getLineCoverage(): array
5467

5568
public function removeCoverageDataForFile(string $filename): void
5669
{
57-
unset($this->lineCoverage[$filename]);
70+
unset($this->lineCoverage[$filename], $this->functionCoverage[$filename]);
5871
}
5972

6073
/**

tests/tests/RawCodeCoverageDataTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ public function testLineDataFromStandardXDebugFormat(): void
3030
}
3131

3232
/**
33-
* In the path-coverage XDebug format, the line data exists inside a "lines" array key.
33+
* In the path-coverage XDebug format, the line data exists inside a "lines" array key where the file has
34+
* classes or functions. For files without them, the data is stored in the line-only format.
3435
*/
3536
public function testLineDataFromPathCoverageXDebugFormat(): void
3637
{
@@ -45,6 +46,11 @@ public function testLineDataFromPathCoverageXDebugFormat(): void
4546

4647
],
4748
],
49+
'/some/path/justAScript.php' => [
50+
18 => 1,
51+
19 => -2,
52+
113 => -1,
53+
],
4854
];
4955

5056
$lineData = [
@@ -53,6 +59,11 @@ public function testLineDataFromPathCoverageXDebugFormat(): void
5359
9 => -2,
5460
13 => -1,
5561
],
62+
'/some/path/justAScript.php' => [
63+
18 => 1,
64+
19 => -2,
65+
113 => -1,
66+
],
5667
];
5768

5869
$dataObject = RawCodeCoverageData::fromXdebugWithPathCoverage($rawDataFromDriver);

0 commit comments

Comments
 (0)