Complex classes like ElectionRunner often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ElectionRunner, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class ElectionRunner |
||
17 | { |
||
18 | /** |
||
19 | * Logger. |
||
20 | * |
||
21 | * @var \Psr\Log\LoggerInterface |
||
22 | */ |
||
23 | protected $logger; |
||
24 | |||
25 | /** |
||
26 | * Election object. |
||
27 | * |
||
28 | * @var \Michaelc\Voting\STV\Election; |
||
29 | */ |
||
30 | protected $election; |
||
31 | |||
32 | /** |
||
33 | * Array of all ballots in election. |
||
34 | * |
||
35 | * @var \MichaelC\Voting\STV\Ballot[] |
||
36 | */ |
||
37 | protected $ballots; |
||
38 | |||
39 | /** |
||
40 | * Quota of votes needed for a candidate to be elected. |
||
41 | * |
||
42 | * @var int |
||
43 | */ |
||
44 | protected $quota; |
||
45 | |||
46 | /** |
||
47 | * Number of candidates elected so far. |
||
48 | * |
||
49 | * @var int |
||
50 | */ |
||
51 | protected $electedCandidates; |
||
52 | |||
53 | /** |
||
54 | * Invalid ballots. |
||
55 | * |
||
56 | * @var \MichaelC\Voting\STV\Ballot[] |
||
57 | */ |
||
58 | protected $rejectedBallots; |
||
59 | |||
60 | /** |
||
61 | * Number of valid ballots |
||
62 | * |
||
63 | * @var int |
||
64 | */ |
||
65 | protected $validBallots; |
||
66 | |||
67 | /** |
||
68 | * Number of winners to still be elected (at current stage) |
||
69 | * |
||
70 | * @var int |
||
71 | */ |
||
72 | protected $candidatesToElect; |
||
73 | |||
74 | /** |
||
75 | * Constructor. |
||
76 | * |
||
77 | * @param Election $election |
||
78 | */ |
||
79 | 2 | public function __construct(Election $election, Logger $logger) |
|
89 | |||
90 | /** |
||
91 | * Run the election. |
||
92 | * |
||
93 | * @return Candidate[] Winning candidates |
||
94 | */ |
||
95 | 1 | public function run() |
|
123 | |||
124 | /** |
||
125 | * Perform the initial vote allocation. |
||
126 | * p. 46 |
||
127 | * |
||
128 | * @return |
||
129 | */ |
||
130 | 1 | protected function firstStep() |
|
145 | |||
146 | /** |
||
147 | * Process re-allocation rounds (elimination re-allocations and surplus re-allocations) |
||
148 | * |
||
149 | * @param Candidate[] $candidates All active candidates to elect |
||
150 | * |
||
151 | * @return Candidate[] |
||
152 | */ |
||
153 | 1 | protected function processReallocationRounds(array &$candidates): array |
|
173 | |||
174 | /** |
||
175 | * Check if any candidates have reached the quota and can be elected. |
||
176 | * |
||
177 | * @param Candidate[] $candidates Array of active candidates to check |
||
178 | * |
||
179 | * @return bool Whether any candidates were changed to elected |
||
180 | */ |
||
181 | 1 | protected function checkCandidates(array $candidates): bool |
|
224 | |||
225 | /** |
||
226 | * Elect an array of candidates |
||
227 | * |
||
228 | * @param Candidate[] $candidates Array of candidates to elect |
||
229 | * |
||
230 | * @return |
||
231 | */ |
||
232 | 1 | protected function electCandidates(array $candidates) |
|
245 | |||
246 | /** |
||
247 | * Allocate the next votes from a Ballot. |
||
248 | * p. 49 |
||
249 | * |
||
250 | * @param Ballot $ballot The ballot to allocate the votes from |
||
251 | * @param float $multiplier Number to multiply the weight by (surplus) |
||
252 | * @param float $divisor The divisor of the weight (Total number of |
||
253 | * candidate votes) |
||
254 | * |
||
255 | * @return Ballot The same ballot passed in modified |
||
256 | */ |
||
257 | 1 | protected function allocateVotes(Ballot $ballot, float $multiplier = 1.0, float $divisor = 1.0): Ballot |
|
293 | |||
294 | /** |
||
295 | * Transfer the votes from one candidate to other candidates. |
||
296 | * |
||
297 | * @param float $surplus The number of surplus votes to transfer |
||
298 | * @param Candidate $candidate The candidate being elected to transfer |
||
299 | * the votes from |
||
300 | * |
||
301 | * @return |
||
302 | */ |
||
303 | 1 | protected function transferSurplusVotes(float $surplus, Candidate $candidate) |
|
318 | |||
319 | /** |
||
320 | * Transfer the votes from one eliminated candidate to other candidates. |
||
321 | * p. 51(2) |
||
322 | * |
||
323 | * @param Candidate $candidate Candidate being eliminated to transfer |
||
324 | * the votes from |
||
325 | * |
||
326 | * @return |
||
327 | */ |
||
328 | 1 | protected function transferEliminatedVotes(Candidate $candidate) |
|
347 | |||
348 | /** |
||
349 | * Elect a candidate after they've passed the threshold. |
||
350 | * |
||
351 | * @param Candidate $candidate |
||
352 | */ |
||
353 | 1 | protected function electCandidate(Candidate $candidate) |
|
373 | |||
374 | /** |
||
375 | * Eliminate the candidate with the lowest number of votes |
||
376 | * and reallocated their votes. |
||
377 | * p. 51 |
||
378 | * |
||
379 | * @param Candidate[] $candidates Array of active candidates |
||
380 | * |
||
381 | * @return int Number of candidates eliminated |
||
382 | */ |
||
383 | 1 | protected function eliminateCandidates(array $candidates): int |
|
401 | |||
402 | /** |
||
403 | * Get candidates with the lowest number of votes |
||
404 | * p. 51 |
||
405 | * p. 52(1) |
||
406 | * |
||
407 | * @param Candidate[] $candidates |
||
408 | * Array of active candidates |
||
409 | * |
||
410 | * @return Candidate[] |
||
411 | * Candidates with lowest score |
||
412 | */ |
||
413 | 1 | public function getLowestCandidates(array $candidates): array |
|
431 | |||
432 | /** |
||
433 | * Reject any invalid ballots. |
||
434 | * p. 46(3) |
||
435 | * |
||
436 | * @return int Number of rejected ballots |
||
437 | */ |
||
438 | 1 | protected function rejectInvalidBallots(): int |
|
455 | |||
456 | /** |
||
457 | * Check if ballot is valid. |
||
458 | * |
||
459 | * @param Ballot $ballot Ballot to test |
||
460 | * |
||
461 | * @return bool True if valid, false if invalid |
||
462 | */ |
||
463 | 2 | public function checkBallotValidity(Ballot $ballot): bool |
|
487 | |||
488 | /** |
||
489 | * Reallocate any remaining votes |
||
490 | * p. 53 |
||
491 | * |
||
492 | * @param Candidate[] $candidates All remaining candidates to elect |
||
493 | * @return |
||
494 | */ |
||
495 | 1 | protected function reallocateRemainingVotes(array &$candidates) |
|
511 | |||
512 | /** |
||
513 | * Get the quota to win. |
||
514 | * p. 47 |
||
515 | * |
||
516 | * @return int |
||
517 | */ |
||
518 | 1 | public function getQuota(): int |
|
530 | } |
||
531 |