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 | ||
| 17 | trait CandidatesProcess | ||
| 18 | { | ||
| 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 | ||
| 142 | 4 | if ( $this->_State > 1 ) : | |
| 143 | 1 | throw new CondorcetException(2); | |
| 144 | endif; | ||
| 145 | |||
| 146 | 3 | if ( !is_array($candidates_input) ) : | |
| 147 | 3 | $candidates_input = [$candidates_input]; | |
| 148 | endif; | ||
| 149 | |||
| 150 | 3 | foreach ($candidates_input as &$candidate) : | |
| 151 | 3 | $candidate_key = $this->getCandidateKey($candidate); | |
| 152 | |||
| 153 | 3 | if ( $candidate_key === null ) : | |
| 154 | throw new CondorcetException(4,$candidate); | ||
| 155 | endif; | ||
| 156 | |||
| 157 | 3 | $candidate = $candidate_key; | |
| 158 | endforeach; | ||
| 159 | |||
| 160 | 3 | $rem = []; | |
| 161 | 3 | foreach ($candidates_input as $candidate_key) : | |
| 162 | 3 | $this->_Candidates[$candidate_key]->destroyLink($this); | |
| 163 | |||
| 164 | 3 | $rem[] = $this->_Candidates[$candidate_key]; | |
| 165 | |||
| 166 | 3 | unset($this->_Candidates[$candidate_key]); | |
| 167 | endforeach; | ||
| 168 | |||
| 169 | 3 | return $rem; | |
| 170 | } | ||
| 171 | |||
| 172 | |||
| 173 | /////////// PARSE CANDIDATES /////////// | ||
| 174 | |||
| 175 | 1 | public function addCandidatesFromJson (string $input) : array | |
| 176 |     { | ||
| 177 | 1 | $input = CondorcetUtil::prepareJson($input); | |
| 178 | |||
| 179 | ////// | ||
| 180 | |||
| 181 | 1 | $adding = []; | |
| 182 | 1 | foreach ($input as $candidate) : | |
| 183 | 1 | $candidate = new Candidate ($candidate); | |
| 184 | |||
| 185 | 1 | if (!$this->canAddCandidate($candidate)) : | |
| 186 | throw new CondorcetException(3); | ||
| 187 | endif; | ||
| 188 | |||
| 189 | 1 | $adding[] = $candidate; | |
| 190 | endforeach; | ||
| 191 | |||
| 192 | // Add Candidates | ||
| 193 | 1 | foreach ($adding as $oneCandidate) : | |
| 194 | 1 | $this->addCandidate($oneCandidate); | |
| 195 | endforeach; | ||
| 196 | |||
| 197 | 1 | return $adding; | |
| 198 | } | ||
| 199 | |||
| 200 | 22 | public function parseCandidates (string $input, bool $isFile = false) : array | |
| 229 | } | ||
| 230 |