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

CoverageCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 43
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C run() 0 33 7
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
}