Passed
Pull Request — master (#22)
by
unknown
14:48 queued 12:08
created

ReportService   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Test Coverage

Coverage 95.18%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
C generateReport() 0 85 12
A __construct() 0 3 1
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 455
    public function __construct(array $reportConfig)
33
    {
34 455
        $this->config = $reportConfig;
35
    }
36
37
    /**
38
     * Generate report.
39
     */
40 280
    public function generateReport(CodeCoverage $coverage): void
41
    {
42 280
        foreach ($this->config as $format => $config) {
43 32
            switch ($format) {
44 280
                case 'php':
45 35
                    $report = new PHP();
46 35
                    $report->process($coverage, $config['target']);
47 35
                    break;
48 245
                case 'clover':
49 70
                    $report = new Clover();
50 70
                    $report->process($coverage, $config['target'], $config['name']);
51 70
                    break;
52 210
                case 'crap4j':
53 70
                    $report = new Crap4j();
54 70
                    $report->process($coverage, $config['target'], $config['name']);
55 70
                    break;
56 140
                case 'html':
57 35
                    if (InstalledVersions::satisfies(new VersionParser(), 'phpunit/php-code-coverage', '^9.0')) {
58 22
                        $report = new HtmlFacade(
59 22
                            $config['lowUpperBound'],
60 22
                            $config['highLowerBound'],
61 22
                            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 14
                                ' and <a href="https://behat.cc">Behat Code Coverage %s</a>',
63 22
                                InstalledVersions::getPrettyVersion('dvdoug/behat-code-coverage')
64 14
                            )
65 14
                        );
66
                    } else {
67 13
                        $thresholds = Thresholds::from(
68 13
                            $config['lowUpperBound'],
69 13
                            $config['highLowerBound'],
70 13
                        );
71 13
                        $colors = Colors::from(
72 13
                            $config['colors']['successLow'],
73 13
                            $config['colors']['successMedium'],
74 13
                            $config['colors']['successHigh'],
75 13
                            $config['colors']['warning'],
76 13
                            $config['colors']['danger'],
77 13
                        );
78 13
                        if ($config['customCSSFile']) {
79
                            $customCss = CustomCssFile::from($config['customCSSFile']);
80
                        } else {
81 13
                            $customCss = CustomCssFile::default();
82
                        }
83 13
                        $report = new HtmlFacade(
84 13
                            sprintf(
85 13
                                ' and <a href="https://behat.cc">Behat Code Coverage %s</a>',
86 13
                                InstalledVersions::getPrettyVersion('dvdoug/behat-code-coverage')
87 13
                            ),
88 13
                            $colors,
89 13
                            $thresholds,
90 13
                            $customCss
91 13
                        );
92
                    }
93 35
                    $report->process($coverage, $config['target']);
94 35
                    break;
95 105
                case 'text':
96 35
                    if (InstalledVersions::satisfies(new VersionParser(), 'phpunit/php-code-coverage', '^9.0')) {
97 22
                        $report = new Text(
98 22
                            $config['lowUpperBound'],
99 22
                            $config['highLowerBound'],
100 22
                            $config['showUncoveredFiles'],
101 22
                            $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 14
                        );
103 22
                        echo $report->process($coverage, $config['showColors']);
104
                    } else {
105 13
                        $thresholds = Thresholds::from(
106 13
                            $config['lowUpperBound'],
107 13
                            $config['highLowerBound'],
108 13
                        );
109 13
                        $report = new Text(
110 13
                            $thresholds,
111 13
                            $config['showUncoveredFiles'],
112 13
                            $config['showOnlySummary']
113 13
                        );
114 13
                        echo $report->process($coverage, $config['showColors']);
115
                    }
116 35
                    break;
117 70
                case 'xml':
118 35
                    $report = new XmlFacade('');
119 35
                    $report->process($coverage, $config['target']);
120 35
                    break;
121 35
                case 'cobertura':
122 35
                    $report = new Cobertura();
123 35
                    $report->process($coverage, $config['target']);
124 35
                    break;
125
            }
126
        }
127
    }
128
}
129