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 |
||
12 | class Result implements IteratorAggregate, ResultInterface |
||
13 | { |
||
14 | /** |
||
15 | * @var mysqli |
||
16 | */ |
||
17 | private $mysqli; |
||
18 | |||
19 | /** |
||
20 | * @var mysqli_stmt |
||
21 | */ |
||
22 | private $stmt; |
||
23 | |||
24 | /** |
||
25 | * @var mysqli_result |
||
26 | */ |
||
27 | private $result; |
||
28 | |||
29 | private $storage = []; |
||
30 | |||
31 | /** |
||
32 | * @var bool |
||
33 | */ |
||
34 | private $storageEnabled = true; |
||
35 | |||
36 | /** |
||
37 | * Result constructor. |
||
38 | * @param mysqli $mysqli |
||
39 | * @param mysqli_stmt $stmt |
||
40 | * @param mysqli_result $result |
||
41 | */ |
||
42 | public function __construct(mysqli $mysqli, mysqli_result $result = null, mysqli_stmt $stmt = null) |
||
48 | |||
49 | /** |
||
50 | * @inheritDoc |
||
51 | */ |
||
52 | public function getLastInsertId() |
||
56 | |||
57 | /** |
||
58 | * @inheritDoc |
||
59 | */ |
||
60 | public function count() |
||
64 | |||
65 | /** |
||
66 | * @inheritDoc |
||
67 | */ |
||
68 | public function asArray(): array |
||
88 | |||
89 | /** |
||
90 | * @inheritDoc |
||
91 | */ |
||
92 | public function asRow(): ?array |
||
116 | |||
117 | /** |
||
118 | * @inheritDoc |
||
119 | */ |
||
120 | public function asList(): array |
||
149 | |||
150 | /** |
||
151 | * @inheritDoc |
||
152 | */ |
||
153 | public function asValue() |
||
182 | |||
183 | /** |
||
184 | * @inheritDoc |
||
185 | */ |
||
186 | public function getIterator() |
||
208 | |||
209 | |||
210 | /** |
||
211 | * If asRow(), asList() or asValue() was called earlier, the iterator may be incomplete. |
||
212 | * In such case we need to rewind the iterator by executing the statement a second time. |
||
213 | * You should avoid to call getIterator() and asRow(), etc. with the same resultset. |
||
214 | * |
||
215 | * @return bool |
||
216 | */ |
||
217 | private function shouldResetResultset(): bool |
||
221 | |||
222 | |||
223 | /** |
||
224 | * Reset the resultset. |
||
225 | */ |
||
226 | private function resetResultset() |
||
233 | |||
234 | /** |
||
235 | * @return ResultInterface |
||
236 | */ |
||
237 | public function withoutStorage(): ResultInterface |
||
244 | } |
||
245 |