1
1
<?php
2
2
3
+ declare (strict_types = 1 );
4
+
3
5
namespace PhpDocChecker ;
4
6
5
7
use DirectoryIterator ;
@@ -66,7 +68,7 @@ class CheckerCommand extends Command
66
68
/**
67
69
* Configure the console command, add options, etc.
68
70
*/
69
- protected function configure ()
71
+ protected function configure (): void
70
72
{
71
73
$ this
72
74
->setName ('check ' )
@@ -84,31 +86,32 @@ protected function configure()
84
86
85
87
/**
86
88
* Execute the actual docblock checker.
89
+ *
87
90
* @param InputInterface $input
88
91
* @param OutputInterface $output
92
+ *
89
93
* @return int
90
94
*/
91
- protected function execute (InputInterface $ input , OutputInterface $ output )
95
+ protected function execute (InputInterface $ input , OutputInterface $ output ): int
92
96
{
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 ' );
101
104
$ 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 );
104
107
105
108
// 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 ));
108
111
}
109
112
110
113
// Check base path ends with a slash:
111
- if (substr ($ this ->basePath , -1 ) != '/ ' ) {
114
+ if (\ substr ($ this ->basePath , -1 ) != '/ ' ) {
112
115
$ this ->basePath .= '/ ' ;
113
116
}
114
117
@@ -118,23 +121,23 @@ protected function execute(InputInterface $input, OutputInterface $output)
118
121
119
122
// Check files:
120
123
$ 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 );
123
126
$ processed = 0 ;
124
- $ fileCountLength = strlen ((string )$ totalFiles );
127
+ $ fileCountLength = \ strlen ((string )$ totalFiles );
125
128
126
129
if ($ this ->verbose ) {
127
130
$ output ->writeln ('<fg=blue>PHPDoc Checker</> ' );
128
131
$ output ->writeln ('' );
129
132
}
130
133
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 );
134
137
135
- while (count ($ chunk )) {
138
+ while (\ count ($ chunk )) {
136
139
$ processed ++;
137
- $ file = array_shift ($ chunk );
140
+ $ file = \ array_shift ($ chunk );
138
141
139
142
list ($ errors , $ warnings ) = $ this ->processFile ($ file );
140
143
@@ -150,23 +153,23 @@ protected function execute(InputInterface $input, OutputInterface $output)
150
153
}
151
154
152
155
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 ) . '%) ' );
155
158
}
156
159
}
157
160
158
161
if ($ this ->verbose ) {
159
- $ time = round (microtime (true ) - $ startTime , 2 );
162
+ $ time = \ round (\ microtime (true ) - $ startTime , 2 );
160
163
$ this ->output ->writeln ('' );
161
164
$ 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</> ' );
166
169
167
170
$ this ->output ->writeln ('' );
168
171
169
- if (count ($ this ->errors ) && !$ input ->getOption ('info-only ' )) {
172
+ if (\ count ($ this ->errors ) && !$ input ->getOption ('info-only ' )) {
170
173
$ this ->output ->writeln ('' );
171
174
$ this ->output ->writeln ('' );
172
175
@@ -185,7 +188,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
185
188
}
186
189
}
187
190
188
- if (count ($ this ->warnings ) && !$ input ->getOption ('info-only ' )) {
191
+ if (\ count ($ this ->warnings ) && !$ input ->getOption ('info-only ' )) {
189
192
foreach ($ this ->warnings as $ error ) {
190
193
$ this ->output ->write ('<fg=yellow>WARNING </> ' );
191
194
@@ -214,18 +217,19 @@ protected function execute(InputInterface $input, OutputInterface $output)
214
217
215
218
// Output JSON if requested:
216
219
if ($ json ) {
217
- print json_encode (array_merge ($ this ->errors , $ this ->warnings ));
220
+ print \ json_encode (\ array_merge ($ this ->errors , $ this ->warnings ));
218
221
}
219
222
220
- return count ($ this ->errors ) || ($ failOnWarnings && count ($ this ->warnings )) ? 1 : 0 ;
223
+ return \ count ($ this ->errors ) || ($ failOnWarnings && \ count ($ this ->warnings )) ? 1 : 0 ;
221
224
}
222
225
223
226
/**
224
227
* Iterate through a directory and check all of the PHP files within it.
228
+ *
225
229
* @param string $path
226
- * @param string[] $worklist
230
+ * @param string[] $workList
227
231
*/
228
- protected function processDirectory ($ path = '' , array &$ worklist = [])
232
+ protected function processDirectory (string $ path = '' , array &$ workList = []): void
229
233
{
230
234
$ dir = new DirectoryIterator ($ this ->basePath . $ path );
231
235
@@ -236,34 +240,36 @@ protected function processDirectory($path = '', array &$worklist = [])
236
240
237
241
$ itemPath = $ path . $ item ->getFilename ();
238
242
239
- if (in_array ($ itemPath , $ this ->exclude )) {
243
+ if (\ in_array ($ itemPath , $ this ->exclude )) {
240
244
continue ;
241
245
}
242
246
243
247
if ($ item ->isFile () && $ item ->getExtension () == 'php ' ) {
244
- $ worklist [] = $ itemPath ;
248
+ $ workList [] = $ itemPath ;
245
249
}
246
250
247
251
if ($ item ->isDir ()) {
248
- $ this ->processDirectory ($ itemPath . '/ ' , $ worklist );
252
+ $ this ->processDirectory ($ itemPath . '/ ' , $ workList );
249
253
}
250
254
}
251
255
}
252
256
253
257
/**
254
258
* Check a specific PHP file for errors.
259
+ *
255
260
* @param string $file
261
+ *
256
262
* @return array
257
263
*/
258
- protected function processFile ($ file )
264
+ protected function processFile (string $ file ): array
259
265
{
260
- $ errors = false ;
261
- $ warnings = false ;
266
+ $ errors = false ;
267
+ $ warnings = false ;
262
268
$ processor = new FileProcessor ($ this ->basePath . $ file );
263
269
264
270
if (!$ this ->skipClasses ) {
265
271
foreach ($ processor ->getClasses () as $ name => $ class ) {
266
- if (is_null ($ class ['docblock ' ])) {
272
+ if (\ is_null ($ class ['docblock ' ])) {
267
273
$ errors = true ;
268
274
$ this ->errors [] = [
269
275
'type ' => 'class ' ,
@@ -277,7 +283,7 @@ protected function processFile($file)
277
283
278
284
if (!$ this ->skipMethods ) {
279
285
foreach ($ processor ->getMethods () as $ name => $ method ) {
280
- if (is_null ($ method ['docblock ' ])) {
286
+ if (\ is_null ($ method ['docblock ' ])) {
281
287
$ errors = true ;
282
288
$ this ->errors [] = [
283
289
'type ' => 'method ' ,
@@ -292,7 +298,7 @@ protected function processFile($file)
292
298
293
299
if (!$ this ->skipSignatures ) {
294
300
foreach ($ processor ->getMethods () as $ name => $ method ) {
295
- if (count ($ method ['params ' ])) {
301
+ if (\ count ($ method ['params ' ])) {
296
302
foreach ($ method ['params ' ] as $ param => $ type ) {
297
303
if (empty ($ method ['docblock ' ]['params ' ][$ param ])) {
298
304
$ warnings = true ;
@@ -304,8 +310,8 @@ protected function processFile($file)
304
310
'line ' => $ method ['line ' ],
305
311
'param ' => $ param ,
306
312
];
307
- } elseif (is_array ($ type )) {
308
- $ docblockTypes = explode ('| ' , $ method ['docblock ' ]['params ' ][$ param ]);
313
+ } elseif (\ is_array ($ type )) {
314
+ $ docblockTypes = \ explode ('| ' , $ method ['docblock ' ]['params ' ][$ param ]);
309
315
$ normalizedType = $ type ;
310
316
$ normalizedType [0 ] = $ docblockTypes [0 ];
311
317
@@ -318,13 +324,13 @@ protected function processFile($file)
318
324
'method ' => $ method ['name ' ],
319
325
'line ' => $ method ['line ' ],
320
326
'param ' => $ param ,
321
- 'param-type ' => implode ('| ' , $ type ),
327
+ 'param-type ' => \ implode ('| ' , $ type ),
322
328
'doc-type ' => $ method ['docblock ' ]['params ' ][$ param ],
323
329
];
324
330
}
325
331
} elseif (!empty ($ type ) && $ method ['docblock ' ]['params ' ][$ param ] !== $ type ) {
326
332
if (
327
- ($ type === 'array ' && substr ($ method ['docblock ' ]['params ' ][$ param ], -2 ) === '[] ' )
333
+ ($ type === 'array ' && \ substr ($ method ['docblock ' ]['params ' ][$ param ], -2 ) === '[] ' )
328
334
|| $ method ['docblock ' ]['params ' ][$ param ] === 'mixed '
329
335
) {
330
336
// Do nothing because this is fine.
@@ -360,8 +366,8 @@ protected function processFile($file)
360
366
'method ' => $ method ['name ' ],
361
367
'line ' => $ method ['line ' ],
362
368
];
363
- } elseif (is_array ($ method ['return ' ])) {
364
- $ docblockTypes = explode ('| ' , $ method ['docblock ' ]['return ' ]);
369
+ } elseif (\ is_array ($ method ['return ' ])) {
370
+ $ docblockTypes = \ explode ('| ' , $ method ['docblock ' ]['return ' ]);
365
371
if ($ method ['return ' ] !== $ docblockTypes ) {
366
372
$ warnings = true ;
367
373
$ this ->warnings [] = [
@@ -370,15 +376,15 @@ protected function processFile($file)
370
376
'class ' => $ method ['class ' ],
371
377
'method ' => $ method ['name ' ],
372
378
'line ' => $ method ['line ' ],
373
- 'return-type ' => implode ('| ' , $ method ['return ' ]),
379
+ 'return-type ' => \ implode ('| ' , $ method ['return ' ]),
374
380
'doc-type ' => $ method ['docblock ' ]['return ' ],
375
381
];
376
382
}
377
383
} elseif ($ method ['docblock ' ]['return ' ] !== $ method ['return ' ]) {
378
384
if (
379
- ($ method ['return ' ] === 'array ' && substr ($ method ['docblock ' ]['return ' ], -2 ) === '[] ' )
385
+ ($ method ['return ' ] === 'array ' && \ substr ($ method ['docblock ' ]['return ' ], -2 ) === '[] ' )
380
386
|| $ method ['docblock ' ]['return ' ] === 'mixed '
381
- || (strpos ($ method ['docblock ' ]['return ' ], '| ' ) !== false && PHP_MAJOR_VERSION < 8 )
387
+ || (\ strpos ($ method ['docblock ' ]['return ' ], '| ' ) !== false && PHP_MAJOR_VERSION < 8 )
382
388
) {
383
389
// Do nothing because this is fine.
384
390
} else {
0 commit comments