Passed
Push — master ( 11230f...5f5cdb )
by Jakub
12:32
created

Collector   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 94.12%

Importance

Changes 4
Bugs 0 Features 2
Metric Value
wmc 8
eloc 16
c 4
b 0
f 2
dl 0
loc 46
ccs 16
cts 17
cp 0.9412
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A start() 0 4 1
A registerEngine() 0 3 1
A finish() 0 6 2
A selectEngine() 0 12 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MyTester\CodeCoverage;
6
7
use MyTester\CodeCoverageException as Exception;
8
use MyTester\ICodeCoverageEngine;
9
10
/**
11
 * Code coverage collector
12
 *
13
 * @author Jakub Konečný
14
 * @internal
15
 */
16
final class Collector
17
{
18
    /** @var ICodeCoverageEngine[] */
19
    private array $engines = [];
20
    private ?ICodeCoverageEngine $currentEngine = null;
21
22 1
    public function registerEngine(ICodeCoverageEngine $engine): void
23
    {
24 1
        $this->engines[] = $engine;
25
    }
26
27
    /**
28
     * @throws Exception
29
     */
30 1
    public function start(): void
31
    {
32 1
        $engine = $this->selectEngine();
33 1
        $engine->start();
34
    }
35
36
    /**
37
     * @throws Exception
38
     */
39 1
    public function finish(): array
40
    {
41 1
        if ($this->currentEngine === null) {
42 1
            throw new Exception("Code coverage collector has not been started.", Exception::COLLECTOR_NOT_STARTED);
43
        }
44 1
        return $this->currentEngine->collect();
45
    }
46
47
    /**
48
     * @throws Exception
49
     */
50 1
    private function selectEngine(): ICodeCoverageEngine
51
    {
52 1
        if ($this->currentEngine !== null) {
53
            return $this->currentEngine;
54
        }
55 1
        foreach ($this->engines as $engine) {
56 1
            if ($engine->isAvailable()) {
57 1
                $this->currentEngine = $engine;
58 1
                return $engine;
59
            }
60
        }
61 1
        throw new Exception("No code coverage engine is available.", Exception::NO_ENGINE_AVAILABLE);
62
    }
63
}
64