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 |
||
24 | class Election |
||
25 | { |
||
26 | |||
27 | /////////// PROPERTIES /////////// |
||
28 | |||
29 | public const MAX_LENGTH_CANDIDATE_ID = 30; // Max length for candidate identifiant string |
||
30 | |||
31 | protected static $_maxParseIteration = null; |
||
32 | protected static $_maxVoteNumber = null; |
||
33 | protected static $_checksumMode = false; |
||
34 | |||
35 | /////////// STATICS METHODS /////////// |
||
36 | |||
37 | // Change max parse iteration |
||
38 | 1 | public static function setMaxParseIteration (?int $value) : ?int |
|
43 | |||
44 | // Change max vote number |
||
45 | 1 | public static function setMaxVoteNumber (?int $value) : ?int |
|
50 | |||
51 | |||
52 | /////////// CONSTRUCTOR /////////// |
||
53 | |||
54 | use CondorcetVersion; |
||
55 | |||
56 | // Mechanics |
||
57 | protected $_State = 1; // 1 = Add Candidates / 2 = Voting / 3 = Some result have been computing |
||
58 | protected $_timer; |
||
59 | |||
60 | // Params |
||
61 | protected $_ImplicitRanking = true; |
||
62 | protected $_VoteWeightRule = false; |
||
63 | protected $_Constraints = []; |
||
64 | |||
65 | ////// |
||
66 | |||
67 | 134 | public function __construct () |
|
73 | |||
74 | 1 | public function __destruct () |
|
78 | |||
79 | 2 | public function __sleep () : array |
|
103 | |||
104 | 1 | public function __wakeup () |
|
110 | |||
111 | 1 | public function __clone () |
|
124 | |||
125 | |||
126 | /////////// TIMER & CHECKSUM /////////// |
||
127 | |||
128 | 2 | public function getGlobalTimer (bool $float = false) { |
|
131 | |||
132 | 2 | public function getLastTimer (bool $float = false) { |
|
135 | |||
136 | 93 | public function getTimerManager () : Timer_Manager { |
|
139 | |||
140 | 1 | public function getChecksum () : string |
|
163 | |||
164 | |||
165 | /////////// LINKS REGULATION /////////// |
||
166 | |||
167 | 1 | protected function registerAllLinks () : void |
|
179 | |||
180 | 1 | protected function destroyAllLink () : void |
|
192 | |||
193 | |||
194 | /////////// IMPLICIT RANKING & VOTE WEIGHT /////////// |
||
195 | |||
196 | 100 | public function getImplicitRankingRule () : bool |
|
200 | |||
201 | 5 | public function setImplicitRanking (bool $rule = true) : bool |
|
207 | |||
208 | 96 | public function isVoteWeightIsAllowed () : bool |
|
212 | |||
213 | 4 | public function allowVoteWeight (bool $rule = true) : bool |
|
219 | |||
220 | |||
221 | /////////// VOTE CONSTRAINT /////////// |
||
222 | |||
223 | 4 | public function addConstraint (string $class) : bool |
|
241 | |||
242 | 2 | public function getConstraints () : array |
|
246 | |||
247 | 2 | public function clearConstraints () : bool |
|
257 | |||
258 | 93 | public function testIfVoteIsValidUnderElectionConstraints (Vote $vote) : bool |
|
268 | |||
269 | |||
270 | /////////// LARGE ELECTION MODE /////////// |
||
271 | |||
272 | 6 | public function setExternalDataHandler (DataHandlerDriverInterface $driver) : bool |
|
281 | |||
282 | 1 | public function removeExternalDataHandler () : bool |
|
291 | |||
292 | |||
293 | /////////// STATE /////////// |
||
294 | |||
295 | // Close the candidate config, be ready for voting (optional) |
||
296 | 117 | public function setStateToVote () : bool |
|
312 | |||
313 | // Prepare to compute results & caching system |
||
314 | 93 | 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 |