|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the Cubiche application. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) Cubiche |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
namespace Cubiche\Tools; |
|
12
|
|
|
|
|
13
|
|
|
use Composer\Script\Event; |
|
14
|
|
|
use Symfony\Component\Console\Application; |
|
15
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
16
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
17
|
|
|
use Symfony\Component\Process\ProcessBuilder; |
|
18
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* CodeQualityTool. |
|
22
|
|
|
* |
|
23
|
|
|
* @author Karel Osorio Ramírez <[email protected]> |
|
24
|
|
|
* @author Ivannis Suárez Jérez <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
class CodeQualityTool extends Application |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var OutputInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $output; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var InputInterface |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $input; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* CodeQualityTool constructor. |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct() |
|
42
|
|
|
{ |
|
43
|
|
|
parent::__construct('Cubiche Code Quality Tool', '1.0.0'); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* {@inheritdoc} |
|
48
|
|
|
*/ |
|
49
|
|
|
public function doRun(InputInterface $input, OutputInterface $output) |
|
50
|
|
|
{ |
|
51
|
|
|
$this->input = $input; |
|
52
|
|
|
$this->output = $output; |
|
53
|
|
|
|
|
54
|
|
|
$output->writeln( |
|
55
|
|
|
\sprintf( |
|
56
|
|
|
'<fg=white;options=bold;bg=blue>%s %s</fg=white;options=bold;bg=blue>', |
|
57
|
|
|
$this->getName(), |
|
58
|
|
|
$this->getVersion() |
|
59
|
|
|
) |
|
60
|
|
|
); |
|
61
|
|
|
|
|
62
|
|
|
$output->writeln('<info>Check composer</info>'); |
|
63
|
|
|
$this->checkComposer(); |
|
64
|
|
|
|
|
65
|
|
|
$output->writeln('<info>Running PHPLint</info>'); |
|
66
|
|
|
if (!$this->phpLint()) { |
|
67
|
|
|
throw new \Exception('There are some PHP syntax errors!'); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$output->writeln('<info>Checking code style</info>'); |
|
71
|
|
|
if (!$this->codeStyle()) { |
|
72
|
|
|
throw new \Exception(sprintf('There are coding standards violations!')); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$output->writeln('<info>Checking code style with PHPCS</info>'); |
|
76
|
|
|
if (!$this->codeStylePsr()) { |
|
77
|
|
|
throw new \Exception(sprintf('There are PHPCS coding standards violations!')); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$output->writeln('<info>Checking code mess with PHPMD</info>'); |
|
81
|
|
|
if (!$this->phPmd()) { |
|
82
|
|
|
throw new \Exception(sprintf('There are PHPMD violations!')); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$output->writeln('<info>Running unit tests</info>'); |
|
86
|
|
|
if (!$this->unitTests()) { |
|
87
|
|
|
throw new \Exception('Fix the unit tests!'); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$output->writeln('<info>Good job dude!</info>'); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Check composer.json and composer.lock files |
|
95
|
|
|
*/ |
|
96
|
|
|
private function checkComposer() |
|
97
|
|
|
{ |
|
98
|
|
|
$composerJsonDetected = false; |
|
99
|
|
|
$composerLockDetected = false; |
|
100
|
|
|
foreach (GitUtils::extractCommitedFiles() as $file) { |
|
|
|
|
|
|
101
|
|
|
if ($file === 'composer.json') { |
|
102
|
|
|
$composerJsonDetected = true; |
|
103
|
|
|
} |
|
104
|
|
|
if ($file === 'composer.lock') { |
|
105
|
|
|
$composerLockDetected = true; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
if ($composerJsonDetected && !$composerLockDetected) { |
|
109
|
|
|
$this->output->writeln( |
|
110
|
|
|
'<bg=yellow;fg=black> |
|
111
|
|
|
composer.lock must be commited if composer.json is modified! |
|
112
|
|
|
</bg=yellow;fg=black>' |
|
113
|
|
|
); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @return bool |
|
119
|
|
|
*/ |
|
120
|
|
|
protected function phpLint() |
|
121
|
|
|
{ |
|
122
|
|
|
$needle = '/(\.php)|(\.inc)$/'; |
|
123
|
|
|
$succeed = true; |
|
124
|
|
|
$config = ConfigUtils::getConfig('phplint', array( |
|
125
|
|
|
'triggered_by' => 'php' |
|
126
|
|
|
)); |
|
127
|
|
|
|
|
128
|
|
|
foreach (GitUtils::commitedFiles($needle) as $file) { |
|
129
|
|
|
$processBuilder = new ProcessBuilder(array($config['triggered_by'], '-l', $file)); |
|
130
|
|
|
$process = $processBuilder->getProcess(); |
|
131
|
|
|
$process->run(); |
|
132
|
|
|
if (!$process->isSuccessful()) { |
|
133
|
|
|
$this->output->writeln($file); |
|
134
|
|
|
$this->output->writeln(sprintf('<error>%s</error>', trim($process->getErrorOutput()))); |
|
135
|
|
|
$succeed = false; |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
return $succeed; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @return bool |
|
144
|
|
|
*/ |
|
145
|
|
|
protected function phPmd() |
|
146
|
|
|
{ |
|
147
|
|
|
$succeed = true; |
|
148
|
|
|
$config = ConfigUtils::getConfig('phpmd', array( |
|
149
|
|
|
'ruleset' => 'controversial', |
|
150
|
|
|
'triggered_by' => 'php' |
|
151
|
|
|
)); |
|
152
|
|
|
|
|
153
|
|
|
$rootPath = realpath(getcwd()); |
|
154
|
|
|
foreach (GitUtils::commitedFiles() as $file) { |
|
155
|
|
|
$processBuilder = new ProcessBuilder( |
|
156
|
|
|
array($config['triggered_by'], 'bin/phpmd', $file, 'text', implode(',', $config['ruleset'])) |
|
157
|
|
|
); |
|
158
|
|
|
$processBuilder->setWorkingDirectory($rootPath); |
|
159
|
|
|
$process = $processBuilder->getProcess(); |
|
160
|
|
|
$process->run(); |
|
161
|
|
|
if (!$process->isSuccessful()) { |
|
162
|
|
|
$this->output->writeln($file); |
|
163
|
|
|
$this->output->writeln(sprintf('<error>%s</error>', trim($process->getErrorOutput()))); |
|
164
|
|
|
$this->output->writeln(sprintf('<info>%s</info>', trim($process->getOutput()))); |
|
165
|
|
|
$succeed = false; |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
return $succeed; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @param string $suiteName |
|
174
|
|
|
* |
|
175
|
|
|
* @return bool |
|
176
|
|
|
*/ |
|
177
|
|
|
protected function unitTests($suiteName = null) |
|
178
|
|
|
{ |
|
179
|
|
|
$config = ConfigUtils::getConfig('test', array( |
|
180
|
|
|
'suites' => array() |
|
181
|
|
|
)); |
|
182
|
|
|
|
|
183
|
|
|
if (count($config['suites']) > 0) { |
|
184
|
|
|
foreach ($config['suites'] as $name => $suite) { |
|
185
|
|
|
if ($suiteName !== null && $name !== $suiteName) { |
|
186
|
|
|
continue; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
$trigger = isset($suite['triggered_by']) ? $suite['triggered_by'] : 'php'; |
|
190
|
|
|
|
|
191
|
|
|
$configFile = '.atoum.php'; |
|
192
|
|
|
if (isset($suite['config_file']) && $suite['config_file'] !== null) { |
|
193
|
|
|
$configFile = $suite['config_file']; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
$bootstrapFile = '.bootstrap.atoum.php'; |
|
197
|
|
|
if (isset($suite['bootstrap_file']) && $suite['bootstrap_file'] !== null) { |
|
198
|
|
|
$bootstrapFile = $suite['bootstrap_file']; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
$arguments = array( |
|
202
|
|
|
$trigger, |
|
203
|
|
|
'bin/atoum', |
|
204
|
|
|
'-c', |
|
205
|
|
|
$configFile, |
|
206
|
|
|
'-bf', |
|
207
|
|
|
$bootstrapFile |
|
208
|
|
|
); |
|
209
|
|
|
|
|
210
|
|
|
if (isset($suite['directories']) && $suite['directories'] !== null) { |
|
211
|
|
|
$arguments[] = '-d'; |
|
212
|
|
|
$arguments[] = implode(" ", $suite['directories']); |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
$processBuilder = new ProcessBuilder($arguments); |
|
216
|
|
|
$processBuilder->setWorkingDirectory(getcwd()); |
|
217
|
|
|
$processBuilder->setTimeout(null); |
|
218
|
|
|
|
|
219
|
|
|
$test = $processBuilder->getProcess(); |
|
220
|
|
|
$test->run( |
|
221
|
|
|
function ($type, $buffer) { |
|
222
|
|
|
$this->output->write($buffer); |
|
223
|
|
|
} |
|
224
|
|
|
); |
|
225
|
|
|
|
|
226
|
|
|
if (!$test->isSuccessful()) { |
|
227
|
|
|
return false; |
|
228
|
|
|
} |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
return true; |
|
232
|
|
|
} else { |
|
233
|
|
|
$this->output->writeln('<comment>There is no tests configuration suites</comment>'); |
|
234
|
|
|
|
|
235
|
|
|
return true; |
|
236
|
|
|
} |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* @param string $directory |
|
241
|
|
|
* |
|
242
|
|
|
* @return bool |
|
243
|
|
|
*/ |
|
244
|
|
|
protected function codeStyle($directory = null) |
|
245
|
|
|
{ |
|
246
|
|
|
$succeed = true; |
|
247
|
|
|
$config = ConfigUtils::getConfig('phpcsfixer', array( |
|
248
|
|
|
'fixers' => [ |
|
249
|
|
|
'-psr0','eof_ending','indentation','linefeed','lowercase_keywords','trailing_spaces', 'short_tag', |
|
250
|
|
|
'php_closing_tag','extra_empty_lines','elseif','function_declaration', '-phpdoc_scalar', '-phpdoc_types' |
|
251
|
|
|
], |
|
252
|
|
|
'triggered_by' => 'php' |
|
253
|
|
|
)); |
|
254
|
|
|
|
|
255
|
|
|
$fixers = implode(',', $config['fixers']); |
|
256
|
|
|
if ($directory !== null && is_dir($directory)) { |
|
257
|
|
|
$processBuilder = new ProcessBuilder(array( |
|
258
|
|
|
$config['triggered_by'], |
|
259
|
|
|
'bin/php-cs-fixer', |
|
260
|
|
|
'--dry-run', |
|
261
|
|
|
'--verbose', |
|
262
|
|
|
'fix', |
|
263
|
|
|
$directory, |
|
264
|
|
|
'--fixers='.$fixers, |
|
265
|
|
|
)); |
|
266
|
|
|
|
|
267
|
|
|
$processBuilder->setWorkingDirectory(getcwd()); |
|
268
|
|
|
$phpCsFixer = $processBuilder->getProcess(); |
|
269
|
|
|
$phpCsFixer->run(); |
|
270
|
|
|
|
|
271
|
|
View Code Duplication |
if (!$phpCsFixer->isSuccessful()) { |
|
|
|
|
|
|
272
|
|
|
$this->output->writeln(sprintf('<error>%s</error>', trim($phpCsFixer->getOutput()))); |
|
273
|
|
|
|
|
274
|
|
|
return false; |
|
275
|
|
|
} |
|
276
|
|
|
} else { |
|
277
|
|
|
foreach (GitUtils::commitedFiles() as $file) { |
|
278
|
|
|
$processBuilder = new ProcessBuilder(array( |
|
279
|
|
|
$config['triggered_by'], |
|
280
|
|
|
'bin/php-cs-fixer', |
|
281
|
|
|
'--dry-run', |
|
282
|
|
|
'--verbose', |
|
283
|
|
|
'fix', |
|
284
|
|
|
$file, |
|
285
|
|
|
'--fixers='.$fixers, |
|
286
|
|
|
)); |
|
287
|
|
|
|
|
288
|
|
|
$processBuilder->setWorkingDirectory(getcwd()); |
|
289
|
|
|
$phpCsFixer = $processBuilder->getProcess(); |
|
290
|
|
|
$phpCsFixer->run(); |
|
291
|
|
|
|
|
292
|
|
View Code Duplication |
if (!$phpCsFixer->isSuccessful()) { |
|
|
|
|
|
|
293
|
|
|
$this->output->writeln(sprintf('<error>%s</error>', trim($phpCsFixer->getOutput()))); |
|
294
|
|
|
$succeed = false; |
|
295
|
|
|
} |
|
296
|
|
|
} |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
return $succeed; |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
/** |
|
303
|
|
|
* @param string $directory |
|
304
|
|
|
* |
|
305
|
|
|
* @return bool |
|
306
|
|
|
*/ |
|
307
|
|
|
protected function codeStylePsr($directory = null) |
|
308
|
|
|
{ |
|
309
|
|
|
$succeed = true; |
|
310
|
|
|
$config = ConfigUtils::getConfig('phpcs', array( |
|
311
|
|
|
'standard' => 'PSR2', |
|
312
|
|
|
'triggered_by' => 'php' |
|
313
|
|
|
)); |
|
314
|
|
|
|
|
315
|
|
|
if ($directory !== null && is_dir($directory)) { |
|
316
|
|
|
$processBuilder = new ProcessBuilder( |
|
317
|
|
|
array($config['triggered_by'], 'bin/phpcs', '--standard='.$config['standard'], $directory) |
|
318
|
|
|
); |
|
319
|
|
|
|
|
320
|
|
|
$processBuilder->setWorkingDirectory(getcwd()); |
|
321
|
|
|
$phpCsFixer = $processBuilder->getProcess(); |
|
322
|
|
|
$phpCsFixer->run(function ($type, $buffer) { |
|
323
|
|
|
$this->output->write($buffer); |
|
324
|
|
|
}); |
|
325
|
|
|
|
|
326
|
|
View Code Duplication |
if (!$phpCsFixer->isSuccessful()) { |
|
|
|
|
|
|
327
|
|
|
$this->output->writeln(sprintf('<error>%s</error>', trim($phpCsFixer->getOutput()))); |
|
328
|
|
|
|
|
329
|
|
|
return false; |
|
330
|
|
|
} |
|
331
|
|
|
} else { |
|
332
|
|
|
foreach (GitUtils::commitedFiles() as $file) { |
|
333
|
|
|
$processBuilder = new ProcessBuilder( |
|
334
|
|
|
array($config['triggered_by'], 'bin/phpcs', '--standard='.$config['standard'], $file) |
|
335
|
|
|
); |
|
336
|
|
|
|
|
337
|
|
|
$processBuilder->setWorkingDirectory(getcwd()); |
|
338
|
|
|
$phpCsFixer = $processBuilder->getProcess(); |
|
339
|
|
|
$phpCsFixer->run(function ($type, $buffer) { |
|
340
|
|
|
$this->output->write($buffer); |
|
341
|
|
|
}); |
|
342
|
|
|
|
|
343
|
|
View Code Duplication |
if (!$phpCsFixer->isSuccessful()) { |
|
|
|
|
|
|
344
|
|
|
$this->output->writeln(sprintf('<error>%s</error>', trim($phpCsFixer->getOutput()))); |
|
345
|
|
|
$succeed = false; |
|
346
|
|
|
} |
|
347
|
|
|
} |
|
348
|
|
|
} |
|
349
|
|
|
|
|
350
|
|
|
return $succeed; |
|
351
|
|
|
} |
|
352
|
|
|
|
|
353
|
|
|
/** |
|
354
|
|
|
* @param Event $event |
|
355
|
|
|
*/ |
|
356
|
|
|
public static function checkHooks(Event $event) |
|
357
|
|
|
{ |
|
358
|
|
|
$io = $event->getIO(); |
|
359
|
|
|
|
|
360
|
|
|
if (!is_dir(getcwd().'/.git/hooks')) { |
|
361
|
|
|
$io->write('<error>The .git/hooks directory does not exist, execute git init</error>'); |
|
362
|
|
|
|
|
363
|
|
|
return; |
|
364
|
|
|
} |
|
365
|
|
|
|
|
366
|
|
|
$gitPath = getcwd().'/.git/hooks/pre-commit'; |
|
367
|
|
|
$docPath = getcwd().'/bin/pre-commit'; |
|
368
|
|
|
$gitHook = @file_get_contents($gitPath); |
|
369
|
|
|
$docHook = @file_get_contents($docPath); |
|
370
|
|
|
if ($gitHook !== $docHook) { |
|
371
|
|
|
self::createSymlink($gitPath, $docPath); |
|
372
|
|
|
} |
|
373
|
|
|
} |
|
374
|
|
|
|
|
375
|
|
|
/** |
|
376
|
|
|
* @param string $symlinkTarget |
|
377
|
|
|
* @param string $symlinkName |
|
378
|
|
|
* |
|
379
|
|
|
* @throws \Exception |
|
380
|
|
|
*/ |
|
381
|
|
|
private static function createSymlink($symlinkTarget, $symlinkName) |
|
382
|
|
|
{ |
|
383
|
|
|
$processBuilder = new ProcessBuilder(array('rm', '-rf', $symlinkTarget)); |
|
384
|
|
|
$process = $processBuilder->getProcess(); |
|
385
|
|
|
$process->run(); |
|
386
|
|
|
|
|
387
|
|
|
if (symlink($symlinkName, $symlinkTarget) === false) { |
|
388
|
|
|
throw new \Exception('Error occured when trying to create a symlink.'); |
|
389
|
|
|
} |
|
390
|
|
|
} |
|
391
|
|
|
} |
|
392
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.