Complex classes like Result 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 Result, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class Result implements \ArrayAccess, \Countable, \Iterator |
||
| 18 | { |
||
| 19 | use CondorcetVersion; |
||
| 20 | |||
| 21 | // Implement Iterator |
||
| 22 | |||
| 23 | 1 | public function rewind() :void { |
|
| 26 | |||
| 27 | 1 | public function current () { |
|
| 30 | |||
| 31 | 1 | public function key () : int { |
|
| 34 | |||
| 35 | 1 | public function next () : void { |
|
| 38 | |||
| 39 | 1 | public function valid () : bool { |
|
| 42 | |||
| 43 | // Implement ArrayAccess |
||
| 44 | |||
| 45 | 1 | public function offsetSet ($offset, $value) : void { |
|
| 48 | |||
| 49 | 1 | public function offsetExists ($offset) : bool { |
|
| 52 | |||
| 53 | 1 | public function offsetUnset ($offset) : void { |
|
| 56 | |||
| 57 | 51 | public function offsetGet ($offset) { |
|
| 60 | |||
| 61 | // Implement Countable |
||
| 62 | |||
| 63 | 3 | public function count () : int { |
|
| 66 | |||
| 67 | |||
| 68 | /////////// CONSTRUCTOR /////////// |
||
| 69 | |||
| 70 | protected $_Result; |
||
| 71 | protected $_UserResult; |
||
| 72 | protected $_stringResult; |
||
| 73 | protected $_CondorcetWinner; |
||
| 74 | protected $_CondorcetLoser; |
||
| 75 | |||
| 76 | protected $_Stats; |
||
| 77 | |||
| 78 | protected $_warning = []; |
||
| 79 | |||
| 80 | protected $_BuildTimeStamp; |
||
| 81 | protected $_fromMethod; |
||
| 82 | protected $_byClass; |
||
| 83 | protected $_ElectionCondorcetVersion; |
||
| 84 | |||
| 85 | |||
| 86 | 86 | public function __construct (string $fromMethod, string $byClass, Election $election, array $result, $stats) |
|
| 101 | |||
| 102 | |||
| 103 | /////////// Get Result /////////// |
||
| 104 | |||
| 105 | 86 | public function getResultAsArray (bool $convertToString = false) : array |
|
| 121 | |||
| 122 | 3 | public function getResultAsString () : string |
|
| 126 | |||
| 127 | 1 | public function getOriginalArrayWithString () : array |
|
| 131 | |||
| 132 | 1 | public function getResultAsInternalKey () : array |
|
| 133 | { |
||
| 134 | 1 | return $this->_Result; |
|
| 135 | } |
||
| 136 | |||
| 137 | 19 | public function getStats () { |
|
| 140 | |||
| 141 | 50 | public function getWinner () { |
|
| 144 | |||
| 145 | 3 | public function getLoser () { |
|
| 148 | |||
| 149 | 1 | public function getCondorcetWinner () { |
|
| 152 | |||
| 153 | 1 | public function getCondorcetLoser () { |
|
| 156 | |||
| 157 | 86 | protected function makeUserResult (Election $election) : array |
|
| 181 | |||
| 182 | |||
| 183 | /////////// Get & Set MetaData /////////// |
||
| 184 | |||
| 185 | 1 | public function addWarning (int $type, string $msg = null) : bool |
|
| 191 | |||
| 192 | 2 | public function getWarning (?int $type = null) : array |
|
| 193 | { |
||
| 194 | 2 | if ($type === null) : |
|
| 195 | 1 | return $this->_warning; |
|
| 196 | else : |
||
| 197 | 2 | $r = []; |
|
| 198 | |||
| 199 | 2 | foreach ($this->_warning as $oneWarning) : |
|
| 200 | 1 | if ($oneWarning['type'] === $type) : |
|
| 201 | 1 | $r[] = $oneWarning; |
|
| 202 | endif; |
||
| 203 | endforeach; |
||
| 204 | |||
| 205 | 2 | return $r; |
|
| 206 | endif; |
||
| 207 | } |
||
| 208 | |||
| 209 | 1 | public function getClassGenerator () : string { |
|
| 212 | |||
| 213 | 1 | public function getMethod () : string { |
|
| 216 | |||
| 217 | 1 | public function getBuildTimeStamp () : float { |
|
| 220 | |||
| 221 | 1 | public function getCondorcetElectionGeneratorVersion () : string { |
|
| 224 | } |
||
| 225 |