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 | 145 | public function __construct () |
|
72 | |||
73 | public function __destruct () |
||
74 | { |
||
75 | $this->destroyAllLink(); |
||
76 | } |
||
77 | |||
78 | 2 | public function __sleep () : array |
|
102 | |||
103 | 1 | public function __wakeup () |
|
109 | |||
110 | 1 | public function __clone () |
|
123 | |||
124 | |||
125 | /////////// TIMER & CHECKSUM /////////// |
||
126 | |||
127 | 2 | public function getGlobalTimer () : float { |
|
128 | 2 | return $this->_timer->getGlobalTimer(); |
|
129 | } |
||
130 | |||
131 | 2 | public function getLastTimer () : float { |
|
132 | 2 | return $this->_timer->getLastTimer(); |
|
133 | } |
||
134 | |||
135 | 93 | public function getTimerManager () : Timer_Manager { |
|
138 | |||
139 | 1 | public function getChecksum () : string |
|
162 | |||
163 | |||
164 | /////////// LINKS REGULATION /////////// |
||
165 | |||
166 | 1 | protected function registerAllLinks () : void |
|
178 | |||
179 | protected function destroyAllLink () : void |
||
180 | { |
||
181 | foreach ($this->_Candidates as $value) : |
||
182 | $value->destroyLink($this); |
||
183 | endforeach; |
||
184 | |||
185 | if ($this->_State > 1) : |
||
186 | foreach ($this->_Votes as $value) : |
||
187 | $value->destroyLink($this); |
||
188 | endforeach; |
||
189 | endif; |
||
190 | } |
||
191 | |||
192 | |||
193 | /////////// IMPLICIT RANKING & VOTE WEIGHT /////////// |
||
194 | |||
195 | 100 | public function getImplicitRankingRule () : bool |
|
199 | |||
200 | 6 | public function setImplicitRanking (bool $rule = true) : bool |
|
206 | |||
207 | 96 | public function isVoteWeightIsAllowed () : bool |
|
211 | |||
212 | 4 | public function allowVoteWeight (bool $rule = true) : bool |
|
218 | |||
219 | |||
220 | /////////// VOTE CONSTRAINT /////////// |
||
221 | |||
222 | 4 | public function addConstraint (string $class) : bool |
|
240 | |||
241 | 2 | public function getConstraints () : array |
|
245 | |||
246 | 2 | public function clearConstraints () : bool |
|
256 | |||
257 | 93 | public function testIfVoteIsValidUnderElectionConstraints (Vote $vote) : bool |
|
267 | |||
268 | |||
269 | /////////// LARGE ELECTION MODE /////////// |
||
270 | |||
271 | 6 | public function setExternalDataHandler (DataHandlerDriverInterface $driver) : bool |
|
280 | |||
281 | 1 | public function removeExternalDataHandler () : bool |
|
290 | |||
291 | |||
292 | /////////// STATE /////////// |
||
293 | |||
294 | // Close the candidate config, be ready for voting (optional) |
||
295 | 123 | public function setStateToVote () : bool |
|
311 | |||
312 | // Prepare to compute results & caching system |
||
313 | 93 | protected function prepareResult () : bool |
|
332 | |||
333 | |||
334 | /////////// CANDIDATES /////////// |
||
335 | |||
336 | use CandidatesProcess; |
||
337 | |||
338 | |||
339 | /////////// VOTING /////////// |
||
340 | |||
341 | use VotesProcess; |
||
342 | |||
343 | |||
344 | /////////// RESULTS /////////// |
||
345 | |||
346 | use ResultsProcess; |
||
347 | } |
||
348 |