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 |
||
19 | class Result implements \ArrayAccess, \Countable, \Iterator |
||
20 | { |
||
21 | use CondorcetVersion; |
||
22 | |||
23 | // Implement Iterator |
||
24 | |||
25 | 1 | public function rewind() :void { |
|
26 | 1 | reset($this->_UserResult); |
|
27 | 1 | } |
|
28 | |||
29 | 1 | public function current () { |
|
30 | 1 | return current($this->_UserResult); |
|
31 | } |
||
32 | |||
33 | 1 | public function key () : int { |
|
34 | 1 | return key($this->_UserResult); |
|
35 | } |
||
36 | |||
37 | 1 | public function next () : void { |
|
38 | 1 | next($this->_UserResult); |
|
39 | 1 | } |
|
40 | |||
41 | 1 | public function valid () : bool { |
|
42 | 1 | return (key($this->_UserResult) === null) ? false : true; |
|
43 | } |
||
44 | |||
45 | // Implement ArrayAccess |
||
46 | |||
47 | 1 | public function offsetSet ($offset, $value) : void { |
|
48 | 1 | throw new CondorcetException (0,"Can't change a result"); |
|
49 | } |
||
50 | |||
51 | public function offsetExists ($offset) : bool { |
||
54 | |||
55 | 1 | public function offsetUnset ($offset) : void { |
|
56 | 1 | throw new CondorcetException (0,"Can't change a result"); |
|
57 | } |
||
58 | |||
59 | 50 | public function offsetGet ($offset) { |
|
62 | |||
63 | // Implement Countable |
||
64 | |||
65 | 3 | public function count () : int { |
|
68 | |||
69 | |||
70 | /////////// CONSTRUCTOR /////////// |
||
71 | |||
72 | protected $_Result; |
||
73 | protected $_UserResult; |
||
74 | protected $_stringResult; |
||
75 | protected $_CondorcetWinner; |
||
76 | protected $_CondorcetLoser; |
||
77 | |||
78 | protected $_Stats; |
||
79 | |||
80 | protected $_warning = []; |
||
81 | |||
82 | protected $_BuildTimeStamp; |
||
83 | protected $_fromMethod; |
||
84 | protected $_byClass; |
||
85 | protected $_ElectionCondorcetVersion; |
||
86 | |||
87 | |||
88 | 72 | public function __construct (string $fromMethod, string $byClass, Election $election, array $result, $stats) |
|
101 | |||
102 | |||
103 | /////////// Get Result /////////// |
||
104 | |||
105 | 72 | public function getResultAsArray (bool $convertToString = false) : array |
|
121 | |||
122 | 3 | public function getResultAsString () |
|
126 | |||
127 | 1 | public function getOriginalArrayWithString () : array |
|
128 | { |
||
129 | 1 | return $this->_stringResult; |
|
130 | } |
||
131 | |||
132 | public function getResultAsInternalKey () : array |
||
136 | |||
137 | 11 | public function getStats () { |
|
140 | |||
141 | 49 | public function getWinner () { |
|
144 | |||
145 | 3 | public function getLoser () { |
|
148 | |||
149 | 1 | public function getCondorcetWinner () { |
|
150 | 1 | return $this->_CondorcetWinner; |
|
151 | } |
||
152 | |||
153 | 1 | public function getCondorcetLoser () { |
|
154 | 1 | return $this->_CondorcetLoser; |
|
155 | } |
||
156 | |||
157 | 72 | 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 ($type = null) : array |
|
208 | |||
209 | 1 | public function getClassGenerator () : string { |
|
210 | 1 | return $this->_byClass; |
|
211 | } |
||
212 | |||
213 | 1 | public function getMethod () : string { |
|
214 | 1 | return $this->_fromMethod; |
|
215 | } |
||
216 | |||
217 | 1 | public function getBuildTimeStamp () : float { |
|
218 | 1 | return (float) $this->_BuildTimeStamp; |
|
220 | |||
221 | 1 | public function getCondorcetElectionGeneratorVersion () : string { |
|
224 | |||
225 | } |
||
226 |