ReportService::generateReport()   B
last analyzed

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 481
    public function __construct(array $reportConfig)
32
    {
33 481
        $this->config = $reportConfig;
34
    }
35
36
    /**
37
     * Generate report.
38
     */
39 296
    public function generateReport(CodeCoverage $coverage): void
40
    {
41 296
        foreach ($this->config as $format => $config) {
42
            switch ($format) {
43 296
                case 'php':
44 37
                    $report = new PHP();
45 37
                    $report->process($coverage, $config['target']);
46 37
                    break;
47 259
                case 'clover':
48 74
                    $report = new Clover();
49 74
                    $report->process($coverage, $config['target'], $config['name']);
50 74
                    break;
51 222
                case 'crap4j':
52 74
                    $report = new Crap4j();
53 74
                    $report->process($coverage, $config['target'], $config['name']);
54 74
                    break;
55 148
                case 'html':
56 37
                    $thresholds = Thresholds::from(
57 37
                        $config['lowUpperBound'],
58 37
                        $config['highLowerBound'],
59 37
                    );
60 37
                    $colors = Colors::from(
61 37
                        $config['colors']['successLow'],
62 37
                        $config['colors']['successMedium'],
63 37
                        $config['colors']['successHigh'],
64 37
                        $config['colors']['warning'],
65 37
                        $config['colors']['danger'],
66 37
                    );
67 37
                    if ($config['customCSSFile']) {
68
                        $customCss = CustomCssFile::from($config['customCSSFile']);
69
                    } else {
70 37
                        $customCss = CustomCssFile::default();
71
                    }
72 37
                    $report = new HtmlFacade(
73 37
                        sprintf(
74 37
                            ' and <a href="https://behat.cc">Behat Code Coverage %s</a>',
75 37
                            InstalledVersions::getPrettyVersion('dvdoug/behat-code-coverage')
76 37
                        ),
77 37
                        $colors,
78 37
                        $thresholds,
79 37
                        $customCss
80 37
                    );
81 37
                    $report->process($coverage, $config['target']);
82 37
                    break;
83 111
                case 'text':
84 37
                    $thresholds = Thresholds::from(
85 37
                        $config['lowUpperBound'],
86 37
                        $config['highLowerBound'],
87 37
                    );
88 37
                    $report = new Text(
89 37
                        $thresholds,
90 37
                        $config['showUncoveredFiles'],
91 37
                        $config['showOnlySummary']
92 37
                    );
93 37
                    echo $report->process($coverage, $config['showColors']);
94 37
                    break;
95 74
                case 'xml':
96 37
                    $report = new XmlFacade('');
97 37
                    $report->process($coverage, $config['target']);
98 37
                    break;
99 37
                case 'cobertura':
100 37
                    $report = new Cobertura();
101 37
                    $report->process($coverage, $config['target']);
102 37
                    break;
103
            }
104
        }
105
    }
106
}
107