Completed
Push — master ( 2bf777...8cb5a2 )
by Michael
03:38
created

Ballot   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 34.38%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 0
dl 0
loc 137
ccs 11
cts 32
cp 0.3438
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getRanking() 0 4 1
A setRanking() 0 6 1
A getWeight() 0 4 1
A setWeight() 0 6 1
A getLevelUsed() 0 4 1
A setLevelUsed() 0 6 1
A getLastChoice() 0 11 2
A getNextChoice() 0 11 2
A getNextChoiceWorth() 0 4 1
1
<?php
2
3
namespace Michaelc\Voting\STV;
4
5
use Michaelc\Voting\STV\Candidate;
6
7
class Ballot
8
{
9
    /**
10
     * Ranking of candidates ids
11
     *
12
     * @var array
13
     */
14
    protected $ranking;
15
16
    /**
17
     * The current weighting or value of this person's vote
18
     *
19
     * @var float
20
     */
21
    protected $weight;
22
23
    /**
24
     * The current preference in use from this ballot
25
     *
26
     * @var integer
27
     */
28
    protected $levelUsed;
29
30
    /**
31
     * Constructor
32
     *
33
     * @param array $ranking The ranking of candidates Key being ranking,
34
     *                       value being a candidate id
35
     */
36 3
    public function __construct(array $ranking)
37
    {
38 3
        $this->weight = 1.0;
39 3
        $this->ranking = $ranking;
40 3
        $this->levelUsed = -1;
41 3
    }
42
43
    /**
44
     * Gets the Ranking of candidates ids.
45
     *
46
     * @return array
47
     */
48 1
    public function getRanking(): array
49
    {
50 1
        return $this->ranking;
51
    }
52
53
    /**
54
     * Sets the Ranking of candidates ids.
55
     *
56
     * @param array $ranking the ranking
57
     *
58
     * @return self
59
     */
60
    public function setRanking(array $ranking)
61
    {
62
        $this->ranking = $ranking;
63
64
        return $this;
65
    }
66
67
    /**
68
     * Gets the The current weighting or value of this person's vote.
69
     *
70
     * @return float
71
     */
72 1
    public function getWeight(): float
73
    {
74 1
        return $this->weight;
75
    }
76
77
    /**
78
     * Sets the The current weighting or value of this person's vote.
79
     *
80
     * @param float $weight the weight
81
     *
82
     * @return self
83
     */
84
    public function setWeight(float $weight)
85
    {
86
        $this->weight = $weight;
87
88
        return $this;
89
    }
90
91
    /**
92
     * Gets the the current preference in use from this ballot.
93
     *
94
     * @return integer
95
     */
96 1
    public function getLevelUsed(): int
97
    {
98 1
        return $this->levelUsed;
99
    }
100
101
    /**
102
     * Sets the the current preference in use from this ballot.
103
     *
104
     * @param integer $levelUsed
105
     *
106
     * @return self
107
     */
108
    public function setLevelUsed(int $levelUsed)
109
    {
110
        $this->levelUsed = $levelUsed;
111
112
        return $this;
113
    }
114
115
    public function getLastChoice()
116
    {
117
        $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...
118
119
        if (empty($this->ranking[$level]))
120
        {
121
          return null;
122
        }
123
124
        return $this->ranking[$level];
125
    }
126
127
    public function getNextChoice()
128
    {
129
        $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...
130
131
        if (empty($this->ranking[$level]))
132
        {
133
          return null;
134
        }
135
136
        return $this->ranking[$level];
137
    }
138
139
    public function getNextChoiceWorth(): float
140
    {
141
        return 0.0;
142
    }
143
}
144