|
31 | 31 | use SebastianBergmann\CodeCoverage\Node\Directory; |
32 | 32 | use SebastianBergmann\CodeCoverage\StaticAnalysis\CachingExecutedFileAnalyser; |
33 | 33 | use SebastianBergmann\CodeCoverage\StaticAnalysis\ExecutedFileAnalyser; |
| 34 | +use SebastianBergmann\CodeCoverage\StaticAnalysis\ParsingExecutedFileAnalyser; |
34 | 35 | use SebastianBergmann\CodeUnitReverseLookup\Wizard; |
35 | 36 |
|
36 | 37 | /** |
@@ -116,20 +117,27 @@ final class CodeCoverage |
116 | 117 | */ |
117 | 118 | private $executedFileAnalyser; |
118 | 119 |
|
| 120 | + /** |
| 121 | + * @var ?string |
| 122 | + */ |
| 123 | + private $cacheDirectory; |
| 124 | + |
119 | 125 | public function __construct(Driver $driver, Filter $filter) |
120 | 126 | { |
121 | 127 | $this->driver = $driver; |
122 | 128 | $this->filter = $filter; |
123 | 129 | $this->data = new ProcessedCodeCoverageData; |
124 | 130 | $this->wizard = new Wizard; |
| 131 | + |
| 132 | + $this->cacheStaticAnalysis('/tmp/cache'); |
125 | 133 | } |
126 | 134 |
|
127 | 135 | /** |
128 | 136 | * Returns the code coverage information as a graph of node objects. |
129 | 137 | */ |
130 | 138 | public function getReport(): Directory |
131 | 139 | { |
132 | | - return (new Builder)->build($this); |
| 140 | + return (new Builder($this->executedFileAnalyser()))->build($this); |
133 | 141 | } |
134 | 142 |
|
135 | 143 | /** |
@@ -364,6 +372,24 @@ public function doNotIgnoreDeprecatedCode(): void |
364 | 372 | $this->ignoreDeprecatedCode = false; |
365 | 373 | } |
366 | 374 |
|
| 375 | + /** |
| 376 | + * @psalm-assert-if-true !null $this->cacheDirectory |
| 377 | + */ |
| 378 | + public function cachesStaticAnalysis(): bool |
| 379 | + { |
| 380 | + return $this->cacheDirectory !== null; |
| 381 | + } |
| 382 | + |
| 383 | + public function cacheStaticAnalysis(string $cacheDirectory): void |
| 384 | + { |
| 385 | + $this->cacheDirectory = $cacheDirectory; |
| 386 | + } |
| 387 | + |
| 388 | + public function doNotCacheStaticAnalysis(): void |
| 389 | + { |
| 390 | + $this->cacheDirectory = null; |
| 391 | + } |
| 392 | + |
367 | 393 | /** |
368 | 394 | * @psalm-param class-string $className |
369 | 395 | */ |
@@ -447,22 +473,14 @@ private function applyFilter(RawCodeCoverageData $data): void |
447 | 473 |
|
448 | 474 | private function applyIgnoredLinesFilter(RawCodeCoverageData $data): void |
449 | 475 | { |
450 | | - if ($this->executedFileAnalyser === null) { |
451 | | - $this->executedFileAnalyser = CachingExecutedFileAnalyser::createInstance( |
452 | | - '/tmp/cache', |
453 | | - $this->useAnnotationsForIgnoringCode, |
454 | | - $this->ignoreDeprecatedCode |
455 | | - ); |
456 | | - } |
457 | | - |
458 | 476 | foreach (array_keys($data->lineCoverage()) as $filename) { |
459 | 477 | if (!$this->filter->isFile($filename)) { |
460 | 478 | continue; |
461 | 479 | } |
462 | 480 |
|
463 | 481 | $data->removeCoverageDataForLines( |
464 | 482 | $filename, |
465 | | - $this->executedFileAnalyser->ignoredLinesFor($filename) |
| 483 | + $this->executedFileAnalyser()->ignoredLinesFor($filename) |
466 | 484 | ); |
467 | 485 | } |
468 | 486 | } |
@@ -615,4 +633,25 @@ private function initializeData(): void |
615 | 633 | $this->append($this->driver->stop(), self::UNCOVERED_FILES); |
616 | 634 | } |
617 | 635 | } |
| 636 | + |
| 637 | + private function executedFileAnalyser(): ExecutedFileAnalyser |
| 638 | + { |
| 639 | + if ($this->executedFileAnalyser !== null) { |
| 640 | + return $this->executedFileAnalyser; |
| 641 | + } |
| 642 | + |
| 643 | + $this->executedFileAnalyser = new ParsingExecutedFileAnalyser( |
| 644 | + $this->useAnnotationsForIgnoringCode, |
| 645 | + $this->ignoreDeprecatedCode |
| 646 | + ); |
| 647 | + |
| 648 | + if ($this->cachesStaticAnalysis()) { |
| 649 | + $this->executedFileAnalyser = new CachingExecutedFileAnalyser( |
| 650 | + $this->cacheDirectory, |
| 651 | + $this->executedFileAnalyser |
| 652 | + ); |
| 653 | + } |
| 654 | + |
| 655 | + return $this->executedFileAnalyser; |
| 656 | + } |
618 | 657 | } |
0 commit comments