| Total Complexity | 6 |
| Total Lines | 51 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 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 | $coverage = $this->getPercent($checkedElements, $totalElements); |
|
| 30 | |||
| 31 | 2 | if ($coverage < $minPercent) { |
|
| 32 | 1 | throw new ErrorException( |
|
| 33 | 1 | sprintf('Code coverage is %d percent, accepted is %d percent', $coverage, $minPercent) |
|
| 34 | ); |
||
| 35 | } |
||
| 36 | |||
| 37 | 1 | return $coverage; |
|
| 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 |
|
| 64 | } |
||
| 65 | } |
||
| 66 |