-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnforceCoverageForMethodsRule.php
More file actions
64 lines (54 loc) · 2.19 KB
/
Copy pathEnforceCoverageForMethodsRule.php
File metadata and controls
64 lines (54 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php declare(strict_types = 1);
namespace ShipMonk\CoverageGuard\Rule;
use LogicException;
use ShipMonk\CoverageGuard\Hierarchy\ClassMethodBlock;
use ShipMonk\CoverageGuard\Hierarchy\CodeBlock;
/**
* @api
*/
final class EnforceCoverageForMethodsRule implements CoverageRule
{
public function __construct(
private readonly int $requiredCoveragePercentage = 1,
private readonly int $minExecutableLines = 0,
private readonly ?int $minMethodChangePercentage = null,
)
{
if ($this->requiredCoveragePercentage < 0 || $this->requiredCoveragePercentage > 100) {
throw new LogicException('Minimal required coverage percentage must be between 0 and 100');
}
if ($this->minMethodChangePercentage !== null && ($this->minMethodChangePercentage < 0 || $this->minMethodChangePercentage > 100)) {
throw new LogicException('Minimal required method change percentage must be between 0 and 100');
}
if ($this->minExecutableLines < 0) {
throw new LogicException('Minimal required executable lines must be at least 0');
}
}
public function inspect(
CodeBlock $codeBlock,
InspectionContext $context,
): ?CoverageError
{
if (!$codeBlock instanceof ClassMethodBlock) {
return null;
}
if (
$this->minMethodChangePercentage !== null
&& $codeBlock->getChangePercentage() < $this->minMethodChangePercentage
) {
return null;
}
if (
$codeBlock->getExecutableLinesCount() >= $this->minExecutableLines
&& $codeBlock->getCoveragePercentage() < $this->requiredCoveragePercentage
) {
$className = $context->getClassName() ?? 'anonymous';
$methodName = $codeBlock->getMethodName();
$ref = "{$className}::{$methodName}";
$coverage = $codeBlock->getCoveragePercentage();
$currentString = $coverage === 0 ? 'no' : "only {$coverage}%";
return CoverageError::create("Method <bold>$ref</bold> has $currentString coverage, expected at least $this->requiredCoveragePercentage%.");
}
return null;
}
}