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 | * Constructor |
||
39 | * |
||
40 | * @param Election $election |
||
41 | */ |
||
42 | 1 | public function __construct(Election $election) |
|
48 | |||
49 | /** |
||
50 | * Run the election |
||
51 | * |
||
52 | * @return \MichaelC\Voting\STV\Candidate[] Winning candidates |
||
53 | */ |
||
54 | public function run() |
||
70 | |||
71 | /** |
||
72 | * Perform the initial vote allocation |
||
73 | * |
||
74 | * @return |
||
75 | */ |
||
76 | public function firstStep() |
||
85 | |||
86 | /** |
||
87 | * Check if any candidates have reached the quota and can be elected |
||
88 | * |
||
89 | * @param array $candidates Array of active candidates to check |
||
90 | * @return bool Whether any candidates were changed to elected |
||
91 | */ |
||
92 | protected function checkCandidates(array &$candidates): bool |
||
105 | |||
106 | /** |
||
107 | * Allocate the next votes from a Ballot |
||
108 | * |
||
109 | * @param Ballot $ballot The ballot to allocate the votes from |
||
110 | * @param float $multiplier Number to multiply the weight by (surplus) |
||
111 | * @param float $divisor The divisor of the weight (Total number of |
||
112 | * candidate votes) |
||
113 | * @return Ballot The same ballot passed in modified |
||
114 | */ |
||
115 | protected function allocateVotes(Ballot &$ballot, float $multiplier = 1.0, float $divisor = 1.0): Ballot |
||
128 | |||
129 | /** |
||
130 | * Transfer the votes from one candidate to other candidates |
||
131 | * |
||
132 | * @param float $surplus The number of surplus votes to transfer |
||
133 | * @param Candidate $candidate The candidate being elected to transfer |
||
134 | * the votes from |
||
135 | * @return |
||
136 | */ |
||
137 | protected function transferSurplusVotes(float $surplus, Candidate $candidate) |
||
152 | |||
153 | /** |
||
154 | * Transfer the votes from one eliminated candidate to other candidates |
||
155 | * |
||
156 | * @param Candidate $candidate Candidate being eliminated to transfer |
||
157 | * the votes from |
||
158 | * @return |
||
159 | */ |
||
160 | protected function transferEliminatedVotes(Candidate $candidate) |
||
174 | |||
175 | /** |
||
176 | * Elect a candidate after they've passed the threshold |
||
177 | * |
||
178 | * @param \Michaelc\Voting\STV\Candidate $candidate |
||
179 | * @return null |
||
180 | */ |
||
181 | protected function electCandidate(Candidate $candidate) |
||
199 | |||
200 | /** |
||
201 | * Eliminate the candidate(s) with the lowest number of votes |
||
202 | * and reallocated their votes |
||
203 | * |
||
204 | * @param array $candidates Array of active candidates |
||
205 | * @return int Number of candidates eliminated |
||
206 | */ |
||
207 | protected function eliminateCandidates(array &$candidates): int |
||
233 | |||
234 | /** |
||
235 | * Get the quota to win |
||
236 | * |
||
237 | * @return int |
||
238 | */ |
||
239 | 1 | public function getQuota(): int |
|
247 | } |
||
248 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: