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 |
||
16 | class Result implements \ArrayAccess, \Countable, \Iterator |
||
17 | { |
||
18 | use CondorcetVersion; |
||
19 | |||
20 | // Implement Iterator |
||
21 | |||
22 | 1 | public function rewind() :void { |
|
25 | |||
26 | 1 | public function current () { |
|
29 | |||
30 | 1 | public function key () : int { |
|
33 | |||
34 | 1 | public function next () : void { |
|
37 | |||
38 | 1 | public function valid () : bool { |
|
41 | |||
42 | // Implement ArrayAccess |
||
43 | |||
44 | 1 | public function offsetSet ($offset, $value) : void { |
|
47 | |||
48 | 1 | public function offsetExists ($offset) : bool { |
|
51 | |||
52 | 1 | public function offsetUnset ($offset) : void { |
|
55 | |||
56 | 51 | public function offsetGet ($offset) { |
|
59 | |||
60 | // Implement Countable |
||
61 | |||
62 | 3 | public function count () : int { |
|
65 | |||
66 | |||
67 | /////////// CONSTRUCTOR /////////// |
||
68 | |||
69 | protected $_Result; |
||
70 | protected $_UserResult; |
||
71 | protected $_stringResult; |
||
72 | protected $_CondorcetWinner; |
||
73 | protected $_CondorcetLoser; |
||
74 | |||
75 | protected $_Stats; |
||
76 | |||
77 | protected $_warning = []; |
||
78 | |||
79 | protected $_BuildTimeStamp; |
||
80 | protected $_fromMethod; |
||
81 | protected $_byClass; |
||
82 | protected $_ElectionCondorcetVersion; |
||
83 | |||
84 | |||
85 | 87 | public function __construct (string $fromMethod, string $byClass, Election $election, array $result, $stats) |
|
100 | |||
101 | |||
102 | /////////// Get Result /////////// |
||
103 | |||
104 | 87 | public function getResultAsArray (bool $convertToString = false) : array |
|
120 | |||
121 | 4 | public function getResultAsString () : string |
|
125 | |||
126 | 1 | public function getOriginalResultArrayWithString () : array |
|
130 | |||
131 | 1 | public function getResultAsInternalKey () : array |
|
135 | |||
136 | 19 | public function getStats () { |
|
139 | |||
140 | 50 | public function getWinner () { |
|
143 | |||
144 | 3 | public function getLoser () { |
|
147 | |||
148 | 1 | public function getCondorcetWinner () : ?Candidate { |
|
151 | |||
152 | 1 | public function getCondorcetLoser () : ?Candidate { |
|
155 | |||
156 | 87 | protected function makeUserResult (Election $election) : array |
|
180 | |||
181 | |||
182 | /////////// Get & Set MetaData /////////// |
||
183 | |||
184 | 1 | public function addWarning (int $type, string $msg = null) : bool |
|
190 | |||
191 | 2 | public function getWarning (?int $type = null) : array |
|
207 | |||
208 | 1 | public function getClassGenerator () : string { |
|
211 | |||
212 | 1 | public function getMethod () : string { |
|
215 | |||
216 | 1 | public function getBuildTimeStamp () : float { |
|
219 | |||
220 | 1 | public function getCondorcetElectionGeneratorVersion () : string { |
|
223 | } |
||
224 |