Passed
Push — master ( b5097b...462a2e )
by Doug
11:34
created

ReportService::generateReport()   C

Complexity

Conditions 11
Paths 11

Size

Total Lines 72
Code Lines 61

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 38
CRAP Score 11

Importance

Changes 4
Bugs 0 Features 1
Metric Value
cc 11
eloc 61
c 4
b 0
f 1
nc 11
nop 1
dl 0
loc 72
ccs 38
cts 38
cp 1
crap 11
rs 6.7042

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 Composer\Semver\VersionParser;
11
use SebastianBergmann\CodeCoverage\CodeCoverage;
12
use SebastianBergmann\CodeCoverage\Report\Clover;
13
use SebastianBergmann\CodeCoverage\Report\Cobertura;
14
use SebastianBergmann\CodeCoverage\Report\Crap4j;
15
use SebastianBergmann\CodeCoverage\Report\Html\Colors;
0 ignored issues
show
Bug introduced by
The type SebastianBergmann\CodeCoverage\Report\Html\Colors was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use SebastianBergmann\CodeCoverage\Report\Html\CustomCssFile;
0 ignored issues
show
Bug introduced by
The type SebastianBergmann\CodeCo...port\Html\CustomCssFile was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use SebastianBergmann\CodeCoverage\Report\Html\Facade as HtmlFacade;
18
use SebastianBergmann\CodeCoverage\Report\Html\Thresholds;
0 ignored issues
show
Bug introduced by
The type SebastianBergmann\CodeCo...\Report\Html\Thresholds was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
use SebastianBergmann\CodeCoverage\Report\PHP;
20
use SebastianBergmann\CodeCoverage\Report\Text;
21
use SebastianBergmann\CodeCoverage\Report\Xml\Facade as XmlFacade;
22
use function sprintf;
23
24
class ReportService
25
{
26
    /**
27
     * @var array
28
     */
29
    private $config;
30 52
31
    /**
32 52
     * Constructor.
33 39
     */
34
    public function __construct(array $reportConfig)
35
    {
36
        $this->config = $reportConfig;
37
    }
38 32
39
    /**
40 32
     * Generate report.
41 16
     */
42 32
    public function generateReport(CodeCoverage $coverage): void
43 8
    {
44 8
        foreach ($this->config as $format => $config) {
45 8
            switch ($format) {
46 28
                case 'clover':
47 8
                    $report = new Clover();
48 8
                    $report->process($coverage, $config['target'], $config['name']);
49 8
                    break;
50 20
                case 'crap4j':
51 4
                    $report = new Crap4j();
52 4
                    $report->process($coverage, $config['target'], $config['name']);
53 4
                    break;
54 4
                case 'html':
55 2
                    if (InstalledVersions::satisfies(new VersionParser(), 'phpunit/php-code-coverage', '^9.0')) {
56 4
                        $report = new HtmlFacade(
57
                            $config['lowUpperBound'],
58
                            $config['highLowerBound'],
59 4
                            sprintf(
60 4
                                ' and <a href="https://behat.cc">Behat Code Coverage %s</a>',
61 16
                                InstalledVersions::getPrettyVersion('dvdoug/behat-code-coverage')
62 4
                            )
63 4
                        );
64 4
                    } else {
65 12
                        $thresholds = Thresholds::from(
66 4
                            $config['lowUpperBound'],
67 4
                            $config['highLowerBound'],
68 4
                        );
69 4
                        $colors = Colors::from(
70 4
                            $config['colors']['successLow'],
71
                            $config['colors']['successMedium'],
72 4
                            $config['colors']['successHigh'],
73 4
                            $config['colors']['warning'],
74 8
                            $config['colors']['danger'],
75 4
                        );
76 4
                        if ($config['customCSSFile']) {
77 4
                            $customCss = CustomCssFile::from($config['customCSSFile']);
78 4
                        } else {
79 4
                            $customCss = CustomCssFile::default();
80 4
                        }
81 4
                        $report = new HtmlFacade(
82
                            sprintf(
0 ignored issues
show
Bug introduced by
sprintf(' and <a href="h.../behat-code-coverage')) of type string is incompatible with the type integer expected by parameter $lowUpperBound of SebastianBergmann\CodeCo...l\Facade::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

82
                            /** @scrutinizer ignore-type */ sprintf(
Loading history...
83
                                ' and <a href="https://behat.cc">Behat Code Coverage %s</a>',
84 24
                                InstalledVersions::getPrettyVersion('dvdoug/behat-code-coverage')
85
                            ),
86
                            $colors,
87
                            $thresholds,
88
                            $customCss
0 ignored issues
show
Unused Code introduced by
The call to SebastianBergmann\CodeCo...l\Facade::__construct() has too many arguments starting with $customCss. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

88
                        $report = /** @scrutinizer ignore-call */ new HtmlFacade(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
89
                        );
90
                    }
91
                    $report->process($coverage, $config['target']);
92
                    break;
93
                case 'php':
94
                    $report = new PHP();
95
                    $report->process($coverage, $config['target']);
96
                    break;
97
                case 'text':
98
                    $report = new Text(
99
                        $config['lowUpperBound'],
100
                        $config['highLowerBound'],
101
                        $config['showUncoveredFiles'],
102
                        $config['showOnlySummary']
103
                    );
104
                    echo $report->process($coverage, $config['showColors']);
105
                    break;
106
                case 'xml':
107
                    $report = new XmlFacade('');
108
                    $report->process($coverage, $config['target']);
109
                    break;
110
                case 'cobertura':
111
                    $report = new Cobertura();
112
                    $report->process($coverage, $config['target']);
113
                    break;
114
            }
115
        }
116
    }
117
}
118