1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Score.php |
4
|
|
|
* |
5
|
|
|
* This file will maintain the Test scores. |
6
|
|
|
* |
7
|
|
|
* PHP version 7.4 |
8
|
|
|
* |
9
|
|
|
* @category Core |
10
|
|
|
* @package RedboxTestSuite |
11
|
|
|
* @author Johnny Mast <[email protected]> |
12
|
|
|
* @license https://opensource.org/licenses/MIT MIT |
13
|
|
|
* @link https://github.com/johnnymast/redbox-testsuite |
14
|
|
|
* @since GIT:1.0 |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace Redbox\Testsuite; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class Score |
21
|
|
|
* |
22
|
|
|
* @package Redbox\Testsuite |
23
|
|
|
*/ |
24
|
|
|
class Score |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* The current score. |
28
|
|
|
* |
29
|
|
|
* @var mixed |
30
|
|
|
*/ |
31
|
|
|
protected $score = 0; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The number of increments this score went over. |
35
|
|
|
* |
36
|
|
|
* @var int |
37
|
|
|
*/ |
38
|
|
|
private int $increments = 0; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Reference to the test the score belongs to. |
42
|
|
|
* |
43
|
|
|
* @var Test |
44
|
|
|
*/ |
45
|
|
|
private Test $test; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Score constructor. |
49
|
|
|
* |
50
|
|
|
* @param Test $test The Test this score belongs to. |
51
|
|
|
*/ |
52
|
32 |
|
public function __construct(Test $test) |
53
|
|
|
{ |
54
|
32 |
|
$this->test = $test; |
55
|
32 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Increment the score by $score amount. |
59
|
|
|
* |
60
|
|
|
* @param mixed $value The value to increment the score with (float/double or int). |
61
|
|
|
* |
62
|
|
|
* @return void |
63
|
|
|
*/ |
64
|
12 |
|
public function increment($value) |
65
|
|
|
{ |
66
|
12 |
|
$this->score += $value; |
67
|
12 |
|
$this->increments++; |
68
|
12 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Return the percentage the score is |
72
|
|
|
* compared to the maximal score. |
73
|
|
|
* |
74
|
|
|
* @return float|int |
75
|
|
|
*/ |
76
|
4 |
|
public function percentage() |
77
|
|
|
{ |
78
|
4 |
|
return round(($this->getScore() / $this->maxScore()) * 100, 2); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Calculate the average for this |
83
|
|
|
* score. |
84
|
|
|
* |
85
|
|
|
* @return float|int |
86
|
|
|
*/ |
87
|
4 |
|
public function average() |
88
|
|
|
{ |
89
|
4 |
|
return ($this->increments > 0) ? ($this->score / $this->increments) : false; |
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Return the number of increments the |
94
|
|
|
* score went over. |
95
|
|
|
* |
96
|
|
|
* @return int |
97
|
|
|
*/ |
98
|
2 |
|
public function getIncrements() |
99
|
|
|
{ |
100
|
2 |
|
return $this->increments; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Return the minimal score. |
105
|
|
|
* |
106
|
|
|
* @return mixed |
107
|
|
|
*/ |
108
|
2 |
|
public function minScore() |
109
|
|
|
{ |
110
|
2 |
|
return $this->test->minScore(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Return the maximal score. |
115
|
|
|
* |
116
|
|
|
* @return mixed |
117
|
|
|
*/ |
118
|
6 |
|
public function maxScore() |
119
|
|
|
{ |
120
|
6 |
|
return $this->test->maxScore(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Return the current score. |
125
|
|
|
* |
126
|
|
|
* @return mixed |
127
|
|
|
*/ |
128
|
22 |
|
public function getScore() |
129
|
|
|
{ |
130
|
22 |
|
return $this->score; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Set to score to a given value. |
135
|
|
|
* |
136
|
|
|
* @param int $value The value to set as the score. |
137
|
|
|
* |
138
|
|
|
* @return void |
139
|
|
|
*/ |
140
|
14 |
|
public function setScore($value) |
141
|
|
|
{ |
142
|
14 |
|
$this->score = $value; |
143
|
14 |
|
} |
144
|
|
|
} |
145
|
|
|
|
If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.