Skip to content

Commit 4520d9d

Browse files
committed
Executable lines differ a lot between standard and path coverage: filter lines only for standard one
1 parent ea7bbe0 commit 4520d9d

File tree

3 files changed

+24
-15
lines changed

3 files changed

+24
-15
lines changed

src/CodeCoverage.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,8 @@ private function applyCoversAnnotationFilter(RawCodeCoverageData $rawData, $line
468468

469469
if (is_array($linesToBeCovered)) {
470470
foreach ($linesToBeCovered as $fileToBeCovered => $includedLines) {
471-
$rawData->keepCoverageDataOnlyForLines($fileToBeCovered, $includedLines);
471+
$rawData->keepLineCoverageDataOnlyForLines($fileToBeCovered, $includedLines);
472+
$rawData->keepFunctionCoverageDataOnlyForLines($fileToBeCovered, $includedLines);
472473
}
473474
}
474475
}
@@ -489,7 +490,7 @@ private function applyFilter(RawCodeCoverageData $data): void
489490
private function applyExecutableLinesFilter(RawCodeCoverageData $data): void
490491
{
491492
foreach (array_keys($data->lineCoverage()) as $filename) {
492-
$data->keepCoverageDataOnlyForLines(
493+
$data->keepLineCoverageDataOnlyForLines(
493494
$filename,
494495
$this->uncoveredFileAnalyser()->executableLinesIn($filename)
495496
);

src/RawCodeCoverageData.php

+18-10
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public function removeCoverageDataForFile(string $filename): void
126126
/**
127127
* @param int[] $lines
128128
*/
129-
public function keepCoverageDataOnlyForLines(string $filename, array $lines): void
129+
public function keepLineCoverageDataOnlyForLines(string $filename, array $lines): void
130130
{
131131
if (!isset($this->lineCoverage[$filename])) {
132132
return;
@@ -136,17 +136,25 @@ public function keepCoverageDataOnlyForLines(string $filename, array $lines): vo
136136
$this->lineCoverage[$filename],
137137
array_flip($lines)
138138
);
139+
}
139140

140-
if (isset($this->functionCoverage[$filename])) {
141-
foreach ($this->functionCoverage[$filename] as $functionName => $functionData) {
142-
foreach ($functionData['branches'] as $branchId => $branch) {
143-
if (count(array_diff(range($branch['line_start'], $branch['line_end']), $lines)) > 0) {
144-
unset($this->functionCoverage[$filename][$functionName]['branches'][$branchId]);
141+
/**
142+
* @param int[] $lines
143+
*/
144+
public function keepFunctionCoverageDataOnlyForLines(string $filename, array $lines): void
145+
{
146+
if (!isset($this->functionCoverage[$filename])) {
147+
return;
148+
}
145149

146-
foreach ($functionData['paths'] as $pathId => $path) {
147-
if (in_array($branchId, $path['path'], true)) {
148-
unset($this->functionCoverage[$filename][$functionName]['paths'][$pathId]);
149-
}
150+
foreach ($this->functionCoverage[$filename] as $functionName => $functionData) {
151+
foreach ($functionData['branches'] as $branchId => $branch) {
152+
if (count(array_diff(range($branch['line_start'], $branch['line_end']), $lines)) > 0) {
153+
unset($this->functionCoverage[$filename][$functionName]['branches'][$branchId]);
154+
155+
foreach ($functionData['paths'] as $pathId => $path) {
156+
if (in_array($branchId, $path['path'], true)) {
157+
unset($this->functionCoverage[$filename][$functionName]['paths'][$pathId]);
150158
}
151159
}
152160
}

tests/tests/RawCodeCoverageDataTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,9 @@ public function testKeepCoverageDataOnlyForLines(): void
211211

212212
$dataObject = RawCodeCoverageData::fromXdebugWithoutPathCoverage($lineDataFromDriver);
213213

214-
$dataObject->keepCoverageDataOnlyForLines('/some/path/SomeClass.php', [9, 13]);
215-
$dataObject->keepCoverageDataOnlyForLines('/some/path/SomeOtherClass.php', [999]);
216-
$dataObject->keepCoverageDataOnlyForLines('/some/path/AnotherClass.php', [28]);
214+
$dataObject->keepLineCoverageDataOnlyForLines('/some/path/SomeClass.php', [9, 13]);
215+
$dataObject->keepLineCoverageDataOnlyForLines('/some/path/SomeOtherClass.php', [999]);
216+
$dataObject->keepLineCoverageDataOnlyForLines('/some/path/AnotherClass.php', [28]);
217217

218218
$this->assertEquals($expectedFilterResult, $dataObject->lineCoverage());
219219
}

0 commit comments

Comments
 (0)