From 0109689324ecb84649e462dff05d61f1d8dadac6 Mon Sep 17 00:00:00 2001 From: Rui Filipe Da Cunha Alves Date: Fri, 14 Jul 2023 18:45:22 +0200 Subject: [PATCH 1/6] Add cache to Coverage Report Directory in order to avoid issues with multiple reporter when using fibers. --- src/CodeCoverage.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/CodeCoverage.php b/src/CodeCoverage.php index 45b2eaca2..35f21d0bc 100644 --- a/src/CodeCoverage.php +++ b/src/CodeCoverage.php @@ -114,6 +114,11 @@ final class CodeCoverage */ private $cacheDirectory; + /** + * @var ?Directory + */ + private $cacheReport; + public function __construct(Driver $driver, Filter $filter) { $this->driver = $driver; @@ -127,7 +132,10 @@ public function __construct(Driver $driver, Filter $filter) */ public function getReport(): Directory { - return (new Builder($this->analyser()))->build($this); + if (!$this->cacheReport) { + $this->cacheReport = (new Builder($this->analyser()))->build($this); + } + return clone $this->cacheReport; } /** From f315a337c4be524ba59f1c53dcf6f72ccc827585 Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Sat, 15 Jul 2023 07:13:07 +0200 Subject: [PATCH 2/6] Fix CS/WS issue --- src/CodeCoverage.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/CodeCoverage.php b/src/CodeCoverage.php index 35f21d0bc..002407c90 100644 --- a/src/CodeCoverage.php +++ b/src/CodeCoverage.php @@ -135,6 +135,7 @@ public function getReport(): Directory if (!$this->cacheReport) { $this->cacheReport = (new Builder($this->analyser()))->build($this); } + return clone $this->cacheReport; } From a73ada381899de182c5cd252da0fdf45b8348c51 Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Sat, 15 Jul 2023 07:14:27 +0200 Subject: [PATCH 3/6] Rename property --- src/CodeCoverage.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/CodeCoverage.php b/src/CodeCoverage.php index 002407c90..e3bc0003e 100644 --- a/src/CodeCoverage.php +++ b/src/CodeCoverage.php @@ -117,7 +117,7 @@ final class CodeCoverage /** * @var ?Directory */ - private $cacheReport; + private $cachedReport; public function __construct(Driver $driver, Filter $filter) { @@ -132,11 +132,11 @@ public function __construct(Driver $driver, Filter $filter) */ public function getReport(): Directory { - if (!$this->cacheReport) { - $this->cacheReport = (new Builder($this->analyser()))->build($this); + if (!$this->cachedReport) { + $this->cachedReport = (new Builder($this->analyser()))->build($this); } - return clone $this->cacheReport; + return clone $this->cachedReport; } /** From b05dcd08c5e754670470be88705138e9b8dbf4fe Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Sat, 15 Jul 2023 07:16:44 +0200 Subject: [PATCH 4/6] Invalidate report cache when underlying data changes --- src/CodeCoverage.php | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/CodeCoverage.php b/src/CodeCoverage.php index e3bc0003e..4b2fbb48b 100644 --- a/src/CodeCoverage.php +++ b/src/CodeCoverage.php @@ -144,9 +144,10 @@ public function getReport(): Directory */ public function clear(): void { - $this->currentId = null; - $this->data = new ProcessedCodeCoverageData; - $this->tests = []; + $this->currentId = null; + $this->data = new ProcessedCodeCoverageData; + $this->tests = []; + $this->cachedReport = null; } /** @@ -211,6 +212,8 @@ public function start($id, bool $clear = false): void $this->currentId = $id; $this->driver->start(); + + $this->cachedReport = null; } /** @@ -229,7 +232,8 @@ public function stop(bool $append = true, $linesToBeCovered = [], array $linesTo $data = $this->driver->stop(); $this->append($data, null, $append, $linesToBeCovered, $linesToBeUsed); - $this->currentId = null; + $this->currentId = null; + $this->cachedReport = null; return $data; } @@ -254,6 +258,8 @@ public function append(RawCodeCoverageData $rawData, $id = null, bool $append = throw new TestIdMissingException; } + $this->cachedReport = null; + $this->applyFilter($rawData); $this->applyExecutableLinesFilter($rawData); @@ -321,6 +327,8 @@ public function merge(self $that): void $this->data->merge($that->data); $this->tests = array_merge($this->tests, $that->getTests()); + + $this->cachedReport = null; } public function enableCheckForUnintentionallyCoveredCode(): void From ba35f966d4b92ead050d59f7923463d928ddc3f3 Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Sat, 15 Jul 2023 07:18:02 +0200 Subject: [PATCH 5/6] Use === operator --- src/CodeCoverage.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CodeCoverage.php b/src/CodeCoverage.php index 4b2fbb48b..31486ccf2 100644 --- a/src/CodeCoverage.php +++ b/src/CodeCoverage.php @@ -132,7 +132,7 @@ public function __construct(Driver $driver, Filter $filter) */ public function getReport(): Directory { - if (!$this->cachedReport) { + if ($this->cachedReport === null) { $this->cachedReport = (new Builder($this->analyser()))->build($this); } From 1e4c868c8b674143ac8d5c6fa5913f2681361104 Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Sat, 15 Jul 2023 07:23:28 +0200 Subject: [PATCH 6/6] Update ChangeLog --- ChangeLog-9.2.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ChangeLog-9.2.md b/ChangeLog-9.2.md index 92a0c024d..bcee18213 100644 --- a/ChangeLog-9.2.md +++ b/ChangeLog-9.2.md @@ -4,6 +4,10 @@ All notable changes are documented in this file using the [Keep a CHANGELOG](htt ## [9.2.27] - 2023-MM-DD +### Changed + +* The result of `CodeCoverage::getReport()` is now cached + ### Fixed * Static analysis cache keys do not include configuration settings that affect source code parsing