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 | trait CandidatesProcess  | 
            ||
| 20 | { | 
            ||
| 21 | |||
| 22 | /////////// CONSTRUCTOR ///////////  | 
            ||
| 23 | |||
| 24 | // Data and global options  | 
            ||
| 25 | protected $_Candidates = []; // Candidate list  | 
            ||
| 26 | protected $_i_CandidateId = 'A';  | 
            ||
| 27 | |||
| 28 | |||
| 29 | /////////// GET CANDIDATES ///////////  | 
            ||
| 30 | |||
| 31 | // Count registered candidates  | 
            ||
| 32 | 101 | public function countCandidates () : int  | 
            |
| 36 | |||
| 37 | // Get the list of registered CANDIDATES  | 
            ||
| 38 | 102 | public function getCandidatesList (bool $stringMode = false) : array  | 
            |
| 52 | |||
| 53 | 110 | public function getCandidateKey ($candidate_id)  | 
            |
| 61 | |||
| 62 | 91 | public function getCandidateId (int $candidate_key, bool $onlyName = false)  | 
            |
| 63 |     { | 
            ||
| 64 | 91 | if (!array_key_exists($candidate_key, $this->_Candidates)) :  | 
            |
| 65 | 1 | return false;  | 
            |
| 66 | else :  | 
            ||
| 67 | 91 | return ($onlyName) ? $this->_Candidates[$candidate_key]->getName() : $this->_Candidates[$candidate_key];  | 
            |
| 68 | endif;  | 
            ||
| 69 | }  | 
            ||
| 70 | |||
| 71 | 138 | public function existCandidateId ($candidate_id, bool $strict = true) : bool  | 
            |
| 75 | |||
| 76 | 3 | public function getCandidateObjectByName (string $s)  | 
            |
| 87 | |||
| 88 | |||
| 89 | /////////// ADD & REMOVE CANDIDATE ///////////  | 
            ||
| 90 | |||
| 91 | // Add a vote candidate before voting  | 
            ||
| 92 | 139 | public function addCandidate ($candidate_id = null) : Candidate  | 
            |
| 93 |     { | 
            ||
| 94 | // only if the vote has not started  | 
            ||
| 95 | 139 | if ( $this->_State > 1 ) :  | 
            |
| 96 | 1 | throw new CondorcetException(2);  | 
            |
| 97 | endif;  | 
            ||
| 98 | |||
| 99 | // Filter  | 
            ||
| 100 | 139 | if ( is_bool($candidate_id) || is_array($candidate_id) || (is_object($candidate_id) && !($candidate_id instanceof Candidate)) ) :  | 
            |
| 101 | 1 | throw new CondorcetException(1);  | 
            |
| 102 | endif;  | 
            ||
| 103 | |||
| 104 | |||
| 105 | // Process  | 
            ||
| 106 | 138 | if ( empty($candidate_id) ) :  | 
            |
| 107 | 5 | while ( !$this->canAddCandidate($this->_i_CandidateId) ) :  | 
            |
| 108 | 5 | $this->_i_CandidateId++;  | 
            |
| 109 | endwhile;  | 
            ||
| 110 | |||
| 111 | 5 | $newCandidate = new Candidate($this->_i_CandidateId);  | 
            |
| 112 | else : // Try to add the candidate_id  | 
            ||
| 113 | 135 | $newCandidate = ($candidate_id instanceof Candidate) ? $candidate_id : new Candidate ((string) $candidate_id);  | 
            |
| 114 | |||
| 115 | 135 | if ( !$this->canAddCandidate($newCandidate) ) :  | 
            |
| 116 | 1 | throw new CondorcetException(3,$candidate_id);  | 
            |
| 117 | endif;  | 
            ||
| 118 | endif;  | 
            ||
| 119 | |||
| 120 | // Register it  | 
            ||
| 121 | 138 | $this->_Candidates[] = $newCandidate;  | 
            |
| 122 | |||
| 123 | // Linking  | 
            ||
| 124 | 138 | $newCandidate->registerLink($this);  | 
            |
| 125 | |||
| 126 | // Disallow other candidate object name matching  | 
            ||
| 127 | 138 | $newCandidate->setProvisionalState(false);  | 
            |
| 128 | |||
| 129 | 138 | return $newCandidate;  | 
            |
| 130 | }  | 
            ||
| 131 | |||
| 132 | 138 | public function canAddCandidate ($candidate_id) : bool  | 
            |
| 136 | |||
| 137 | // Destroy a register vote candidate before voting  | 
            ||
| 138 | 4 | public function removeCandidate ($list) : array  | 
            |
| 139 |     { | 
            ||
| 140 | // only if the vote has not started  | 
            ||
| 141 | 4 | if ( $this->_State > 1 ) :  | 
            |
| 142 | 1 | throw new CondorcetException(2);  | 
            |
| 143 | endif;  | 
            ||
| 144 | |||
| 145 | 3 | if ( !is_array($list) ) :  | 
            |
| 146 | 3 | $list = [$list];  | 
            |
| 147 | endif;  | 
            ||
| 148 | |||
| 149 | 3 | foreach ($list as &$candidate_id) :  | 
            |
| 150 | 3 | $candidate_key = $this->getCandidateKey($candidate_id);  | 
            |
| 151 | |||
| 152 | 3 | if ( $candidate_key === false ) :  | 
            |
| 153 | throw new CondorcetException(4,$candidate_id);  | 
            ||
| 154 | endif;  | 
            ||
| 155 | |||
| 156 | 3 | $candidate_id = $candidate_key;  | 
            |
| 157 | endforeach;  | 
            ||
| 158 | |||
| 159 | 3 | $rem = [];  | 
            |
| 160 | 3 | foreach ($list as $candidate_key) :  | 
            |
| 161 | 3 | $this->_Candidates[$candidate_key]->destroyLink($this);  | 
            |
| 162 | |||
| 163 | 3 | $rem[] = $this->_Candidates[$candidate_key];  | 
            |
| 164 | |||
| 165 | 3 | unset($this->_Candidates[$candidate_key]);  | 
            |
| 166 | endforeach;  | 
            ||
| 167 | |||
| 168 | 3 | return $rem;  | 
            |
| 169 | }  | 
            ||
| 170 | |||
| 171 | |||
| 172 | /////////// PARSE CANDIDATES ///////////  | 
            ||
| 173 | |||
| 174 | 1 | public function jsonCandidates (string $input)  | 
            |
| 195 | |||
| 196 | 18 | public function parseCandidates (string $input, bool $allowFile = true)  | 
            |
| 197 |     { | 
            ||
| 198 | 18 | $input = CondorcetUtil::prepareParse($input, $allowFile);  | 
            |
| 224 | }  | 
            ||
| 225 |