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 |
||
17 | class Result implements \ArrayAccess, \Countable, \Iterator |
||
18 | { |
||
19 | use CondorcetVersion; |
||
20 | |||
21 | // Implement Iterator |
||
22 | |||
23 | 1 | public function rewind() :void { |
|
24 | 1 | reset($this->_UserResult); |
|
25 | 1 | } |
|
26 | |||
27 | 1 | public function current () { |
|
28 | 1 | return current($this->_UserResult); |
|
29 | } |
||
30 | |||
31 | 1 | public function key () : int { |
|
32 | 1 | return key($this->_UserResult); |
|
33 | } |
||
34 | |||
35 | 1 | public function next () : void { |
|
36 | 1 | next($this->_UserResult); |
|
37 | 1 | } |
|
38 | |||
39 | 1 | public function valid () : bool { |
|
40 | 1 | return (key($this->_UserResult) === null) ? false : true; |
|
41 | } |
||
42 | |||
43 | // Implement ArrayAccess |
||
44 | |||
45 | 1 | public function offsetSet ($offset, $value) : void { |
|
46 | 1 | throw new CondorcetException (0,"Can't change a result"); |
|
47 | } |
||
48 | |||
49 | 1 | public function offsetExists ($offset) : bool { |
|
50 | 1 | return isset($this->_UserResult[$offset]); |
|
51 | } |
||
52 | |||
53 | 1 | public function offsetUnset ($offset) : void { |
|
54 | 1 | throw new CondorcetException (0,"Can't change a result"); |
|
55 | } |
||
56 | |||
57 | 50 | public function offsetGet ($offset) { |
|
58 | 50 | return $this->_UserResult[$offset] ?? null; |
|
59 | } |
||
60 | |||
61 | // Implement Countable |
||
62 | |||
63 | 3 | public function count () : int { |
|
64 | 3 | return count($this->_UserResult); |
|
65 | } |
||
66 | |||
67 | |||
68 | /////////// CONSTRUCTOR /////////// |
||
69 | |||
70 | protected $_Result; |
||
71 | protected $_UserResult; |
||
72 | protected $_stringResult; |
||
73 | protected $_CondorcetWinner; |
||
74 | protected $_CondorcetLoser; |
||
75 | |||
76 | protected $_Stats; |
||
77 | |||
78 | protected $_warning = []; |
||
79 | |||
80 | protected $_BuildTimeStamp; |
||
81 | protected $_fromMethod; |
||
82 | protected $_byClass; |
||
83 | protected $_ElectionCondorcetVersion; |
||
84 | |||
85 | |||
86 | 84 | public function __construct (string $fromMethod, string $byClass, Election $election, array $result, $stats) |
|
87 | { |
||
88 | 84 | ksort($result, SORT_NUMERIC); |
|
89 | |||
90 | 84 | $this->_Result = $result; |
|
91 | 84 | $this->_UserResult = $this->makeUserResult($election); |
|
92 | 84 | $this->_stringResult = $this->getResultAsArray(true); |
|
93 | 84 | $this->_Stats = $stats; |
|
94 | 84 | $this->_fromMethod = $fromMethod; |
|
95 | 84 | $this->_byClass = $byClass; |
|
96 | 84 | $this->_ElectionCondorcetVersion = $election->getObjectVersion(); |
|
97 | 84 | $this->_CondorcetWinner = $election->getWinner(); |
|
98 | 84 | $this->_CondorcetLoser = $election->getLoser(); |
|
99 | 84 | $this->_BuildTimeStamp = microtime(true); |
|
100 | 84 | } |
|
101 | |||
102 | |||
103 | /////////// Get Result /////////// |
||
104 | |||
105 | 84 | 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 | 19 | 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 | 84 | 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 { |
|
212 | |||
213 | 1 | public function getMethod () : string { |
|
216 | |||
217 | 1 | public function getBuildTimeStamp () : float { |
|
220 | |||
221 | 1 | public function getCondorcetElectionGeneratorVersion () : string { |
|
224 | } |
||
225 |