From 52f5d29bcf8b36467600fbd3c570b87ec07685e1 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 27 Aug 2025 15:09:36 +0200 Subject: [PATCH 1/3] Improve performance by skipping empty lines after filter has been applied --- src/CodeCoverage.php | 1 + src/Data/RawCodeCoverageData.php | 4 +--- tests/tests/Data/RawCodeCoverageDataTest.php | 1 + 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/CodeCoverage.php b/src/CodeCoverage.php index 9c8853025..819a17fb7 100644 --- a/src/CodeCoverage.php +++ b/src/CodeCoverage.php @@ -403,6 +403,7 @@ private function applyCoversAndUsesFilter(RawCodeCoverageData $rawData, array|fa foreach (array_keys($filesWithNoCoverage) as $fileWithNoCoverage) { $rawData->removeCoverageDataForFile($fileWithNoCoverage); } + $rawData->skipEmptyLines(); if (is_array($linesToBeCovered)) { foreach ($linesToBeCovered as $fileToBeCovered => $includedLines) { diff --git a/src/Data/RawCodeCoverageData.php b/src/Data/RawCodeCoverageData.php index 444f410a7..9b4304648 100644 --- a/src/Data/RawCodeCoverageData.php +++ b/src/Data/RawCodeCoverageData.php @@ -103,8 +103,6 @@ private function __construct(array $lineCoverage, array $functionCoverage) { $this->lineCoverage = $lineCoverage; $this->functionCoverage = $functionCoverage; - - $this->skipEmptyLines(); } public function clear(): void @@ -251,7 +249,7 @@ public function removeCoverageDataForLines(string $filename, array $lines): void * * @see https://github.com/sebastianbergmann/php-code-coverage/issues/799 */ - private function skipEmptyLines(): void + public function skipEmptyLines(): void { foreach ($this->lineCoverage as $filename => $coverage) { foreach ($this->getEmptyLinesForFile($filename) as $emptyLine) { diff --git a/tests/tests/Data/RawCodeCoverageDataTest.php b/tests/tests/Data/RawCodeCoverageDataTest.php index 98c4f45cb..0d2470dd5 100644 --- a/tests/tests/Data/RawCodeCoverageDataTest.php +++ b/tests/tests/Data/RawCodeCoverageDataTest.php @@ -326,6 +326,7 @@ public function testCoverageForFileWithInlineAnnotations(): void 33, ], ); + $coverage->skipEmptyLines(); $this->assertEquals( [ From 177af01a8db1c530e40f1cfa160ecd27da9ae99e Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 27 Aug 2025 15:26:02 +0200 Subject: [PATCH 2/3] invoke skipEmptyLines() even earlier --- src/CodeCoverage.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/CodeCoverage.php b/src/CodeCoverage.php index 819a17fb7..154e97213 100644 --- a/src/CodeCoverage.php +++ b/src/CodeCoverage.php @@ -403,7 +403,6 @@ private function applyCoversAndUsesFilter(RawCodeCoverageData $rawData, array|fa foreach (array_keys($filesWithNoCoverage) as $fileWithNoCoverage) { $rawData->removeCoverageDataForFile($fileWithNoCoverage); } - $rawData->skipEmptyLines(); if (is_array($linesToBeCovered)) { foreach ($linesToBeCovered as $fileToBeCovered => $includedLines) { @@ -416,6 +415,8 @@ private function applyCoversAndUsesFilter(RawCodeCoverageData $rawData, array|fa private function applyFilter(RawCodeCoverageData $data): void { if ($this->filter->isEmpty()) { + $data->skipEmptyLines(); + return; } @@ -424,6 +425,7 @@ private function applyFilter(RawCodeCoverageData $data): void $data->removeCoverageDataForFile($filename); } } + $data->skipEmptyLines(); } private function applyExecutableLinesFilter(RawCodeCoverageData $data): void From ded3d8286b6e24f5f4ffd9815500b86b36f680ac Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 27 Aug 2025 16:28:38 +0200 Subject: [PATCH 3/3] Simplify applyFilter() --- src/CodeCoverage.php | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/CodeCoverage.php b/src/CodeCoverage.php index 154e97213..d96f5ab2a 100644 --- a/src/CodeCoverage.php +++ b/src/CodeCoverage.php @@ -414,17 +414,14 @@ private function applyCoversAndUsesFilter(RawCodeCoverageData $rawData, array|fa private function applyFilter(RawCodeCoverageData $data): void { - if ($this->filter->isEmpty()) { - $data->skipEmptyLines(); - - return; - } - - foreach (array_keys($data->lineCoverage()) as $filename) { - if ($this->filter->isExcluded($filename)) { - $data->removeCoverageDataForFile($filename); + if (!$this->filter->isEmpty()) { + foreach (array_keys($data->lineCoverage()) as $filename) { + if ($this->filter->isExcluded($filename)) { + $data->removeCoverageDataForFile($filename); + } } } + $data->skipEmptyLines(); }