Skip to content

Commit dac4941

Browse files
Fix compatibility with latest phpspec container
1 parent 2ccc352 commit dac4941

File tree

4 files changed

+202
-23
lines changed

4 files changed

+202
-23
lines changed

phpstan.neon

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,14 @@ parameters:
77
# phpstan has hard time to check whenever we are using PHPUnit 10 or PHPUnit 9
88
-
99
message: '#Class SebastianBergmann\\CodeCoverage\\Report\\Text constructor invoked with 4 parameters, 1-3 required\.#'
10+
count: 1
1011
path: ./src/CodeCoverageExtension.php
12+
-
13+
message: "#^Parameter \\#1 \\$thresholds of class SebastianBergmann\\\\CodeCoverage\\\\Report\\\\Text constructor expects SebastianBergmann\\\\CodeCoverage\\\\Report\\\\Thresholds, int given\\.$#"
14+
count: 1
15+
path: src/CodeCoverageExtension.php
16+
17+
-
18+
message: "#^Parameter \\#2 \\$showUncoveredFiles of class SebastianBergmann\\\\CodeCoverage\\\\Report\\\\Text constructor expects bool, int given\\.$#"
19+
count: 1
20+
path: src/CodeCoverageExtension.php

src/CodeCoverageExtension.php

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -100,62 +100,56 @@ public function load(ServiceContainer $container, array $params = []): void
100100
$options['show_only_summary'] = false;
101101
}
102102

103-
return $options;
103+
return new CodeCoverageOptions($options);
104104
});
105105

