Passed
Push — master ( 2bb340...72bcc8 )
by Doug
07:21 queued 05:29
created

ReportService   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Test Coverage

Coverage 79.75%

Importance

Changes 8
Bugs 0 Features 1
Metric Value
eloc 73
c 8
b 0
f 1
dl 0
loc 100
ccs 63
cts 79
cp 0.7975
rs 10
wmc 13

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
C generateReport() 0 85 12
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;
16
use SebastianBergmann\CodeCoverage\Report\Html\CustomCssFile;
17
use SebastianBergmann\CodeCoverage\Report\Html\Facade as HtmlFacade;
18
use SebastianBergmann\CodeCoverage\Report\PHP;
19
use SebastianBergmann\CodeCoverage\Report\Text;
20
use SebastianBergmann\CodeCoverage\Report\Thresholds;
21
use SebastianBergmann\CodeCoverage\Report\Xml\Facade as XmlFacade;
22
23
use function sprintf;
24
25
class ReportService
26
{
27
    private array $config;
28
29
    /**
30
     * Constructor.
31
     */
32 468
    public function __construct(array $reportConfig)
33
    {
34 468
        $this->config = $reportConfig;
35
    }
36
37
    /**
38
     * Generate report.
39
     */
40 288
    public function generateReport(CodeCoverage $coverage): void
41
    {
42 288
        foreach ($this->config as $format => $config) {
43
            switch ($format) {
44 288
                case 'php':
45 36
                    $report = new PHP();
46 36
                    $report->process($coverage, $config['target']);
47 36
                    break;
48 252
                case 'clover':
49 72
                    $report = new Clover();
50 72
                    $report->process($coverage, $config['target'], $config['name']);
51 72
                    break;
52 216
                case 'crap4j':
53 72
                    $report = new Crap4j();
54 72
                    $report->process($coverage, $config['target'], $config['name']);
55 72
                    break;
56 144
                case 'html':
57 36
                    if (InstalledVersions::satisfies(new VersionParser(), 'phpunit/php-code-coverage', '^9.0')) {
58
                        $report = new HtmlFacade(
59
                            $config['lowUpperBound'],
60
                            $config['highLowerBound'],
61
                            sprintf(
0 ignored issues
show
Bug introduced by
sprintf(' and <a href="h.../behat-code-coverage')) of type string is incompatible with the type SebastianBergmann\CodeCo...\Report\Thresholds|null expected by parameter $thresholds 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

61
                            /** @scrutinizer ignore-type */ sprintf(
Loading history...
62
                                ' and <a href="https://behat.cc">Behat Code Coverage %s</a>',
63
                                InstalledVersions::getPrettyVersion('dvdoug/behat-code-coverage')
64
                            )
65
                        );
66
                    } else {
67 36
                        $thresholds = Thresholds::from(
68 36
                            $config['lowUpperBound'],
69 36
                            $config['highLowerBound'],
70 36
                        );
71 36
                        $colors = Colors::from(
72 36
                            $config['colors']['successLow'],
73 36
                            $config['colors']['successMedium'],
74 36
                            $config['colors']['successHigh'],
75 36
                            $config['colors']['warning'],
76 36
                            $config['colors']['danger'],
77 36
                        );
78 36
                        if ($config['customCSSFile']) {
79
                            $customCss = CustomCssFile::from($config['customCSSFile']);
80
                        } else {
81 36
                            $customCss = CustomCssFile::default();
82
                        }
83 36
                        $report = new HtmlFacade(
84 36
                            sprintf(
85 36
                                ' and <a href="https://behat.cc">Behat Code Coverage %s</a>',
86 36
                                InstalledVersions::getPrettyVersion('dvdoug/behat-code-coverage')
87 36
                            ),
88 36
                            $colors,
89 36
                            $thresholds,
90 36
                            $customCss
91 36
                        );
92
                    }
93 36
                    $report->process($coverage, $config['target']);
94 36
                    break;
95 108
                case 'text':
96 36
                    if (InstalledVersions::satisfies(new VersionParser(), 'phpunit/php-code-coverage', '^9.0')) {
97
                        $report = new Text(
98
                            $config['lowUpperBound'],
99
                            $config['highLowerBound'],
100
                            $config['showUncoveredFiles'],
101
                            $config['showOnlySummary']
0 ignored issues
show
Unused Code introduced by
The call to SebastianBergmann\CodeCo...ort\Text::__construct() has too many arguments starting with $config['showOnlySummary']. ( Ignorable by Annotation )

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

101
                        $report = /** @scrutinizer ignore-call */ new Text(

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...
102
                        );
103
                        echo $report->process($coverage, $config['showColors']);
104
                    } else {
105 36
                        $thresholds = Thresholds::from(
106 36
                            $config['lowUpperBound'],
107 36
                            $config['highLowerBound'],
108 36
                        );
109 36
                        $report = new Text(
110 36
                            $thresholds,
111 36
                            $config['showUncoveredFiles'],
112 36
                            $config['showOnlySummary']
113 36
                        );
114 36
                        echo $report->process($coverage, $config['showColors']);
115
                    }
116 36
                    break;
117 72
                case 'xml':
118 36
                    $report = new XmlFacade('');
119 36
                    $report->process($coverage, $config['target']);
120 36
                    break;
121 36
                case 'cobertura':
122 36
                    $report = new Cobertura();
123 36
                    $report->process($coverage, $config['target']);
124 36
                    break;
125
            }
126
        }
127
    }
128
}
129