1 | <?php |
||
7 | class VoteHandler |
||
8 | { |
||
9 | /** |
||
10 | * Election object. |
||
11 | * |
||
12 | * @var \Michaelc\Voting\STV\Election; |
||
13 | */ |
||
14 | protected $election; |
||
15 | |||
16 | /** |
||
17 | * Array of all ballots in election. |
||
18 | * |
||
19 | * @var \MichaelC\Voting\STV\Ballot[] |
||
20 | */ |
||
21 | protected $ballots; |
||
22 | |||
23 | /** |
||
24 | * Quota of votes needed for a candidate to be elected. |
||
25 | * |
||
26 | * @var int |
||
27 | */ |
||
28 | protected $quota; |
||
29 | |||
30 | /** |
||
31 | * Number of candidates elected so far. |
||
32 | * |
||
33 | * @var int |
||
34 | */ |
||
35 | protected $electedCandidates; |
||
36 | |||
37 | /** |
||
38 | * Invalid ballots. |
||
39 | * |
||
40 | * @var \MichaelC\Voting\STV\Ballot[] |
||
41 | */ |
||
42 | protected $rejectedBallots; |
||
43 | |||
44 | /** |
||
45 | * Logger. |
||
46 | * |
||
47 | * @var Psr\Log\LoggerInterface |
||
48 | */ |
||
49 | protected $logger; |
||
50 | |||
51 | /** |
||
52 | * Constructor. |
||
53 | * |
||
54 | * @param Election $election |
||
55 | */ |
||
56 | 1 | public function __construct(Election $election, Logger $logger) |
|
63 | |||
64 | /** |
||
65 | * Run the election. |
||
66 | * |
||
67 | * @return \MichaelC\Voting\STV\Candidate[] Winning candidates |
||
68 | */ |
||
69 | public function run() |
||
90 | |||
91 | /** |
||
92 | * Perform the initial vote allocation. |
||
93 | * |
||
94 | * @return |
||
95 | */ |
||
96 | protected function firstStep() |
||
114 | |||
115 | /** |
||
116 | * Check if any candidates have reached the quota and can be elected. |
||
117 | * |
||
118 | * @param array $candidates Array of active candidates to check |
||
119 | * |
||
120 | * @return bool Whether any candidates were changed to elected |
||
121 | */ |
||
122 | protected function checkCandidates(array $candidates): bool |
||
139 | |||
140 | /** |
||
141 | * Allocate the next votes from a Ballot. |
||
142 | * |
||
143 | * @param Ballot $ballot The ballot to allocate the votes from |
||
144 | * @param float $multiplier Number to multiply the weight by (surplus) |
||
145 | * @param float $divisor The divisor of the weight (Total number of |
||
146 | * candidate votes) |
||
147 | * |
||
148 | * @return Ballot The same ballot passed in modified |
||
149 | */ |
||
150 | protected function allocateVotes(Ballot $ballot, float $multiplier = 1.0, float $divisor = 1.0): Ballot |
||
169 | |||
170 | /** |
||
171 | * Transfer the votes from one candidate to other candidates. |
||
172 | * |
||
173 | * @param float $surplus The number of surplus votes to transfer |
||
174 | * @param Candidate $candidate The candidate being elected to transfer |
||
175 | * the votes from |
||
176 | * |
||
177 | * @return |
||
178 | */ |
||
179 | protected function transferSurplusVotes(float $surplus, Candidate $candidate) |
||
198 | |||
199 | /** |
||
200 | * Transfer the votes from one eliminated candidate to other candidates. |
||
201 | * |
||
202 | * @param Candidate $candidate Candidate being eliminated to transfer |
||
203 | * the votes from |
||
204 | * |
||
205 | * @return |
||
206 | */ |
||
207 | protected function transferEliminatedVotes(Candidate $candidate) |
||
224 | |||
225 | /** |
||
226 | * Elect a candidate after they've passed the threshold. |
||
227 | * |
||
228 | * @param \Michaelc\Voting\STV\Candidate $candidate |
||
229 | */ |
||
230 | protected function electCandidate(Candidate $candidate) |
||
250 | |||
251 | /** |
||
252 | * Eliminate the candidate with the lowest number of votes |
||
253 | * and reallocated their votes. |
||
254 | * |
||
255 | * TODO: Eliminate all lowest candidates after step one, then |
||
256 | * randomly choose. |
||
257 | * |
||
258 | * @param \Michaelc\Voting\STV\Candidate[] $candidates |
||
259 | * Array of active candidates |
||
260 | * |
||
261 | * @return int Number of candidates eliminated |
||
262 | */ |
||
263 | protected function eliminateCandidates(array $candidates): int |
||
278 | |||
279 | /** |
||
280 | * Get candidates with the lowest number of votes. |
||
281 | * |
||
282 | * @param \Michaelc\Voting\STV\Candidate[] $candidates |
||
283 | * Array of active candidates |
||
284 | * |
||
285 | * @return \Michaelc\Voting\STV\Candidate[] |
||
286 | * Candidates with lowest score |
||
287 | */ |
||
288 | public function getLowestCandidates(array $candidates): array |
||
309 | |||
310 | /** |
||
311 | * Reject any invalid ballots. |
||
312 | * |
||
313 | * @return int Number of rejected ballots |
||
314 | */ |
||
315 | protected function rejectInvalidBallots(): int |
||
332 | |||
333 | /** |
||
334 | * Check if ballot is valid. |
||
335 | * |
||
336 | * @param Ballot $Ballot Ballot to test |
||
337 | * |
||
338 | * @return bool True if valid, false if invalid |
||
339 | */ |
||
340 | public function checkBallotValidity(Ballot $ballot): bool |
||
368 | |||
369 | /** |
||
370 | * Get the quota to win. |
||
371 | * |
||
372 | * @return int |
||
373 | */ |
||
374 | public function getQuota(): int |
||
386 | } |
||
387 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..