Skip to content

Commit af906be

Browse files
Extract value object
1 parent 2c3ab74 commit af906be

File tree

4 files changed

+79
-38
lines changed

4 files changed

+79
-38
lines changed

src/Report/Html/Facade.php

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use function str_ends_with;
1717
use SebastianBergmann\CodeCoverage\CodeCoverage;
1818
use SebastianBergmann\CodeCoverage\Directory as DirectoryUtil;
19-
use SebastianBergmann\CodeCoverage\InvalidArgumentException;
2019
use SebastianBergmann\CodeCoverage\Node\Directory as DirectoryNode;
2120
use SebastianBergmann\Template\Template;
2221

@@ -26,25 +25,16 @@ final class Facade
2625

2726
private string $generator;
2827

29-
private int $lowUpperBound;
30-
31-
private int $highLowerBound;
32-
3328
private Colors $colors;
3429

35-
public function __construct(int $lowUpperBound = 50, int $highLowerBound = 90, string $generator = '', ?Colors $colors = null)
36-
{
37-
if ($lowUpperBound > $highLowerBound) {
38-
throw new InvalidArgumentException(
39-
'$lowUpperBound must not be larger than $highLowerBound'
40-
);
41-
}
30+
private Thresholds $thresholds;
4231

43-
$this->generator = $generator;
44-
$this->highLowerBound = $highLowerBound;
45-
$this->lowUpperBound = $lowUpperBound;
46-
$this->colors = $colors ?? Colors::default();
47-
$this->templatePath = __DIR__ . '/Renderer/Template/';
32+
public function __construct(string $generator = '', ?Colors $colors = null, ?Thresholds $thresholds = null)
33+
{
34+
$this->generator = $generator;
35+
$this->colors = $colors ?? Colors::default();
36+
$this->thresholds = $thresholds ?? Thresholds::default();
37+
$this->templatePath = __DIR__ . '/Renderer/Template/';
4838
}
4939

5040
public function process(CodeCoverage $coverage, string $target): void
@@ -57,26 +47,23 @@ public function process(CodeCoverage $coverage, string $target): void
5747
$this->templatePath,
5848
$this->generator,
5949
$date,
60-
$this->lowUpperBound,
61-
$this->highLowerBound,
50+
$this->thresholds,
6251
$coverage->collectsBranchAndPathCoverage()
6352
);
6453

6554
$directory = new Directory(
6655
$this->templatePath,
6756
$this->generator,
6857
$date,
69-
$this->lowUpperBound,
70-
$this->highLowerBound,
58+
$this->thresholds,
7159
$coverage->collectsBranchAndPathCoverage()
7260
);
7361

7462
$file = new File(
7563
$this->templatePath,
7664
$this->generator,
7765
$date,
78-
$this->lowUpperBound,
79-
$this->highLowerBound,
66+
$this->thresholds,
8067
$coverage->collectsBranchAndPathCoverage()
8168
);
8269

src/Report/Html/Renderer.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,18 @@ abstract class Renderer
3232

3333
protected string $date;
3434

35-
protected int $lowUpperBound;
36-
37-
protected int $highLowerBound;
35+
protected Thresholds $thresholds;
3836

3937
protected bool $hasBranchCoverage;
4038

4139
protected string $version;
4240

43-
public function __construct(string $templatePath, string $generator, string $date, int $lowUpperBound, int $highLowerBound, bool $hasBranchCoverage)
41+
public function __construct(string $templatePath, string $generator, string $date, Thresholds $thresholds, bool $hasBranchCoverage)
4442
{
4543
$this->templatePath = $templatePath;
4644
$this->generator = $generator;
4745
$this->date = $date;
48-
$this->lowUpperBound = $lowUpperBound;
49-
$this->highLowerBound = $highLowerBound;
46+
$this->thresholds = $thresholds;
5047
$this->version = Version::id();
5148
$this->hasBranchCoverage = $hasBranchCoverage;
5249
}
@@ -178,8 +175,8 @@ protected function setCommonTemplateVariables(Template $template, AbstractNode $
178175
'version' => $this->version,
179176
'runtime' => $this->runtimeString(),
180177
'generator' => $this->generator,
181-
'low_upper_bound' => $this->lowUpperBound,
182-
'high_lower_bound' => $this->highLowerBound,
178+
'low_upper_bound' => $this->thresholds->lowUpperBound(),
179+
'high_lower_bound' => $this->thresholds->highLowerBound(),
183180
]
184181
);
185182
}
@@ -267,12 +264,12 @@ protected function coverageBar(float $percent): string
267264

