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() |
||
92 | |||
93 | /** |
||
94 | * Perform the initial vote allocation |
||
95 | * |
||
96 | * @return |
||
97 | */ |
||
98 | protected function firstStep() |
||
117 | |||
118 | /** |
||
119 | * Check if any candidates have reached the quota and can be elected |
||
120 | * |
||
121 | * @param array $candidates Array of active candidates to check |
||
122 | * @return bool Whether any candidates were changed to elected |
||
123 | */ |
||
124 | protected function checkCandidates(array $candidates): bool |
||
143 | |||
144 | /** |
||
145 | * Allocate the next votes from a Ballot |
||
146 | * |
||
147 | * @param Ballot $ballot The ballot to allocate the votes from |
||
148 | * @param float $multiplier Number to multiply the weight by (surplus) |
||
149 | * @param float $divisor The divisor of the weight (Total number of |
||
150 | * candidate votes) |
||
151 | * @return Ballot The same ballot passed in modified |
||
152 | */ |
||
153 | protected function allocateVotes(Ballot $ballot, float $multiplier = 1.0, float $divisor = 1.0): Ballot |
||
173 | |||
174 | /** |
||
175 | * Transfer the votes from one candidate to other candidates |
||
176 | * |
||
177 | * @param float $surplus The number of surplus votes to transfer |
||
178 | * @param Candidate $candidate The candidate being elected to transfer |
||
179 | * the votes from |
||
180 | * @return |
||
181 | */ |
||
182 | protected function transferSurplusVotes(float $surplus, Candidate $candidate) |
||
203 | |||
204 | /** |
||
205 | * Transfer the votes from one eliminated candidate to other candidates |
||
206 | * |
||
207 | * @param Candidate $candidate Candidate being eliminated to transfer |
||
208 | * the votes from |
||
209 | * @return |
||
210 | */ |
||
211 | protected function transferEliminatedVotes(Candidate $candidate) |
||
230 | |||
231 | /** |
||
232 | * Elect a candidate after they've passed the threshold |
||
233 | * |
||
234 | * @param \Michaelc\Voting\STV\Candidate $candidate |
||
235 | * @return null |
||
236 | */ |
||
237 | protected function electCandidate(Candidate $candidate) |
||
259 | |||
260 | /** |
||
261 | * Eliminate the candidate with the lowest number of votes |
||
262 | * and reallocated their votes |
||
263 | * |
||
264 | * TODO: Eliminate all lowest candidates after step one, then |
||
265 | * randomly choose. |
||
266 | * |
||
267 | * @param \Michaelc\Voting\STV\Candidate[] $candidates |
||
268 | * Array of active candidates |
||
269 | * @return int Number of candidates eliminated |
||
270 | */ |
||
271 | protected function eliminateCandidates(array $candidates): int |
||
287 | |||
288 | /** |
||
289 | * Get candidates with the lowest number of votes |
||
290 | * |
||
291 | * @param \Michaelc\Voting\STV\Candidate[] $candidates |
||
292 | * Array of active candidates |
||
293 | * @return \Michaelc\Voting\STV\Candidate[] |
||
294 | * Candidates with lowest score |
||
295 | */ |
||
296 | public function getLowestCandidates(array $candidates): array |
||
321 | |||
322 | /** |
||
323 | * Reject any invalid ballots |
||
324 | * |
||
325 | * @return int Number of rejected ballots |
||
326 | */ |
||
327 | protected function rejectInvalidBallots(): int |
||
346 | |||
347 | /** |
||
348 | * Check if ballot is valid |
||
349 | * |
||
350 | * @param Ballot $Ballot Ballot to test |
||
351 | * @return bool True if valid, false if invalid |
||
352 | */ |
||
353 | public function checkBallotValidity(Ballot $ballot): bool |
||
386 | |||
387 | /** |
||
388 | * Get the quota to win |
||
389 | * |
||
390 | * @return int |
||
391 | */ |
||
392 | public function getQuota(): int |
||
404 | } |
||
405 |
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..