1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace uuf6429\ElderBrother\Action; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
6
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
7
|
|
|
use Symfony\Component\Process\Process; |
8
|
|
|
use uuf6429\ElderBrother\Change\FileList; |
9
|
|
|
|
10
|
|
|
class PhpLinter extends ActionAbstract |
11
|
|
|
{ |
12
|
|
|
/** @var FileList */ |
13
|
|
|
protected $files; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Ensures that all the provided files are valid PHP files, terminating the |
17
|
|
|
* process with an error and non-zero exit code, if not. |
18
|
|
|
* |
19
|
|
|
* @param FileList $files The files to check |
20
|
|
|
*/ |
21
|
7 |
|
public function __construct(FileList $files) |
22
|
|
|
{ |
23
|
7 |
|
$this->files = $files; |
24
|
7 |
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
2 |
|
public function getName() |
30
|
|
|
{ |
31
|
2 |
|
return 'PHP Syntax Check (PhpLinter)'; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
3 |
|
public function isSupported() |
38
|
|
|
{ |
39
|
3 |
|
$process = new Process('php -v'); |
40
|
|
|
|
41
|
3 |
|
if ($process->run() !== 0) { |
42
|
|
|
$this->logger->warning( |
43
|
|
|
sprintf( |
44
|
|
|
'PHP could not be executed successfully (exit code: %d): %s', |
45
|
|
|
$process->getExitCode(), |
46
|
|
|
$process->getOutput() |
47
|
|
|
) |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
3 |
|
return $process->isSuccessful(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
6 |
|
public function execute(InputInterface $input, OutputInterface $output) |
58
|
|
|
{ |
59
|
6 |
|
$files = $this->files->toArray(); |
60
|
|
|
|
61
|
6 |
|
if (empty($files)) { |
62
|
1 |
|
return; |
63
|
|
|
} |
64
|
|
|
|
65
|
5 |
|
$progress = $this->createProgressBar($input, $output); |
66
|
5 |
|
$progress->start(count($files)); |
67
|
|
|
|
68
|
5 |
|
$failed = []; |
69
|
|
|
|
70
|
5 |
|
foreach ($files as $file) { |
71
|
5 |
|
$progress->setMessage('Checking <info>' . $file . '</info>...'); |
72
|
5 |
|
$process = new Process('php -l ' . escapeshellarg($file)); |
73
|
|
|
|
74
|
5 |
|
if ($process->run() !== 0) { |
75
|
3 |
|
$failed[$file] = array_filter( |
76
|
3 |
|
explode("\n", str_replace(PHP_EOL, "\n", $process->getErrorOutput() ?: $process->getOutput())), |
77
|
3 |
|
function ($line) { |
78
|
3 |
|
return strlen($line) |
79
|
3 |
|
&& substr($line, 0, 15) !== 'Errors parsing '; |
80
|
3 |
|
} |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
5 |
|
$progress->advance(); |
85
|
|
|
} |
86
|
|
|
|
87
|
5 |
|
if (count($failed)) { |
88
|
3 |
|
$message = 'PhpLinter failed for the following file(s):'; |
89
|
3 |
|
foreach ($failed as $file => $result) { |
90
|
3 |
|
$message .= PHP_EOL . '- ' . $file . ':'; |
91
|
3 |
|
$message .= PHP_EOL . ' - ' . implode(PHP_EOL . ' - ', $result); |
92
|
|
|
} |
93
|
3 |
|
throw new \RuntimeException($message); |
94
|
|
|
} |
95
|
|
|
|
96
|
2 |
|
$progress->setMessage('Finished.'); |
97
|
2 |
|
$progress->finish(); |
98
|
2 |
|
} |
99
|
|
|
} |
100
|
|
|
|