Conditions | 48 |
Paths | > 20000 |
Total Lines | 261 |
Code Lines | 157 |
Lines | 36 |
Ratio | 13.79 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
230 | private function run() |
||
231 | { |
||
232 | // The class that manages all reporters for the run. |
||
233 | $this->reporter = new Reporter($this->config); |
||
234 | |||
235 | // Include bootstrap files. |
||
236 | foreach ($this->config->bootstrap as $bootstrap) { |
||
237 | include $bootstrap; |
||
238 | } |
||
239 | |||
240 | if ($this->config->stdin === true) { |
||
241 | $fileContents = $this->config->stdinContent; |
||
242 | if ($fileContents === null) { |
||
243 | $handle = fopen('php://stdin', 'r'); |
||
244 | stream_set_blocking($handle, true); |
||
245 | $fileContents = stream_get_contents($handle); |
||
246 | fclose($handle); |
||
247 | } |
||
248 | |||
249 | $todo = new FileList($this->config, $this->ruleset); |
||
250 | $dummy = new DummyFile($fileContents, $this->ruleset, $this->config); |
||
251 | $todo->addFile($dummy->path, $dummy); |
||
252 | |||
253 | $numFiles = 1; |
||
254 | } else { |
||
255 | if (empty($this->config->files) === true) { |
||
256 | echo 'ERROR: You must supply at least one file or directory to process.'.PHP_EOL.PHP_EOL; |
||
257 | $this->config->printUsage(); |
||
258 | exit(0); |
||
259 | } |
||
260 | |||
261 | if (PHP_CodeSniffer_VERBOSITY > 0) { |
||
262 | echo 'Creating file list... '; |
||
263 | } |
||
264 | |||
265 | $todo = new FileList($this->config, $this->ruleset); |
||
266 | $numFiles = count($todo); |
||
267 | |||
268 | if (PHP_CodeSniffer_VERBOSITY > 0) { |
||
269 | echo "DONE ($numFiles files in queue)".PHP_EOL; |
||
270 | } |
||
271 | |||
272 | if ($this->config->cache === true) { |
||
273 | if (PHP_CodeSniffer_VERBOSITY > 0) { |
||
274 | echo 'Loading cache... '; |
||
275 | } |
||
276 | |||
277 | Cache::load($this->ruleset, $this->config); |
||
278 | |||
279 | if (PHP_CodeSniffer_VERBOSITY > 0) { |
||
280 | $size = Cache::getSize(); |
||
281 | echo "DONE ($size files in cache)".PHP_EOL; |
||
282 | } |
||
283 | } |
||
284 | }//end if |
||
285 | |||
286 | $numProcessed = 0; |
||
287 | $dots = 0; |
||
288 | $maxLength = strlen($numFiles); |
||
289 | $lastDir = ''; |
||
290 | $childProcs = array(); |
||
291 | |||
292 | // Turn all sniff errors into exceptions. |
||
293 | set_error_handler(array($this, 'handleErrors')); |
||
294 | |||
295 | // If verbosity is too high, turn off parallelism so the |
||
296 | // debug output is clean. |
||
297 | if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
298 | $this->config->parallel = 1; |
||
299 | } |
||
300 | |||
301 | if ($this->config->parallel === 1) { |
||
302 | // Running normally. |
||
303 | foreach ($todo as $path => $file) { |
||
304 | $currDir = dirname($path); |
||
305 | View Code Duplication | if ($lastDir !== $currDir) { |
|
306 | if (PHP_CodeSniffer_VERBOSITY > 0 || (PHP_CodeSniffer_CBF === true && $this->config->stdin === false)) { |
||
307 | echo 'Changing into directory '.Common::stripBasepath($currDir, $this->config->basepath).PHP_EOL; |
||
308 | } |
||
309 | |||
310 | $lastDir = $currDir; |
||
311 | } |
||
312 | |||
313 | $this->processFile($file); |
||
314 | |||
315 | $numProcessed++; |
||
316 | |||
317 | if (PHP_CodeSniffer_VERBOSITY > 0 |
||
318 | || $this->config->interactive === true |
||
319 | || $this->config->showProgress === false |
||
320 | ) { |
||
321 | continue; |
||
322 | } |
||
323 | |||
324 | // Show progress information. |
||
325 | if ($file->ignored === true) { |
||
326 | echo 'S'; |
||
327 | } else { |
||
328 | $errors = $file->getErrorCount(); |
||
329 | $warnings = $file->getWarningCount(); |
||
330 | View Code Duplication | if ($errors > 0) { |
|
331 | if ($this->config->colors === true) { |
||
332 | echo "\033[31m"; |
||
333 | } |
||
334 | |||
335 | echo 'E'; |
||
336 | } else if ($warnings > 0) { |
||
337 | if ($this->config->colors === true) { |
||
338 | echo "\033[33m"; |
||
339 | } |
||
340 | |||
341 | echo 'W'; |
||
342 | } else { |
||
343 | echo '.'; |
||
344 | } |
||
345 | |||
346 | if ($this->config->colors === true) { |
||
347 | echo "\033[0m"; |
||
348 | } |
||
349 | }//end if |
||
350 | |||
351 | $dots++; |
||
352 | View Code Duplication | if ($dots === 60) { |
|
353 | $padding = ($maxLength - strlen($numProcessed)); |
||
354 | echo str_repeat(' ', $padding); |
||
355 | $percent = round(($numProcessed / $numFiles) * 100); |
||
356 | echo " $numProcessed / $numFiles ($percent%)".PHP_EOL; |
||
357 | $dots = 0; |
||
358 | } |
||
359 | }//end foreach |
||
360 | } else { |
||
361 | // Batching and forking. |
||
362 | $numFiles = count($todo); |
||
363 | $numPerBatch = ceil($numFiles / $this->config->parallel); |
||
364 | |||
365 | for ($batch = 0; $batch < $this->config->parallel; $batch++) { |
||
366 | $startAt = ($batch * $numPerBatch); |
||
367 | if ($startAt >= $numFiles) { |
||
368 | break; |
||
369 | } |
||
370 | |||
371 | $endAt = ($startAt + $numPerBatch); |
||
372 | if ($endAt > $numFiles) { |
||
373 | $endAt = $numFiles; |
||
374 | } |
||
375 | |||
376 | $childOutFilename = tempnam(sys_get_temp_dir(), 'phpcs-child'); |
||
377 | $pid = pcntl_fork(); |
||
378 | if ($pid === -1) { |
||
379 | throw new RuntimeException('Failed to create child process'); |
||
380 | } else if ($pid !== 0) { |
||
381 | $childProcs[] = array( |
||
382 | 'pid' => $pid, |
||
383 | 'out' => $childOutFilename, |
||
384 | ); |
||
385 | } else { |
||
386 | // Move forward to the start of the batch. |
||
387 | $todo->rewind(); |
||
388 | for ($i = 0; $i < $startAt; $i++) { |
||
389 | $todo->next(); |
||
390 | } |
||
391 | |||
392 | // Reset the reporter to make sure only figures from this |
||
393 | // file batch are recorded. |
||
394 | $this->reporter->totalFiles = 0; |
||
395 | $this->reporter->totalErrors = 0; |
||
396 | $this->reporter->totalWarnings = 0; |
||
397 | $this->reporter->totalFixable = 0; |
||
398 | |||
399 | // Process the files. |
||
400 | $pathsProcessed = array(); |
||
401 | ob_start(); |
||
402 | for ($i = $startAt; $i < $endAt; $i++) { |
||
403 | $path = $todo->key(); |
||
404 | $file = $todo->current(); |
||
405 | |||
406 | $currDir = dirname($path); |
||
407 | View Code Duplication | if ($lastDir !== $currDir) { |
|
408 | if (PHP_CodeSniffer_VERBOSITY > 0 || (PHP_CodeSniffer_CBF === true && $this->config->stdin === false)) { |
||
409 | echo 'Changing into directory '.Common::stripBasepath($currDir, $this->config->basepath).PHP_EOL; |
||
410 | } |
||
411 | |||
412 | $lastDir = $currDir; |
||
413 | } |
||
414 | |||
415 | $this->processFile($file); |
||
416 | |||
417 | $pathsProcessed[] = $path; |
||
418 | $todo->next(); |
||
419 | } |
||
420 | |||
421 | $debugOutput = ob_get_contents(); |
||
422 | ob_end_clean(); |
||
423 | |||
424 | // Write information about the run to the filesystem |
||
425 | // so it can be picked up by the main process. |
||
426 | $childOutput = array( |
||
427 | 'totalFiles' => $this->reporter->totalFiles, |
||
428 | 'totalErrors' => $this->reporter->totalErrors, |
||
429 | 'totalWarnings' => $this->reporter->totalWarnings, |
||
430 | 'totalFixable' => $this->reporter->totalFixable, |
||
431 | ); |
||
432 | |||
433 | $output = '<'.'?php'."\n".' $childOutput = '; |
||
434 | $output .= var_export($childOutput, true); |
||
435 | $output .= ";\n\$debugOutput = "; |
||
436 | $output .= var_export($debugOutput, true); |
||
437 | |||
438 | if ($this->config->cache === true) { |
||
439 | $childCache = array(); |
||
440 | foreach ($pathsProcessed as $path) { |
||
441 | $childCache[$path] = Cache::get($path); |
||
442 | } |
||
443 | |||
444 | $output .= ";\n\$childCache = "; |
||
445 | $output .= var_export($childCache, true); |
||
446 | } |
||
447 | |||
448 | $output .= ";\n?".'>'; |
||
449 | file_put_contents($childOutFilename, $output); |
||
450 | exit($pid); |
||
451 | }//end if |
||
452 | }//end for |
||
453 | |||
454 | $this->processChildProcs($childProcs); |
||
455 | }//end if |
||
456 | |||
457 | restore_error_handler(); |
||
458 | |||
459 | if (PHP_CodeSniffer_VERBOSITY === 0 |
||
460 | && $this->config->interactive === false |
||
461 | && $this->config->showProgress === true |
||
462 | ) { |
||
463 | echo PHP_EOL.PHP_EOL; |
||
464 | } |
||
465 | |||
466 | if ($this->config->cache === true) { |
||
467 | Cache::save(); |
||
468 | } |
||
469 | |||
470 | $ignoreWarnings = Config::getConfigData('ignore_warnings_on_exit'); |
||
471 | $ignoreErrors = Config::getConfigData('ignore_errors_on_exit'); |
||
472 | |||
473 | $return = ($this->reporter->totalErrors + $this->reporter->totalWarnings); |
||
474 | if ($ignoreErrors !== null) { |
||
475 | $ignoreErrors = (bool) $ignoreErrors; |
||
476 | if ($ignoreErrors === true) { |
||
477 | $return -= $this->reporter->totalErrors; |
||
478 | } |
||
479 | } |
||
480 | |||
481 | if ($ignoreWarnings !== null) { |
||
482 | $ignoreWarnings = (bool) $ignoreWarnings; |
||
483 | if ($ignoreWarnings === true) { |
||
484 | $return -= $this->reporter->totalWarnings; |
||
485 | } |
||
486 | } |
||
487 | |||
488 | return $return; |
||
489 | |||
490 | }//end run() |
||
491 | |||
695 |
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.