Complex classes like Election 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 Election, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class Election |
||
| 24 | { |
||
| 25 | |||
| 26 | /////////// PROPERTIES /////////// |
||
| 27 | |||
| 28 | public const MAX_LENGTH_CANDIDATE_ID = 30; // Max length for candidate identifiant string |
||
| 29 | |||
| 30 | protected static $_maxParseIteration = null; |
||
| 31 | protected static $_maxVoteNumber = null; |
||
| 32 | protected static $_checksumMode = false; |
||
| 33 | |||
| 34 | /////////// STATICS METHODS /////////// |
||
| 35 | |||
| 36 | // Change max parse iteration |
||
| 37 | 3 | public static function setMaxParseIteration (?int $value) : ?int |
|
| 42 | |||
| 43 | // Change max vote number |
||
| 44 | 1 | public static function setMaxVoteNumber (?int $value) : ?int |
|
| 49 | |||
| 50 | |||
| 51 | /////////// CONSTRUCTOR /////////// |
||
| 52 | |||
| 53 | use CondorcetVersion; |
||
| 54 | |||
| 55 | // Mechanics |
||
| 56 | protected $_State = 1; // 1 = Add Candidates / 2 = Voting / 3 = Some result have been computing |
||
| 57 | protected $_timer; |
||
| 58 | |||
| 59 | // Params |
||
| 60 | protected $_ImplicitRanking = true; |
||
| 61 | protected $_VoteWeightRule = false; |
||
| 62 | protected $_Constraints = []; |
||
| 63 | |||
| 64 | ////// |
||
| 65 | |||
| 66 | 155 | public function __construct () |
|
| 72 | |||
| 73 | public function __destruct () |
||
| 77 | |||
| 78 | 2 | public function __sleep () : array |
|
| 101 | |||
| 102 | 1 | public function __wakeup () |
|
| 108 | |||
| 109 | 1 | public function __clone () |
|
| 122 | |||
| 123 | |||
| 124 | /////////// TIMER & CHECKSUM /////////// |
||
| 125 | |||
| 126 | 2 | public function getGlobalTimer () : float { |
|
| 129 | |||
| 130 | 2 | public function getLastTimer () : float { |
|
| 133 | |||
| 134 | 95 | public function getTimerManager () : Timer_Manager { |
|
| 137 | |||
| 138 | 1 | public function getChecksum () : string |
|
| 161 | |||
| 162 | |||
| 163 | /////////// LINKS REGULATION /////////// |
||
| 164 | |||
| 165 | 1 | protected function registerAllLinks () : void |
|
| 177 | |||
| 178 | protected function destroyAllLink () : void |
||
| 190 | |||
| 191 | |||
| 192 | /////////// IMPLICIT RANKING & VOTE WEIGHT /////////// |
||
| 193 | |||
| 194 | 102 | public function getImplicitRankingRule () : bool |
|
| 198 | |||
| 199 | 6 | public function setImplicitRanking (bool $rule = true) : bool |
|
| 205 | |||
| 206 | 98 | public function isVoteWeightIsAllowed () : bool |
|
| 210 | |||
| 211 | 4 | public function allowVoteWeight (bool $rule = true) : bool |
|
| 217 | |||
| 218 | |||
| 219 | /////////// VOTE CONSTRAINT /////////// |
||
| 220 | |||
| 221 | 4 | public function addConstraint (string $class) : bool |
|
| 239 | |||
| 240 | 2 | public function getConstraints () : array |
|
| 244 | |||
| 245 | 2 | public function clearConstraints () : bool |
|
| 255 | |||
| 256 | 95 | public function testIfVoteIsValidUnderElectionConstraints (Vote $vote) : bool |
|
| 266 | |||
| 267 | |||
| 268 | /////////// LARGE ELECTION MODE /////////// |
||
| 269 | |||
| 270 | 7 | public function setExternalDataHandler (DataHandlerDriverInterface $driver) : bool |
|
| 279 | |||
| 280 | 1 | public function removeExternalDataHandler () : bool |
|
| 289 | |||
| 290 | |||
| 291 | /////////// STATE /////////// |
||
| 292 | |||
| 293 | public function getState () : int |
||
| 297 | |||
| 298 | // Close the candidate config, be ready for voting (optional) |
||
| 299 | 128 | public function setStateToVote () : bool |
|
| 315 | |||
| 316 | // Prepare to compute results & caching system |
||
| 317 | 96 | protected function prepareResult () : bool |
|
| 336 | |||
| 337 | |||
| 338 | /////////// CANDIDATES /////////// |
||
| 339 | |||
| 340 | use CandidatesProcess; |
||
| 341 | |||
| 342 | |||
| 343 | /////////// VOTING /////////// |
||
| 344 | |||
| 345 | use VotesProcess; |
||
| 346 | |||
| 347 | |||
| 348 | /////////// RESULTS /////////// |
||
| 349 | |||
| 350 | use ResultsProcess; |
||
| 351 | } |
||
| 352 |