Passed
Branch master (3fa968)
by Johnny
03:32 queued 01:42
created

Score::getScore()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
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 14
class Score
25
{
26 14
    /**
27 14
     * The current score.
28
     *
29
     * @var mixed
30
     */
31
    protected $score = 0;
32 4
    
33
    /**
34 4
     * The number of increments this score went over.
35 4
     *
36 4
     * @var int
37
     */
38
    private int $increments = 0;
39
    
40
    /**
41
     * Reference to the test the score belongs to.
42
     *
43
     * @var Test
44 2
     */
45
    private Test $test;
46 2
    
47
    /**
48
     * Score constructor.
49
     *
50
     * @param Test $test The Test this score belongs to.
51
     */
52
    public function __construct(Test $test)
53
    {
54
        $this->test = $test;
55 2
    }
56
    
57 2
    /**
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
    public function increment($value)
65
    {
66
        $this->score += $value;
67
        $this->increments++;
68
    }
69
    
70
    /**
71
     * Return the percentage the score is
72
     * compared to the maximal score.
73
     *
74
     * @return float|int
75
     */
76 1
    public function percentage()
77
    {
78 1
        return round(($this->maxScore() / 100) * $this->score, 2);
79
    }
80
    
81
    /**
82
     * Calculate the average for this
83
     * score.
84
     *
85
     * @return float|int
86 3
     */
87
    public function average()
88 3
    {
89
        return ($this->increments > 0) ? ($this->score / $this->increments) : false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->increments...his->increments : false could also return false which is incompatible with the documented return type double|integer. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
90
    }
91
    
92
    /**
93
     * Return the number of increments the
94 8
     * score went over.
95
     *
96 8
     * @return int
97
     */
98
    public function getIncrements()
99
    {
100
        return $this->increments;
101
    }
102 6
    
103
    /**
104 6
     * Return the minimal score.
105 6
     *
106
     * @return int
107
     */
108
    public function minScore()
109
    {
110
        return $this->test->minScore();
111
    }
112
    
113
    /**
114
     * Return the maximal score.
115
     *
116
     * @return int
117
     */
118
    public function maxScore()
119
    {
120
        return $this->test->maxScore();
121
    }
122
    
123
    /**
124
     * Return the current score.
125
     *
126
     * @return int
127
     */
128
    public function getScore()
129
    {
130
        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
    public function setScore($value = 0)
141
    {
142
        $this->score = $value;
143
    }
144
}
145