/
PhpCodingStyleCommand.php
232 lines (216 loc) · 8.21 KB
/
PhpCodingStyleCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
namespace Concrete\Core\Console\Command;
use Concrete\Core\Console\Command;
use Concrete\Core\Error\UserMessageException;
use Concrete\Core\Support\CodingStyle\PhpFixer;
use Concrete\Core\Support\CodingStyle\PhpFixerOptions;
use PhpCsFixer\Error\ErrorsManager;
use PhpCsFixer\FixerFileProcessedEvent;
use RuntimeException;
use SplFileInfo;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class PhpCodingStyleCommand extends Command
{
protected $description = 'Check or fix the PHP coding style.';
public function __construct($name = null)
{
$defaultWebRoot = PhpFixerOptions::getDefaultWebRoot();
$this->signature = <<<EOT
c5:phpcs
{--no-cache : Specify this flag to turn off the coding style cache}
{--webroot={$defaultWebRoot} : Specify the webroot (use - to auto-detect it)}
{--p|php= : The minimum PHP version }
{action : Either "fix" or "check"}
{path* : The path to one or more files or directories }
EOT
;
parent::__construct($name);
}
public function handle(PhpFixer $fixer, EventDispatcherInterface $eventDispatcher)
{
$action = $this->input->getArgument('action');
switch ($action) {
case 'fix':
$dryRun = false;
break;
case 'check':
$dryRun = true;
break;
default:
throw new RuntimeException(sprintf('Unknown "%s" action: accepted values are "fix" and "check".', $action));
}
$splFileInfos = [];
foreach ($this->input->getArgument('path') as $path) {
$absolutePath = realpath($path);
if ($absolutePath === false) {
throw new RuntimeException(sprintf('The file/directory "%s" could not be found (it is not readable)', $path));
}
$splFileInfo = new SplFileInfo($absolutePath);
if ($dryRun === false && !$splFileInfo->isWritable()) {
throw new RuntimeException(sprintf('The file/directory "%s" is not writable', $path));
}
$splFileInfos[] = $splFileInfo;
}
$webroot = (string) $this->input->getOption('webroot');
if ($webroot !== '') {
if ($webroot === '-') {
$webroot = $this->detectWebRoot($splFileInfos[0]->getPathname());
}
$fixer->getOptions()->setWebRoot($webroot);
}
$fixer->getOptions()
->setIsCacheDisabled($this->input->getOption('no-cache'))
->setMinimumPhpVersion($this->input->getOption('php'))
;
list($counters, $changes, $errors) = $fixer->fix($this->input, $this->output, $splFileInfos, $dryRun);
/* @var array $counters */
/* @var array $changes */
/* @var \PhpCsFixer\Error\ErrorsManager $errors */
$this->printChanges($changes, $dryRun);
$this->printErrors($errors, $dryRun);
$this->printCountersTable($counters, $dryRun);
if ($dryRun) {
return empty($changes) && $errors->isEmpty() ? 0 : 1;
}
return $errors->isEmpty() ? 0 : 1;
}
protected function configure()
{
$this->setHelp(<<<EOT
Check or fix the PHP coding style.
Return values when checking the coding style:
- 0: the coding style is valid and no error occurred
- 1: some fixes are needed or some error occurred
Return values when applying the coding style:
- 0: no error occurred
- 1: some error occurred
EOT
);
}
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Console\Command::initialize()
*/
protected function initialize(InputInterface $input, OutputInterface $output)
{
$this->canRunAsRoot = $input->getArgument('action') !== 'fix';
return parent::initialize($input, $output);
}
/**
* @param array $changes
* @param bool $dryRun
*/
protected function printChanges(array $changes, $dryRun)
{
if (!$dryRun) {
return;
}
foreach ($changes as $file => $change) {
$this->output->writeln(sprintf('### CHANGES REQUIRED IN FILE %s ###', $file));
$this->output->writeln($change['diff']);
}
}
/**
* @param \PhpCsFixer\Error\ErrorsManager $errors
* @param bool $dryRun
*/
protected function printErrors(ErrorsManager $errors, $dryRun)
{
foreach ([
'LINTING ERRORS' => $errors->getInvalidErrors(),
'FIXER ERRORS' => $errors->getExceptionErrors(),
'POST-PROCESSING ERRORS' => $errors->getLintErrors(),
] as $name => $list) {
if (empty($list)) {
continue;
}
$this->output->writeln(sprintf('<error>### %s ###</error>', $name));
foreach ($list as $index => $error) {
$this->output->writeln(sprintf('<error>%s. File %s</error>', $index + 1, $error->getFilePath()));
$exception = $error->getSource();
if ($exception) {
$lines = explode("\n", str_replace("\r\n", "\n", trim($exception->getMessage())));
foreach ($lines as $line) {
$this->output->writeln(sprintf('<error> %s</error>', $line));
}
}
}
$this->output->writeln('');
}
}
/**
* @param array $counters
* @param bool $dryRun
*/
protected function printCountersTable(array $counters, $dryRun)
{
$rows = [];
$headerLength = 0;
$valueLength = 0;
ksort($counters);
foreach ($counters as $state => $count) {
$count = number_format($count, 0, '.', ',');
switch ($state) {
case FixerFileProcessedEvent::STATUS_INVALID:
$header = 'Files ignored for syntax errors';
break;
break;
case FixerFileProcessedEvent::STATUS_SKIPPED:
$header = 'Skipped files';
break;
case FixerFileProcessedEvent::STATUS_NO_CHANGES:
$header = $dryRun ? 'Files already fixed' : 'Unchanged files';
break;
case FixerFileProcessedEvent::STATUS_FIXED:
$header = $dryRun ? 'Files that should be fixed' : 'Fixed files';
break;
case FixerFileProcessedEvent::STATUS_EXCEPTION:
$header = 'Fixer exceptions';
break;
case FixerFileProcessedEvent::STATUS_LINT:
$header = 'Linter errors';
break;
case FixerFileProcessedEvent::STATUS_UNKNOWN:
default:
$header = 'unknown';
break;
}
$rows[] = [$header, $count];
$headerLength = max($headerLength, strlen($header));
$valueLength = max($valueLength, strlen($count));
}
if (!empty($rows)) {
$this->output->writeln('Summary:');
foreach ($rows as $row) {
$this->output->writeln('- ' . str_pad($row[0], $headerLength, ' ', STR_PAD_RIGHT) . ': ' . str_pad($row[1], $valueLength, ' ', STR_PAD_LEFT));
}
}
}
/**
* @param string $startingPoint
*
* @throws \Concrete\Core\Error\UserMessageException
*
* @return string
*/
private function detectWebRoot($startingPoint)
{
if (is_file($startingPoint)) {
$startingPoint = dirname($startingPoint);
}
$startingPoint = rtrim(str_replace(DIRECTORY_SEPARATOR, '/', $startingPoint), '/');
if (strpos($startingPoint, '/') === false) {
throw new UserMessageException('Unable to detect the webroot');
}
if (is_file("{$startingPoint}/index.php") && is_file("{$startingPoint}/concrete/dispatcher.php")) {
return $startingPoint;
}
if (preg_match('_^/+$_', $startingPoint) || preg_match('_^\w:/*$_', $startingPoint)) {
throw new UserMessageException('Unable to detect the webroot');
}
return $this->detectWebRoot(dirname($startingPoint));
}
}