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 | function rewind() :void { |
||
| 29 | |||
| 30 | function current () { |
||
| 33 | |||
| 34 | function key () : int { |
||
| 37 | |||
| 38 | function next () : void { |
||
| 41 | |||
| 42 | function valid () : bool { |
||
| 45 | |||
| 46 | // Implement ArrayAccess |
||
| 47 | |||
| 48 | public function offsetSet ($offset, $value) : void { |
||
| 51 | |||
| 52 | public function offsetExists ($offset) : bool { |
||
| 55 | |||
| 56 | public function offsetUnset ($offset) : void { |
||
| 59 | |||
| 60 | 2 | public function offsetGet ($offset) { |
|
| 63 | |||
| 64 | // Implement Countable |
||
| 65 | |||
| 66 | 2 | 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 | 4 | public function __construct (string $fromMethod, string $byClass, Election $election, array $result, $stats) |
|
| 102 | |||
| 103 | |||
| 104 | /////////// Get Result /////////// |
||
| 105 | |||
| 106 | 4 | public function getResultAsArray (bool $convertToString = false) : array |
|
| 122 | |||
| 123 | public function getResultAsString () |
||
| 127 | |||
| 128 | public function getOriginalArrayWithString () : array |
||
| 132 | |||
| 133 | public function getResultAsInternalKey () : array |
||
| 137 | |||
| 138 | 1 | public function getStats () { |
|
| 141 | |||
| 142 | 2 | public function getWinner () { |
|
| 145 | |||
| 146 | 2 | public function getLoser () { |
|
| 149 | |||
| 150 | public function getCondorcetWinner () { |
||
| 153 | |||
| 154 | public function getCondorcetLoser () { |
||
| 157 | |||
| 158 | 4 | protected function makeUserResult (Election $election) : array |
|
| 182 | |||
| 183 | |||
| 184 | /////////// Get & Set MetaData /////////// |
||
| 185 | |||
| 186 | public function addWarning (int $type, string $msg = null) : bool |
||
| 192 | |||
| 193 | 1 | public function getWarning ($type = null) : array |
|
| 209 | |||
| 210 | public function getClassGenerator () : string { |
||
| 213 | |||
| 214 | public function getMethod () : string { |
||
| 217 | |||
| 218 | public function getBuildTimeStamp () : float { |
||
| 221 | |||
| 222 | public function getCondorcetElectionGeneratorVersion () : string { |
||
| 225 | |||
| 226 | } |
||
| 227 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.