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 |
||
| 9 | class ElectionRunner |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * Logger. |
||
| 13 | * |
||
| 14 | * @var \Psr\Log\LoggerInterface |
||
| 15 | */ |
||
| 16 | protected $logger; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Election object. |
||
| 20 | * |
||
| 21 | * @var \Michaelc\Voting\STV\Election; |
||
| 22 | */ |
||
| 23 | protected $election; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Array of all ballots in election. |
||
| 27 | * |
||
| 28 | * @var \MichaelC\Voting\STV\Ballot[] |
||
| 29 | */ |
||
| 30 | protected $ballots; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Quota of votes needed for a candidate to be elected. |
||
| 34 | * |
||
| 35 | * @var int |
||
| 36 | */ |
||
| 37 | protected $quota; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Number of candidates elected so far. |
||
| 41 | * |
||
| 42 | * @var int |
||
| 43 | */ |
||
| 44 | protected $electedCandidates; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Invalid ballots. |
||
| 48 | * |
||
| 49 | * @var \MichaelC\Voting\STV\Ballot[] |
||
| 50 | */ |
||
| 51 | protected $rejectedBallots; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Number of valid ballots |
||
| 55 | * |
||
| 56 | * @var int |
||
| 57 | */ |
||
| 58 | protected $validBallots; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Number of winners to still be elected (at current stage) |
||
| 62 | * |
||
| 63 | * @var int |
||
| 64 | */ |
||
| 65 | protected $candidatesToElect; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Constructor. |
||
| 69 | * |
||
| 70 | * @param Election $election |
||
| 71 | */ |
||
| 72 | 2 | public function __construct(Election $election, Logger $logger) |
|
| 82 | |||
| 83 | /** |
||
| 84 | * Run the election. |
||
| 85 | * |
||
| 86 | * @return Candidate[] Winning candidates |
||
| 87 | */ |
||
| 88 | 1 | public function run() |
|
| 107 | |||
| 108 | /** |
||
| 109 | * Perform the initial vote allocation. |
||
| 110 | * |
||
| 111 | * @return |
||
| 112 | */ |
||
| 113 | 1 | protected function firstStep() |
|
| 127 | |||
| 128 | /** |
||
| 129 | * Process re-allocation rounds (elimination re-allocations and surplus re-allocations) |
||
| 130 | * |
||
| 131 | * @param Candidate[] $candidates All active candidates to elect |
||
| 132 | * |
||
| 133 | * @return Candidate[] |
||
| 134 | */ |
||
| 135 | 1 | protected function processReallocationRounds(array &$candidates): array |
|
| 154 | |||
| 155 | /** |
||
| 156 | * Check if any candidates have reached the quota and can be elected. |
||
| 157 | * |
||
| 158 | * @param Candidate[] $candidates Array of active candidates to check |
||
| 159 | * |
||
| 160 | * @return bool Whether any candidates were changed to elected |
||
| 161 | */ |
||
| 162 | 1 | protected function checkCandidates(array $candidates): bool |
|
| 196 | |||
| 197 | /** |
||
| 198 | * Elect an array of candidates |
||
| 199 | * |
||
| 200 | * @param Candidate[] $candidates Array of candidates to elect |
||
| 201 | * |
||
| 202 | * @return |
||
| 203 | */ |
||
| 204 | 1 | protected function electCandidates(array $candidates) |
|
| 212 | |||
| 213 | /** |
||
| 214 | * Allocate the next votes from a Ballot. |
||
| 215 | * |
||
| 216 | * @param Ballot $ballot The ballot to allocate the votes from |
||
| 217 | * @param float $multiplier Number to multiply the weight by (surplus) |
||
| 218 | * @param float $divisor The divisor of the weight (Total number of |
||
| 219 | * candidate votes) |
||
| 220 | * |
||
| 221 | * @return Ballot The same ballot passed in modified |
||
| 222 | */ |
||
| 223 | 1 | protected function allocateVotes(Ballot $ballot, float $multiplier = 1.0, float $divisor = 1.0): Ballot |
|
| 250 | |||
| 251 | /** |
||
| 252 | * Transfer the votes from one candidate to other candidates. |
||
| 253 | * |
||
| 254 | * @param float $surplus The number of surplus votes to transfer |
||
| 255 | * @param Candidate $candidate The candidate being elected to transfer |
||
| 256 | * the votes from |
||
| 257 | * |
||
| 258 | * @return |
||
| 259 | */ |
||
| 260 | 1 | protected function transferSurplusVotes(float $surplus, Candidate $candidate) |
|
| 275 | |||
| 276 | /** |
||
| 277 | * Transfer the votes from one eliminated candidate to other candidates. |
||
| 278 | * |
||
| 279 | * @param Candidate $candidate Candidate being eliminated to transfer |
||
| 280 | * the votes from |
||
| 281 | * |
||
| 282 | * @return |
||
| 283 | */ |
||
| 284 | 1 | protected function transferEliminatedVotes(Candidate $candidate) |
|
| 300 | |||
| 301 | /** |
||
| 302 | * Elect a candidate after they've passed the threshold. |
||
| 303 | * |
||
| 304 | * @param Candidate $candidate |
||
| 305 | */ |
||
| 306 | 1 | protected function electCandidate(Candidate $candidate) |
|
| 326 | |||
| 327 | /** |
||
| 328 | * Eliminate the candidate with the lowest number of votes |
||
| 329 | * and reallocated their votes. |
||
| 330 | * |
||
| 331 | * @param Candidate[] $candidates Array of active candidates |
||
| 332 | * |
||
| 333 | * @return int Number of candidates eliminated |
||
| 334 | */ |
||
| 335 | 1 | protected function eliminateCandidates(array $candidates): int |
|
| 350 | |||
| 351 | /** |
||
| 352 | * Get candidates with the lowest number of votes. |
||
| 353 | * |
||
| 354 | * @param Candidate[] $candidates |
||
| 355 | * Array of active candidates |
||
| 356 | * |
||
| 357 | * @return Candidate[] |
||
| 358 | * Candidates with lowest score |
||
| 359 | */ |
||
| 360 | 1 | public function getLowestCandidates(array $candidates): array |
|
| 378 | |||
| 379 | /** |
||
| 380 | * Reject any invalid ballots. |
||
| 381 | * |
||
| 382 | * @return int Number of rejected ballots |
||
| 383 | */ |
||
| 384 | 1 | protected function rejectInvalidBallots(): int |
|
| 401 | |||
| 402 | /** |
||
| 403 | * Check if ballot is valid. |
||
| 404 | * |
||
| 405 | * @param Ballot $ballot Ballot to test |
||
| 406 | * |
||
| 407 | * @return bool True if valid, false if invalid |
||
| 408 | */ |
||
| 409 | 2 | public function checkBallotValidity(Ballot $ballot): bool |
|
| 433 | |||
| 434 | /** |
||
| 435 | * Reallocate any remaining votes |
||
| 436 | * |
||
| 437 | * @param Candidate[] $candidates All remaining candidates to elect |
||
| 438 | * @return |
||
| 439 | */ |
||
| 440 | 1 | protected function reallocateRemainingVotes(array &$candidates) |
|
| 456 | |||
| 457 | /** |
||
| 458 | * Get the quota to win. |
||
| 459 | * |
||
| 460 | * @return int |
||
| 461 | */ |
||
| 462 | 1 | public function getQuota(): int |
|
| 474 | } |
||
| 475 |