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
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Checker is a clover coverage checker |
12
|
|
|
* |
13
|
|
|
* @author Pierre Fromager <info@pier_infor.fr> |
14
|
|
|
* @version 1.0 |
15
|
|
|
* @package PierInfor\Undercover |
16
|
|
|
*/ |
17
|
|
|
class Checker implements IChecker |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
protected $parser; |
21
|
|
|
protected $filename; |
22
|
|
|
protected $thresholds; |
23
|
|
|
protected $results; |
24
|
|
|
protected $error; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* constructor |
28
|
|
|
*/ |
29
|
7 |
|
public function __construct() |
30
|
|
|
{ |
31
|
7 |
|
$this->parser = new Parser(); |
32
|
7 |
|
$this->init(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* destructor |
37
|
|
|
*/ |
38
|
7 |
|
public function __destruct() |
39
|
|
|
{ |
40
|
7 |
|
$this->parser = null; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* runner |
45
|
|
|
* |
46
|
|
|
* @return int |
47
|
|
|
*/ |
48
|
1 |
|
public function run(): int |
49
|
|
|
{ |
50
|
1 |
|
$this->check(); |
51
|
1 |
|
return ($this->error && $this->isBlocking()) |
52
|
|
|
? 1 |
53
|
1 |
|
: 0; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* initializer |
58
|
|
|
* |
59
|
|
|
* @return IChecker |
60
|
|
|
*/ |
61
|
1 |
|
protected function init(): IChecker |
62
|
|
|
{ |
63
|
1 |
|
$this->thresholds = $this->parser->getArgs()->getThresholds(); |
64
|
1 |
|
$this->results = []; |
65
|
1 |
|
$this->error = false; |
66
|
1 |
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* display msg and set error if under coverage |
71
|
|
|
* |
72
|
|
|
* @return IChecker |
73
|
|
|
*/ |
74
|
1 |
|
protected function check(): IChecker |
75
|
|
|
{ |
76
|
1 |
|
if (!empty($results = $this->getResults())) { |
77
|
1 |
|
echo self::TITLE; |
78
|
1 |
|
$errCount = 0; |
79
|
1 |
|
foreach ($results as $k => $v) { |
80
|
1 |
|
$valid = $v >= $this->thresholds[$k]; |
81
|
1 |
|
if (!$valid) { |
82
|
1 |
|
++$errCount; |
83
|
|
|
} |
84
|
1 |
|
echo PHP_EOL . $this->getMsgLine($k, $v, $valid); |
85
|
|
|
} |
86
|
1 |
|
echo PHP_EOL . self::T_BEFORE; |
87
|
1 |
|
$this->error = ($errCount > 0); |
88
|
|
|
} |
89
|
1 |
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* return formated msg line |
94
|
|
|
* |
95
|
|
|
* @param string $k |
96
|
|
|
* @param float $v |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
1 |
|
protected function getMsgLine(string $k, float $v, bool $valid): string |
100
|
|
|
{ |
101
|
1 |
|
return sprintf( |
102
|
1 |
|
self::MSG_FORMAT, |
103
|
1 |
|
ucfirst($k), |
104
|
|
|
$v, |
105
|
1 |
|
'limit', |
106
|
1 |
|
$this->thresholds[$k], |
107
|
1 |
|
$valid ? self::_OK : self::_KO |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* return parser results |
113
|
|
|
* |
114
|
|
|
* @return array |
115
|
|
|
*/ |
116
|
1 |
|
protected function getResults(): array |
117
|
|
|
{ |
118
|
1 |
|
return $this->parser->parse()->getResults(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* return true if blocking mode set |
123
|
|
|
* |
124
|
|
|
* @return array |
125
|
|
|
*/ |
126
|
1 |
|
protected function isBlocking(): bool |
127
|
|
|
{ |
128
|
1 |
|
return $this->parser->getArgs()->isBlocking(); |
|
|
|
|
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|