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 |
||
| 20 | class Election |
||
| 21 | { |
||
| 22 | |||
| 23 | /////////// PROPERTIES /////////// |
||
| 24 | |||
| 25 | public const MAX_LENGTH_CANDIDATE_ID = 30; // Max length for candidate identifiant string |
||
| 26 | |||
| 27 | protected static $_maxParseIteration = null; |
||
| 28 | protected static $_maxVoteNumber = null; |
||
| 29 | protected static $_checksumMode = false; |
||
| 30 | |||
| 31 | /////////// STATICS METHODS /////////// |
||
| 32 | |||
| 33 | // Change max parse iteration |
||
| 34 | 3 | public static function setMaxParseIteration (?int $maxParseIterations) : ?int |
|
| 39 | |||
| 40 | // Change max vote number |
||
| 41 | 1 | public static function setMaxVoteNumber (?int $maxVotesNumber) : ?int |
|
| 46 | |||
| 47 | |||
| 48 | /////////// CONSTRUCTOR /////////// |
||
| 49 | |||
| 50 | use CondorcetVersion; |
||
| 51 | |||
| 52 | // Mechanics |
||
| 53 | protected $_State = 1; // 1 = Add Candidates / 2 = Voting / 3 = Some result have been computing |
||
| 54 | protected $_timer; |
||
| 55 | |||
| 56 | // Params |
||
| 57 | protected $_ImplicitRanking = true; |
||
| 58 | protected $_VoteWeightRule = false; |
||
| 59 | protected $_Constraints = []; |
||
| 60 | |||
| 61 | ////// |
||
| 62 | |||
| 63 | 158 | public function __construct () |
|
| 69 | |||
| 70 | public function __destruct () |
||
| 74 | |||
| 75 | 2 | public function __sleep () : array |
|
| 98 | |||
| 99 | 2 | public function __wakeup () |
|
| 105 | |||
| 106 | 1 | public function __clone () |
|
| 119 | |||
| 120 | |||
| 121 | /////////// TIMER & CHECKSUM /////////// |
||
| 122 | |||
| 123 | 2 | public function getGlobalTimer () : float { |
|
| 126 | |||
| 127 | 2 | public function getLastTimer () : float { |
|
| 130 | |||
| 131 | 95 | public function getTimerManager () : Timer_Manager { |
|
| 134 | |||
| 135 | 1 | public function getChecksum () : string |
|
| 158 | |||
| 159 | |||
| 160 | /////////// LINKS REGULATION /////////// |
||
| 161 | |||
| 162 | 1 | protected function registerAllLinks () : void |
|
| 174 | |||
| 175 | protected function destroyAllLink () : void |
||
| 187 | |||
| 188 | |||
| 189 | /////////// IMPLICIT RANKING & VOTE WEIGHT /////////// |
||
| 190 | |||
| 191 | 102 | public function getImplicitRankingRule () : bool |
|
| 195 | |||
| 196 | 6 | public function setImplicitRanking (bool $rule = true) : bool |
|
| 202 | |||
| 203 | 98 | public function isVoteWeightAllowed () : bool |
|
| 207 | |||
| 208 | 4 | public function allowVoteWeight (bool $rule = true) : bool |
|
| 214 | |||
| 215 | |||
| 216 | /////////// VOTE CONSTRAINT /////////// |
||
| 217 | |||
| 218 | 4 | public function addConstraint (string $constraintClass) : bool |
|
| 236 | |||
| 237 | 2 | public function getConstraints () : array |
|
| 241 | |||
| 242 | 2 | public function clearConstraints () : bool |
|
| 252 | |||
| 253 | 95 | public function testIfVoteIsValidUnderElectionConstraints (Vote $vote) : bool |
|
| 263 | |||
| 264 | |||
| 265 | /////////// LARGE ELECTION MODE /////////// |
||
| 266 | |||
| 267 | 7 | public function setExternalDataHandler (DataHandlerDriverInterface $driver) : bool |
|
| 276 | |||
| 277 | 1 | public function removeExternalDataHandler () : bool |
|
| 286 | |||
| 287 | |||
| 288 | /////////// STATE /////////// |
||
| 289 | |||
| 290 | 131 | public function getState () : int |
|
| 294 | |||
| 295 | // Close the candidate config, be ready for voting (optional) |
||
| 296 | 131 | public function setStateToVote () : bool |
|
| 312 | |||
| 313 | // Prepare to compute results & caching system |
||
| 314 | 96 | protected function prepareResult () : bool |
|
| 333 | |||
| 334 | |||
| 335 | /////////// CANDIDATES /////////// |
||
| 336 | |||
| 337 | use CandidatesProcess; |
||
| 338 | |||
| 339 | |||
| 340 | /////////// VOTING /////////// |
||
| 341 | |||
| 342 | use VotesProcess; |
||
| 343 | |||
| 344 | |||
| 345 | /////////// RESULTS /////////// |
||
| 346 | |||
| 347 | use ResultsProcess; |
||
| 348 | } |
||
| 349 |