Passed
Push — master ( 7d6f7a...eb4fb1 )
by Pierre
02:15
created

Processor::getClassesRatio()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 8
ccs 6
cts 6
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PierInfor\Undercover;
6
7
use PierInfor\Undercover\Interfaces\IProcessor;
8
use PierInfor\Undercover\Interfaces\IArgs;
9
10
/**
11
 * Processor is a clover coverage processor
12
 *
13
 * @author Pierre Fromager <info@pier_infor.fr>
14
 * @version 1.0
15
 * @package PierInfor\Undercover
16
 */
17
class Processor implements IProcessor
18
{
19
20
    protected $results;
21
    protected $parser;
22
23
    /**
24
     * constructor
25
     */
26 9
    public function __construct()
27
    {
28 9
        $this->init();
29
    }
30
31
    /**
32
     * initializer
33
     *
34
     * @return IProcessor
35
     */
36 1
    protected function init(): IProcessor
37
    {
38 1
        $this->results = [];
39 1
        return $this;
40
    }
41
42
    /**
43
     * process datas from parser and return a processor instance
44
     *
45
     * @return IProcessor
46
     */
47 1
    public function process(int $coveredClasses, \SimpleXMLElement $metrics): IProcessor
48
    {
49 1
        $this->results = [
50 1
            IArgs::_LINES => $this->getElementsRatio($metrics),
51 1
            IArgs::_METHODS => $this->getMethodsRatio($metrics),
52 1
            IArgs::_STATEMENTS => $this->getStatementsRatio($metrics),
53 1
            IArgs::_CLASSES => $this->getClassesRatio(
54 1
                $coveredClasses,
55
                $metrics
56
            )
57
        ];
58 1
        return $this;
59
    }
60
61
    /**
62
     * return process results
63
     *
64
     * @return array
65
     */
66 1
    public function getResults(): array
67
    {
68 1
        return $this->results;
69
    }
70
71
    /**
72
     * returns lines coverage ratio as float
73
     *
74
     * @param \SimpleXMLElement $mets
75
     * @return float
76
     */
77 1
    protected function getElementsRatio(\SimpleXMLElement $mets): float
78
    {
79 1
        return isset($mets[self::_ELEMENTS])
80 1
            ? $this->getRatio(
81 1
                (float) $mets[self::COVERED_ELEMENTS],
82 1
                (float) $mets[self::_ELEMENTS]
83
            )
84 1
            : 0;
85
    }
86
87
    /**
88
     * returns methods coverage ratio as float
89
     *
90
     * @param \SimpleXMLElement $mets
91
     * @return float
92
     */
93 1
    protected function getMethodsRatio(\SimpleXMLElement $mets): float
94
    {
95 1
        return isset($mets[IArgs::_METHODS])
96 1
            ? $this->getRatio(
97 1
                (float) $mets[self::COVERED_METHODS],
98 1
                (float) $mets[IArgs::_METHODS]
99
            )
100 1
            : 0;
101
    }
102
103
    /**
104
     * returns statements coverage ratio as float
105
     *
106
     * @param \SimpleXMLElement $mets
107
     * @return float
108
     */
109 1
    protected function getStatementsRatio(\SimpleXMLElement $mets): float
110
    {
111 1
        return isset($mets[IArgs::_STATEMENTS])
112 1
            ? $this->getRatio(
113 1
                (float) $mets[self::COVERED_STATEMENTS],
114 1
                (float) $mets[IArgs::_STATEMENTS]
115
            )
116 1
            : 0;
117
    }
118
119
    /**
120
     * returns classes coverage ratio as float
121
     *
122
     * @param integer $coveredClasses
123
     * @param \SimpleXMLElement $mets
124
     * @return float
125
     */
126 1
    protected function getClassesRatio(int $coveredClasses, \SimpleXMLElement $mets): float
127
    {
128 1
        return isset($mets[IArgs::_CLASSES])
129 1
            ? $this->getRatio(
130 1
                (float) $coveredClasses,
131 1
                (float) $mets[IArgs::_CLASSES]
132
            )
133 1
            : 0;
134
    }
135
136
    /**
137
     * return ratio computation as float
138
     *
139
     * @param float $min
140
     * @param float $max
141
     * @return float
142
     */
143 1
    protected function getRatio(float $min, float $max): float
144
    {
145 1
        if ($max == 0) {
146 1
            $max = INF;
147
        }
148 1
        return ($min / $max) * 100;
149
    }
150
}
151