Completed
Push — master ( 27f6d9...d341e0 )
by Michael
03:41
created

Ballot::getNextChoice()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
crap 6
1
<?php
2
3
namespace Michaelc\Voting\STV;
4
5
class Ballot
6
{
7
    /**
8
     * Ranking of candidates ids
9
     *
10
     * @var int[]
11
     */
12
    protected $ranking;
13
14
    /**
15
     * The current weighting or value of this person's vote
16
     *
17
     * @var float
18
     */
19
    protected $weight;
20
21
    /**
22
     * The current preference in use from this ballot
23
     *
24
     * @var int
25
     */
26
    protected $levelUsed;
27
28
    /**
29
     * Constructor
30
     *
31
     * @param int[] $ranking The ranking of candidates Key being ranking,
32
     *                       value being a candidate id. Zero-indexed (Key
33
     *                       0 for first choice)
34
     */
35 3
    public function __construct(array $ranking)
36
    {
37 3
        $this->weight = 1.0;
38 3
        $this->ranking = $ranking;
39 3
        $this->levelUsed = -1;
40 3
    }
41
42
    /**
43
     * Gets the Ranking of candidates ids.
44
     *
45
     * @return array
46
     */
47 1
    public function getRanking(): array
48
    {
49 1
        return $this->ranking;
50
    }
51
52
    /**
53
     * Sets the Ranking of candidates ids.
54
     *
55
     * @param array $ranking the ranking
56
     *
57
     * @return self
58
     */
59
    public function setRanking(array $ranking)
60
    {
61
        $this->ranking = $ranking;
62
63
        return $this;
64
    }
65
66
    /**
67
     * Gets the The current weighting or value of this person's vote.
68
     *
69
     * @return float
70
     */
71 1
    public function getWeight(): float
72
    {
73 1
        return $this->weight;
74
    }
75
76
    /**
77
     * Sets the The current weighting or value of this person's vote.
78
     *
79
     * @param  float    $weight    The weight
80
     * @return float    $weight    The inputted weight
81
     */
82
    public function setWeight(float $weight): float
83
    {
84
        $this->weight = $weight;
85
86
        return $weight;
87
    }
88
89
    /**
90
     * Gets the the current preference in use from this ballot.
91
     *
92
     * @return int
93
     */
94 1
    public function getLevelUsed(): int
95
    {
96 1
        return $this->levelUsed;
97
    }
98
99
    /**
100
     * Sets the the current preference in use from this ballot.
101
     *
102
     * @return int
103
     */
104
    public function incrementLevelUsed(): int
105
    {
106
        $this->levelUsed++;
107
108
        return $this->levelUSed;
0 ignored issues
show
Bug introduced by
The property levelUSed does not seem to exist. Did you mean levelUsed?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
109
    }
110
111
    public function getLastChoice()
112
    {
113
        $level = $levelUsed;
0 ignored issues
show
Bug introduced by
The variable $levelUsed does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
114
115
        if (empty($this->ranking[$level]))
116
        {
117
            return null;
118
        }
119
120
        return $this->ranking[$level];
121
    }
122
123
    public function getNextChoice()
124
    {
125
        $level = $levelUsed + 1;
0 ignored issues
show
Bug introduced by
The variable $levelUsed does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
126
127
        if (empty($this->ranking[$level]))
128
        {
129
            return null;
130
        }
131
132
        return $this->ranking[$level];
133
    }
134
}
135