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 | 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 | * Array of each stage of the election and the vote totals of each candidate | ||
| 76 | * | ||
| 77 | * @var array | ||
| 78 | */ | ||
| 79 | protected $steps; | ||
| 80 | |||
| 81 | /** | ||
| 82 | * Constructor. | ||
| 83 | * | ||
| 84 | * @param Election $election | ||
| 85 | */ | ||
| 86 | 2 | public function __construct(Election $election, Logger $logger) | |
| 97 | |||
| 98 | /** | ||
| 99 | * Run the election. | ||
| 100 | * | ||
| 101 | * @return Candidate[] Winning candidates | ||
| 102 | */ | ||
| 103 | 1 | public function run() | |
| 130 | |||
| 131 | /** | ||
| 132 | * Perform the initial vote allocation. | ||
| 133 | * p. 46. | ||
| 134 | * | ||
| 135 | * @return | ||
| 136 | */ | ||
| 137 | 1 | protected function firstStep() | |
| 154 | |||
| 155 | /** | ||
| 156 | * Process re-allocation rounds (elimination re-allocations and surplus re-allocations). | ||
| 157 | * | ||
| 158 | * @param Candidate[] $candidates All active candidates to elect | ||
| 159 | * | ||
| 160 | * @return Candidate[] | ||
| 161 | */ | ||
| 162 | 1 | protected function processReallocationRounds(array &$candidates): array | |
| 184 | |||
| 185 | /** | ||
| 186 | * Check if any candidates have reached the quota and can be elected. | ||
| 187 | * | ||
| 188 | * @param Candidate[] $candidates Array of active candidates to check | ||
| 189 | * | ||
| 190 | * @return bool Whether any candidates were changed to elected | ||
| 191 | */ | ||
| 192 | 1 | protected function checkCandidates(array $candidates): bool | |
| 233 | |||
| 234 | /** | ||
| 235 | * Elect an array of candidates. | ||
| 236 | * | ||
| 237 | * @param Candidate[] $candidates Array of candidates to elect | ||
| 238 | * | ||
| 239 | * @return | ||
| 240 | */ | ||
| 241 | 1 | protected function electCandidates(array $candidates) | |
| 253 | |||
| 254 | /** | ||
| 255 | * Allocate the next votes from a Ballot. | ||
| 256 | * p. 49. | ||
| 257 | * | ||
| 258 | * @param Ballot $ballot The ballot to allocate the votes from | ||
| 259 | * @param float $multiplier Number to multiply the weight by (surplus) | ||
| 260 | * @param float $divisor The divisor of the weight (Total number of | ||
| 261 | * candidate votes) | ||
| 262 | * | ||
| 263 | * @return Ballot The same ballot passed in modified | ||
| 264 | */ | ||
| 265 | 1 | protected function allocateVotes(Ballot $ballot, float $multiplier = 1.0, float $divisor = 1.0): Ballot | |
| 300 | |||
| 301 | /** | ||
| 302 | * Transfer the votes from one candidate to other candidates. | ||
| 303 | * | ||
| 304 | * @param float $surplus The number of surplus votes to transfer | ||
| 305 | * @param Candidate $candidate The candidate being elected to transfer | ||
| 306 | * the votes from | ||
| 307 | * | ||
| 308 | * @return | ||
| 309 | */ | ||
| 310 | 1 | protected function transferSurplusVotes(float $surplus, Candidate $candidate) | |
| 325 | |||
| 326 | /** | ||
| 327 | * Transfer the votes from one eliminated candidate to other candidates. | ||
| 328 | * p. 51(2). | ||
| 329 | * | ||
| 330 | * @param Candidate $candidate Candidate being eliminated to transfer | ||
| 331 | * the votes from | ||
| 332 | * | ||
| 333 | * @return | ||
| 334 | */ | ||
| 335 | 1 | protected function transferEliminatedVotes(Candidate $candidate) | |
| 354 | |||
| 355 | /** | ||
| 356 | * Elect a candidate after they've passed the threshold. | ||
| 357 | * | ||
| 358 | * @param Candidate $candidate | ||
| 359 | */ | ||
| 360 | 1 | protected function electCandidate(Candidate $candidate) | |
| 379 | |||
| 380 | /** | ||
| 381 | * Eliminate the candidate with the lowest number of votes | ||
| 382 | * and reallocated their votes. | ||
| 383 | * p. 51. | ||
| 384 | * | ||
| 385 | * @param Candidate[] $candidates Array of active candidates | ||
| 386 | * | ||
| 387 | * @return int Number of candidates eliminated | ||
| 388 | */ | ||
| 389 | 1 | protected function eliminateCandidates(array $candidates): int | |
| 407 | |||
| 408 | /** | ||
| 409 | * Get candidates with the lowest number of votes | ||
| 410 | * p. 51 and p. 52(1). | ||
| 411 | * | ||
| 412 | * @param Candidate[] $candidates | ||
| 413 | * Array of active candidates | ||
| 414 | * | ||
| 415 | * @return Candidate[] | ||
| 416 | * Candidates with lowest score | ||
| 417 | */ | ||
| 418 | 1 | public function getLowestCandidates(array $candidates): array | |
| 436 | |||
| 437 | /** | ||
| 438 | * Reject any invalid ballots. | ||
| 439 | * p. 46(3). | ||
| 440 | * | ||
| 441 | * @return int Number of rejected ballots | ||
| 442 | */ | ||
| 443 | 1 | protected function rejectInvalidBallots(): int | |
| 460 | |||
| 461 | /** | ||
| 462 | * Check if ballot is valid. | ||
| 463 | * | ||
| 464 | * @param Ballot $ballot Ballot to test | ||
| 465 | * | ||
| 466 | * @return bool True if valid, false if invalid | ||
| 467 | */ | ||
| 468 | 2 | public function checkBallotValidity(Ballot $ballot): bool | |
| 494 | |||
| 495 | /** | ||
| 496 | * Reallocate any remaining votes | ||
| 497 | * p. 53. | ||
| 498 | * | ||
| 499 | * @param Candidate[] $candidates All remaining candidates to elect | ||
| 500 | * | ||
| 501 | * @return | ||
| 502 | */ | ||
| 503 | 1 | protected function reallocateRemainingVotes(array &$candidates) | |
| 517 | |||
| 518 | /** | ||
| 519 | * Get the quota to win. | ||
| 520 | * p. 47. | ||
| 521 | * | ||
| 522 | * TODO: Move this out of this method and use params/args | ||
| 523 | * | ||
| 524 | * @return int | ||
| 525 | */ | ||
| 526 | 1 | public function setQuota(): int | |
| 538 | |||
| 539 | /** | ||
| 540 | * Return the quota for the election | ||
| 541 | * | ||
| 542 | * @return int | ||
| 543 | */ | ||
| 544 | 1 | public function getQuota(): int | |
| 548 | |||
| 549 | /** | ||
| 550 | * Gets an array of the state of all candidates at the end of each | ||
| 551 | * phase/step | ||
| 552 | * | ||
| 553 | * @return array | ||
| 554 | */ | ||
| 555 | 1 | public function getSteps(): array | |
| 559 | } | ||
| 560 |