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 |
||
11 | class Result implements IteratorAggregate, ResultInterface |
||
12 | { |
||
13 | /** |
||
14 | * @var PDO |
||
15 | */ |
||
16 | private $pdo; |
||
17 | |||
18 | /** |
||
19 | * @var Statement |
||
20 | */ |
||
21 | private $stmt; |
||
22 | |||
23 | /** |
||
24 | * @var array |
||
25 | */ |
||
26 | private $storage = []; |
||
27 | |||
28 | /** |
||
29 | * @var bool |
||
30 | */ |
||
31 | private $storageEnabled = true; |
||
32 | |||
33 | /** |
||
34 | * Result constructor. |
||
35 | * @param PDO $pdo |
||
36 | * @param Statement $stmt |
||
37 | */ |
||
38 | public function __construct(PDO $pdo, PDOStatement $stmt = null) |
||
43 | |||
44 | /** |
||
45 | * @inheritDoc |
||
46 | */ |
||
47 | public function getLastInsertId() |
||
51 | |||
52 | /** |
||
53 | * @inheritDoc |
||
54 | */ |
||
55 | public function count() |
||
59 | |||
60 | /** |
||
61 | * @inheritDoc |
||
62 | */ |
||
63 | public function asArray(): array |
||
86 | |||
87 | /** |
||
88 | * @inheritDoc |
||
89 | */ |
||
90 | public function asRow(): ?array |
||
113 | |||
114 | /** |
||
115 | * @inheritDoc |
||
116 | */ |
||
117 | public function asList(): array |
||
147 | |||
148 | /** |
||
149 | * @inheritDoc |
||
150 | */ |
||
151 | public function asValue() |
||
179 | |||
180 | |||
181 | /** |
||
182 | * @inheritDoc |
||
183 | */ |
||
184 | public function getIterator() |
||
210 | |||
211 | /** |
||
212 | * If asRow(), asList() or asValue() was called earlier, the iterator may be incomplete. |
||
213 | * In such case we need to rewind the iterator by executing the statement a second time. |
||
214 | * You should avoid to call getIterator() and asRow(), etc. with the same resultset. |
||
215 | * |
||
216 | * @return bool |
||
217 | */ |
||
218 | private function shouldResetResultset(): bool |
||
222 | |||
223 | /** |
||
224 | * Reset the resultset. |
||
225 | */ |
||
226 | private function resetResultset() |
||
230 | |||
231 | /** |
||
232 | * @return ResultInterface |
||
233 | */ |
||
234 | public function withoutStorage(): ResultInterface |
||
241 | } |
||
242 |
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.