Repository navigation
Expand file tree
/
Copy pathcoverage-guard.php
More file actions
56 lines (44 loc) · 1.85 KB
/
Copy pathcoverage-guard.php
File metadata and controls
56 lines (44 loc) · 1.85 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
<?php declare(strict_types = 1);
use ShipMonk\CoverageGuard\Config;
use ShipMonk\CoverageGuard\Excluder\IgnoreThrowNewExceptionLineExcluder;
use ShipMonk\CoverageGuard\Hierarchy\ClassMethodBlock;
use ShipMonk\CoverageGuard\Hierarchy\CodeBlock;
use ShipMonk\CoverageGuard\Rule\CoverageError;
use ShipMonk\CoverageGuard\Rule\CoverageRule;
use ShipMonk\CoverageGuard\Rule\InspectionContext;
$config = new Config();
$config->addRule(new class implements CoverageRule {
public function inspect(
CodeBlock $codeBlock,
InspectionContext $context,
): ?CoverageError
{
if (!$codeBlock instanceof ClassMethodBlock) {
return null;
}
$methodReflection = $context->getMethodReflection();
if ($methodReflection === null) {
return null; // e.g. anonymous class methods
}
$classReflection = $methodReflection->getDeclaringClass();
$coverage = $codeBlock->getCoveragePercentage();
// @api class methods to have at least 50% coverage
if ($this->isPublicApiClass($classReflection) && $codeBlock->getCoveragePercentage() < 50) {
return CoverageError::create("Method <bold>{$codeBlock->getMethodName()}</bold> of @api class has only $coverage% coverage, expected at least 50%.");
}
return null;
}
/**
* @param ReflectionClass<object> $classReflection
*/
private function isPublicApiClass(ReflectionClass $classReflection): bool
{
return $classReflection->getDocComment() !== false && str_contains($classReflection->getDocComment(), '@api');
}
});
$config->addExecutableLineExcluder(new IgnoreThrowNewExceptionLineExcluder([LogicException::class]));
$localConfig = __DIR__ . '/coverage-guard.local.php';
if (is_file($localConfig)) {
require $localConfig; // handy for $config->setEditorUrl()
}
return $config;