Passed
Push — develop ( 30cf64...589229 )
by Guillaume
06:18 queued 04:10
created

ArgumentsBuilder   F

Complexity

Total Complexity 112

Size/Duplication

Total Lines 792
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 572
dl 0
loc 792
rs 2
c 0
b 0
f 0
wmc 112

How to fix   Complexity   

Complex Class

Complex classes like ArgumentsBuilder often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ArgumentsBuilder, and based on these observations, apply Extract Interface, too.

1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of PHPUnit.
4
 *
5
 * (c) Sebastian Bergmann <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace PHPUnit\TextUI\Arguments;
11
12
use PHPUnit\Runner\TestSuiteSorter;
13
use PHPUnit\TextUI\Configuration\Extension;
14
use PHPUnit\TextUI\DefaultResultPrinter;
15
use PHPUnit\Util\Exception as UtilException;
16
use PHPUnit\Util\Getopt;
17
use PHPUnit\Util\Log\TeamCity;
18
use PHPUnit\Util\TestDox\CliTestDoxPrinter;
19
20
/**
21
 * @internal This class is not covered by the backward compatibility promise for PHPUnit
22
 */
23
final class ArgumentsBuilder
24
{
25
    private const LONG_OPTIONS = [
26
        'atleast-version=',
27
        'prepend=',
28
        'bootstrap=',
29
        'cache-result',
30
        'do-not-cache-result',
31
        'cache-result-file=',
32
        'check-version',
33
        'colors==',
34
        'columns=',
35
        'configuration=',
36
        'coverage-clover=',
37
        'coverage-crap4j=',
38
        'coverage-html=',
39
        'coverage-php=',
40
        'coverage-text==',
41
        'coverage-xml=',
42
        'debug',
43
        'disallow-test-output',
44
        'disallow-resource-usage',
45
        'disallow-todo-tests',
46
        'default-time-limit=',
47
        'enforce-time-limit',
48
        'exclude-group=',
49
        'extensions=',
50
        'filter=',
51
        'generate-configuration',
52
        'globals-backup',
53
        'group=',
54
        'help',
55
        'resolve-dependencies',
56
        'ignore-dependencies',
57
        'include-path=',
58
        'list-groups',
59
        'list-suites',
60
        'list-tests',
61
        'list-tests-xml=',
62
        'loader=',
63
        'log-junit=',
64
        'log-teamcity=',
65
        'no-configuration',
66
        'no-coverage',
67
        'no-logging',
68
        'no-interaction',
69
        'no-extensions',
70
        'order-by=',
71
        'printer=',
72
        'process-isolation',
73
        'repeat=',
74
        'dont-report-useless-tests',
75
        'random-order',
76
        'random-order-seed=',
77
        'reverse-order',
78
        'reverse-list',
79
        'static-backup',
80
        'stderr',
81
        'stop-on-defect',
82
        'stop-on-error',
83
        'stop-on-failure',
84
        'stop-on-warning',
85
        'stop-on-incomplete',
86
        'stop-on-risky',
87
        'stop-on-skipped',
88
        'fail-on-warning',
89
        'fail-on-risky',
90
        'strict-coverage',
91
        'disable-coverage-ignore',
92
        'strict-global-state',
93
        'teamcity',
94
        'testdox',
95
        'testdox-group=',
96
        'testdox-exclude-group=',
97
        'testdox-html=',
98
        'testdox-text=',
99
        'testdox-xml=',
100
        'test-suffix=',
101
        'testsuite=',
102
        'verbose',
103
        'version',
104
        'whitelist=',
105
        'dump-xdebug-filter=',
106
    ];
107
108
    private const SHORT_OPTIONS = 'd:c:hv';
109
110
    public function fromParameters(array $parameters, array $additionalLongOptions): Arguments
111
    {
112
        try {
113
            $options = Getopt::parse(
114
                $parameters,
115
                self::SHORT_OPTIONS,
116
                \array_merge(self::LONG_OPTIONS, $additionalLongOptions)
117
            );
118
        } catch (UtilException $e) {
119
            throw new Exception(
120
                $e->getMessage(),
121
                (int) $e->getCode(),
122
                $e
123
            );
124
        }
125
126
        $argument                                   = null;
127
        $atLeastVersion                             = null;
128
        $backupGlobals                              = null;
129
        $backupStaticAttributes                     = null;
130
        $beStrictAboutChangesToGlobalState          = null;
131
        $beStrictAboutResourceUsageDuringSmallTests = null;
132
        $bootstrap                                  = null;
133
        $cacheResult                                = null;
134
        $cacheResultFile                            = null;
135
        $checkVersion                               = null;
136
        $colors                                     = null;
137
        $columns                                    = null;
138
        $configuration                              = null;
139
        $coverageClover                             = null;
140
        $coverageCrap4J                             = null;
141
        $coverageHtml                               = null;
142
        $coveragePhp                                = null;
143
        $coverageText                               = null;
144
        $coverageTextShowUncoveredFiles             = null;
145
        $coverageTextShowOnlySummary                = null;
146
        $coverageXml                                = null;
147
        $debug                                      = null;
148
        $defaultTimeLimit                           = null;
149
        $disableCodeCoverageIgnore                  = null;
150
        $disallowTestOutput                         = null;
151
        $disallowTodoAnnotatedTests                 = null;
152
        $enforceTimeLimit                           = null;
153
        $excludeGroups                              = null;
154
        $executionOrder                             = null;
155
        $executionOrderDefects                      = null;
156
        $extensions                                 = [];
157
        $unavailableExtensions                      = [];
158
        $failOnIncomplete                           = null;
159
        $failOnRisky                                = null;
160
        $failOnSkipped                              = null;
161
        $failOnWarning                              = null;
162
        $filter                                     = null;
163
        $generateConfiguration                      = null;
164
        $groups                                     = null;
165
        $help                                       = null;
166
        $includePath                                = null;
167
        $iniSettings                                = [];
168
        $junitLogfile                               = null;
169
        $listGroups                                 = null;
170
        $listSuites                                 = null;
171
        $listTests                                  = null;
172
        $listTestsXml                               = null;
173
        $loader                                     = null;
174
        $noCoverage                                 = null;
175
        $noExtensions                               = null;
176
        $noInteraction                              = null;
177
        $noLogging                                  = null;
178
        $printer                                    = null;
179
        $processIsolation                           = null;
180
        $randomOrderSeed                            = null;
181
        $repeat                                     = null;
182
        $reportUselessTests                         = null;
183
        $resolveDependencies                        = null;
184
        $reverseList                                = null;
185
        $stderr                                     = null;
186
        $strictCoverage                             = null;
187
        $stopOnDefect                               = null;
188
        $stopOnError                                = null;
189
        $stopOnFailure                              = null;
190
        $stopOnIncomplete                           = null;
191
        $stopOnRisky                                = null;
192
        $stopOnSkipped                              = null;
193
        $stopOnWarning                              = null;
194
        $teamcityLogfile                            = null;
195
        $testdoxExcludeGroups                       = null;
196
        $testdoxGroups                              = null;
197
        $testdoxHtmlFile                            = null;
198
        $testdoxTextFile                            = null;
199
        $testdoxXmlFile                             = null;
200
        $testSuffixes                               = null;
201
        $testSuite                                  = null;
202
        $unrecognizedOptions                        = [];
203
        $unrecognizedOrderBy                        = null;
204
        $useDefaultConfiguration                    = null;
205
        $verbose                                    = null;
206
        $version                                    = null;
207
        $whitelist                                  = null;
208
        $xdebugFilterFile                           = null;
209
210
        if (isset($options[1][0])) {
211
            $argument = $options[1][0];
212
        }
213
214
        foreach ($options[0] as $option) {
215
            switch ($option[0]) {
216
                case '--colors':
217
                    $colors = $option[1] ?: DefaultResultPrinter::COLOR_AUTO;
218
219
                    break;
220
221
                case '--bootstrap':
222
                    $bootstrap = $option[1];
223
224
                    break;
225
226
                case '--cache-result':
227
                    $cacheResult = true;
228
229
                    break;
230
231
                case '--do-not-cache-result':
232
                    $cacheResult = false;
233
234
                    break;
235
236
                case '--cache-result-file':
237
                    $cacheResultFile = $option[1];
238
239
                    break;
240
241
                case '--columns':
242
                    if (\is_numeric($option[1])) {
243
                        $columns = (int) $option[1];
244
                    } elseif ($option[1] === 'max') {
245
                        $columns = 'max';
246
                    }
247
248
                    break;
249
250
                case 'c':
251
                case '--configuration':
252
                    $configuration = $option[1];
253
254
                    break;
255
256
                case '--coverage-clover':
257
                    $coverageClover = $option[1];
258
259
                    break;
260
261
                case '--coverage-crap4j':
262
                    $coverageCrap4J = $option[1];
263
264
                    break;
265
266
                case '--coverage-html':
267
                    $coverageHtml = $option[1];
268
269
                    break;
270
271
                case '--coverage-php':
272
                    $coveragePhp = $option[1];
273
274
                    break;
275
276
                case '--coverage-text':
277
                    if ($option[1] === null) {
278
                        $option[1] = 'php://stdout';
279
                    }
280
281
                    $coverageText                   = $option[1];
282
                    $coverageTextShowUncoveredFiles = false;
283
                    $coverageTextShowOnlySummary    = false;
284
285
                    break;
286
287
                case '--coverage-xml':
288
                    $coverageXml = $option[1];
289
290
                    break;
291
292
                case 'd':
293
                    $tmp = \explode('=', $option[1]);
294
295
                    if (isset($tmp[0])) {
296
                        if (isset($tmp[1])) {
297
                            $iniSettings[$tmp[0]] = $tmp[1];
298
                        } else {
299
                            $iniSettings[$tmp[0]] = '1';
300
                        }
301
                    }
302
303
                    break;
304
305
                case '--debug':
306
                    $debug = true;
307
308
                    break;
309
310
                case 'h':
311
                case '--help':
312
                    $help = true;
313
314
                    break;
315
316
                case '--filter':
317
                    $filter = $option[1];
318
319
                    break;
320
321
                case '--testsuite':
322
                    $testSuite = $option[1];
323
324
                    break;
325
326
                case '--generate-configuration':
327
                    $generateConfiguration = true;
328
329
                    break;
330
331
                case '--group':
332
                    $groups = \explode(',', $option[1]);
333
334
                    break;
335
336
                case '--exclude-group':
337
                    $excludeGroups = \explode(',', $option[1]);
338
339
                    break;
340
341
                case '--test-suffix':
342
                    $testSuffixes = \explode(',', $option[1]);
343
344
                    break;
345
346
                case '--include-path':
347
                    $includePath = $option[1];
348
349
                    break;
350
351
                case '--list-groups':
352
                    $listGroups = true;
353
354
                    break;
355
356
                case '--list-suites':
357
                    $listSuites = true;
358
359
                    break;
360
361
                case '--list-tests':
362
                    $listTests = true;
363
364
                    break;
365
366
                case '--list-tests-xml':
367
                    $listTestsXml = $option[1];
368
369
                    break;
370
371
                case '--printer':
372
                    $printer = $option[1];
373
374
                    break;
375
376
                case '--loader':
377
                    $loader = $option[1];
378
379
                    break;
380
381
                case '--log-junit':
382
                    $junitLogfile = $option[1];
383
384
                    break;
385
386
                case '--log-teamcity':
387
                    $teamcityLogfile = $option[1];
388
389
                    break;
390
391
                case '--order-by':
392
                    foreach (\explode(',', $option[1]) as $order) {
393
                        switch ($order) {
394
                            case 'default':
395
                                $executionOrder        = TestSuiteSorter::ORDER_DEFAULT;
396
                                $executionOrderDefects = TestSuiteSorter::ORDER_DEFAULT;
397
                                $resolveDependencies   = true;
398
399
                                break;
400
401
                            case 'defects':
402
                                $executionOrderDefects = TestSuiteSorter::ORDER_DEFECTS_FIRST;
403
404
                                break;
405
406
                            case 'depends':
407
                                $resolveDependencies = true;
408
409
                                break;
410
411
                            case 'duration':
412
                                $executionOrder = TestSuiteSorter::ORDER_DURATION;
413
414
                                break;
415
416
                            case 'no-depends':
417
                                $resolveDependencies = false;
418
419
                                break;
420
421
                            case 'random':
422
                                $executionOrder = TestSuiteSorter::ORDER_RANDOMIZED;
423
424
                                break;
425
426
                            case 'reverse':
427
                                $executionOrder = TestSuiteSorter::ORDER_REVERSED;
428
429
                                break;
430
431
                            case 'size':
432
                                $executionOrder = TestSuiteSorter::ORDER_SIZE;
433
434
                                break;
435
436
                            default:
437
                                $unrecognizedOrderBy = $order;
438
                        }
439
                    }
440
441
                    break;
442
443
                case '--process-isolation':
444
                    $processIsolation = true;
445
446
                    break;
447
448
                case '--repeat':
449
                    $repeat = (int) $option[1];
450
451
                    break;
452
453
                case '--stderr':
454
                    $stderr = true;
455
456
                    break;
457
458
                case '--stop-on-defect':
459
                    $stopOnDefect = true;
460
461
                    break;
462
463
                case '--stop-on-error':
464
                    $stopOnError = true;
465
466
                    break;
467
468
                case '--stop-on-failure':
469
                    $stopOnFailure = true;
470
471
                    break;
472
473
                case '--stop-on-warning':
474
                    $stopOnWarning = true;
475
476
                    break;
477
478
                case '--stop-on-incomplete':
479
                    $stopOnIncomplete = true;
480
481
                    break;
482
483
                case '--stop-on-risky':
484
                    $stopOnRisky = true;
485
486
                    break;
487
488
                case '--stop-on-skipped':
489
                    $stopOnSkipped = true;
490
491
                    break;
492
493
                case '--fail-on-incomplete':
494
                    $failOnIncomplete = true;
495
496
                    break;
497
498
                case '--fail-on-risky':
499
                    $failOnRisky = true;
500
501
                    break;
502
503
                case '--fail-on-Skipped':
504
                    $failOnSkipped = true;
505
506
                    break;
507
508
                case '--fail-on-warning':
509
                    $failOnWarning = true;
510
511
                    break;
512
513
                case '--teamcity':
514
                    $printer = TeamCity::class;
515
516
                    break;
517
518
                case '--testdox':
519
                    $printer = CliTestDoxPrinter::class;
520
521
                    break;
522
523
                case '--testdox-group':
524
                    $testdoxGroups = \explode(',', $option[1]);
525
526
                    break;
527
528
                case '--testdox-exclude-group':
529
                    $testdoxExcludeGroups = \explode(',', $option[1]);
530
531
                    break;
532
533
                case '--testdox-html':
534
                    $testdoxHtmlFile = $option[1];
535
536
                    break;
537
538
                case '--testdox-text':
539
                    $testdoxTextFile = $option[1];
540
541
                    break;
542
543
                case '--testdox-xml':
544
                    $testdoxXmlFile = $option[1];
545
546
                    break;
547
548
                case '--no-configuration':
549
                    $useDefaultConfiguration = false;
550
551
                    break;
552
553
                case '--extensions':
554
                    foreach (\explode(',', $option[1]) as $extensionClass) {
555
                        if (!\class_exists($extensionClass)) {
556
                            $unavailableExtensions[] = $extensionClass;
557
558
                            continue;
559
                        }
560
561
                        $extensions[] = new Extension($extensionClass, '', []);
562
                    }
563
564
                    break;
565
566
                case '--no-extensions':
567
                    $noExtensions = true;
568
569
                    break;
570
571
                case '--no-coverage':
572
                    $noCoverage = true;
573
574
                    break;
575
576
                case '--no-logging':
577
                    $noLogging = true;
578
579
                    break;
580
581
                case '--no-interaction':
582
                    $noInteraction = true;
583
584
                    break;
585
586
                case '--globals-backup':
587
                    $backupGlobals = true;
588
589
                    break;
590
591
                case '--static-backup':
592
                    $backupStaticAttributes = true;
593
594
                    break;
595
596
                case 'v':
597
                case '--verbose':
598
                    $verbose = true;
599
600
                    break;
601
602
                case '--atleast-version':
603
                    $atLeastVersion = $option[1];
604
605
                    break;
606
607
                case '--version':
608
                    $version = true;
609
610
                    break;
611
612
                case '--dont-report-useless-tests':
613
                    $reportUselessTests = false;
614
615
                    break;
616
617
                case '--strict-coverage':
618
                    $strictCoverage = true;
619
620
                    break;
621
622
                case '--disable-coverage-ignore':
623
                    $disableCodeCoverageIgnore = true;
624
625
                    break;
626
627
                case '--strict-global-state':
628
                    $beStrictAboutChangesToGlobalState = true;
629
630
                    break;
631
632
                case '--disallow-test-output':
633
                    $disallowTestOutput = true;
634
635
                    break;
636
637
                case '--disallow-resource-usage':
638
                    $beStrictAboutResourceUsageDuringSmallTests = true;
639
640
                    break;
641
642
                case '--default-time-limit':
643
                    $defaultTimeLimit = (int) $option[1];
644
645
                    break;
646
647
                case '--enforce-time-limit':
648
                    $enforceTimeLimit = true;
649
650
                    break;
651
652
                case '--disallow-todo-tests':
653
                    $disallowTodoAnnotatedTests = true;
654
655
                    break;
656
657
                case '--reverse-list':
658
                    $reverseList = true;
659
660
                    break;
661
662
                case '--check-version':
663
                    $checkVersion = true;
664
665
                    break;
666
667
                case '--whitelist':
668
                    if ($whitelist === null) {
669
                        $whitelist = [];
670
                    }
671
672
                    $whitelist[] = $option[1];
673
674
                    break;
675
676
                case '--random-order':
677
                    $executionOrder = TestSuiteSorter::ORDER_RANDOMIZED;
678
679
                    break;
680
681
                case '--random-order-seed':
682
                    $randomOrderSeed = (int) $option[1];
683
684
                    break;
685
686
                case '--resolve-dependencies':
687
                    $resolveDependencies = true;
688
689
                    break;
690
691
                case '--ignore-dependencies':
692
                    $resolveDependencies = false;
693
694
                    break;
695
696
                case '--reverse-order':
697
                    $executionOrder = TestSuiteSorter::ORDER_REVERSED;
698
699
                    break;
700
701
                case '--dump-xdebug-filter':
702
                    $xdebugFilterFile = $option[1];
703
704
                    break;
705
706
                default:
707
                    $unrecognizedOptions[\str_replace('--', '', $option[0])] = $option[1];
708
            }
709
        }
710
711
        if (empty($extensions)) {
712
            $extensions = null;
713
        }
714
715
        if (empty($unavailableExtensions)) {
716
            $unavailableExtensions = null;
717
        }
718
719
        if (empty($iniSettings)) {
720
            $iniSettings = null;
721
        }
722
723
        if (empty($unrecognizedOptions)) {
724
            $unrecognizedOptions = null;
725
        }
726
727
        if (empty($whitelist)) {
728
            $whitelist = null;
729
        }
730
731
        return new Arguments(
732
            $argument,
733
            $atLeastVersion,
734
            $backupGlobals,
735
            $backupStaticAttributes,
736
            $beStrictAboutChangesToGlobalState,
737
            $beStrictAboutResourceUsageDuringSmallTests,
738
            $bootstrap,
739
            $cacheResult,
740
            $cacheResultFile,
741
            $checkVersion,
742
            $colors,
743
            $columns,
744
            $configuration,
745
            $coverageClover,
746
            $coverageCrap4J,
747
            $coverageHtml,
748
            $coveragePhp,
749
            $coverageText,
750
            $coverageTextShowUncoveredFiles,
751
            $coverageTextShowOnlySummary,
752
            $coverageXml,
753
            $debug,
754
            $defaultTimeLimit,
755
            $disableCodeCoverageIgnore,
756
            $disallowTestOutput,
757
            $disallowTodoAnnotatedTests,
758
            $enforceTimeLimit,
759
            $excludeGroups,
760
            $executionOrder,
761
            $executionOrderDefects,
762
            $extensions,
763
            $unavailableExtensions,
764
            $failOnIncomplete,
765
            $failOnRisky,
766
            $failOnSkipped,
767
            $failOnWarning,
768
            $filter,
769
            $generateConfiguration,
770
            $groups,
771
            $help,
772
            $includePath,
773
            $iniSettings,
774
            $junitLogfile,
775
            $listGroups,
776
            $listSuites,
777
            $listTests,
778
            $listTestsXml,
779
            $loader,
780
            $noCoverage,
781
            $noExtensions,
782
            $noInteraction,
783
            $noLogging,
784
            $printer,
785
            $processIsolation,
786
            $randomOrderSeed,
787
            $repeat,
788
            $reportUselessTests,
789
            $resolveDependencies,
790
            $reverseList,
791
            $stderr,
792
            $strictCoverage,
793
            $stopOnDefect,
794
            $stopOnError,
795
            $stopOnFailure,
796
            $stopOnIncomplete,
797
            $stopOnRisky,
798
            $stopOnSkipped,
799
            $stopOnWarning,
800
            $teamcityLogfile,
801
            $testdoxExcludeGroups,
802
            $testdoxGroups,
803
            $testdoxHtmlFile,
804
            $testdoxTextFile,
805
            $testdoxXmlFile,
806
            $testSuffixes,
807
            $testSuite,
808
            $unrecognizedOptions,
809
            $unrecognizedOrderBy,
810
            $useDefaultConfiguration,
811
            $verbose,
812
            $version,
813
            $whitelist,
814
            $xdebugFilterFile
815
        );
816
    }
817
}
818