1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PierInfor\Undercover; |
6
|
|
|
|
7
|
|
|
use PierInfor\Undercover\Interfaces\IReporter; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Reporter is a coverage reporter |
11
|
|
|
* |
12
|
|
|
* @author Pierre Fromager <info@pier_infor.fr> |
13
|
|
|
* @version 1.0 |
14
|
|
|
* @package PierInfor\Undercover |
15
|
|
|
*/ |
16
|
|
|
class Reporter implements IReporter |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
protected $results; |
20
|
|
|
protected $thresholds; |
21
|
|
|
protected $error; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* constructor |
25
|
|
|
*/ |
26
|
|
|
public function __construct() |
27
|
|
|
{ |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* display report |
32
|
|
|
* |
33
|
|
|
* @param array $results |
34
|
|
|
* @param array $thresholds |
35
|
|
|
* @return void |
36
|
|
|
*/ |
37
|
2 |
|
public function report(array $results, array $thresholds): IReporter |
38
|
|
|
{ |
39
|
2 |
|
$this->results = $results; |
40
|
2 |
|
$this->thresholds = $thresholds; |
41
|
2 |
|
if (!empty($this->getResults())) { |
42
|
2 |
|
echo $this->getReport(); |
43
|
|
|
} |
44
|
2 |
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* return results |
49
|
|
|
* |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
1 |
|
protected function getResults(): array |
53
|
|
|
{ |
54
|
1 |
|
return is_null($this->results) |
55
|
1 |
|
? [] |
56
|
1 |
|
: $this->results; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* return thresholds |
61
|
|
|
* |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
1 |
|
protected function getThresholds(): array |
65
|
|
|
{ |
66
|
1 |
|
return is_null($this->thresholds) |
67
|
1 |
|
? [] |
68
|
1 |
|
: $this->thresholds; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* return threshold as float for a given a key |
73
|
|
|
* |
74
|
|
|
* @return float |
75
|
|
|
*/ |
76
|
1 |
|
protected function getThreshold(string $key): float |
77
|
|
|
{ |
78
|
1 |
|
$tresholds = $this->getThresholds(); |
79
|
1 |
|
if (empty($tresholds) || !isset($tresholds[$key])) { |
80
|
1 |
|
return (float) 0; |
81
|
|
|
} |
82
|
1 |
|
return $tresholds[$key]; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* return report |
87
|
|
|
* |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
1 |
|
protected function getReport(): string |
91
|
|
|
{ |
92
|
1 |
|
return sprintf( |
93
|
1 |
|
IReporter::REPORT_FORMAT, |
94
|
1 |
|
$this->getHeader(), |
95
|
1 |
|
$this->getBody(), |
96
|
1 |
|
$this->getFooter() |
97
|
|
|
); |
98
|
|
|
; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* return header |
103
|
|
|
* |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
1 |
|
protected function getHeader(): string |
107
|
|
|
{ |
108
|
1 |
|
return IReporter::HEADER; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* return body |
113
|
|
|
* |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
1 |
|
protected function getBody(): string |
117
|
|
|
{ |
118
|
1 |
|
$body = ''; |
119
|
1 |
|
foreach ($this->getResults() as $key => $value) { |
120
|
1 |
|
$threshold = $this->getThresholds()[$key]; |
121
|
1 |
|
$body .= $this->getMsgLine( |
122
|
1 |
|
$key, |
123
|
|
|
$value, |
124
|
1 |
|
($value >= $threshold) |
125
|
|
|
); |
126
|
|
|
} |
127
|
1 |
|
return $body; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* return footer |
132
|
|
|
* |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
1 |
|
protected function getFooter(): string |
136
|
|
|
{ |
137
|
1 |
|
return IReporter::T_BEFORE |
138
|
1 |
|
. str_repeat('-', \strlen(IReporter::TITLE)) |
139
|
1 |
|
. IReporter::T_AFTER; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* return formated msg line |
144
|
|
|
* |
145
|
|
|
* @param string $key |
146
|
|
|
* @param float $value |
147
|
|
|
* @return string |
148
|
|
|
*/ |
149
|
1 |
|
protected function getMsgLine(string $key, float $value, bool $valid): string |
150
|
|
|
{ |
151
|
1 |
|
return sprintf( |
152
|
1 |
|
IReporter::MSG_FORMAT, |
153
|
1 |
|
ucfirst($key), |
154
|
|
|
$value, |
155
|
1 |
|
IReporter::_LIMIT, |
156
|
1 |
|
$this->getThreshold($key), |
157
|
1 |
|
$valid ? IReporter::_OK : IReporter::_KO |
158
|
|
|
); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|