Passed
Pull Request — master (#51)
by
unknown
10:28
created

CodeCoverageRatioListener::simplifyRatio()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FriendsOfPhpSpec\PhpSpec\CodeCoverage\Listener;
6
7
use FriendsOfPhpSpec\PhpSpec\CodeCoverage\Exception\LowCoverageRatioException;
8
use PhpSpec\Event\SuiteEvent;
9
use SebastianBergmann\CodeCoverage\CodeCoverage;
10
use SebastianBergmann\CodeCoverage\ProcessedCodeCoverageData;
11
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
12
13
use function count;
14
15
final class CodeCoverageRatioListener implements EventSubscriberInterface
16
{
17
    /**
18
     * @var CodeCoverage
19
     */
20
    private $coverage;
21
22
    /**
23
     * @var float
24
     */
25
    private $minimumCoverage;
26
27
    /**
28
     * CodeCoverageRatioListener constructor.
29
     *
30
     * @param float $minimumCoverage
31
     */
32
    public function __construct(CodeCoverage $coverage, $minimumCoverage)
33
    {
34
        $this->coverage = $coverage;
35
        $this->minimumCoverage = (float) $minimumCoverage;
36
    }
37
38
    public function afterSuite(SuiteEvent $event): void
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

38
    public function afterSuite(/** @scrutinizer ignore-unused */ SuiteEvent $event): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
39
    {
40
        $actualCoverageRatio = $this->simplifyRatio($this->calculateRatio($this->coverage->getData()));
41
42
        if ($actualCoverageRatio < $this->minimumCoverage) {
43
            throw new LowCoverageRatioException(sprintf(
44
                'Test suites only cover %1.02f%% of the required %1.02f%% minimum coverage',
45
                $actualCoverageRatio,
46
                $this->minimumCoverage
47
            ));
48
        }
49
    }
50
51
    public static function getSubscribedEvents()
52
    {
53
        return [
54
            'afterSuite' => ['afterSuite', -1000],
55
        ];
56
    }
57
58
    /**
59
     * @return float
60
     */
61
    private function calculateRatio(ProcessedCodeCoverageData $coverageData)
62
    {
63
        return $this->calculateRatioForEachFiles($coverageData->lineCoverage());
64
    }
65
66
    /**
67
     * @param array<string, array> $lineCoverage
68
     * @param float $initialRatio
69
     *
70
     * @return float
71
     */
72
    private function calculateRatioForEachFiles(array $lineCoverage, $initialRatio = 0.0)
73
    {
74
        if ($lines = array_shift($lineCoverage)) {
75
            $ratio = $this->calculateRatioForEachFiles($lineCoverage, count(array_filter($lines)) / count($lines));
76
        }
77
78
        return $ratio ?? $initialRatio;
79
    }
80
81
    /**
82
     * @param float $ratio
83
     *
84
     * @return float
85
     */
86
    private function simplifyRatio($ratio)
87
    {
88
        return (ceil($ratio * 10000) / 10000) * 100;
89
    }
90
}
91