1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Michaelc\Voting\STV; |
4
|
|
|
|
5
|
|
|
class VoteHandler |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Election object |
9
|
|
|
* |
10
|
|
|
* @var \Michaelc\Voting\STV\Election; |
11
|
|
|
*/ |
12
|
|
|
protected $election; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Array of all ballots in election |
16
|
|
|
* |
17
|
|
|
* @var \MichaelC\Voting\STV\Ballot[] |
18
|
|
|
*/ |
19
|
|
|
protected $ballots; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Quota of votes needed for a candidate to be elected |
23
|
|
|
* |
24
|
|
|
* @var int |
25
|
|
|
*/ |
26
|
|
|
protected $quota; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Number of candidates elected so far |
30
|
|
|
* |
31
|
|
|
* @var int |
32
|
|
|
*/ |
33
|
|
|
protected $electedCandidates; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Invalid ballots |
37
|
|
|
* |
38
|
|
|
* @var \MichaelC\Voting\STV\Ballot[] |
39
|
|
|
*/ |
40
|
|
|
protected $rejectedBallots; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Constructor |
44
|
|
|
* |
45
|
|
|
* @param Election $election |
46
|
|
|
*/ |
47
|
1 |
|
public function __construct(Election $election) |
48
|
|
|
{ |
49
|
1 |
|
$this->election = $election; |
50
|
1 |
|
$this->ballots = $this->election->getBallots(); |
51
|
1 |
|
$this->quota = $this->getQuota(); |
52
|
1 |
|
$this->rejectedBallots = []; |
53
|
1 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Run the election |
57
|
|
|
* |
58
|
|
|
* @return \MichaelC\Voting\STV\Candidate[] Winning candidates |
59
|
|
|
*/ |
60
|
|
|
public function run() |
61
|
|
|
{ |
62
|
|
|
$this->rejectInvalidBallots(); |
63
|
|
|
|
64
|
|
|
$this->firstStep(); |
65
|
|
|
|
66
|
|
|
$candidates = $this->election->getActiveCandidates(); |
67
|
|
|
|
68
|
|
|
while ($this->electedCandidates < $this->election->getWinnersCount()) |
69
|
|
|
{ |
70
|
|
|
if (!$this->checkCandidates($candidates)) |
71
|
|
|
{ |
72
|
|
|
$this->eliminateCandidates($candidates); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $this->election->getElectedCandidates(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Perform the initial vote allocation |
81
|
|
|
* |
82
|
|
|
* @return |
83
|
|
|
*/ |
84
|
|
|
protected function firstStep() |
85
|
|
|
{ |
86
|
|
|
foreach ($this->ballots as $i => $ballot) |
87
|
|
|
{ |
88
|
|
|
$this->allocateVotes($ballot); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Check if any candidates have reached the quota and can be elected |
96
|
|
|
* |
97
|
|
|
* @param array $candidates Array of active candidates to check |
98
|
|
|
* @return bool Whether any candidates were changed to elected |
99
|
|
|
*/ |
100
|
|
|
protected function checkCandidates(array &$candidates): bool |
101
|
|
|
{ |
102
|
|
|
foreach ($candidates as $i => $candidate) |
103
|
|
|
{ |
104
|
|
|
if ($candidate->getVotes() >= $this->quota) |
105
|
|
|
{ |
106
|
|
|
$this->electCandidate($candidate); |
107
|
|
|
$elected = true; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return ($elected ?? false); |
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Allocate the next votes from a Ballot |
116
|
|
|
* |
117
|
|
|
* @param Ballot $ballot The ballot to allocate the votes from |
118
|
|
|
* @param float $multiplier Number to multiply the weight by (surplus) |
119
|
|
|
* @param float $divisor The divisor of the weight (Total number of |
120
|
|
|
* candidate votes) |
121
|
|
|
* @return Ballot The same ballot passed in modified |
122
|
|
|
*/ |
123
|
|
|
protected function allocateVotes(Ballot &$ballot, float $multiplier = 1.0, float $divisor = 1.0): Ballot |
124
|
|
|
{ |
125
|
|
|
$weight = $ballot->setWeight(($ballot->getWeight() * $multiplier) / $divisor); |
126
|
|
|
$candidate = $ballot->getNextChoice(); |
127
|
|
|
|
128
|
|
|
if ($candidate !== null) |
129
|
|
|
{ |
130
|
|
|
$this->election->getCandidate($candidate)->addVotes($weight); |
131
|
|
|
$ballot->incrementLevelUsed(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $ballot; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Transfer the votes from one candidate to other candidates |
139
|
|
|
* |
140
|
|
|
* @param float $surplus The number of surplus votes to transfer |
141
|
|
|
* @param Candidate $candidate The candidate being elected to transfer |
142
|
|
|
* the votes from |
143
|
|
|
* @return |
144
|
|
|
*/ |
145
|
|
|
protected function transferSurplusVotes(float $surplus, Candidate $candidate) |
146
|
|
|
{ |
147
|
|
|
$totalVotes = $candidate->getVotes(); |
148
|
|
|
$candidateId = $candidate->getId(); |
149
|
|
|
|
150
|
|
|
foreach ($this->ballots as $i => $ballot) |
151
|
|
|
{ |
152
|
|
|
if ($ballot->getLastChoice() == $candidateId) |
153
|
|
|
{ |
154
|
|
|
$this->allocateVotes($ballot, $surplus, $totalVotes); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Transfer the votes from one eliminated candidate to other candidates |
163
|
|
|
* |
164
|
|
|
* @param Candidate $candidate Candidate being eliminated to transfer |
165
|
|
|
* the votes from |
166
|
|
|
* @return |
167
|
|
|
*/ |
168
|
|
|
protected function transferEliminatedVotes(Candidate $candidate) |
169
|
|
|
{ |
170
|
|
|
$candidateId = $candidate->getId(); |
171
|
|
|
|
172
|
|
|
foreach ($this->ballots as $i => $ballot) |
173
|
|
|
{ |
174
|
|
|
if ($ballot->getLastChoice() == $candidateId) |
175
|
|
|
{ |
176
|
|
|
$this->allocateVotes($ballot); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Elect a candidate after they've passed the threshold |
185
|
|
|
* |
186
|
|
|
* @param \Michaelc\Voting\STV\Candidate $candidate |
187
|
|
|
* @return null |
188
|
|
|
*/ |
189
|
|
|
protected function electCandidate(Candidate $candidate) |
190
|
|
|
{ |
191
|
|
|
if ($candidate->getVotes() < $this->quota) |
192
|
|
|
{ |
193
|
|
|
throw new Exception("We shouldn't be electing someone who hasn't met the quota"); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
$candidate->setState(Candidate::ELECTED); |
197
|
|
|
$this->electedCandidates++; |
198
|
|
|
|
199
|
|
|
if ($this->electedCandidates < $this->election->getWinnersCount()) |
200
|
|
|
{ |
201
|
|
|
$surplus = $candidate->getVotes() - $this->quota; |
202
|
|
|
$this->transferSurplusVotes($surplus, $candidate); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
return; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Eliminate the candidate(s) with the lowest number of votes |
210
|
|
|
* and reallocated their votes |
211
|
|
|
* |
212
|
|
|
* @param \Michaelc\Voting\STV\Candidate[] $candidates |
213
|
|
|
* Array of active candidates |
214
|
|
|
* @return int Number of candidates eliminated |
215
|
|
|
*/ |
216
|
|
|
protected function eliminateCandidates(array &$candidates): int |
217
|
|
|
{ |
218
|
|
|
$minimumCandidates = $this->getLowestCandidates($candidates); |
219
|
|
|
|
220
|
|
|
foreach ($minimumCandidates as $minimumCandidate) |
221
|
|
|
{ |
222
|
|
|
$this->transferEliminatedVotes($minimumCandidate); |
223
|
|
|
$minimumCandidate->setState(Candidate::DEFEATED); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
return count($minimumCandidates); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Get candidates with the lowest number of votes |
231
|
|
|
* |
232
|
|
|
* @param \Michaelc\Voting\STV\Candidate[] $candidates |
233
|
|
|
* Array of active candidates |
234
|
|
|
* @return \Michaelc\Voting\STV\Candidate[] |
235
|
|
|
* Candidates with lowest score |
236
|
|
|
*/ |
237
|
|
|
protected function getLowestCandidates(array $candidates): array |
238
|
|
|
{ |
239
|
|
|
$minimum = 0; |
240
|
|
|
$minimumCandidates = []; |
241
|
|
|
|
242
|
|
|
foreach ($candidates as $i => $candidate) |
243
|
|
|
{ |
244
|
|
|
if ($candidate->getVotes() > $minimum) |
245
|
|
|
{ |
246
|
|
|
$minimum = $candidate->getVotes(); |
247
|
|
|
unset($minimumCandidates); |
248
|
|
|
$minimumCandidates[] = $candidate; |
249
|
|
|
} |
250
|
|
|
elseif ($candidate->getVotes() == $minimum) |
251
|
|
|
{ |
252
|
|
|
$minimumCandidates[] = $candidate; |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
return $minimumCandidates; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Reject any invalid ballots |
261
|
|
|
* |
262
|
|
|
* @return int Number of rejected ballots |
263
|
|
|
*/ |
264
|
|
|
protected function rejectInvalidBallots(): int |
265
|
|
|
{ |
266
|
|
|
foreach ($this->ballots as $i => $ballot) |
267
|
|
|
{ |
268
|
|
|
if (!$this->checkBallotValidity($ballot)) |
269
|
|
|
{ |
270
|
|
|
$this->rejectedBallots[] = clone $ballot; |
271
|
|
|
unset($this->ballots[$i]); |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
return count($this->rejectedBallots); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Check if ballot is valid |
280
|
|
|
* |
281
|
|
|
* @param Ballot &$Ballot Ballot to test |
282
|
|
|
* @return bool True if valid, false if invalid |
283
|
|
|
*/ |
284
|
|
|
private function checkBallotValidity(Ballot &$Ballot): bool |
|
|
|
|
285
|
|
|
{ |
286
|
|
|
if (count($ballot->getRanking()) > $this->election->getCandidateCount()) |
|
|
|
|
287
|
|
|
{ |
288
|
|
|
return false; |
289
|
|
|
} |
290
|
|
|
else |
291
|
|
|
{ |
292
|
|
|
$candidateIds = $this->election->getCandidateIds(); |
293
|
|
|
|
294
|
|
|
foreach ($ballot->getRanking() as $i => $candidate) |
|
|
|
|
295
|
|
|
{ |
296
|
|
|
if (!in_array($candidate, $candidateIds)) |
297
|
|
|
{ |
298
|
|
|
return false; |
299
|
|
|
} |
300
|
|
|
} |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
return true; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* Get the quota to win |
308
|
|
|
* |
309
|
|
|
* @return int |
310
|
|
|
*/ |
311
|
1 |
|
public function getQuota(): int |
312
|
|
|
{ |
313
|
1 |
|
return floor( |
314
|
1 |
|
($this->election->getNumBallots() / |
315
|
1 |
|
($this->election->getWinnersCount() + 1) |
316
|
|
|
) |
317
|
1 |
|
+ 1); |
318
|
|
|
} |
319
|
|
|
} |
320
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: