Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like CandidatesProcess 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 CandidatesProcess, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | |||
20 | /////////// CONSTRUCTOR /////////// |
||
21 | |||
22 | // Data and global options |
||
23 | protected $_Candidates = []; // Candidate list |
||
24 | protected $_AutomaticNewCandidateName = 'A'; |
||
25 | |||
26 | |||
27 | /////////// GET CANDIDATES /////////// |
||
28 | |||
29 | // Count registered candidates |
||
30 | 103 | public function countCandidates () : int |
|
31 | { |
||
32 | 103 | return count($this->_Candidates); |
|
33 | } |
||
34 | |||
35 | 103 | public function getCandidatesList () : array |
|
36 | { |
||
37 | 103 | return $this->_Candidates; |
|
38 | } |
||
39 | |||
40 | // Get the list of registered CANDIDATES |
||
41 | 2 | public function getCandidatesListAsString () : array |
|
42 | { |
||
43 | 2 | $result = []; |
|
44 | |||
45 | 2 | foreach ($this->_Candidates as $candidateKey => &$oneCandidate) : |
|
46 | 2 | $result[$candidateKey] = $oneCandidate->getName(); |
|
47 | endforeach; |
||
48 | |||
49 | 2 | return $result; |
|
50 | } |
||
51 | |||
52 | 114 | public function getCandidateKey ($candidate) : ?int |
|
53 | { |
||
54 | 114 | if ($candidate instanceof Candidate) : |
|
55 | 95 | $r = array_search($candidate, $this->_Candidates, true); |
|
56 | else: |
||
57 | 112 | $r = array_search(trim((string) $candidate), $this->_Candidates, false); |
|
58 | endif; |
||
59 | |||
60 | 114 | return ($r !== false) ? $r : null; |
|
61 | } |
||
62 | |||
63 | 93 | public function getCandidateObjectFromKey (int $candidate_key) : ?Candidate |
|
71 | |||
72 | 155 | public function isRegisteredCandidate ($candidate, bool $strictMode = true) : bool |
|
76 | |||
77 | 3 | public function getCandidateObjectFromName (string $candidateName) : ?Candidate |
|
88 | |||
89 | |||
90 | /////////// ADD & REMOVE CANDIDATE /////////// |
||
91 | |||
92 | // Add a vote candidate before voting |
||
93 | 156 | public function addCandidate ($candidate = null) : Candidate |
|
132 | |||
133 | 155 | public function canAddCandidate ($candidate) : bool |
|
137 | |||
138 | // Destroy a register vote candidate before voting |
||
139 | 4 | public function removeCandidates ($candidates_input) : array |
|
140 | { |
||
141 | // only if the vote has not started |
||
171 | |||
172 | |||
173 | /////////// PARSE CANDIDATES /////////// |
||
174 | |||
175 | 1 | public function addCandidatesFromJson (string $input) : array |
|
199 | |||
200 | 22 | public function parseCandidates (string $input, bool $isFile = false) : array |
|
201 | { |
||
202 | 22 | $input = CondorcetUtil::prepareParse($input, $isFile); |
|
203 | |||
204 | 22 | $adding = []; |
|
205 | 22 | foreach ($input as $line) : |
|
206 | // Empty Line |
||
207 | 22 | if (empty($line)) : |
|
208 | 1 | continue; |
|
209 | endif; |
||
210 | |||
211 | // addCandidate |
||
212 | 22 | if (self::$_maxParseIteration !== null && count($adding) >= self::$_maxParseIteration) : |
|
213 | 1 | throw new CondorcetException(12, (string) self::$_maxParseIteration); |
|
214 | endif; |
||
215 | |||
216 | 22 | if (!$this->canAddCandidate($line)) : |
|
217 | 2 | throw new CondorcetException(3); |
|
218 | endif; |
||
219 | |||
220 | 22 | $adding[] = $line; |
|
221 | endforeach; |
||
222 | |||
223 | 20 | foreach ($adding as $oneNewCandidate) : |
|
224 | 20 | $this->addCandidate($oneNewCandidate); |
|
225 | endforeach; |
||
226 | |||
230 |