Check   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 16
c 3
b 0
f 0
dl 0
loc 51
ccs 19
cts 19
cp 1
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getTotals() 0 10 2
A getPercent() 0 3 1
A run() 0 17 3
1
<?php
2
3
namespace Coverage;
4
5
use ErrorException;
6
use InvalidArgumentException;
7
use SimpleXMLElement;
8
use function sprintf;
9
10
/**
11
 * Class Check
12
 */
13
class Check
14
{
15
    /**
16
     * @param string $coverageFilePath
17
     * @param int $minPercent
18
     * @return string
19
     * @throws ErrorException
20
     */
21 3
    public function run(string $coverageFilePath, int $minPercent): string
22
    {
23 3
        if (!file_exists($coverageFilePath)) {
24 1
            throw new InvalidArgumentException('Invalid path file: '.$coverageFilePath);
25
        }
26
27 2
        $metrics = (new SimpleXMLElement(file_get_contents($coverageFilePath)))->xpath('//metrics');
28 2
        [$totalElements, $checkedElements] = $this->getTotals($metrics);
29 2
        $coveragePercent = $this->getPercent($checkedElements, $totalElements);
30
31 2
        if ($coveragePercent < $minPercent) {
32 1
            throw new ErrorException(
33 1
                sprintf('Code coverage is %d percent, accepted is %d percent', $coveragePercent, $minPercent)
34
            );
35
        }
36
37 1
        return $coveragePercent;
38
    }
39
40
    /**
41
     * @param int $checkedElements
42
     * @param int $totalElements
43
     * @return int
44
     */
45 2
    private function getPercent(int $checkedElements, int $totalElements): int
46
    {
47 2
        return ($checkedElements / $totalElements) * 100;
48
    }
49
50
    /**
51
     * @param array $metrics
52
     * @return array
53
     */
54 2
    private function getTotals(array $metrics): array
55
    {
56 2
        $totalElements = 0;
57 2
        $checkedElements = 0;
58
59 2
        foreach ($metrics as $metric) {
60 2
            $totalElements += (int)$metric['elements'];
61 2
            $checkedElements += (int)$metric['coveredelements'];
62
        }
63 2
        return [$totalElements, $checkedElements];
64
    }
65
}
66