Complex classes like ResultSet 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 ResultSet, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class ResultSet implements ResultSetInterface |
||
9 | { |
||
10 | /** |
||
11 | * @var int |
||
12 | */ |
||
13 | protected $num_rows = 0; |
||
14 | |||
15 | /** |
||
16 | * @var int |
||
17 | */ |
||
18 | protected $cursor = 0; |
||
19 | |||
20 | /** |
||
21 | * @var int |
||
22 | */ |
||
23 | protected $next_cursor = 0; |
||
24 | |||
25 | /** |
||
26 | * @var int |
||
27 | */ |
||
28 | protected $affected_rows = 0; // leave to 0 so SELECT etc. will be coherent |
||
29 | |||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | protected $fields; |
||
34 | |||
35 | /** |
||
36 | * @var null|array |
||
37 | */ |
||
38 | protected $stored; |
||
39 | |||
40 | /** |
||
41 | * @var null|array |
||
42 | */ |
||
43 | protected $fetched; |
||
44 | |||
45 | /** |
||
46 | * @var ResultSetAdapterInterface |
||
47 | */ |
||
48 | protected $adapter; |
||
49 | |||
50 | /** |
||
51 | * @param ResultSetAdapterInterface $adapter |
||
52 | */ |
||
53 | public function __construct(ResultSetAdapterInterface $adapter) |
||
62 | |||
63 | /** |
||
64 | * @inheritdoc |
||
65 | */ |
||
66 | public function hasRow($num) |
||
70 | |||
71 | /** |
||
72 | * @inheritdoc |
||
73 | */ |
||
74 | public function hasNextRow() |
||
78 | |||
79 | /** |
||
80 | * @inheritdoc |
||
81 | */ |
||
82 | public function getAffectedRows() |
||
86 | |||
87 | /** |
||
88 | * @inheritdoc |
||
89 | */ |
||
90 | public function offsetExists($offset) |
||
94 | |||
95 | /** |
||
96 | * @inheritdoc |
||
97 | */ |
||
98 | public function offsetGet($offset) |
||
102 | |||
103 | /** |
||
104 | * @inheritdoc |
||
105 | * @codeCoverageIgnore |
||
106 | */ |
||
107 | public function offsetSet($offset, $value) |
||
111 | |||
112 | /** |
||
113 | * @inheritdoc |
||
114 | * @codeCoverageIgnore |
||
115 | */ |
||
116 | public function offsetUnset($offset) |
||
120 | |||
121 | /** |
||
122 | * @inheritdoc |
||
123 | */ |
||
124 | public function current() |
||
131 | |||
132 | /** |
||
133 | * @inheritdoc |
||
134 | */ |
||
135 | public function next() |
||
139 | |||
140 | /** |
||
141 | * @inheritdoc |
||
142 | */ |
||
143 | public function key() |
||
147 | |||
148 | /** |
||
149 | * @inheritdoc |
||
150 | */ |
||
151 | public function valid() |
||
159 | |||
160 | /** |
||
161 | * @inheritdoc |
||
162 | */ |
||
163 | public function rewind() |
||
173 | |||
174 | /** |
||
175 | * Returns the number of rows in the result set |
||
176 | * @inheritdoc |
||
177 | */ |
||
178 | public function count() |
||
182 | |||
183 | protected function init() |
||
192 | |||
193 | /** |
||
194 | * @param array $numeric_array |
||
195 | * |
||
196 | * @return array |
||
197 | */ |
||
198 | protected function makeAssoc($numeric_array) |
||
207 | |||
208 | /** |
||
209 | * @param bool $assoc |
||
210 | * |
||
211 | * @return array|bool|null |
||
212 | */ |
||
213 | protected function fetchFromStore($assoc = true) |
||
227 | |||
228 | /** |
||
229 | * @param bool $assoc |
||
230 | * |
||
231 | * @return array|bool |
||
232 | */ |
||
233 | protected function fetchAllFromStore($assoc) |
||
249 | |||
250 | /** |
||
251 | * @param bool $assoc |
||
252 | * |
||
253 | * @return array |
||
254 | */ |
||
255 | protected function fetchAll($assoc = true) |
||
268 | |||
269 | /** |
||
270 | * @inheritdoc |
||
271 | */ |
||
272 | public function store() |
||
286 | |||
287 | /** |
||
288 | * @inheritdoc |
||
289 | */ |
||
290 | public function getStored() |
||
299 | |||
300 | /** |
||
301 | * @inheritdoc |
||
302 | */ |
||
303 | public function toRow($num) |
||
318 | |||
319 | /** |
||
320 | * @inheritdoc |
||
321 | */ |
||
322 | public function toNextRow() |
||
328 | |||
329 | /** |
||
330 | * @inheritdoc |
||
331 | */ |
||
332 | public function fetchAllAssoc() |
||
336 | |||
337 | /** |
||
338 | * @inheritdoc |
||
339 | */ |
||
340 | public function fetchAllNum() |
||
344 | |||
345 | /** |
||
346 | * @inheritdoc |
||
347 | */ |
||
348 | public function fetchAssoc() |
||
352 | |||
353 | /** |
||
354 | * @inheritdoc |
||
355 | */ |
||
356 | public function fetchNum() |
||
360 | |||
361 | /** |
||
362 | * @param bool $assoc |
||
363 | * |
||
364 | * @return array|null |
||
365 | */ |
||
366 | protected function fetch($assoc = true) |
||
380 | |||
381 | /** |
||
382 | * @inheritdoc |
||
383 | */ |
||
384 | public function freeResult() |
||
390 | } |
||
391 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.