Skip to content

Commit e29538a

Browse files
authored
Merge pull request #64 from balazscsaba2006/master
Fix main PHPSpec and PHP 8.4 compatibility: Wrap service container arrays in objects
2 parents 2ccc352 + 03663fb commit e29538a

File tree

5 files changed

+213
-28
lines changed

5 files changed

+213
-28
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

spec/CodeCoverageExtensionSpec.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Exception;
88
use FriendsOfPhpSpec\PhpSpec\CodeCoverage\CodeCoverageExtension;
9+
use FriendsOfPhpSpec\PhpSpec\CodeCoverage\CodeCoverageOptions;
910
use PhpSpec\ObjectBehavior;
1011
use PhpSpec\ServiceContainer\IndexedServiceContainer;
1112

@@ -25,9 +26,10 @@ public function it_should_allow_to_set_show_only_summary_option(): void
2526
$container->setParam('code_coverage', ['show_only_summary' => true]);
2627
$this->load($container);
2728

29+
/** @var CodeCoverageOptions $options */
2830
$options = $container->get('code_coverage.options');
2931

30-
if (true !== $options['show_only_summary']) {
32+
if (true !== $options->showOnlySummary()) {
3133
throw new Exception('show_only_summary was not set');
3234
}
3335
}
@@ -37,9 +39,10 @@ public function it_should_not_use_show_only_summary_option_by_default(): void
3739
$container = new IndexedServiceContainer();
3840
$this->load($container, []);
3941

42+
/** @var CodeCoverageOptions $options */
4043
$options = $container->get('code_coverage.options');
4144

42-
if (false !== $options['show_only_summary']) {
45+
if (false !== $options->showOnlySummary()) {
4346
throw new Exception('show_only_summary should be `false` by default');
4447
}
4548
}
@@ -50,9 +53,10 @@ public function it_should_transform_format_into_array(): void
5053
$container->setParam('code_coverage', ['format' => 'html']);
5154
$this->load($container);
5255

56+
/** @var CodeCoverageOptions $options */
5357
$options = $container->get('code_coverage.options');
5458

55-
if ($options['format'] !== ['html']) {
59+
if ($options->getFormats() !== ['html']) {
5660
throw new Exception('Default format is not transformed to an array');
5761
}
5862
}
@@ -62,9 +66,10 @@ public function it_should_use_html_format_by_default(): void
6266
$container = new IndexedServiceContainer();
6367
$this->load($container, []);
6468

69+
/** @var CodeCoverageOptions $options */
6570
$options = $container->get('code_coverage.options');
6671

67-
if ($options['format'] !== ['html']) {
72+
if ($options->getFormats() !== ['html']) {
6873
throw new Exception('Default format is not html');
6974
}
7075
}
@@ -75,9 +80,10 @@ public function it_should_use_singular_output(): void
7580
$container->setParam('code_coverage', ['output' => 'test', 'format' => 'foo']);
7681
$this->load($container);
7782

83+
/** @var CodeCoverageOptions $options */
7884
$options = $container->get('code_coverage.options');
7985

80-
if (['foo' => 'test'] !== $options['output']) {
86+
if (['foo' => 'test'] !== $options->getOutputPaths()) {
8187
throw new Exception('Default format is not singular output');
8288
}
8389
}

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(), // @phpstan-ignore-line Version 10.0.0+ uses Thresholds
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)