Passed
Push — master ( 19f9b1...a9d84c )
by Doug
11:47 queued 10:00
created

ReportService   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Test Coverage

Coverage 95.18%

Importance

Changes 7
Bugs 0 Features 1
Metric Value
eloc 73
c 7
b 0
f 1
dl 0
loc 103
ccs 79
cts 83
cp 0.9518
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
    /**
28
     * @var array
29
     */
30
    private $config;
31
32
    /**
33
     * Constructor.
34
     */
35 468
    public function __construct(array $reportConfig)
36
    {
37 468
        $this->config = $reportConfig;
38
    }
39
40
    /**
41
     * Generate report.
42
     */
43 288
    public function generateReport(CodeCoverage $coverage): void
44
    {
45 288
        foreach ($this->config as $format => $config) {
46 24
            switch ($format) {
47 288
                case 'clover':
48 72
                    $report = new Clover();
49 72
                    $report->process($coverage, $config['target'], $config['name']);
50 72
                    break;
51 252
                case 'crap4j':
52 72
                    $report = new Crap4j();
53 72
                    $report->process($coverage, $config['target'], $config['name']);
54 72
                    break;
55 180
                case 'html':
56 36
                    if (InstalledVersions::satisfies(new VersionParser(), 'phpunit/php-code-coverage', '^9.0')) {
57 18
                        $report = new HtmlFacade(
58 18
                            $config['lowUpperBound'],
59 18
                            $config['highLowerBound'],
60 18
                            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

60
                            /** @scrutinizer ignore-type */ sprintf(
Loading history...
61 12
                                ' and <a href="https://behat.cc">Behat Code Coverage %s</a>',
62 18
                                InstalledVersions::getPrettyVersion('dvdoug/behat-code-coverage')
63 12
                            )
64 12
                        );
65
                    } else {
66 18
                        $thresholds = Thresholds::from(
67 18
                            $config['lowUpperBound'],
68 18
                            $config['highLowerBound'],
69 18
                        );
70 18
                        $colors = Colors::from(
71 18
                            $config['colors']['successLow'],
72 18
                            $config['colors']['successMedium'],
73 18
                            $config['colors']['successHigh'],
74 18
                            $config['colors']['warning'],
75 18
                            $config['colors']['danger'],
76 18
                        );
77 18
                        if ($config['customCSSFile']) {
78
                            $customCss = CustomCssFile::from($config['customCSSFile']);
79
                        } else {
80 18
                            $customCss = CustomCssFile::default();
81
                        }
82 18
                        $report = new HtmlFacade(
83 18
                            sprintf(
84 18
                                ' and <a href="https://behat.cc">Behat Code Coverage %s</a>',
85 18
                                InstalledVersions::getPrettyVersion('dvdoug/behat-code-coverage')
86 18
                            ),
87 18
                            $colors,
88 18
                            $thresholds,
89 18
                            $customCss
90 18
                        );
91
                    }
92 36
                    $report->process($coverage, $config['target']);
93 36
                    break;
94 144
                case 'php':
95 36
                    $report = new PHP();
96 36
                    $report->process($coverage, $config['target']);
97 36
                    break;
98 108
                case 'text':
99 36
                    if (InstalledVersions::satisfies(new VersionParser(), 'phpunit/php-code-coverage', '^9.0')) {
100 18
                        $report = new Text(
101 18
                            $config['lowUpperBound'],
102 18
                            $config['highLowerBound'],
103 18
                            $config['showUncoveredFiles'],
104 18
                            $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

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