Skip to content

Commit

Permalink
Extract value object
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Feb 22, 2022
1 parent 2c3ab74 commit af906be
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 38 deletions.
33 changes: 10 additions & 23 deletions src/Report/Html/Facade.php
Expand Up @@ -16,7 +16,6 @@
use function str_ends_with;
use SebastianBergmann\CodeCoverage\CodeCoverage;
use SebastianBergmann\CodeCoverage\Directory as DirectoryUtil;
use SebastianBergmann\CodeCoverage\InvalidArgumentException;
use SebastianBergmann\CodeCoverage\Node\Directory as DirectoryNode;
use SebastianBergmann\Template\Template;

Expand All @@ -26,25 +25,16 @@ final class Facade

private string $generator;

private int $lowUpperBound;

private int $highLowerBound;

private Colors $colors;

public function __construct(int $lowUpperBound = 50, int $highLowerBound = 90, string $generator = '', ?Colors $colors = null)
{
if ($lowUpperBound > $highLowerBound) {
throw new InvalidArgumentException(
'$lowUpperBound must not be larger than $highLowerBound'
);
}
private Thresholds $thresholds;

$this->generator = $generator;
$this->highLowerBound = $highLowerBound;
$this->lowUpperBound = $lowUpperBound;
$this->colors = $colors ?? Colors::default();
$this->templatePath = __DIR__ . '/Renderer/Template/';
public function __construct(string $generator = '', ?Colors $colors = null, ?Thresholds $thresholds = null)
{
$this->generator = $generator;
$this->colors = $colors ?? Colors::default();
$this->thresholds = $thresholds ?? Thresholds::default();
$this->templatePath = __DIR__ . '/Renderer/Template/';
}

public function process(CodeCoverage $coverage, string $target): void
Expand All @@ -57,26 +47,23 @@ public function process(CodeCoverage $coverage, string $target): void
$this->templatePath,
$this->generator,
$date,
$this->lowUpperBound,
$this->highLowerBound,
$this->thresholds,
$coverage->collectsBranchAndPathCoverage()
);

$directory = new Directory(
$this->templatePath,
$this->generator,
$date,
$this->lowUpperBound,
$this->highLowerBound,
$this->thresholds,
$coverage->collectsBranchAndPathCoverage()
);

$file = new File(
$this->templatePath,
$this->generator,
$date,
$this->lowUpperBound,
$this->highLowerBound,
$this->thresholds,
$coverage->collectsBranchAndPathCoverage()
);

Expand Down
19 changes: 8 additions & 11 deletions src/Report/Html/Renderer.php
Expand Up @@ -32,21 +32,18 @@ abstract class Renderer

protected string $date;

protected int $lowUpperBound;

protected int $highLowerBound;
protected Thresholds $thresholds;

protected bool $hasBranchCoverage;

protected string $version;

public function __construct(string $templatePath, string $generator, string $date, int $lowUpperBound, int $highLowerBound, bool $hasBranchCoverage)
public function __construct(string $templatePath, string $generator, string $date, Thresholds $thresholds, bool $hasBranchCoverage)
{
$this->templatePath = $templatePath;
$this->generator = $generator;
$this->date = $date;
$this->lowUpperBound = $lowUpperBound;
$this->highLowerBound = $highLowerBound;
$this->thresholds = $thresholds;
$this->version = Version::id();
$this->hasBranchCoverage = $hasBranchCoverage;
}
Expand Down Expand Up @@ -178,8 +175,8 @@ protected function setCommonTemplateVariables(Template $template, AbstractNode $
'version' => $this->version,
'runtime' => $this->runtimeString(),
'generator' => $this->generator,
'low_upper_bound' => $this->lowUpperBound,
'high_lower_bound' => $this->highLowerBound,
'low_upper_bound' => $this->thresholds->lowUpperBound(),
'high_lower_bound' => $this->thresholds->highLowerBound(),
]
);
}
Expand Down Expand Up @@ -267,12 +264,12 @@ protected function coverageBar(float $percent): string

protected function colorLevel(float $percent): string
{
if ($percent <= $this->lowUpperBound) {
if ($percent <= $this->thresholds->lowUpperBound()) {
return 'danger';
}

if ($percent > $this->lowUpperBound &&
$percent < $this->highLowerBound) {
if ($percent > $this->thresholds->lowUpperBound() &&
$percent < $this->thresholds->highLowerBound()) {
return 'warning';
}

Expand Down
8 changes: 4 additions & 4 deletions src/Report/Html/Renderer/Dashboard.php
Expand Up @@ -188,7 +188,7 @@ private function insufficientCoverage(array $classes, string $baseLink): array

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

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

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

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

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

if ($class['coverage'] < $this->highLowerBound &&
if ($class['coverage'] < $this->thresholds->highLowerBound() &&
$class['ccn'] > count($class['methods'])) {
$classRisks[$className] = $class['crap'];
}
Expand Down
57 changes: 57 additions & 0 deletions src/Report/Html/Thresholds.php
@@ -0,0 +1,57 @@
<?php declare(strict_types=1);
/*
* This file is part of phpunit/php-code-coverage.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\CodeCoverage\Report\Html;

use SebastianBergmann\CodeCoverage\InvalidArgumentException;

/**
* @psalm-immutable
*/
final class Thresholds
{
private int $lowUpperBound;

private int $highLowerBound;

public static function default(): self
{
return new self(50, 90);
}

/**
* @throws InvalidArgumentException
*/
public static function from(int $lowUpperBound, int $highLowerBound): self
{
if ($lowUpperBound > $highLowerBound) {
throw new InvalidArgumentException(
'$lowUpperBound must not be larger than $highLowerBound'
);
}

return new self($lowUpperBound, $highLowerBound);
}

private function __construct(int $lowUpperBound, int $highLowerBound)
{
$this->lowUpperBound = $lowUpperBound;
$this->highLowerBound = $highLowerBound;
}

public function lowUpperBound(): int
{
return $this->lowUpperBound;
}

public function highLowerBound(): int
{
return $this->highLowerBound;
}
}

0 comments on commit af906be

Please sign in to comment.