-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCheckerCommand.php
More file actions
285 lines (229 loc) · 10.2 KB
/
Copy pathCheckerCommand.php
File metadata and controls
285 lines (229 loc) · 10.2 KB
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<?php
declare(strict_types=1);
namespace PhpDocChecker;
use DirectoryIterator;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Console command to check a directory of PHP files for Docblocks.
*
* @package PHPDoc Checker
*
* @author Dmitry Khomutov <poisoncorpsee@gmail.com>
* @author Dan Cryer <dan@block8.co.uk>
*/
class CheckerCommand extends Command
{
protected string $basePath = './';
protected bool $verbose = true;
protected array $errors = [];
protected array $warnings = [];
protected array $exclude = [];
protected array $files = [];
protected OutputInterface $output;
protected int $passed = 0;
protected CheckerFileProcessor $checkerFileProcessor;
/**
* Configure the console command, add options, etc.
*/
protected function configure(): void
{
$this
->setName('check')
->setDescription('Check PHP files within a directory for appropriate use of Docblocks.')
->addOption('exclude', 'x', InputOption::VALUE_REQUIRED, 'Files and directories to exclude.', null)
->addOption('directory', 'd', InputOption::VALUE_REQUIRED, 'Directory to scan.', './')
->addOption('files', 'f', InputOption::VALUE_REQUIRED, 'Files to scan.', null)
->addOption('skip-classes', null, InputOption::VALUE_NONE, 'Don\'t check classes for docblocks.')
->addOption('skip-methods', null, InputOption::VALUE_NONE, 'Don\'t check methods for docblocks.')
->addOption('skip-signatures', null, InputOption::VALUE_NONE, 'Don\'t check docblocks against method signatures.')
->addOption('json', 'j', InputOption::VALUE_NONE, 'Output JSON instead of a log.')
->addOption('files-per-line', 'l', InputOption::VALUE_REQUIRED, 'Number of files per line in progress', 50)
->addOption('fail-on-warnings', 'w', InputOption::VALUE_NONE, 'Consider the check failed if any warnings are produced.')
->addOption('info-only', 'i', InputOption::VALUE_NONE, 'Information-only mode, just show summary.');
}
/**
* Execute the actual docblock checker.
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$exclude = $input->getOption('exclude');
$json = $input->getOption('json');
$this->basePath = $input->getOption('directory');
$files = $input->getOption('files');
$this->verbose = !$json;
$this->output = $output;
$failOnWarnings = $input->getOption('fail-on-warnings');
$startTime = \microtime(true);
$skipClasses = $input->getOption('skip-classes');
$skipMethods = $input->getOption('skip-methods');
$skipSignatures = $input->getOption('skip-signatures');
$this->checkerFileProcessor = new CheckerFileProcessor(
$this->basePath,
$skipClasses,
$skipMethods,
$skipSignatures
);
// Set up excludes:
if (!\is_null($exclude)) {
$this->exclude = \array_map('trim', \explode(',', $exclude));
}
// Set up files:
if (!\is_null($files)) {
$this->files = \array_map('trim', \explode(',', $files));
}
// Check base path ends with a slash:
if (\substr($this->basePath, -1) != '/') {
$this->basePath .= '/';
}
// Get files to check:
$files = [];
if (count($this->files) > 0) {
$this->processFiles('', $this->files, $files);
} else {
$this->processDirectory('', $files);
}
// Check files:
$filesPerLine = (int)$input->getOption('files-per-line');
$totalFiles = \count($files);
$files = \array_chunk($files, $filesPerLine);
$processed = 0;
$fileCountLength = \strlen((string)$totalFiles);
if ($this->verbose) {
$output->writeln('<fg=blue>PHPDoc Checker</>');
$output->writeln('');
}
while (\count($files)) {
$chunk = \array_shift($files);
$chunkFiles = \count($chunk);
while (\count($chunk)) {
$processed++;
$file = \array_shift($chunk);
list($errors, $warnings) = $this->processFile($file);
if ($this->verbose) {
if ($errors) {
$this->output->write('<fg=red>F</>');
} elseif ($warnings) {
$this->output->write('<fg=yellow>W</>');
} else {
$this->output->write('<info>.</info>');
}
}
}
if ($this->verbose) {
$this->output->write(\str_pad('', $filesPerLine - $chunkFiles));
$this->output->writeln(' ' . \str_pad((string)$processed, $fileCountLength, ' ', STR_PAD_LEFT) . '/' . $totalFiles . ' (' . \floor((100/$totalFiles) * $processed) . '%)');
}
}
if ($this->verbose) {
$time = \round(\microtime(true) - $startTime, 2);
$this->output->writeln('');
$this->output->writeln('');
$this->output->writeln('Checked ' . \number_format($totalFiles) . ' files in ' . $time . ' seconds.');
$this->output->write('<info>' . \number_format($this->passed) . ' Passed</info>');
$this->output->write(' / <fg=red>' . \number_format(\count($this->errors)) . ' Errors</>');
$this->output->write(' / <fg=yellow>' . \number_format(\count($this->warnings)) . ' Warnings</>');
$this->output->writeln('');
if (\count($this->errors) && !$input->getOption('info-only')) {
$this->output->writeln('');
$this->output->writeln('');
foreach ($this->errors as $error) {
$this->output->write('<fg=red>ERROR </> ' . $error['file'] . ':' . $error['line'] . ' - ');
if ($error['type'] == 'class') {
$this->output->write('Class <info>' . $error['class'] . '</info> is missing a docblock.');
}
if ($error['type'] == 'method') {
$this->output->write('Method <info>' . $error['class'] . '::' . $error['method'] . '</info> is missing a docblock.');
}
$this->output->writeln('');
}
}
if (\count($this->warnings) && !$input->getOption('info-only')) {
foreach ($this->warnings as $error) {
$this->output->write('<fg=yellow>WARNING </> ');
if ($error['type'] == 'param-missing') {
$this->output->write('<info>' . $error['class'] . '::' . $error['method'] . '</info> - @param <fg=blue>'.$error['param'] . '</> missing.');
}
if ($error['type'] == 'param-mismatch') {
$this->output->write('<info>' . $error['class'] . '::' . $error['method'] . '</info> - @param <fg=blue>'.$error['param'] . '</> ('.$error['doc-type'].') does not match method signature ('.$error['param-type'].').');
}
if ($error['type'] == 'return-missing') {
$this->output->write('<info>' . $error['class'] . '::' . $error['method'] . '</info> - @return missing.');
}
if ($error['type'] == 'return-mismatch') {
$this->output->write('<info>' . $error['class'] . '::' . $error['method'] . '</info> - @return <fg=blue>'.$error['doc-type'] . '</> does not match method signature ('.$error['return-type'].').');
}
$this->output->writeln('');
}
}
$this->output->writeln('');
}
// Output JSON if requested:
if ($json) {
print \json_encode(\array_merge($this->errors, $this->warnings));
}
return \count($this->errors) || ($failOnWarnings && \count($this->warnings)) ? 1 : 0;
}
/**
* Iterate through a directory and check all of the PHP files within it.
*
* @param string[] $workList
*/
protected function processDirectory(string $path = '', array &$workList = []): void
{
$dir = new DirectoryIterator($this->basePath . $path);
foreach ($dir as $item) {
if ($item->isDot()) {
continue;
}
$itemPath = $path . $item->getFilename();
if (\in_array($itemPath, $this->exclude, true)) {
continue;
}
if ($item->isFile() && $item->getExtension() == 'php') {
$workList[] = $itemPath;
}
if ($item->isDir()) {
$this->processDirectory($itemPath . '/', $workList);
}
}
}
/**
* Iterate through the files and check them out
*
* @param string[] $files
* @param string[] $workList
*/
protected function processFiles(string $path = '', array $files = [], array &$workList = []): void
{
foreach ($files as $item) {
$itemPath = $path . $item;
if (\in_array($itemPath, $this->exclude, true)) {
continue;
}
if (is_file($itemPath) && pathinfo($itemPath)["extension"] == 'php') {
$workList[] = $itemPath;
}
}
}
/**
* Check a specific PHP file for errors.
*
*
*/
protected function processFile(string $file): array
{
$result = $this->checkerFileProcessor->processFile($file);
$this->errors = \array_merge($this->errors, $result['errors']);
$this->warnings = \array_merge($this->warnings, $result['warnings']);
if (0 === \count($result['errors'])) {
$this->passed += 1;
}
return [
(0 !== \count($result['errors'])),
(0 !== \count($result['warnings'])),
];
}
}