268265
protected function colorLevel(float $percent): string
269266
{
270-
if ($percent <= $this->lowUpperBound) {
267+
if ($percent <= $this->thresholds->lowUpperBound()) {
271268
return 'danger';
272269
}
273270

274-
if ($percent > $this->lowUpperBound &&
275-
$percent < $this->highLowerBound) {
271+
if ($percent > $this->thresholds->lowUpperBound() &&
272+
$percent < $this->thresholds->highLowerBound()) {
276273
return 'warning';
277274
}
278275

src/Report/Html/Renderer/Dashboard.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ private function insufficientCoverage(array $classes, string $baseLink): array
188188

189189
foreach ($classes as $className => $class) {
190190
foreach ($class['methods'] as $methodName => $method) {
191-
if ($method['coverage'] < $this->highLowerBound) {
191+
if ($method['coverage'] < $this->thresholds->highLowerBound()) {
192192
$key = $methodName;
193193

194194
if ($className !== '*') {
@@ -199,7 +199,7 @@ private function insufficientCoverage(array $classes, string $baseLink): array
199199
}
200200
}
201201

202-
if ($class['coverage'] < $this->highLowerBound) {
202+
if ($class['coverage'] < $this->thresholds->highLowerBound()) {
203203
$leastTestedClasses[$className] = $class['coverage'];
204204
}
205205
}
@@ -242,7 +242,7 @@ private function projectRisks(array $classes, string $baseLink): array
242242

243243
foreach ($classes as $className => $class) {
244244
foreach ($class['methods'] as $methodName => $method) {
245-
if ($method['coverage'] < $this->highLowerBound && $method['ccn'] > 1) {
245+
if ($method['coverage'] < $this->thresholds->highLowerBound() && $method['ccn'] > 1) {
246246
$key = $methodName;
247247

248248
if ($className !== '*') {
@@ -253,7 +253,7 @@ private function projectRisks(array $classes, string $baseLink): array
253253
}
254254
}
255255

256-
if ($class['coverage'] < $this->highLowerBound &&
256+
if ($class['coverage'] < $this->thresholds->highLowerBound() &&
257257
$class['ccn'] > count($class['methods'])) {
258258
$classRisks[$className] = $class['crap'];
259259
}

src/Report/Html/Thresholds.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of phpunit/php-code-coverage.
4+
*
5+
* (c) Sebastian Bergmann <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace SebastianBergmann\CodeCoverage\Report\Html;
11+
12+
use SebastianBergmann\CodeCoverage\InvalidArgumentException;
13+
14+
/**
15+
* @psalm-immutable
16+
*/
17+
final class Thresholds
18+
{
19+
private int $lowUpperBound;
20+
21+
private int $highLowerBound;
22+
23+
public static function default(): self
24+
{
25+
return new self(50, 90);
26+
}
27+
28+
/**
29+
* @throws InvalidArgumentException
30+
*/
31+
public static function from(int $lowUpperBound, int $highLowerBound): self
32+
{
33+
if ($lowUpperBound > $highLowerBound) {
34+
throw new InvalidArgumentException(
35+
'$lowUpperBound must not be larger than $highLowerBound'
36+
);
37+
}
38+
39+
return new self($lowUpperBound, $highLowerBound);
40+
}
41+
42+
private function __construct(int $lowUpperBound, int $highLowerBound)
43+
{
44+
$this->lowUpperBound = $lowUpperBound;
45+
$this->highLowerBound = $highLowerBound;
46+
}
47+
48+
public function lowUpperBound(): int
49+
{
50+
return $this->lowUpperBound;
51+
}
52+
53+
public function highLowerBound(): int
54+
{
55+
return $this->highLowerBound;
56+
}
57+
}

0 commit comments

Comments
 (0)