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 |
||
21 | class Result implements \ArrayAccess, \Countable, \Iterator |
||
22 | { |
||
23 | use CondorcetVersion; |
||
24 | |||
25 | // Implement Iterator |
||
26 | |||
27 | 1 | public function rewind() :void { |
|
30 | |||
31 | 1 | public function current () { |
|
34 | |||
35 | 1 | public function key () : int { |
|
38 | |||
39 | 1 | public function next () : void { |
|
42 | |||
43 | 1 | public function valid () : bool { |
|
46 | |||
47 | // Implement ArrayAccess |
||
48 | |||
49 | 1 | public function offsetSet ($offset, $value) : void { |
|
52 | |||
53 | 1 | public function offsetExists ($offset) : bool { |
|
56 | |||
57 | 1 | public function offsetUnset ($offset) : void { |
|
60 | |||
61 | 50 | public function offsetGet ($offset) { |
|
64 | |||
65 | // Implement Countable |
||
66 | |||
67 | 3 | public function count () : int { |
|
70 | |||
71 | |||
72 | /////////// CONSTRUCTOR /////////// |
||
73 | |||
74 | protected $_Result; |
||
75 | protected $_UserResult; |
||
76 | protected $_stringResult; |
||
77 | protected $_CondorcetWinner; |
||
78 | protected $_CondorcetLoser; |
||
79 | |||
80 | protected $_Stats; |
||
81 | |||
82 | protected $_warning = []; |
||
83 | |||
84 | protected $_BuildTimeStamp; |
||
85 | protected $_fromMethod; |
||
86 | protected $_byClass; |
||
87 | protected $_ElectionCondorcetVersion; |
||
88 | |||
89 | |||
90 | 72 | public function __construct (string $fromMethod, string $byClass, Election $election, array $result, $stats) |
|
103 | |||
104 | |||
105 | /////////// Get Result /////////// |
||
106 | |||
107 | 72 | public function getResultAsArray (bool $convertToString = false) : array |
|
123 | |||
124 | 3 | public function getResultAsString () |
|
128 | |||
129 | 1 | public function getOriginalArrayWithString () : array |
|
133 | |||
134 | public function getResultAsInternalKey () : array |
||
138 | |||
139 | 11 | public function getStats () { |
|
142 | |||
143 | 49 | public function getWinner () { |
|
146 | |||
147 | 3 | public function getLoser () { |
|
150 | |||
151 | 1 | public function getCondorcetWinner () { |
|
154 | |||
155 | 1 | public function getCondorcetLoser () { |
|
158 | |||
159 | 72 | protected function makeUserResult (Election $election) : array |
|
183 | |||
184 | |||
185 | /////////// Get & Set MetaData /////////// |
||
186 | |||
187 | 1 | public function addWarning (int $type, string $msg = null) : bool |
|
193 | |||
194 | 2 | public function getWarning ($type = null) : array |
|
210 | |||
211 | 1 | public function getClassGenerator () : string { |
|
214 | |||
215 | 1 | public function getMethod () : string { |
|
218 | |||
219 | 1 | public function getBuildTimeStamp () : float { |
|
222 | |||
223 | 1 | public function getCondorcetElectionGeneratorVersion () : string { |
|
226 | |||
227 | } |
||
228 |