Passed
Push — master ( c6d4b5...1256de )
by Pierre
01:47
created

Checker::getError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PierInfor\Undercover;
6
7
use PierInfor\Undercover\Interfaces\IChecker;
8
use PierInfor\Undercover\Parser;
9
use PierInfor\Undercover\Reporter;
10
11
/**
12
 * Checker is a clover coverage checker
13
 *
14
 * @author Pierre Fromager <info@pier_infor.fr>
15
 * @version 1.0
16
 * @package PierInfor\Undercover
17
 */
18
class Checker implements IChecker
19
{
20
21
    protected $parser;
22
    protected $thresholds;
23
    protected $error;
24
    protected $reporter;
25
26
    /**
27
     * constructor
28
     */
29 7
    public function __construct()
30
    {
31 7
        $this->init();
32
    }
33
34
    /**
35
     * runner
36
     *
37
     * @return int
38
     */
39 1
    public function run(): int
40
    {
41 1
        $this->check();
42 1
        return ($this->getError() && $this->isBlocking())
43 1
            ? 1
44 1
            : 0;
45
    }
46
47
    /**
48
     * initializer
49
     *
50
     * @return IChecker
51
     */
52 1
    protected function init(): IChecker
53
    {
54 1
        $this->parser = new Parser();
55 1
        $this->reporter = new Reporter();
56 1
        $this->thresholds = $this->parser->getArgs()->getThresholds();
57 1
        $this->error = false;
58 1
        return $this;
59
    }
60
61
    /**
62
     * display msg and set error if under coverage
63
     *
64
     * @return IChecker
65
     */
66 1
    protected function check(): IChecker
67
    {
68 1
        if (!empty($results = $this->getResults())) {
69 1
            $errCount = 0;
70 1
            foreach ($results as $k => $v) {
71 1
                $valid = $v >= $this->thresholds[$k];
72 1
                if (!$valid) {
73 1
                    ++$errCount;
74
                }
75
            }
76 1
            $this->error = ($errCount > 0);
77 1
            $this->reporter->report($results, $this->thresholds);
78
        }
79 1
        return $this;
80
    }
81
82
    /**
83
     * return parser results
84
     *
85
     * @return array
86
     */
87 1
    protected function getResults(): array
88
    {
89 1
        return $this->parser->parse()->getResults();
90
    }
91
92
    /**
93
     * return true if blocking mode set
94
     *
95
     * @return array
96
     */
97 1
    protected function isBlocking(): bool
98
    {
99 1
        return $this->parser->getArgs()->isBlocking();
100
    }
101
102
    /**
103
     * return true if error happens
104
     *
105
     * @return array
106
     */
107 1
    protected function getError(): bool
108
    {
109 1
        return $this->error;
110
    }
111
}
112