|
1
|
|
|
<?php |
|
2
|
|
|
if ( ! function_exists('glob_recursive')) |
|
3
|
|
|
{ |
|
4
|
|
|
// Does not support flag GLOB_BRACE |
|
5
|
|
|
function glob_recursive($pattern, $flags = 0) |
|
6
|
|
|
{ |
|
7
|
|
|
$files = glob($pattern, $flags); |
|
8
|
|
|
|
|
9
|
|
|
foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) |
|
10
|
|
|
{ |
|
11
|
|
|
$files = array_merge($files, glob_recursive($dir.'/'.basename($pattern), $flags)); |
|
12
|
|
|
} |
|
13
|
|
|
|
|
14
|
|
|
return $files; |
|
15
|
|
|
} |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* This is project's console commands configuration for Robo task runner. |
|
20
|
|
|
* |
|
21
|
|
|
* @see http://robo.li/ |
|
22
|
|
|
*/ |
|
23
|
|
|
class RoboFile extends \Robo\Tasks { |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Directories used by analysis tools |
|
27
|
|
|
* |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $taskDirs = [ |
|
31
|
|
|
'build/logs', |
|
32
|
|
|
'build/pdepend', |
|
33
|
|
|
'build/phpdox', |
|
34
|
|
|
]; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Directories to remove with the clean task |
|
38
|
|
|
* |
|
39
|
|
|
* @var array |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $cleanDirs = [ |
|
42
|
|
|
'coverage', |
|
43
|
|
|
'docs', |
|
44
|
|
|
'phpdoc', |
|
45
|
|
|
'build/logs', |
|
46
|
|
|
'build/phpdox', |
|
47
|
|
|
'build/pdepend' |
|
48
|
|
|
]; |
|
49
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Do static analysis tasks |
|
53
|
|
|
*/ |
|
54
|
|
|
public function analyze() |
|
55
|
|
|
{ |
|
56
|
|
|
$this->prepare(); |
|
57
|
|
|
$this->lint(); |
|
58
|
|
|
$this->phploc(TRUE); |
|
59
|
|
|
$this->phpcs(TRUE); |
|
60
|
|
|
$this->dependencyReport(); |
|
61
|
|
|
$this->phpcpdReport(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Run all tests, generate coverage, generate docs, generate code statistics |
|
66
|
|
|
*/ |
|
67
|
|
|
public function build() |
|
68
|
|
|
{ |
|
69
|
|
|
$this->analyze(); |
|
70
|
|
|
$this->coverage(); |
|
71
|
|
|
$this->docs(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Cleanup temporary files |
|
76
|
|
|
*/ |
|
77
|
|
|
public function clean() |
|
78
|
|
|
{ |
|
79
|
|
|
$cleanFiles = [ |
|
80
|
|
|
'build/humbug.json', |
|
81
|
|
|
'build/humbug-log.txt', |
|
82
|
|
|
]; |
|
83
|
|
|
array_map(function ($file) { |
|
84
|
|
|
@unlink($file); |
|
|
|
|
|
|
85
|
|
|
}, $cleanFiles); |
|
86
|
|
|
|
|
87
|
|
|
// So the task doesn't complain, |
|
88
|
|
|
// make any 'missing' dirs to cleanup |
|
89
|
|
|
array_map(function ($dir) { |
|
90
|
|
|
if ( ! is_dir($dir)) |
|
91
|
|
|
{ |
|
92
|
|
|
`mkdir -p {$dir}`; |
|
93
|
|
|
} |
|
94
|
|
|
}, $this->cleanDirs); |
|
95
|
|
|
|
|
96
|
|
|
$this->_cleanDir($this->cleanDirs); |
|
97
|
|
|
$this->_deleteDir($this->cleanDirs); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Run unit tests and generate coverage reports |
|
102
|
|
|
*/ |
|
103
|
|
|
public function coverage() |
|
104
|
|
|
{ |
|
105
|
|
|
$this->taskPhpUnit() |
|
106
|
|
|
->configFile('build/phpunit.xml') |
|
107
|
|
|
->printed(true) |
|
108
|
|
|
->run(); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Generate documentation with phpdox |
|
113
|
|
|
*/ |
|
114
|
|
|
public function docs() |
|
115
|
|
|
{ |
|
116
|
|
|
$cmd_parts = [ |
|
117
|
|
|
'cd build', |
|
118
|
|
|
'../vendor/bin/phpdox', |
|
119
|
|
|
'cd ..' |
|
120
|
|
|
]; |
|
121
|
|
|
$this->_run($cmd_parts, ' && '); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Verify that source files are valid |
|
126
|
|
|
*/ |
|
127
|
|
|
public function lint() |
|
128
|
|
|
{ |
|
129
|
|
|
$files = $this->getAllSourceFiles(); |
|
130
|
|
|
|
|
131
|
|
|
$chunks = array_chunk($files, (int)`getconf _NPROCESSORS_ONLN`); |
|
132
|
|
|
|
|
133
|
|
|
foreach($chunks as $chunk) |
|
134
|
|
|
{ |
|
135
|
|
|
$this->parallelLint($chunk); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Run mutation tests with humbug |
|
142
|
|
|
* |
|
143
|
|
|
* @param bool $stats - if true, generates stats rather than running mutation tests |
|
144
|
|
|
*/ |
|
145
|
|
|
public function mutate($stats = FALSE) |
|
146
|
|
|
{ |
|
147
|
|
|
$test_parts = [ |
|
148
|
|
|
'vendor/bin/humbug' |
|
149
|
|
|
]; |
|
150
|
|
|
|
|
151
|
|
|
$stat_parts = [ |
|
152
|
|
|
'vendor/bin/humbug', |
|
153
|
|
|
'--skip-killed=yes', |
|
154
|
|
|
'-v', |
|
155
|
|
|
'./build/humbug.json' |
|
156
|
|
|
]; |
|
157
|
|
|
|
|
158
|
|
|
$cmd_parts = ($stats) ? $stat_parts : $test_parts; |
|
159
|
|
|
$this->_run($cmd_parts); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Run the phpcs tool |
|
164
|
|
|
* |
|
165
|
|
|
* @param bool $report - if true, generates reports instead of direct output |
|
166
|
|
|
*/ |
|
167
|
|
|
public function phpcs($report = FALSE) |
|
168
|
|
|
{ |
|
169
|
|
|
$dir = __DIR__; |
|
|
|
|
|
|
170
|
|
|
|
|
171
|
|
|
$report_cmd_parts = [ |
|
172
|
|
|
'vendor/bin/phpcs', |
|
173
|
|
|
"--standard=./build/CodeIgniter", |
|
174
|
|
|
"--report-checkstyle=./build/logs/phpcs.xml", |
|
175
|
|
|
]; |
|
176
|
|
|
|
|
177
|
|
|
$normal_cmd_parts = [ |
|
178
|
|
|
'vendor/bin/phpcs', |
|
179
|
|
|
"--standard=./build/CodeIgniter", |
|
180
|
|
|
]; |
|
181
|
|
|
|
|
182
|
|
|
$cmd_parts = ($report) ? $report_cmd_parts : $normal_cmd_parts; |
|
183
|
|
|
|
|
184
|
|
|
$this->_run($cmd_parts); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Run the phploc tool |
|
189
|
|
|
* |
|
190
|
|
|
* @param bool $report - if true, generates reports instead of direct output |
|
191
|
|
|
*/ |
|
192
|
|
|
public function phploc($report = FALSE) |
|
193
|
|
|
{ |
|
194
|
|
|
// Command for generating reports |
|
195
|
|
|
$report_cmd_parts = [ |
|
196
|
|
|
'vendor/bin/phploc', |
|
197
|
|
|
'--count-tests', |
|
198
|
|
|
'--log-csv=build/logs/phploc.csv', |
|
199
|
|
|
'--log-xml=build/logs/phploc.xml', |
|
200
|
|
|
'src', |
|
201
|
|
|
'tests' |
|
202
|
|
|
]; |
|
203
|
|
|
|
|
204
|
|
|
// Command for generating direct output |
|
205
|
|
|
$normal_cmd_parts = [ |
|
206
|
|
|
'vendor/bin/phploc', |
|
207
|
|
|
'--count-tests', |
|
208
|
|
|
'src', |
|
209
|
|
|
'tests' |
|
210
|
|
|
]; |
|
211
|
|
|
|
|
212
|
|
|
$cmd_parts = ($report) ? $report_cmd_parts : $normal_cmd_parts; |
|
213
|
|
|
|
|
214
|
|
|
$this->_run($cmd_parts); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* Create temporary directories |
|
219
|
|
|
*/ |
|
220
|
|
|
public function prepare() |
|
221
|
|
|
{ |
|
222
|
|
|
array_map([$this, '_mkdir'], $this->taskDirs); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* Lint php files and run unit tests |
|
227
|
|
|
*/ |
|
228
|
|
|
public function test() |
|
229
|
|
|
{ |
|
230
|
|
|
$this->lint(); |
|
231
|
|
|
$this->taskPHPUnit() |
|
232
|
|
|
->configFile('phpunit.xml') |
|
233
|
|
|
->printed(true) |
|
234
|
|
|
->run(); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* Watches for file updates, and automatically runs appropriate actions |
|
239
|
|
|
*/ |
|
240
|
|
|
public function watch() |
|
241
|
|
|
{ |
|
242
|
|
|
$this->taskWatch() |
|
243
|
|
|
->monitor('composer.json', function() { |
|
244
|
|
|
$this->taskComposerUpdate()->run(); |
|
245
|
|
|
}) |
|
246
|
|
|
->monitor('src', function () { |
|
247
|
|
|
$this->taskExec('test')->run(); |
|
248
|
|
|
}) |
|
249
|
|
|
->monitor('tests', function () { |
|
250
|
|
|
$this->taskExec('test')->run(); |
|
251
|
|
|
}) |
|
252
|
|
|
->run(); |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* Create pdepend reports |
|
257
|
|
|
*/ |
|
258
|
|
|
protected function dependencyReport() |
|
259
|
|
|
{ |
|
260
|
|
|
$cmd_parts = [ |
|
261
|
|
|
'vendor/bin/pdepend', |
|
262
|
|
|
'--jdepend-xml=build/logs/jdepend.xml', |
|
263
|
|
|
'--jdepend-chart=build/pdepend/dependencies.svg', |
|
264
|
|
|
'--overview-pyramid=build/pdepend/overview-pyramid.svg', |
|
265
|
|
|
'src' |
|
266
|
|
|
]; |
|
267
|
|
|
$this->_run($cmd_parts); |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
/** |
|
271
|
|
|
* Get the total list of source files, including tests |
|
272
|
|
|
* |
|
273
|
|
|
* @return array |
|
274
|
|
|
*/ |
|
275
|
|
|
protected function getAllSourceFiles() |
|
276
|
|
|
{ |
|
277
|
|
|
$files = array_merge( |
|
278
|
|
|
glob_recursive('build/*.php'), |
|
279
|
|
|
glob_recursive('src/*.php'), |
|
280
|
|
|
glob_recursive('tests/*.php'), |
|
281
|
|
|
glob('*.php') |
|
282
|
|
|
); |
|
283
|
|
|
|
|
284
|
|
|
sort($files); |
|
285
|
|
|
|
|
286
|
|
|
return $files; |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
/** |
|
290
|
|
|
* Run php's linter in one parallel task for the passed chunk |
|
291
|
|
|
* |
|
292
|
|
|
* @param array $chunk |
|
293
|
|
|
*/ |
|
294
|
|
|
protected function parallelLint(array $chunk) |
|
295
|
|
|
{ |
|
296
|
|
|
$task = $this->taskParallelExec() |
|
297
|
|
|
->timeout(5) |
|
298
|
|
|
->printed(FALSE); |
|
299
|
|
|
|
|
300
|
|
|
foreach($chunk as $file) |
|
301
|
|
|
{ |
|
302
|
|
|
$task = $task->process("php -l {$file}"); |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
$task->run(); |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
/** |
|
309
|
|
|
* Generate copy paste detector report |
|
310
|
|
|
*/ |
|
311
|
|
|
protected function phpcpdReport() |
|
312
|
|
|
{ |
|
313
|
|
|
$cmd_parts = [ |
|
314
|
|
|
'vendor/bin/phpcpd', |
|
315
|
|
|
'--log-pmd build/logs/pmd-cpd.xml', |
|
316
|
|
|
'src' |
|
317
|
|
|
]; |
|
318
|
|
|
$this->_run($cmd_parts); |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
/** |
|
322
|
|
|
* Shortcut for joining an array of command arguments |
|
323
|
|
|
* and then running it |
|
324
|
|
|
* |
|
325
|
|
|
* @param array $cmd_parts - command arguments |
|
326
|
|
|
* @param string $join_on - what to join the command arguments with |
|
327
|
|
|
*/ |
|
328
|
|
|
protected function _run(array $cmd_parts, $join_on = ' ') |
|
329
|
|
|
{ |
|
330
|
|
|
$this->taskExec(implode($join_on, $cmd_parts))->run(); |
|
331
|
|
|
} |
|
332
|
|
|
} |
If you suppress an error, we recommend checking for the error condition explicitly: