johnnymast /
redbox-testsuite
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * Score.php |
||
| 5 | * |
||
| 6 | * This file will maintain the Test scores. |
||
| 7 | * |
||
| 8 | * PHP version 7.4 |
||
| 9 | * |
||
| 10 | * @category Core |
||
| 11 | * @package RedboxTestSuite |
||
| 12 | * @author Johnny Mast <[email protected]> |
||
| 13 | * @license https://opensource.org/licenses/MIT MIT |
||
| 14 | * @link https://github.com/johnnymast/redbox-testsuite |
||
| 15 | * @since 1.0 |
||
| 16 | */ |
||
| 17 | |||
| 18 | namespace Redbox\Testsuite; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Class Score |
||
| 22 | * |
||
| 23 | * @package Redbox\Testsuite |
||
| 24 | */ |
||
| 25 | class Score |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * The current score. |
||
| 29 | * |
||
| 30 | * @var mixed |
||
| 31 | */ |
||
| 32 | protected $score = 0; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * The number of increments this score went over. |
||
| 36 | * |
||
| 37 | * @var int |
||
| 38 | */ |
||
| 39 | private int $increments = 0; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Reference to the test the score belongs to. |
||
| 43 | * |
||
| 44 | * @var TestCase |
||
| 45 | */ |
||
| 46 | private TestCase $test; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Array containing score information. |
||
| 50 | * |
||
| 51 | * @var array |
||
| 52 | 32 | */ |
|
| 53 | private array $results = []; |
||
| 54 | 32 | ||
| 55 | 32 | /** |
|
| 56 | * Score constructor. |
||
| 57 | * |
||
| 58 | * @param TestCase $test The Test this score belongs to. |
||
| 59 | */ |
||
| 60 | public function __construct(TestCase $test) |
||
| 61 | { |
||
| 62 | $this->test = $test; |
||
| 63 | |||
| 64 | 12 | $this->reset(); |
|
| 65 | } |
||
| 66 | 12 | ||
| 67 | 12 | /** |
|
| 68 | 12 | * Reset the values to default. |
|
| 69 | * |
||
| 70 | * @return void |
||
| 71 | */ |
||
| 72 | public function reset(): void |
||
| 73 | { |
||
| 74 | $this->score = $this->minScore(); |
||
| 75 | $this->increments = 0; |
||
| 76 | 4 | $this->results = []; |
|
| 77 | } |
||
| 78 | 4 | ||
| 79 | /** |
||
| 80 | * Increment the score by $score amount. |
||
| 81 | * |
||
| 82 | * @param mixed $value The value to increment the score with (float/double or int). |
||
| 83 | * @param string $motivation A motivation for this score. |
||
| 84 | * @param string $answer Option to provide the given answer. |
||
| 85 | * |
||
| 86 | * @return void |
||
| 87 | 4 | */ |
|
| 88 | public function increment($value, $motivation = '', $answer = ''): void |
||
| 89 | 4 | { |
|
| 90 | $this->score += $value; |
||
| 91 | $this->results[$this->increments] = [ |
||
| 92 | 'score' => $value, |
||
| 93 | 'increment' => $this->increments, |
||
| 94 | 'motivation' => $motivation, |
||
| 95 | 'answer' => $answer, |
||
| 96 | ]; |
||
| 97 | $this->increments++; |
||
| 98 | 2 | } |
|
| 99 | |||
| 100 | 2 | /** |
|
| 101 | * Return the percentage the score is |
||
| 102 | * compared to the maximal score. |
||
| 103 | * |
||
| 104 | * @return float|int |
||
| 105 | */ |
||
| 106 | public function percentage() |
||
| 107 | { |
||
| 108 | 2 | return round(($this->getScore() / $this->maxScore()) * 100, 2); |
|
| 109 | } |
||
| 110 | 2 | ||
| 111 | /** |
||
| 112 | * Calculate the average for this |
||
| 113 | * score. |
||
| 114 | * |
||
| 115 | * @return float|int |
||
| 116 | */ |
||
| 117 | public function average() |
||
| 118 | 6 | { |
|
| 119 | return ($this->increments > 0) ? ($this->score / $this->increments) : false; |
||
|
0 ignored issues
–
show
|
|||
| 120 | 6 | } |
|
| 121 | |||
| 122 | /** |
||
| 123 | * Return the number of increments the |
||
| 124 | * score went over. |
||
| 125 | * |
||
| 126 | * @return int |
||
| 127 | */ |
||
| 128 | 22 | public function getIncrements(): int |
|
| 129 | { |
||
| 130 | 22 | return $this->increments; |
|
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Return information about the scores. |
||
| 135 | * |
||
| 136 | * @return array |
||
| 137 | */ |
||
| 138 | public function getScoreInfo(): array |
||
| 139 | { |
||
| 140 | 14 | return $this->results; |
|
| 141 | } |
||
| 142 | 14 | ||
| 143 | 14 | /** |
|
| 144 | * Return the minimal score. |
||
| 145 | * |
||
| 146 | * @return mixed |
||
| 147 | */ |
||
| 148 | public function minScore() |
||
| 149 | { |
||
| 150 | return $this->test->minScore(); |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Return the maximal score. |
||
| 155 | * |
||
| 156 | * @return mixed |
||
| 157 | */ |
||
| 158 | public function maxScore() |
||
| 159 | { |
||
| 160 | return $this->test->maxScore(); |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Return the current score. |
||
| 165 | * |
||
| 166 | * @return mixed |
||
| 167 | */ |
||
| 168 | public function getScore(): int |
||
| 169 | { |
||
| 170 | return $this->score; |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Return the parent class instance. |
||
| 175 | * |
||
| 176 | * @return TestCase |
||
| 177 | */ |
||
| 178 | public function getTest(): TestCase |
||
| 179 | { |
||
| 180 | return $this->test; |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Set to score to a given value. |
||
| 185 | * |
||
| 186 | * @param mixed $value The value to set as the score. |
||
| 187 | * |
||
| 188 | * @return void |
||
| 189 | */ |
||
| 190 | public function setScore($value): void |
||
| 191 | { |
||
| 192 | $this->score = $value; |
||
| 193 | } |
||
| 194 | } |
||
| 195 |
If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.