1 | <?php |
||
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) |
|
54 | |||
55 | /** |
||
56 | * Run the election |
||
57 | * |
||
58 | * @return \MichaelC\Voting\STV\Candidate[] Winning candidates |
||
59 | */ |
||
60 | public function run() |
||
78 | |||
79 | /** |
||
80 | * Perform the initial vote allocation |
||
81 | * |
||
82 | * @return |
||
83 | */ |
||
84 | protected function firstStep() |
||
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 |
||
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 |
||
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) |
||
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) |
||
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) |
||
207 | |||
208 | /** |
||
209 | * Eliminate the candidate(s) with the lowest number of votes |
||
210 | * and reallocated their votes |
||
211 | * |
||
212 | * @param array $candidates Array of active candidates |
||
213 | * @return int Number of candidates eliminated |
||
214 | */ |
||
215 | protected function eliminateCandidates(array &$candidates): int |
||
242 | |||
243 | /** |
||
244 | * Reject any invalid ballots |
||
245 | * |
||
246 | * @return int Number of rejected ballots |
||
247 | */ |
||
248 | protected function rejectInvalidBallots(): int |
||
261 | |||
262 | /** |
||
263 | * Check if ballot is valid |
||
264 | * |
||
265 | * @param Ballot &$Ballot Ballot to test |
||
266 | * @return bool True if valid, false if invalid |
||
267 | */ |
||
268 | private function checkBallotValidity(Ballot &$Ballot): bool |
||
289 | |||
290 | /** |
||
291 | * Get the quota to win |
||
292 | * |
||
293 | * @return int |
||
294 | */ |
||
295 | 1 | public function getQuota(): int |
|
303 | } |
||
304 |
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: