Passed
Push — master ( c6d4b5...1256de )
by Pierre
01:47
created

Reporter::getResults()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 5
ccs 4
cts 4
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\IReporter;
8
9
/**
10
 * Reporter is a coverage reporter
11
 *
12
 * @author Pierre Fromager <info@pier_infor.fr>
13
 * @version 1.0
14
 * @package PierInfor\Undercover
15
 */
16
class Reporter implements IReporter
17
{
18
19
    protected $results;
20
    protected $thresholds;
21
    protected $error;
22
23
    /**
24
     * constructor
25
     */
26
    public function __construct()
27
    {
28
    }
29
30
    /**
31
     * display report
32
     *
33
     * @param array $results
34
     * @param array $thresholds
35
     * @return void
36
     */
37 2
    public function report(array $results, array $thresholds): IReporter
38
    {
39 2
        $this->results = $results;
40 2
        $this->thresholds = $thresholds;
41 2
        if (!empty($this->getResults())) {
42 2
            echo $this->getReport();
43
        }
44 2
        return $this;
45
    }
46
47
    /**
48
     * return results
49
     *
50
     * @return array
51
     */
52 1
    protected function getResults(): array
53
    {
54 1
        return is_null($this->results)
55 1
            ? []
56 1
            : $this->results;
57
    }
58
59
    /**
60
     * return thresholds
61
     *
62
     * @return array
63
     */
64 1
    protected function getThresholds(): array
65
    {
66 1
        return is_null($this->thresholds)
67 1
            ? []
68 1
            : $this->thresholds;
69
    }
70
71
    /**
72
     * return report
73
     *
74
     * @return string
75
     */
76 1
    protected function getReport(): string
77
    {
78 1
        return sprintf(
79 1
            IReporter::REPORT_FORMAT,
80 1
            $this->getHeader(),
81 1
            $this->getBody(),
82 1
            $this->getFooter()
83
        );
84
        ;
85
    }
86
87
    /**
88
     * return header
89
     *
90
     * @return string
91
     */
92 1
    protected function getHeader(): string
93
    {
94 1
        return IReporter::HEADER;
95
    }
96
97
    /**
98
     * return body
99
     *
100
     * @return string
101
     */
102 1
    protected function getBody(): string
103
    {
104 1
        $body = '';
105 1
        foreach ($this->getResults() as $k => $v) {
106 1
            $threshold = $this->getThresholds()[$k];
107 1
            $body .= $this->getMsgLine(
108 1
                $k,
109
                $v,
110 1
                ($v >= $threshold)
111
            );
112
        }
113 1
        return $body;
114
    }
115
116
    /**
117
     * return footer
118
     *
119
     * @return string
120
     */
121 1
    protected function getFooter(): string
122
    {
123 1
        return IReporter::T_BEFORE
124 1
            . str_repeat('-', \strlen(IReporter::TITLE))
125 1
            . IReporter::T_AFTER;
126
    }
127
128
    /**
129
     * return formated msg line
130
     *
131
     * @param string $k
132
     * @param float $v
133
     * @return string
134
     */
135 1
    protected function getMsgLine(string $k, float $v, bool $valid): string
136
    {
137 1
        return sprintf(
138 1
            IReporter::MSG_FORMAT,
139 1
            ucfirst($k),
140
            $v,
141 1
            IReporter::_LIMIT,
142 1
            $this->thresholds[$k],
143 1
            $valid ? IReporter::_OK : IReporter::_KO
144
        );
145
    }
146
}
147