Passed
Push — master ( 27ea89...2a04dc )
by Doug
02:21
created

ReportService::generateReport()   B

Complexity

Conditions 10
Paths 10

Size

Total Lines 64
Code Lines 55

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 59
CRAP Score 10.0004

Importance

Changes 7
Bugs 0 Features 1
Metric Value
cc 10
eloc 55
c 7
b 0
f 1
nc 10
nop 1
dl 0
loc 64
ccs 59
cts 60
cp 0.9833
crap 10.0004
rs 7.1151

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
/**
3
 * Behat Code Coverage
4
 */
5
declare(strict_types=1);
6
7
namespace DVDoug\Behat\CodeCoverage\Service;
8
9
use Composer\InstalledVersions;
10
use SebastianBergmann\CodeCoverage\CodeCoverage;
11
use SebastianBergmann\CodeCoverage\Report\Clover;
12
use SebastianBergmann\CodeCoverage\Report\Cobertura;
13
use SebastianBergmann\CodeCoverage\Report\Crap4j;
14
use SebastianBergmann\CodeCoverage\Report\Html\Colors;
15
use SebastianBergmann\CodeCoverage\Report\Html\CustomCssFile;
16
use SebastianBergmann\CodeCoverage\Report\Html\Facade as HtmlFacade;
17
use SebastianBergmann\CodeCoverage\Report\PHP;
18
use SebastianBergmann\CodeCoverage\Report\Text;
19
use SebastianBergmann\CodeCoverage\Report\Thresholds;
20
use SebastianBergmann\CodeCoverage\Report\Xml\Facade as XmlFacade;
21
22
use function sprintf;
23
24
class ReportService
25
{
26
    private array $config;
27
28
    /**
29
     * Constructor.
30
     */
31 468
    public function __construct(array $reportConfig)
32
    {
33 468
        $this->config = $reportConfig;
34
    }
35
36
    /**
37
     * Generate report.
38
     */
39 288
    public function generateReport(CodeCoverage $coverage): void
40
    {
41 288
        foreach ($this->config as $format => $config) {
42
            switch ($format) {
43 288
                case 'php':
44 36
                    $report = new PHP();
45 36
                    $report->process($coverage, $config['target']);
46 36
                    break;
47 252
                case 'clover':
48 72
                    $report = new Clover();
49 72
                    $report->process($coverage, $config['target'], $config['name']);
50 72
                    break;
51 216
                case 'crap4j':
52 72
                    $report = new Crap4j();
53 72
                    $report->process($coverage, $config['target'], $config['name']);
54 72
                    break;
55 144
                case 'html':
56 36
                    $thresholds = Thresholds::from(
57 36
                        $config['lowUpperBound'],
58 36
                        $config['highLowerBound'],
59 36
                    );
60 36
                    $colors = Colors::from(
61 36
                        $config['colors']['successLow'],
62 36
                        $config['colors']['successMedium'],
63 36
                        $config['colors']['successHigh'],
64 36
                        $config['colors']['warning'],
65 36
                        $config['colors']['danger'],
66 36
                    );
67 36
                    if ($config['customCSSFile']) {
68
                        $customCss = CustomCssFile::from($config['customCSSFile']);
69
                    } else {
70 36
                        $customCss = CustomCssFile::default();
71
                    }
72 36
                    $report = new HtmlFacade(
73 36
                        sprintf(
74 36
                            ' and <a href="https://behat.cc">Behat Code Coverage %s</a>',
75 36
                            InstalledVersions::getPrettyVersion('dvdoug/behat-code-coverage')
76 36
                        ),
77 36
                        $colors,
78 36
                        $thresholds,
79 36
                        $customCss
80 36
                    );
81 36
                    $report->process($coverage, $config['target']);
82 36
                    break;
83 108
                case 'text':
84 36
                    $thresholds = Thresholds::from(
85 36
                        $config['lowUpperBound'],
86 36
                        $config['highLowerBound'],
87 36
                    );
88 36
                    $report = new Text(
89 36
                        $thresholds,
90 36
                        $config['showUncoveredFiles'],
91 36
                        $config['showOnlySummary']
92 36
                    );
93 36
                    echo $report->process($coverage, $config['showColors']);
94 36
                    break;
95 72
                case 'xml':
96 36
                    $report = new XmlFacade('');
97 36
                    $report->process($coverage, $config['target']);
98 36
                    break;
99 36
                case 'cobertura':
100 36
                    $report = new Cobertura();
101 36
                    $report->process($coverage, $config['target']);
102 36
                    break;
103
            }
104
        }
105
    }
106
}
107