106106
$container->define('code_coverage.reports', static function (ServiceContainer $container) {
107-
/** @var array<string, mixed> $options */
108-
$options = $container->get('code_coverage.options');
107+
/** @var CodeCoverageOptions $optionsWrapper */
108+
$optionsWrapper = $container->get('code_coverage.options');
109+
$options = $optionsWrapper->getOptions();
109110

110111
$reports = [];
111112

112-
foreach ($options['format'] as $format) {
113+
foreach ($optionsWrapper->getFormats() as $format) {
113114
switch ($format) {
114115
case 'clover':
115116
$reports['clover'] = new Report\Clover();
116-
117117
break;
118118
case 'php':
119119
$reports['php'] = new Report\PHP();
120-
121120
break;
122121
case 'text':
123122
$reports['text'] = version_compare(Version::id(), '10.0.0', '>=') && class_exists(Thresholds::class)
124123
? new Report\Text(
125-
Thresholds::from($options['lower_upper_bound'], $options['high_lower_bound']),
126-
$options['show_uncovered_files'],
127-
$options['show_only_summary']
124+
Thresholds::from($optionsWrapper->getLowerUpperBound(), $optionsWrapper->getHighLowerBound()),
125+
$optionsWrapper->showUncoveredFiles(),
126+
$optionsWrapper->showOnlySummary()
128127
)
129128
: new Report\Text(
130-
$options['lower_upper_bound'],
131-
$options['high_lower_bound'],
132-
$options['show_uncovered_files'],
133-
$options['show_only_summary']
129+
$optionsWrapper->getLowerUpperBound(),
130+
$optionsWrapper->getHighLowerBound(),
131+
$optionsWrapper->showUncoveredFiles(),
132+
$optionsWrapper->showOnlySummary()
134133
);
135-
136134
break;
137135
case 'xml':
138136
$reports['xml'] = new Report\Xml\Facade(Version::id());
139-
140137
break;
141138
case 'crap4j':
142139
$reports['crap4j'] = new Report\Crap4j();
143-
144140
break;
145141
case 'html':
146142
$reports['html'] = new Report\Html\Facade();
147-
148143
break;
149144
case 'cobertura':
150145
$reports['cobertura'] = new Report\Cobertura();
151-
152146
break;
153147
}
154148
}
155149

156150
$container->setParam('code_coverage', $options);
157151

158-
return $reports;
152+
return new CodeCoverageReports($reports);
159153
});
160154

161155
$container->define('event_dispatcher.listeners.code_coverage', static function (ServiceContainer $container) {
@@ -169,11 +163,19 @@ public function load(ServiceContainer $container, array $params = []): void
169163
/** @var CodeCoverage $codeCoverage */
170164
$codeCoverage = $container->get('code_coverage');
171165

172-
/** @var array<string, object> $codeCoverageReports */
173-
$codeCoverageReports = $container->get('code_coverage.reports');
166+
/** @var CodeCoverageReports $codeCoverageReportsWrapper */
167+
$codeCoverageReportsWrapper = $container->get('code_coverage.reports');
168+
169+
/** @var CodeCoverageOptions $optionsWrapper */
170+
$optionsWrapper = $container->get('code_coverage.options');
174171

175-
$listener = new CodeCoverageListener($consoleIO, $codeCoverage, $codeCoverageReports, $skipCoverage);
176-
$listener->setOptions($container->getParam('code_coverage', []));
172+
$listener = new CodeCoverageListener(
173+
$consoleIO,
174+
$codeCoverage,
175+
$codeCoverageReportsWrapper->getReports(),
176+
$skipCoverage
177+
);
178+
$listener->setOptions($optionsWrapper->getOptions());
177179

178180
return $listener;
179181
}, ['event_dispatcher.listeners']);

src/CodeCoverageOptions.php

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the friends-of-phpspec/phpspec-code-coverage package.
5+
*
6+
* @author ek9 <dev@ek9.co>
7+
* @license MIT
8+
*
9+
* For the full copyright and license information, please see the LICENSE file
10+
* that was distributed with this source code.
11+
*/
12+
13+
declare(strict_types=1);
14+
15+
namespace FriendsOfPhpSpec\PhpSpec\CodeCoverage;
16+
17+
class CodeCoverageOptions
18+
{
19+
/**
20+
* @var array<string, mixed>
21+
*/
22+
private $options;
23+
24+
/**
25+
* @param array<string, mixed> $options
26+
*/
27+
public function __construct(array $options)
28+
{
29+
$this->options = $options;
30+
}
31+
32+
/**
33+
* @return array<string, mixed>
34+
*/
35+
public function getOptions(): array
36+
{
37+
return $this->options;
38+
}
39+
40+
/**
41+
* @return mixed
42+
*/
43+
public function get(string $key)
44+
{
45+
return $this->options[$key] ?? null;
46+
}
47+
48+
/**
49+
* @param mixed $default
50+
*
51+
* @return mixed
52+
*/
53+
public function getWithDefault(string $key, $default)
54+
{
55+
return $this->options[$key] ?? $default;
56+
}
57+
58+
/**
59+
* @return array<string>
60+
*/
61+
public function getFormats(): array
62+
{
63+
return $this->options['format'] ?? ['html'];
64+
}
65+
66+
/**
67+
* @return array<string, string>
68+
*/
69+
public function getOutputPaths(): array
70+
{
71+
return $this->options['output'] ?? [];
72+
}
73+
74+
/**
75+
* @return bool
76+
*/
77+
public function showUncoveredFiles(): bool
78+
{
79+
return $this->options['show_uncovered_files'] ?? true;
80+
}
81+
82+
/**
83+
* @return int
84+
*/
85+
public function getLowerUpperBound(): int
86+
{
87+
return $this->options['lower_upper_bound'] ?? 35;
88+
}
89+
90+
/**
91+
* @return int
92+
*/
93+
public function getHighLowerBound(): int
94+
{
95+
return $this->options['high_lower_bound'] ?? 70;
96+
}
97+
98+
/**
99+
* @return bool
100+
*/
101+
public function showOnlySummary(): bool
102+
{
103+
return $this->options['show_only_summary'] ?? false;
104+
}
105+
}

src/CodeCoverageReports.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the friends-of-phpspec/phpspec-code-coverage package.
5+
*
6+
* @author ek9 <dev@ek9.co>
7+
* @license MIT
8+
*
9+
* For the full copyright and license information, please see the LICENSE file
10+
* that was distributed with this source code.
11+
*/
12+
13+
declare(strict_types=1);
14+
15+
namespace FriendsOfPhpSpec\PhpSpec\CodeCoverage;
16+
17+
class CodeCoverageReports
18+
{
19+
/**
20+
* @var array<string, object>
21+
*/
22+
private $reports;
23+
24+
/**
25+
* @param array<string, object> $reports
26+
*/
27+
public function __construct(array $reports)
28+
{
29+
$this->reports = $reports;
30+
}
31+
32+
/**
33+
* @return array<string, object>
34+
*/
35+
public function getReports(): array
36+
{
37+
return $this->reports;
38+
}
39+
40+
public function getReport(string $format): ?object
41+
{
42+
return $this->reports[$format] ?? null;
43+
}
44+
45+
/**
46+
* @return array<string>
47+
*/
48+
public function getAvailableFormats(): array
49+
{
50+
return array_keys($this->reports);
51+
}
52+
53+
public function hasReport(string $format): bool
54+
{
55+
return isset($this->reports[$format]);
56+
}
57+
58+
public function count(): int
59+
{
60+
return count($this->reports);
61+
}
62+
}

0 commit comments

Comments
 (0)