Completed
Push — master ( 6d12c1...fefd2b )
by Vincenzo
03:27
created

CoverageCommand::run()   C

Complexity

Conditions 7
Paths 24

Size

Total Lines 33
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 33
rs 6.7272
cc 7
eloc 20
nc 24
nop 0
1
<?php
2
3
4
namespace App\Console;
5
6
7
use App\Lib\Slime\Console\SlimeCommand;
8
use InvalidArgumentException;
9
use SimpleXMLElement;
10
11
class CoverageCommand extends SlimeCommand
12
{
13
    protected $acceptedArgs = [
14
15
    ];
16
17
    /**
18
     * @return int
19
     */
20
    public function run()
21
    {
22
        $inputFile = isset($this->args[0]) ? $this->args[0] : "clover.xml";
23
        $targetCoverage = (int)(isset($argv[1]) ? $argv[1] : 1);
0 ignored issues
show
Bug introduced by
The variable $argv seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
24
        $percentage = min(100, max(0, $targetCoverage));
25
26
        if (!file_exists($inputFile)) {
27
            throw new InvalidArgumentException('Invalid input file provided');
28
        }
29
30
        if (!$percentage) {
31
            throw new InvalidArgumentException('An integer checked percentage must be given as second parameter');
32
        }
33
34
        $xml = new SimpleXMLElement(file_get_contents($inputFile));
35
        $metrics = $xml->xpath('//metrics');
36
        $totalElements = 0;
37
        $checkedElements = 0;
38
39
        foreach ($metrics as $metric) {
40
            $totalElements += (int)$metric['elements'];
41
            $checkedElements += (int)$metric['coveredelements'];
42
        }
43
44
        $coverage = ($checkedElements / $totalElements) * 100;
45
46
        if ($coverage < $percentage) {
47
            echo 'Code coverage is ' . $coverage . '%, which is below the accepted ' . $percentage . '%' . PHP_EOL;
48
            exit(1);
49
        }
50
51
        echo 'Code coverage is ' . round($coverage, 2) . '% - OK!' . PHP_EOL;
52
    }
53
}