Skip to content

Commit 23a9b06

Browse files
committed
Fixed types.
1 parent 1d8e7d8 commit 23a9b06

File tree

6 files changed

+179
-136
lines changed

6 files changed

+179
-136
lines changed

bin/phpdoc-checker

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env php
2+
<?php declare(strict_types = 1);
23

3-
<?php
44
/**
55
* PHP Docblock Checker
66
*
@@ -9,17 +9,18 @@
99
* @link http://www.phptesting.org/
1010
*/
1111

12-
$ownVendorAutoloader = dirname(__DIR__) . '/vendor/autoload.php';
13-
$parentVendorAutoloader = dirname(__DIR__) . '/../../autoload.php';
12+
$ownVendorAutoloader = \dirname(__DIR__) . '/vendor/autoload.php';
13+
$parentVendorAutoloader = \dirname(__DIR__) . '/../../autoload.php';
1414

15-
if (file_exists($ownVendorAutoloader)) {
15+
if (\file_exists($ownVendorAutoloader)) {
1616
require_once($ownVendorAutoloader);
17-
} elseif (file_exists($parentVendorAutoloader)) {
17+
} elseif (\file_exists($parentVendorAutoloader)) {
1818
require_once($parentVendorAutoloader);
1919
}
2020

2121
$command = new PhpDocChecker\CheckerCommand();
2222

2323
$application = new PhpDocChecker\CheckerApplication('PHP Docblock Checker', '@package_version@');
2424
$application->add($command);
25+
2526
$application->run();

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@
3232
"require": {
3333
"php": ">=7.1.0",
3434
"ext-json": "*",
35-
"nikic/php-parser": "^4.2",
36-
"symfony/console": "^4.3"
35+
"ext-ctype": "*",
36+
"nikic/php-parser": "^4.12",
37+
"symfony/console": "^4.4"
3738
},
3839
"bin": [
3940
"bin/phpdoc-checker"

src/CheckerApplication.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
<?php
22

3+
declare(strict_types = 1);
4+
35
namespace PhpDocChecker;
46

57
use Symfony\Component\Console\Application;
8+
use Symfony\Component\Console\Input\InputDefinition;
69
use Symfony\Component\Console\Input\InputInterface;
710

811
/**
@@ -12,10 +15,11 @@ class CheckerApplication extends Application
1215
{
1316
/**
1417
* Override the default command name logic and return our check command.
18+
*
1519
* @param InputInterface $input
1620
* @return string
1721
*/
18-
protected function getCommandName(InputInterface $input)
22+
protected function getCommandName(InputInterface $input): string
1923
{
2024
return 'check';
2125
}
@@ -24,7 +28,7 @@ protected function getCommandName(InputInterface $input)
2428
* Overridden so that the application doesn't expect the command
2529
* name to be the first argument.
2630
*/
27-
public function getDefinition()
31+
public function getDefinition(): InputDefinition
2832
{
2933
$inputDefinition = parent::getDefinition();
3034
// clear out the normal first argument, which is the command name

src/CheckerCommand.php

Lines changed: 60 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types = 1);
4+
35
namespace PhpDocChecker;
46

57
use DirectoryIterator;
@@ -66,7 +68,7 @@ class CheckerCommand extends Command
6668
/**
6769
* Configure the console command, add options, etc.
6870
*/
69-
protected function configure()
71+
protected function configure(): void
7072
{
7173
$this
7274
->setName('check')
@@ -84,31 +86,32 @@ protected function configure()
8486

8587
/**
8688
* Execute the actual docblock checker.
89+
*
8790
* @param InputInterface $input
8891
* @param OutputInterface $output
92+
*
8993
* @return int
9094
*/
91-
protected function execute(InputInterface $input, OutputInterface $output)
95+
protected function execute(InputInterface $input, OutputInterface $output): int
9296
{
93-
// Process options:
94-
$exclude = $input->getOption('exclude');
95-
$json = $input->getOption('json');
96-
$this->basePath = $input->getOption('directory');
97-
$this->verbose = !$json;
98-
$this->output = $output;
99-
$this->skipClasses = $input->getOption('skip-classes');
100-
$this->skipMethods = $input->getOption('skip-methods');
97+
$exclude = $input->getOption('exclude');
98+
$json = $input->getOption('json');
99+
$this->basePath = $input->getOption('directory');
100+
$this->verbose = !$json;
101+
$this->output = $output;
102+
$this->skipClasses = $input->getOption('skip-classes');
103+
$this->skipMethods = $input->getOption('skip-methods');
101104
$this->skipSignatures = $input->getOption('skip-signatures');
102-
$failOnWarnings = $input->getOption('fail-on-warnings');
103-
$startTime = microtime(true);
105+
$failOnWarnings = $input->getOption('fail-on-warnings');
106+
$startTime = \microtime(true);
104107

105108
// Set up excludes:
106-
if (!is_null($exclude)) {
107-
$this->exclude = array_map('trim', explode(',', $exclude));
109+
if (!\is_null($exclude)) {
110+
$this->exclude = \array_map('trim', \explode(',', $exclude));
108111
}
109112

110113
// Check base path ends with a slash:
111-
if (substr($this->basePath, -1) != '/') {
114+
if (\substr($this->basePath, -1) != '/') {
112115
$this->basePath .= '/';
113116
}
114117

@@ -118,23 +121,23 @@ protected function execute(InputInterface $input, OutputInterface $output)
118121

119122
// Check files:
120123
$filesPerLine = (int)$input->getOption('files-per-line');
121-
$totalFiles = count($files);
122-
$files = array_chunk($files, $filesPerLine);
124+
$totalFiles = \count($files);
125+
$files = \array_chunk($files, $filesPerLine);
123126
$processed = 0;
124-
$fileCountLength = strlen((string)$totalFiles);
127+
$fileCountLength = \strlen((string)$totalFiles);
125128

126129
if ($this->verbose) {
127130
$output->writeln('<fg=blue>PHPDoc Checker</>');
128131
$output->writeln('');
129132
}
130133

131-
while (count($files)) {
132-
$chunk = array_shift($files);
133-
$chunkFiles = count($chunk);
134+
while (\count($files)) {
135+
$chunk = \array_shift($files);
136+
$chunkFiles = \count($chunk);
134137

135-
while (count($chunk)) {
138+
while (\count($chunk)) {
136139
$processed++;
137-
$file = array_shift($chunk);
140+
$file = \array_shift($chunk);
138141

139142
list($errors, $warnings) = $this->processFile($file);
140143

@@ -150,23 +153,23 @@ protected function execute(InputInterface $input, OutputInterface $output)
150153
}
151154

152155
if ($this->verbose) {
153-
$this->output->write(str_pad('', $filesPerLine - $chunkFiles));
154-
$this->output->writeln(' ' . str_pad($processed, $fileCountLength, ' ', STR_PAD_LEFT) . '/' . $totalFiles . ' (' . floor((100/$totalFiles) * $processed) . '%)');
156+
$this->output->write(\str_pad('', $filesPerLine - $chunkFiles));
157+
$this->output->writeln(' ' . \str_pad((string)$processed, $fileCountLength, ' ', STR_PAD_LEFT) . '/' . $totalFiles . ' (' . \floor((100/$totalFiles) * $processed) . '%)');
155158
}
156159
}
157160

158161
if ($this->verbose) {
159-
$time = round(microtime(true) - $startTime, 2);
162+
$time = \round(\microtime(true) - $startTime, 2);
160163
$this->output->writeln('');
161164
$this->output->writeln('');
162-
$this->output->writeln('Checked ' . number_format($totalFiles) . ' files in ' . $time . ' seconds.');
163-
$this->output->write('<info>' . number_format($this->passed) . ' Passed</info>');
164-
$this->output->write(' / <fg=red>'.number_format(count($this->errors)).' Errors</>');
165-
$this->output->write(' / <fg=yellow>'.number_format(count($this->warnings)).' Warnings</>');
165+
$this->output->writeln('Checked ' . \number_format($totalFiles) . ' files in ' . $time . ' seconds.');
166+
$this->output->write('<info>' . \number_format($this->passed) . ' Passed</info>');
167+
$this->output->write(' / <fg=red>' . \number_format(\count($this->errors)) . ' Errors</>');
168+
$this->output->write(' / <fg=yellow>' . \number_format(\count($this->warnings)) . ' Warnings</>');
166169

167170
$this->output->writeln('');
168171

169-
if (count($this->errors) && !$input->getOption('info-only')) {
172+
if (\count($this->errors) && !$input->getOption('info-only')) {
170173
$this->output->writeln('');
171174
$this->output->writeln('');
172175

@@ -185,7 +188,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
185188
}
186189
}
187190

188-
if (count($this->warnings) && !$input->getOption('info-only')) {
191+
if (\count($this->warnings) && !$input->getOption('info-only')) {
189192
foreach ($this->warnings as $error) {
190193
$this->output->write('<fg=yellow>WARNING </> ');
191194

@@ -214,18 +217,19 @@ protected function execute(InputInterface $input, OutputInterface $output)
214217

215218
// Output JSON if requested:
216219
if ($json) {
217-
print json_encode(array_merge($this->errors, $this->warnings));
220+
print \json_encode(\array_merge($this->errors, $this->warnings));
218221
}
219222

220-
return count($this->errors) || ($failOnWarnings && count($this->warnings)) ? 1 : 0;
223+
return \count($this->errors) || ($failOnWarnings && \count($this->warnings)) ? 1 : 0;
221224
}
222225

223226
/**
224227
* Iterate through a directory and check all of the PHP files within it.
228+
*
225229
* @param string $path
226-
* @param string[] $worklist
230+
* @param string[] $workList
227231
*/
228-
protected function processDirectory($path = '', array &$worklist = [])
232+
protected function processDirectory(string $path = '', array &$workList = []): void
229233
{
230234
$dir = new DirectoryIterator($this->basePath . $path);
231235

@@ -236,34 +240,36 @@ protected function processDirectory($path = '', array &$worklist = [])
236240

237241
$itemPath = $path . $item->getFilename();
238242

239-
if (in_array($itemPath, $this->exclude)) {
243+
if (\in_array($itemPath, $this->exclude)) {
240244
continue;
241245
}
242246

243247
if ($item->isFile() && $item->getExtension() == 'php') {
244-
$worklist[] = $itemPath;
248+
$workList[] = $itemPath;
245249
}
246250

247251
if ($item->isDir()) {
248-
$this->processDirectory($itemPath . '/', $worklist);
252+
$this->processDirectory($itemPath . '/', $workList);
249253
}
250254
}
251255
}
252256

253257
/**
254258
* Check a specific PHP file for errors.
259+
*
255260
* @param string $file
261+
*
256262
* @return array
257263
*/
258-
protected function processFile($file)
264+
protected function processFile(string $file): array
259265
{
260-
$errors = false;
261-
$warnings = false;
266+
$errors = false;
267+
$warnings = false;
262268
$processor = new FileProcessor($this->basePath . $file);
263269

264270
if (!$this->skipClasses) {
265271
foreach ($processor->getClasses() as $name => $class) {
266-
if (is_null($class['docblock'])) {
272+
if (\is_null($class['docblock'])) {
267273
$errors = true;
268274
$this->errors[] = [
269275
'type' => 'class',
@@ -277,7 +283,7 @@ protected function processFile($file)
277283

278284
if (!$this->skipMethods) {
279285
foreach ($processor->getMethods() as $name => $method) {
280-
if (is_null($method['docblock'])) {
286+
if (\is_null($method['docblock'])) {
281287
$errors = true;
282288
$this->errors[] = [
283289
'type' => 'method',
@@ -292,7 +298,7 @@ protected function processFile($file)
292298

293299
if (!$this->skipSignatures) {
294300
foreach ($processor->getMethods() as $name => $method) {
295-
if (count($method['params'])) {
301+
if (\count($method['params'])) {
296302
foreach ($method['params'] as $param => $type) {
297303
if (empty($method['docblock']['params'][$param])) {
298304
$warnings = true;
@@ -304,8 +310,8 @@ protected function processFile($file)
304310
'line' => $method['line'],
305311
'param' => $param,
306312
];
307-
} elseif (is_array($type)) {
308-
$docblockTypes = explode('|', $method['docblock']['params'][$param]);
313+
} elseif (\is_array($type)) {
314+
$docblockTypes = \explode('|', $method['docblock']['params'][$param]);
309315
$normalizedType = $type;
310316
$normalizedType[0] = $docblockTypes[0];
311317

@@ -318,13 +324,13 @@ protected function processFile($file)
318324
'method' => $method['name'],
319325
'line' => $method['line'],
320326
'param' => $param,
321-
'param-type' => implode('|', $type),
327+
'param-type' => \implode('|', $type),
322328
'doc-type' => $method['docblock']['params'][$param],
323329
];
324330
}
325331
} elseif (!empty($type) && $method['docblock']['params'][$param] !== $type) {
326332
if (
327-
($type === 'array' && substr($method['docblock']['params'][$param], -2) === '[]')
333+
($type === 'array' && \substr($method['docblock']['params'][$param], -2) === '[]')
328334
|| $method['docblock']['params'][$param] === 'mixed'
329335
) {
330336
// Do nothing because this is fine.
@@ -360,8 +366,8 @@ protected function processFile($file)
360366
'method' => $method['name'],
361367
'line' => $method['line'],
362368
];
363-
} elseif (is_array($method['return'])) {
364-
$docblockTypes = explode('|', $method['docblock']['return']);
369+
} elseif (\is_array($method['return'])) {
370+
$docblockTypes = \explode('|', $method['docblock']['return']);
365371
if ($method['return'] !== $docblockTypes) {
366372
$warnings = true;
367373
$this->warnings[] = [
@@ -370,15 +376,15 @@ protected function processFile($file)
370376
'class' => $method['class'],
371377
'method' => $method['name'],
372378
'line' => $method['line'],
373-
'return-type' => implode('|', $method['return']),
379+
'return-type' => \implode('|', $method['return']),
374380
'doc-type' => $method['docblock']['return'],
375381
];
376382
}
377383
} elseif ($method['docblock']['return'] !== $method['return']) {
378384
if (
379-
($method['return'] === 'array' && substr($method['docblock']['return'], -2) === '[]')
385+
($method['return'] === 'array' && \substr($method['docblock']['return'], -2) === '[]')
380386
|| $method['docblock']['return'] === 'mixed'
381-
|| (strpos($method['docblock']['return'], '|') !== false && PHP_MAJOR_VERSION < 8)
387+
|| (\strpos($method['docblock']['return'], '|') !== false && PHP_MAJOR_VERSION < 8)
382388
) {
383389
// Do nothing because this is fine.
384390
} else {

0 commit comments

Comments
 (0)