1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of cloak. |
5
|
|
|
* |
6
|
|
|
* (c) Noritaka Horio <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace cloak\configuration; |
13
|
|
|
|
14
|
|
|
use cloak\AnalyzerConfiguration; |
15
|
|
|
use cloak\value\CoverageBounds; |
16
|
|
|
use cloak\analyzer\Analyzer; |
17
|
|
|
use cloak\analyzer\AnalyzeDriver; |
18
|
|
|
use cloak\analyzer\AdapterResolver; |
19
|
|
|
use cloak\reporter\Reporter; |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class ConfigurationBuilder |
24
|
|
|
* @package cloak\configuration |
25
|
|
|
*/ |
26
|
|
|
class ConfigurationBuilder |
27
|
|
|
{ |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var \cloak\analyzer\AnalyzeDriver |
31
|
|
|
*/ |
32
|
|
|
private $driver; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var \cloak\reporter\Reporter |
36
|
|
|
*/ |
37
|
|
|
private $reporter; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string[] |
41
|
|
|
*/ |
42
|
|
|
private $includeFiles; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string[] |
46
|
|
|
*/ |
47
|
|
|
private $excludeFiles; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var \cloak\value\CoverageBounds |
51
|
|
|
*/ |
52
|
|
|
private $coverageBounds; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
private $reportDirectory; |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
public function __construct() |
61
|
|
|
{ |
62
|
|
|
$this->includeFiles = []; |
63
|
|
|
$this->excludeFiles = []; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param AnalyzeDriver $driver |
68
|
|
|
* @return $this |
69
|
|
|
*/ |
70
|
|
|
public function driver(AnalyzeDriver $driver) |
71
|
|
|
{ |
72
|
|
|
$this->driver = $driver; |
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param Reporter $reporter |
78
|
|
|
* @return $this |
79
|
|
|
*/ |
80
|
|
|
public function reporter(Reporter $reporter) |
81
|
|
|
{ |
82
|
|
|
$this->reporter = $reporter; |
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $pattern |
88
|
|
|
* @return $this |
89
|
|
|
*/ |
90
|
|
|
public function includeFile($pattern) |
91
|
|
|
{ |
92
|
|
|
$this->includeFiles[] = $pattern; |
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string $pattern |
98
|
|
|
* @return $this |
99
|
|
|
*/ |
100
|
|
|
public function excludeFile($pattern) |
101
|
|
|
{ |
102
|
|
|
$this->excludeFiles[] = $pattern; |
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param string[] $patterns |
108
|
|
|
* @return $this |
109
|
|
|
*/ |
110
|
|
|
public function includeFiles(array $patterns) |
111
|
|
|
{ |
112
|
|
|
foreach ($patterns as $pattern) { |
113
|
|
|
$this->includeFile($pattern); |
114
|
|
|
} |
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param string[] $patterns |
120
|
|
|
* @return $this |
121
|
|
|
*/ |
122
|
|
|
public function excludeFiles(array $patterns) |
123
|
|
|
{ |
124
|
|
|
foreach ($patterns as $pattern) { |
125
|
|
|
$this->excludeFile($pattern); |
126
|
|
|
} |
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param string $directoryPath |
132
|
|
|
* @return $this |
133
|
|
|
*/ |
134
|
|
|
public function reportDirectory($directoryPath) |
135
|
|
|
{ |
136
|
|
|
$this->reportDirectory = $directoryPath; |
137
|
|
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param float $critical |
142
|
|
|
* @param float $satisfactory |
143
|
|
|
* @return $this |
144
|
|
|
*/ |
145
|
|
|
public function coverageBounds($critical, $satisfactory) |
146
|
|
|
{ |
147
|
|
|
$this->coverageBounds = new CoverageBounds($critical, $satisfactory); |
148
|
|
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
protected function detectDriver() |
152
|
|
|
{ |
153
|
|
|
if ($this->driver instanceof AnalyzeDriver) { |
154
|
|
|
return; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
$resolver = new AdapterResolver([ |
158
|
|
|
'\cloak\analyzer\adapter\XdebugAdapter', |
159
|
|
|
'\cloak\analyzer\adapter\HHVMAdapter' |
160
|
|
|
]); |
161
|
|
|
$adapter = $resolver->resolve(); |
162
|
|
|
|
163
|
|
|
$this->driver = new Analyzer($adapter); |
|
|
|
|
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @return AnalyzeDriver |
168
|
|
|
*/ |
169
|
|
|
public function getDriver() |
170
|
|
|
{ |
171
|
|
|
return $this->driver; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @return Reporter |
176
|
|
|
*/ |
177
|
|
|
public function getReporter() |
178
|
|
|
{ |
179
|
|
|
return $this->reporter; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @return string[] |
184
|
|
|
*/ |
185
|
|
|
public function getIncludeFiles() |
186
|
|
|
{ |
187
|
|
|
return $this->includeFiles; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @return string[] |
192
|
|
|
*/ |
193
|
|
|
public function getExcludeFiles() |
194
|
|
|
{ |
195
|
|
|
return $this->excludeFiles; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @return CoverageBounds |
200
|
|
|
*/ |
201
|
|
|
public function getCoverageBounds() |
202
|
|
|
{ |
203
|
|
|
return $this->coverageBounds; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @return string |
208
|
|
|
*/ |
209
|
|
|
public function getReportDirectory() |
210
|
|
|
{ |
211
|
|
|
return $this->reportDirectory; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @return AnalyzerConfiguration |
216
|
|
|
*/ |
217
|
|
|
public function build() |
218
|
|
|
{ |
219
|
|
|
$this->detectDriver(); |
220
|
|
|
|
221
|
|
|
$values = [ |
222
|
|
|
'driver' => $this->driver, |
223
|
|
|
'reporter' => $this->reporter, |
224
|
|
|
'includeFiles' => $this->includeFiles, |
225
|
|
|
'excludeFiles' => $this->excludeFiles, |
226
|
|
|
'coverageBounds' => $this->coverageBounds, |
227
|
|
|
'reportDirectory' => $this->reportDirectory |
228
|
|
|
]; |
229
|
|
|
|
230
|
|
|
return new AnalyzerConfiguration($values); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
} |
234
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: