Conditions | 142 |
Paths | > 20000 |
Total Lines | 658 |
Code Lines | 395 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
279 | protected function handleArguments(array $argv): void |
||
280 | { |
||
281 | try { |
||
282 | $this->options = Getopt::getopt( |
||
283 | $argv, |
||
284 | 'd:c:hv', |
||
285 | \array_keys($this->longOptions) |
||
286 | ); |
||
287 | } catch (Exception $t) { |
||
288 | $this->exitWithErrorMessage($t->getMessage()); |
||
289 | } |
||
290 | |||
291 | foreach ($this->options[0] as $option) { |
||
292 | switch ($option[0]) { |
||
293 | case '--colors': |
||
294 | $this->arguments['colors'] = $option[1] ?: ResultPrinter::COLOR_AUTO; |
||
295 | |||
296 | break; |
||
297 | |||
298 | case '--bootstrap': |
||
299 | $this->arguments['bootstrap'] = $option[1]; |
||
300 | |||
301 | break; |
||
302 | |||
303 | case '--cache-result': |
||
304 | $this->arguments['cacheResult'] = true; |
||
305 | |||
306 | break; |
||
307 | |||
308 | case '--cache-result-file': |
||
309 | $this->arguments['cacheResultFile'] = $option[1]; |
||
310 | |||
311 | break; |
||
312 | |||
313 | case '--columns': |
||
314 | if (\is_numeric($option[1])) { |
||
315 | $this->arguments['columns'] = (int) $option[1]; |
||
316 | } elseif ($option[1] === 'max') { |
||
317 | $this->arguments['columns'] = 'max'; |
||
318 | } |
||
319 | |||
320 | break; |
||
321 | |||
322 | case 'c': |
||
323 | case '--configuration': |
||
324 | $this->arguments['configuration'] = $option[1]; |
||
325 | |||
326 | break; |
||
327 | |||
328 | case '--coverage-clover': |
||
329 | $this->arguments['coverageClover'] = $option[1]; |
||
330 | |||
331 | break; |
||
332 | |||
333 | case '--coverage-crap4j': |
||
334 | $this->arguments['coverageCrap4J'] = $option[1]; |
||
335 | |||
336 | break; |
||
337 | |||
338 | case '--coverage-html': |
||
339 | $this->arguments['coverageHtml'] = $option[1]; |
||
340 | |||
341 | break; |
||
342 | |||
343 | case '--coverage-php': |
||
344 | $this->arguments['coveragePHP'] = $option[1]; |
||
345 | |||
346 | break; |
||
347 | |||
348 | case '--coverage-text': |
||
349 | if ($option[1] === null) { |
||
350 | $option[1] = 'php://stdout'; |
||
351 | } |
||
352 | |||
353 | $this->arguments['coverageText'] = $option[1]; |
||
354 | $this->arguments['coverageTextShowUncoveredFiles'] = false; |
||
355 | $this->arguments['coverageTextShowOnlySummary'] = false; |
||
356 | |||
357 | break; |
||
358 | |||
359 | case '--coverage-xml': |
||
360 | $this->arguments['coverageXml'] = $option[1]; |
||
361 | |||
362 | break; |
||
363 | |||
364 | case 'd': |
||
365 | $ini = \explode('=', $option[1]); |
||
366 | |||
367 | if (isset($ini[0])) { |
||
368 | if (isset($ini[1])) { |
||
369 | \ini_set($ini[0], $ini[1]); |
||
370 | } else { |
||
371 | \ini_set($ini[0], true); |
||
372 | } |
||
373 | } |
||
374 | |||
375 | break; |
||
376 | |||
377 | case '--debug': |
||
378 | $this->arguments['debug'] = true; |
||
379 | |||
380 | break; |
||
381 | |||
382 | case 'h': |
||
383 | case '--help': |
||
384 | $this->showHelp(); |
||
385 | exit(TestRunner::SUCCESS_EXIT); |
||
386 | |||
387 | break; |
||
388 | |||
389 | case '--filter': |
||
390 | $this->arguments['filter'] = $option[1]; |
||
391 | |||
392 | break; |
||
393 | |||
394 | case '--testsuite': |
||
395 | $this->arguments['testsuite'] = $option[1]; |
||
396 | |||
397 | break; |
||
398 | |||
399 | case '--generate-configuration': |
||
400 | $this->printVersionString(); |
||
401 | |||
402 | print 'Generating phpunit.xml in ' . \getcwd() . \PHP_EOL . \PHP_EOL; |
||
403 | |||
404 | print 'Bootstrap script (relative to path shown above; default: vendor/autoload.php): '; |
||
405 | $bootstrapScript = \trim(\fgets(\STDIN)); |
||
406 | |||
407 | print 'Tests directory (relative to path shown above; default: tests): '; |
||
408 | $testsDirectory = \trim(\fgets(\STDIN)); |
||
409 | |||
410 | print 'Source directory (relative to path shown above; default: src): '; |
||
411 | $src = \trim(\fgets(\STDIN)); |
||
412 | |||
413 | if ($bootstrapScript === '') { |
||
414 | $bootstrapScript = 'vendor/autoload.php'; |
||
415 | } |
||
416 | |||
417 | if ($testsDirectory === '') { |
||
418 | $testsDirectory = 'tests'; |
||
419 | } |
||
420 | |||
421 | if ($src === '') { |
||
422 | $src = 'src'; |
||
423 | } |
||
424 | |||
425 | $generator = new ConfigurationGenerator; |
||
426 | |||
427 | \file_put_contents( |
||
428 | 'phpunit.xml', |
||
429 | $generator->generateDefaultConfiguration( |
||
430 | Version::series(), |
||
431 | $bootstrapScript, |
||
432 | $testsDirectory, |
||
433 | $src |
||
434 | ) |
||
435 | ); |
||
436 | |||
437 | print \PHP_EOL . 'Generated phpunit.xml in ' . \getcwd() . \PHP_EOL; |
||
438 | |||
439 | exit(TestRunner::SUCCESS_EXIT); |
||
440 | |||
441 | break; |
||
442 | |||
443 | case '--group': |
||
444 | $this->arguments['groups'] = \explode(',', $option[1]); |
||
445 | |||
446 | break; |
||
447 | |||
448 | case '--exclude-group': |
||
449 | $this->arguments['excludeGroups'] = \explode( |
||
450 | ',', |
||
451 | $option[1] |
||
452 | ); |
||
453 | |||
454 | break; |
||
455 | |||
456 | case '--test-suffix': |
||
457 | $this->arguments['testSuffixes'] = \explode( |
||
458 | ',', |
||
459 | $option[1] |
||
460 | ); |
||
461 | |||
462 | break; |
||
463 | |||
464 | case '--include-path': |
||
465 | $includePath = $option[1]; |
||
466 | |||
467 | break; |
||
468 | |||
469 | case '--list-groups': |
||
470 | $this->arguments['listGroups'] = true; |
||
471 | |||
472 | break; |
||
473 | |||
474 | case '--list-suites': |
||
475 | $this->arguments['listSuites'] = true; |
||
476 | |||
477 | break; |
||
478 | |||
479 | case '--list-tests': |
||
480 | $this->arguments['listTests'] = true; |
||
481 | |||
482 | break; |
||
483 | |||
484 | case '--list-tests-xml': |
||
485 | $this->arguments['listTestsXml'] = $option[1]; |
||
486 | |||
487 | break; |
||
488 | |||
489 | case '--printer': |
||
490 | $this->arguments['printer'] = $option[1]; |
||
491 | |||
492 | break; |
||
493 | |||
494 | case '--loader': |
||
495 | $this->arguments['loader'] = $option[1]; |
||
496 | |||
497 | break; |
||
498 | |||
499 | case '--log-junit': |
||
500 | $this->arguments['junitLogfile'] = $option[1]; |
||
501 | |||
502 | break; |
||
503 | |||
504 | case '--log-teamcity': |
||
505 | $this->arguments['teamcityLogfile'] = $option[1]; |
||
506 | |||
507 | break; |
||
508 | |||
509 | case '--order-by': |
||
510 | $this->handleOrderByOption($option[1]); |
||
511 | |||
512 | break; |
||
513 | |||
514 | case '--process-isolation': |
||
515 | $this->arguments['processIsolation'] = true; |
||
516 | |||
517 | break; |
||
518 | |||
519 | case '--repeat': |
||
520 | $this->arguments['repeat'] = (int) $option[1]; |
||
521 | |||
522 | break; |
||
523 | |||
524 | case '--stderr': |
||
525 | $this->arguments['stderr'] = true; |
||
526 | |||
527 | break; |
||
528 | |||
529 | case '--stop-on-defect': |
||
530 | $this->arguments['stopOnDefect'] = true; |
||
531 | |||
532 | break; |
||
533 | |||
534 | case '--stop-on-error': |
||
535 | $this->arguments['stopOnError'] = true; |
||
536 | |||
537 | break; |
||
538 | |||
539 | case '--stop-on-failure': |
||
540 | $this->arguments['stopOnFailure'] = true; |
||
541 | |||
542 | break; |
||
543 | |||
544 | case '--stop-on-warning': |
||
545 | $this->arguments['stopOnWarning'] = true; |
||
546 | |||
547 | break; |
||
548 | |||
549 | case '--stop-on-incomplete': |
||
550 | $this->arguments['stopOnIncomplete'] = true; |
||
551 | |||
552 | break; |
||
553 | |||
554 | case '--stop-on-risky': |
||
555 | $this->arguments['stopOnRisky'] = true; |
||
556 | |||
557 | break; |
||
558 | |||
559 | case '--stop-on-skipped': |
||
560 | $this->arguments['stopOnSkipped'] = true; |
||
561 | |||
562 | break; |
||
563 | |||
564 | case '--fail-on-warning': |
||
565 | $this->arguments['failOnWarning'] = true; |
||
566 | |||
567 | break; |
||
568 | |||
569 | case '--fail-on-risky': |
||
570 | $this->arguments['failOnRisky'] = true; |
||
571 | |||
572 | break; |
||
573 | |||
574 | case '--teamcity': |
||
575 | $this->arguments['printer'] = TeamCity::class; |
||
576 | |||
577 | break; |
||
578 | |||
579 | case '--testdox': |
||
580 | $this->arguments['printer'] = CliTestDoxPrinter::class; |
||
581 | |||
582 | break; |
||
583 | |||
584 | case '--testdox-group': |
||
585 | $this->arguments['testdoxGroups'] = \explode( |
||
586 | ',', |
||
587 | $option[1] |
||
588 | ); |
||
589 | |||
590 | break; |
||
591 | |||
592 | case '--testdox-exclude-group': |
||
593 | $this->arguments['testdoxExcludeGroups'] = \explode( |
||
594 | ',', |
||
595 | $option[1] |
||
596 | ); |
||
597 | |||
598 | break; |
||
599 | |||
600 | case '--testdox-html': |
||
601 | $this->arguments['testdoxHTMLFile'] = $option[1]; |
||
602 | |||
603 | break; |
||
604 | |||
605 | case '--testdox-text': |
||
606 | $this->arguments['testdoxTextFile'] = $option[1]; |
||
607 | |||
608 | break; |
||
609 | |||
610 | case '--testdox-xml': |
||
611 | $this->arguments['testdoxXMLFile'] = $option[1]; |
||
612 | |||
613 | break; |
||
614 | |||
615 | case '--no-configuration': |
||
616 | $this->arguments['useDefaultConfiguration'] = false; |
||
617 | |||
618 | break; |
||
619 | |||
620 | case '--no-extensions': |
||
621 | $this->arguments['noExtensions'] = true; |
||
622 | |||
623 | break; |
||
624 | |||
625 | case '--no-coverage': |
||
626 | $this->arguments['noCoverage'] = true; |
||
627 | |||
628 | break; |
||
629 | |||
630 | case '--no-logging': |
||
631 | $this->arguments['noLogging'] = true; |
||
632 | |||
633 | break; |
||
634 | |||
635 | case '--globals-backup': |
||
636 | $this->arguments['backupGlobals'] = true; |
||
637 | |||
638 | break; |
||
639 | |||
640 | case '--static-backup': |
||
641 | $this->arguments['backupStaticAttributes'] = true; |
||
642 | |||
643 | break; |
||
644 | |||
645 | case 'v': |
||
646 | case '--verbose': |
||
647 | $this->arguments['verbose'] = true; |
||
648 | |||
649 | break; |
||
650 | |||
651 | case '--atleast-version': |
||
652 | if (\version_compare(Version::id(), $option[1], '>=')) { |
||
653 | exit(TestRunner::SUCCESS_EXIT); |
||
654 | } |
||
655 | |||
656 | exit(TestRunner::FAILURE_EXIT); |
||
657 | |||
658 | break; |
||
659 | |||
660 | case '--version': |
||
661 | $this->printVersionString(); |
||
662 | exit(TestRunner::SUCCESS_EXIT); |
||
663 | |||
664 | break; |
||
665 | |||
666 | case '--dont-report-useless-tests': |
||
667 | $this->arguments['reportUselessTests'] = false; |
||
668 | |||
669 | break; |
||
670 | |||
671 | case '--strict-coverage': |
||
672 | $this->arguments['strictCoverage'] = true; |
||
673 | |||
674 | break; |
||
675 | |||
676 | case '--disable-coverage-ignore': |
||
677 | $this->arguments['disableCodeCoverageIgnore'] = true; |
||
678 | |||
679 | break; |
||
680 | |||
681 | case '--strict-global-state': |
||
682 | $this->arguments['beStrictAboutChangesToGlobalState'] = true; |
||
683 | |||
684 | break; |
||
685 | |||
686 | case '--disallow-test-output': |
||
687 | $this->arguments['disallowTestOutput'] = true; |
||
688 | |||
689 | break; |
||
690 | |||
691 | case '--disallow-resource-usage': |
||
692 | $this->arguments['beStrictAboutResourceUsageDuringSmallTests'] = true; |
||
693 | |||
694 | break; |
||
695 | |||
696 | case '--default-time-limit': |
||
697 | $this->arguments['defaultTimeLimit'] = (int) $option[1]; |
||
698 | |||
699 | break; |
||
700 | |||
701 | case '--enforce-time-limit': |
||
702 | $this->arguments['enforceTimeLimit'] = true; |
||
703 | |||
704 | break; |
||
705 | |||
706 | case '--disallow-todo-tests': |
||
707 | $this->arguments['disallowTodoAnnotatedTests'] = true; |
||
708 | |||
709 | break; |
||
710 | |||
711 | case '--reverse-list': |
||
712 | $this->arguments['reverseList'] = true; |
||
713 | |||
714 | break; |
||
715 | |||
716 | case '--check-version': |
||
717 | $this->handleVersionCheck(); |
||
718 | |||
719 | break; |
||
720 | |||
721 | case '--whitelist': |
||
722 | $this->arguments['whitelist'] = $option[1]; |
||
723 | |||
724 | break; |
||
725 | |||
726 | case '--random-order': |
||
727 | $this->handleOrderByOption('random'); |
||
728 | |||
729 | break; |
||
730 | |||
731 | case '--random-order-seed': |
||
732 | $this->arguments['randomOrderSeed'] = (int) $option[1]; |
||
733 | |||
734 | break; |
||
735 | |||
736 | case '--resolve-dependencies': |
||
737 | $this->handleOrderByOption('depends'); |
||
738 | |||
739 | break; |
||
740 | |||
741 | case '--ignore-dependencies': |
||
742 | $this->arguments['resolveDependencies'] = false; |
||
743 | |||
744 | break; |
||
745 | |||
746 | case '--reverse-order': |
||
747 | $this->handleOrderByOption('reverse'); |
||
748 | |||
749 | break; |
||
750 | |||
751 | case '--dump-xdebug-filter': |
||
752 | $this->arguments['xdebugFilterFile'] = $option[1]; |
||
753 | |||
754 | break; |
||
755 | |||
756 | default: |
||
757 | $optionName = \str_replace('--', '', $option[0]); |
||
758 | |||
759 | $handler = null; |
||
760 | |||
761 | if (isset($this->longOptions[$optionName])) { |
||
762 | $handler = $this->longOptions[$optionName]; |
||
763 | } elseif (isset($this->longOptions[$optionName . '='])) { |
||
764 | $handler = $this->longOptions[$optionName . '=']; |
||
765 | } |
||
766 | |||
767 | if (isset($handler) && \is_callable([$this, $handler])) { |
||
768 | $this->$handler($option[1]); |
||
769 | } |
||
770 | } |
||
771 | } |
||
772 | |||
773 | $this->handleCustomTestSuite(); |
||
774 | |||
775 | if (!isset($this->arguments['test'])) { |
||
776 | if (isset($this->options[1][0])) { |
||
777 | $this->arguments['test'] = $this->options[1][0]; |
||
778 | } |
||
779 | |||
780 | if (isset($this->options[1][1])) { |
||
781 | $testFile = \realpath($this->options[1][1]); |
||
782 | |||
783 | if ($testFile === false) { |
||
784 | $this->exitWithErrorMessage( |
||
785 | \sprintf( |
||
786 | 'Cannot open file "%s".', |
||
787 | $this->options[1][1] |
||
788 | ) |
||
789 | ); |
||
790 | } |
||
791 | $this->arguments['testFile'] = $testFile; |
||
792 | } else { |
||
793 | $this->arguments['testFile'] = ''; |
||
794 | } |
||
795 | |||
796 | if (isset($this->arguments['test']) && |
||
797 | \is_file($this->arguments['test']) && |
||
798 | \substr($this->arguments['test'], -5, 5) != '.phpt') { |
||
799 | $this->arguments['testFile'] = \realpath($this->arguments['test']); |
||
800 | $this->arguments['test'] = \substr($this->arguments['test'], 0, \strrpos($this->arguments['test'], '.')); |
||
801 | } |
||
802 | } |
||
803 | |||
804 | if (!isset($this->arguments['testSuffixes'])) { |
||
805 | $this->arguments['testSuffixes'] = ['Test.php', '.phpt']; |
||
806 | } |
||
807 | |||
808 | if (isset($includePath)) { |
||
809 | \ini_set( |
||
810 | 'include_path', |
||
811 | $includePath . \PATH_SEPARATOR . \ini_get('include_path') |
||
812 | ); |
||
813 | } |
||
814 | |||
815 | if ($this->arguments['loader'] !== null) { |
||
816 | $this->arguments['loader'] = $this->handleLoader($this->arguments['loader']); |
||
817 | } |
||
818 | |||
819 | if (isset($this->arguments['configuration']) && |
||
820 | \is_dir($this->arguments['configuration'])) { |
||
821 | $configurationFile = $this->arguments['configuration'] . '/phpunit.xml'; |
||
822 | |||
823 | if (\file_exists($configurationFile)) { |
||
824 | $this->arguments['configuration'] = \realpath( |
||
825 | $configurationFile |
||
826 | ); |
||
827 | } elseif (\file_exists($configurationFile . '.dist')) { |
||
828 | $this->arguments['configuration'] = \realpath( |
||
829 | $configurationFile . '.dist' |
||
830 | ); |
||
831 | } |
||
832 | } elseif (!isset($this->arguments['configuration']) && |
||
833 | $this->arguments['useDefaultConfiguration']) { |
||
834 | if (\file_exists('phpunit.xml')) { |
||
835 | $this->arguments['configuration'] = \realpath('phpunit.xml'); |
||
836 | } elseif (\file_exists('phpunit.xml.dist')) { |
||
837 | $this->arguments['configuration'] = \realpath( |
||
838 | 'phpunit.xml.dist' |
||
839 | ); |
||
840 | } |
||
841 | } |
||
842 | |||
843 | if (isset($this->arguments['configuration'])) { |
||
844 | try { |
||
845 | $configuration = Configuration::getInstance( |
||
846 | $this->arguments['configuration'] |
||
847 | ); |
||
848 | } catch (Throwable $t) { |
||
849 | print $t->getMessage() . \PHP_EOL; |
||
850 | exit(TestRunner::FAILURE_EXIT); |
||
851 | } |
||
852 | |||
853 | $phpunitConfiguration = $configuration->getPHPUnitConfiguration(); |
||
854 | |||
855 | $configuration->handlePHPConfiguration(); |
||
856 | |||
857 | /* |
||
858 | * Issue #1216 |
||
859 | */ |
||
860 | if (isset($this->arguments['bootstrap'])) { |
||
861 | $this->handleBootstrap($this->arguments['bootstrap']); |
||
862 | } elseif (isset($phpunitConfiguration['bootstrap'])) { |
||
863 | $this->handleBootstrap($phpunitConfiguration['bootstrap']); |
||
864 | } |
||
865 | |||
866 | /* |
||
867 | * Issue #657 |
||
868 | */ |
||
869 | if (isset($phpunitConfiguration['stderr']) && !isset($this->arguments['stderr'])) { |
||
870 | $this->arguments['stderr'] = $phpunitConfiguration['stderr']; |
||
871 | } |
||
872 | |||
873 | if (isset($phpunitConfiguration['extensionsDirectory']) && !isset($this->arguments['noExtensions']) && \extension_loaded('phar')) { |
||
874 | $this->handleExtensions($phpunitConfiguration['extensionsDirectory']); |
||
875 | } |
||
876 | |||
877 | if (isset($phpunitConfiguration['columns']) && !isset($this->arguments['columns'])) { |
||
878 | $this->arguments['columns'] = $phpunitConfiguration['columns']; |
||
879 | } |
||
880 | |||
881 | if (!isset($this->arguments['printer']) && isset($phpunitConfiguration['printerClass'])) { |
||
882 | if (isset($phpunitConfiguration['printerFile'])) { |
||
883 | $file = $phpunitConfiguration['printerFile']; |
||
884 | } else { |
||
885 | $file = ''; |
||
886 | } |
||
887 | |||
888 | $this->arguments['printer'] = $this->handlePrinter( |
||
889 | $phpunitConfiguration['printerClass'], |
||
890 | $file |
||
891 | ); |
||
892 | } |
||
893 | |||
894 | if (isset($phpunitConfiguration['testSuiteLoaderClass'])) { |
||
895 | if (isset($phpunitConfiguration['testSuiteLoaderFile'])) { |
||
896 | $file = $phpunitConfiguration['testSuiteLoaderFile']; |
||
897 | } else { |
||
898 | $file = ''; |
||
899 | } |
||
900 | |||
901 | $this->arguments['loader'] = $this->handleLoader( |
||
902 | $phpunitConfiguration['testSuiteLoaderClass'], |
||
903 | $file |
||
904 | ); |
||
905 | } |
||
906 | |||
907 | if (!isset($this->arguments['testsuite']) && isset($phpunitConfiguration['defaultTestSuite'])) { |
||
908 | $this->arguments['testsuite'] = $phpunitConfiguration['defaultTestSuite']; |
||
909 | } |
||
910 | |||
911 | if (!isset($this->arguments['test'])) { |
||
912 | $testSuite = $configuration->getTestSuiteConfiguration($this->arguments['testsuite'] ?? ''); |
||
913 | |||
914 | if ($testSuite !== null) { |
||
915 | $this->arguments['test'] = $testSuite; |
||
916 | } |
||
917 | } |
||
918 | } elseif (isset($this->arguments['bootstrap'])) { |
||
919 | $this->handleBootstrap($this->arguments['bootstrap']); |
||
920 | } |
||
921 | |||
922 | if (isset($this->arguments['printer']) && |
||
923 | \is_string($this->arguments['printer'])) { |
||
924 | $this->arguments['printer'] = $this->handlePrinter($this->arguments['printer']); |
||
925 | } |
||
926 | |||
927 | if (isset($this->arguments['test']) && \is_string($this->arguments['test']) && \substr($this->arguments['test'], -5, 5) == '.phpt') { |
||
928 | $test = new PhptTestCase($this->arguments['test']); |
||
929 | |||
930 | $this->arguments['test'] = new TestSuite; |
||
931 | $this->arguments['test']->addTest($test); |
||
932 | } |
||
933 | |||
934 | if (!isset($this->arguments['test'])) { |
||
935 | $this->showHelp(); |
||
936 | exit(TestRunner::EXCEPTION_EXIT); |
||
937 | } |
||
1385 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.