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 |
||
| 20 | class Result implements \ArrayAccess, \Countable, \Iterator |
||
| 21 | { |
||
| 22 | use CondorcetVersion; |
||
| 23 | |||
| 24 | // Implement Iterator |
||
| 25 | |||
| 26 | 1 | public function rewind() :void { |
|
| 27 | 1 | reset($this->_UserResult); |
|
| 28 | 1 | } |
|
| 29 | |||
| 30 | 1 | public function current () { |
|
| 31 | 1 | return current($this->_UserResult); |
|
| 32 | } |
||
| 33 | |||
| 34 | 1 | public function key () : int { |
|
| 35 | 1 | return key($this->_UserResult); |
|
| 36 | } |
||
| 37 | |||
| 38 | 1 | public function next () : void { |
|
| 39 | 1 | next($this->_UserResult); |
|
| 40 | 1 | } |
|
| 41 | |||
| 42 | 1 | public function valid () : bool { |
|
| 43 | 1 | return (key($this->_UserResult) === null) ? false : true; |
|
| 44 | } |
||
| 45 | |||
| 46 | // Implement ArrayAccess |
||
| 47 | |||
| 48 | 1 | public function offsetSet ($offset, $value) : void { |
|
| 49 | 1 | throw new CondorcetException (0,"Can't change a result"); |
|
| 50 | } |
||
| 51 | |||
| 52 | 1 | public function offsetExists ($offset) : bool { |
|
| 53 | 1 | return isset($this->_UserResult[$offset]); |
|
| 54 | } |
||
| 55 | |||
| 56 | 1 | public function offsetUnset ($offset) : void { |
|
| 57 | 1 | throw new CondorcetException (0,"Can't change a result"); |
|
| 58 | } |
||
| 59 | |||
| 60 | 50 | public function offsetGet ($offset) { |
|
| 63 | |||
| 64 | // Implement Countable |
||
| 65 | |||
| 66 | 3 | public function count () : int { |
|
| 69 | |||
| 70 | |||
| 71 | /////////// CONSTRUCTOR /////////// |
||
| 72 | |||
| 73 | protected $_Result; |
||
| 74 | protected $_UserResult; |
||
| 75 | protected $_stringResult; |
||
| 76 | protected $_CondorcetWinner; |
||
| 77 | protected $_CondorcetLoser; |
||
| 78 | |||
| 79 | protected $_Stats; |
||
| 80 | |||
| 81 | protected $_warning = []; |
||
| 82 | |||
| 83 | protected $_BuildTimeStamp; |
||
| 84 | protected $_fromMethod; |
||
| 85 | protected $_byClass; |
||
| 86 | protected $_ElectionCondorcetVersion; |
||
| 87 | |||
| 88 | |||
| 89 | 72 | public function __construct (string $fromMethod, string $byClass, Election $election, array $result, $stats) |
|
| 102 | |||
| 103 | |||
| 104 | /////////// Get Result /////////// |
||
| 105 | |||
| 106 | 72 | public function getResultAsArray (bool $convertToString = false) : array |
|
| 122 | |||
| 123 | 3 | public function getResultAsString () |
|
| 124 | { |
||
| 125 | 3 | return VoteUtil::getRankingAsString($this->getResultAsArray(true)); |
|
| 126 | } |
||
| 127 | |||
| 128 | 1 | public function getOriginalArrayWithString () : array |
|
| 129 | { |
||
| 130 | 1 | return $this->_stringResult; |
|
| 131 | } |
||
| 132 | |||
| 133 | public function getResultAsInternalKey () : array |
||
| 137 | |||
| 138 | 11 | public function getStats () { |
|
| 141 | |||
| 142 | 49 | public function getWinner () { |
|
| 145 | |||
| 146 | 3 | public function getLoser () { |
|
| 149 | |||
| 150 | 1 | public function getCondorcetWinner () { |
|
| 151 | 1 | return $this->_CondorcetWinner; |
|
| 152 | } |
||
| 153 | |||
| 154 | 1 | public function getCondorcetLoser () { |
|
| 155 | 1 | return $this->_CondorcetLoser; |
|
| 156 | } |
||
| 157 | |||
| 158 | 72 | protected function makeUserResult (Election $election) : array |
|
| 182 | |||
| 183 | |||
| 184 | /////////// Get & Set MetaData /////////// |
||
| 185 | |||
| 186 | 1 | public function addWarning (int $type, string $msg = null) : bool |
|
| 187 | { |
||
| 188 | 1 | $this->_warning[] = ['type' => $type, 'msg' => $msg]; |
|
| 189 | |||
| 190 | 1 | return true; |
|
| 191 | } |
||
| 192 | |||
| 193 | 2 | public function getWarning ($type = null) : array |
|
| 194 | { |
||
| 209 | |||
| 210 | 1 | public function getClassGenerator () : string { |
|
| 213 | |||
| 214 | 1 | public function getMethod () : string { |
|
| 217 | |||
| 218 | 1 | public function getBuildTimeStamp () : float { |
|
| 221 | |||
| 222 | 1 | public function getCondorcetElectionGeneratorVersion () : string { |
|
| 225 | |||
| 226 | } |
||
| 227 |