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 | public $quota; |
||
45 | |||
46 | /** |
||
47 | * Number of candidates elected so far. |
||
48 | * |
||
49 | * @var int |
||
50 | */ |
||
51 | public $electedCandidates; |
||
52 | |||
53 | /** |
||
54 | * Invalid ballots. |
||
55 | * |
||
56 | * @var \MichaelC\Voting\STV\Ballot[] |
||
57 | */ |
||
58 | public $rejectedBallots; |
||
59 | |||
60 | /** |
||
61 | * Number of valid ballots |
||
62 | * |
||
63 | * @var int |
||
64 | */ |
||
65 | public $validBallots; |
||
66 | |||
67 | /** |
||
68 | * Number of winners to still be elected (at current stage) |
||
69 | * |
||
70 | * @var int |
||
71 | */ |
||
72 | public $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() |
|
122 | |||
123 | /** |
||
124 | * Perform the initial vote allocation. |
||
125 | * p. 46 |
||
126 | * |
||
127 | * @return |
||
128 | */ |
||
129 | 1 | protected function firstStep() |
|
144 | |||
145 | /** |
||
146 | * Process re-allocation rounds (elimination re-allocations and surplus re-allocations) |
||
147 | * |
||
148 | * @param Candidate[] $candidates All active candidates to elect |
||
149 | * |
||
150 | * @return Candidate[] |
||
151 | */ |
||
152 | 1 | protected function processReallocationRounds(array &$candidates): array |
|
172 | |||
173 | /** |
||
174 | * Check if any candidates have reached the quota and can be elected. |
||
175 | * |
||
176 | * @param Candidate[] $candidates Array of active candidates to check |
||
177 | * |
||
178 | * @return bool Whether any candidates were changed to elected |
||
179 | */ |
||
180 | 1 | protected function checkCandidates(array $candidates): bool |
|
223 | |||
224 | /** |
||
225 | * Elect an array of candidates |
||
226 | * |
||
227 | * @param Candidate[] $candidates Array of candidates to elect |
||
228 | * |
||
229 | * @return |
||
230 | */ |
||
231 | 1 | protected function electCandidates(array $candidates) |
|
244 | |||
245 | /** |
||
246 | * Allocate the next votes from a Ballot. |
||
247 | * p. 49 |
||
248 | * |
||
249 | * @param Ballot $ballot The ballot to allocate the votes from |
||
250 | * @param float $multiplier Number to multiply the weight by (surplus) |
||
251 | * @param float $divisor The divisor of the weight (Total number of |
||
252 | * candidate votes) |
||
253 | * |
||
254 | * @return Ballot The same ballot passed in modified |
||
255 | */ |
||
256 | 1 | protected function allocateVotes(Ballot $ballot, float $multiplier = 1.0, float $divisor = 1.0): Ballot |
|
292 | |||
293 | /** |
||
294 | * Transfer the votes from one candidate to other candidates. |
||
295 | * |
||
296 | * @param float $surplus The number of surplus votes to transfer |
||
297 | * @param Candidate $candidate The candidate being elected to transfer |
||
298 | * the votes from |
||
299 | * |
||
300 | * @return |
||
301 | */ |
||
302 | 1 | protected function transferSurplusVotes(float $surplus, Candidate $candidate) |
|
317 | |||
318 | /** |
||
319 | * Transfer the votes from one eliminated candidate to other candidates. |
||
320 | * p. 51(2) |
||
321 | * |
||
322 | * @param Candidate $candidate Candidate being eliminated to transfer |
||
323 | * the votes from |
||
324 | * |
||
325 | * @return |
||
326 | */ |
||
327 | 1 | protected function transferEliminatedVotes(Candidate $candidate) |
|
346 | |||
347 | /** |
||
348 | * Elect a candidate after they've passed the threshold. |
||
349 | * |
||
350 | * @param Candidate $candidate |
||
351 | */ |
||
352 | 1 | protected function electCandidate(Candidate $candidate) |
|
371 | |||
372 | /** |
||
373 | * Eliminate the candidate with the lowest number of votes |
||
374 | * and reallocated their votes. |
||
375 | * p. 51 |
||
376 | * |
||
377 | * @param Candidate[] $candidates Array of active candidates |
||
378 | * |
||
379 | * @return int Number of candidates eliminated |
||
380 | */ |
||
381 | 1 | protected function eliminateCandidates(array $candidates): int |
|
399 | |||
400 | /** |
||
401 | * Get candidates with the lowest number of votes |
||
402 | * p. 51 and p. 52(1) |
||
403 | * |
||
404 | * @param Candidate[] $candidates |
||
405 | * Array of active candidates |
||
406 | * |
||
407 | * @return Candidate[] |
||
408 | * Candidates with lowest score |
||
409 | */ |
||
410 | 1 | public function getLowestCandidates(array $candidates): array |
|
428 | |||
429 | /** |
||
430 | * Reject any invalid ballots. |
||
431 | * p. 46(3) |
||
432 | * |
||
433 | * @return int Number of rejected ballots |
||
434 | */ |
||
435 | 1 | protected function rejectInvalidBallots(): int |
|
452 | |||
453 | /** |
||
454 | * Check if ballot is valid. |
||
455 | * |
||
456 | * @param Ballot $ballot Ballot to test |
||
457 | * |
||
458 | * @return bool True if valid, false if invalid |
||
459 | */ |
||
460 | 2 | public function checkBallotValidity(Ballot $ballot): bool |
|
488 | |||
489 | /** |
||
490 | * Reallocate any remaining votes |
||
491 | * p. 53 |
||
492 | * |
||
493 | * @param Candidate[] $candidates All remaining candidates to elect |
||
494 | * @return |
||
495 | */ |
||
496 | 1 | protected function reallocateRemainingVotes(array &$candidates) |
|
512 | |||
513 | /** |
||
514 | * Get the quota to win. |
||
515 | * p. 47 |
||
516 | * |
||
517 | * TODO: Move this out of this method and use params/args |
||
518 | * |
||
519 | * @return int |
||
520 | */ |
||
521 | 1 | public function setQuota(): int |
|
533 | } |
||
534 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.