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 |
||
27 | class Election |
||
28 | { |
||
29 | |||
30 | /////////// PROPERTIES /////////// |
||
31 | |||
32 | public const MAX_LENGTH_CANDIDATE_ID = 30; // Max length for candidate identifiant string |
||
33 | |||
34 | protected static $_maxParseIteration = null; |
||
35 | protected static $_maxVoteNumber = null; |
||
36 | protected static $_checksumMode = false; |
||
37 | |||
38 | /////////// STATICS METHODS /////////// |
||
39 | |||
40 | // Change max parse iteration |
||
41 | 1 | public static function setMaxParseIteration (?int $value) : ?int |
|
46 | |||
47 | // Change max vote number |
||
48 | 1 | public static function setMaxVoteNumber (?int $value) : ?int |
|
53 | |||
54 | |||
55 | /////////// CONSTRUCTOR /////////// |
||
56 | |||
57 | use CondorcetVersion; |
||
58 | |||
59 | // Mechanics |
||
60 | protected $_State = 1; // 1 = Add Candidates / 2 = Voting / 3 = Some result have been computing |
||
61 | protected $_timer; |
||
62 | |||
63 | // Params |
||
64 | protected $_ImplicitRanking = true; |
||
65 | protected $_VoteWeightRule = false; |
||
66 | |||
67 | ////// |
||
68 | |||
69 | 111 | public function __construct () |
|
75 | |||
76 | 1 | public function __destruct () |
|
80 | |||
81 | 2 | public function __sleep () : array |
|
104 | |||
105 | 1 | public function __wakeup () |
|
111 | |||
112 | 1 | public function __clone () |
|
125 | |||
126 | |||
127 | /////////// TIMER & CHECKSUM /////////// |
||
128 | |||
129 | 2 | public function getGlobalTimer (bool $float = false) { |
|
132 | |||
133 | 2 | public function getLastTimer (bool $float = false) { |
|
136 | |||
137 | 80 | public function getTimerManager () : Timer_Manager { |
|
140 | |||
141 | 1 | public function getChecksum () : string |
|
164 | |||
165 | |||
166 | /////////// LINKS REGULATION /////////// |
||
167 | |||
168 | 1 | protected function registerAllLinks () : void |
|
180 | |||
181 | 1 | protected function destroyAllLink () : void |
|
193 | |||
194 | |||
195 | /////////// IMPLICIT RANKING & VOTE WEIGHT /////////// |
||
196 | |||
197 | 86 | public function getImplicitRankingRule () : bool |
|
201 | |||
202 | 3 | public function setImplicitRanking (bool $rule = true) : bool |
|
208 | |||
209 | 82 | public function isVoteWeightIsAllowed () : bool |
|
213 | |||
214 | 1 | public function allowVoteWeight (bool $rule = true) : bool |
|
220 | |||
221 | |||
222 | /////////// LARGE ELECTION MODE /////////// |
||
223 | |||
224 | 3 | public function setExternalDataHandler (DataHandlerDriverInterface $driver) : bool |
|
233 | |||
234 | 1 | public function removeExternalDataHandler () : bool |
|
243 | |||
244 | |||
245 | /////////// STATE /////////// |
||
246 | |||
247 | // Close the candidate config, be ready for voting (optional) |
||
248 | 98 | public function setStateToVote () : bool |
|
264 | |||
265 | // Prepare to compute results & caching system |
||
266 | 80 | protected function prepareResult () : bool |
|
285 | |||
286 | |||
287 | /////////// CANDIDATES /////////// |
||
288 | |||
289 | use CandidatesProcess; |
||
290 | |||
291 | |||
292 | /////////// VOTING /////////// |
||
293 | |||
294 | use VotesProcess; |
||
295 | |||
296 | |||
297 | /////////// RESULTS /////////// |
||
298 | |||
299 | use ResultsProcess; |
||
300 | |||
301 | } |
||
302 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.