Complex classes like Query 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 Query, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class Query |
||
21 | { |
||
22 | const DEFAULT_LIMIT = 100; |
||
23 | const MAX_LIMIT = 1000; |
||
24 | |||
25 | /** |
||
26 | * @var Model|string |
||
27 | */ |
||
28 | private $model; |
||
29 | |||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | private $joins; |
||
34 | |||
35 | /** |
||
36 | * @var array |
||
37 | */ |
||
38 | private $eagerLoaded; |
||
39 | |||
40 | /** |
||
41 | * @var array |
||
42 | */ |
||
43 | private $where; |
||
44 | |||
45 | /** |
||
46 | * @var int |
||
47 | */ |
||
48 | private $limit; |
||
49 | |||
50 | /** |
||
51 | * @var int |
||
52 | */ |
||
53 | private $start; |
||
54 | |||
55 | /** |
||
56 | * @var array |
||
57 | */ |
||
58 | private $sort; |
||
59 | |||
60 | /** |
||
61 | * @param Model|string $model |
||
62 | */ |
||
63 | public function __construct($model = '') |
||
73 | |||
74 | /** |
||
75 | * Gets the model class associated with this query. |
||
76 | * |
||
77 | * @return Model|string |
||
78 | */ |
||
79 | public function getModel() |
||
83 | |||
84 | /** |
||
85 | * Sets the limit for this query. |
||
86 | * |
||
87 | * @return $this |
||
88 | */ |
||
89 | public function limit(int $limit) |
||
95 | |||
96 | /** |
||
97 | * Gets the limit for this query. |
||
98 | */ |
||
99 | public function getLimit(): int |
||
103 | |||
104 | /** |
||
105 | * Sets the start offset. |
||
106 | * |
||
107 | * @return $this |
||
108 | */ |
||
109 | public function start(int $start) |
||
115 | |||
116 | /** |
||
117 | * Gets the start offset. |
||
118 | */ |
||
119 | public function getStart(): int |
||
123 | |||
124 | /** |
||
125 | * Sets the sort pattern for the query. |
||
126 | * |
||
127 | * @param array|string $sort |
||
128 | * |
||
129 | * @return $this |
||
130 | */ |
||
131 | public function sort($sort) |
||
156 | |||
157 | /** |
||
158 | * Gets the sort parameters. |
||
159 | */ |
||
160 | public function getSort(): array |
||
164 | |||
165 | /** |
||
166 | * Sets the where parameters. |
||
167 | * Accepts the following forms: |
||
168 | * i) where(['name' => 'Bob']) |
||
169 | * ii) where('name', 'Bob') |
||
170 | * iii) where('balance', 100, '>') |
||
171 | * iv) where('balance > 100'). |
||
172 | * |
||
173 | * @param array|string $where |
||
174 | * @param mixed $value optional value |
||
175 | * @param string|null $condition optional condition |
||
176 | * |
||
177 | * @return $this |
||
178 | */ |
||
179 | public function where($where, $value = null, $condition = null) |
||
207 | |||
208 | /** |
||
209 | * Gets the where parameters. |
||
210 | */ |
||
211 | public function getWhere(): array |
||
215 | |||
216 | /** |
||
217 | * Adds a join to the query. Matches a property on this model |
||
218 | * to the ID of the model we are joining. |
||
219 | * |
||
220 | * @param string $model model being joined |
||
221 | * @param string $column name of local property |
||
222 | * |
||
223 | * @return $this |
||
224 | */ |
||
225 | public function join($model, string $column, string $foreignKey) |
||
231 | |||
232 | /** |
||
233 | * Gets the joins. |
||
234 | */ |
||
235 | public function getJoins(): array |
||
239 | |||
240 | /** |
||
241 | * Marks a relationship property on the model that should be eager loaded. |
||
242 | * |
||
243 | * @param string $k local property containing the relationship |
||
244 | * |
||
245 | * @return $this |
||
246 | */ |
||
247 | public function with(string $k) |
||
255 | |||
256 | /** |
||
257 | * Gets the relationship properties that are going to be eager-loaded. |
||
258 | */ |
||
259 | public function getWith(): array |
||
263 | |||
264 | /** |
||
265 | * Executes the query against the model's driver. |
||
266 | * |
||
267 | * @return Model[] results |
||
268 | */ |
||
269 | public function execute(): array |
||
278 | |||
279 | /** |
||
280 | * Creates an iterator for a search. |
||
281 | * |
||
282 | * @return Iterator |
||
283 | */ |
||
284 | public function all() |
||
288 | |||
289 | /** |
||
290 | * Finds exactly one model. If zero or more than one are found |
||
291 | * then this function will fail. |
||
292 | * |
||
293 | * @throws ModelNotFoundException when the result is not exactly one model |
||
294 | */ |
||
295 | public function one(): Model |
||
309 | |||
310 | /** |
||
311 | * Finds exactly one model. If zero or more than one are found |
||
312 | * then this function will return null. |
||
313 | * |
||
314 | * @throws ModelNotFoundException when the result is not exactly one model |
||
315 | */ |
||
316 | public function oneOrNull(): ?Model |
||
322 | |||
323 | /** |
||
324 | * Executes the query against the model's driver and returns the first result. |
||
325 | * |
||
326 | * @return Model[] |
||
327 | */ |
||
328 | public function first(int $limit = 1): array |
||
332 | |||
333 | /** |
||
334 | * Gets the number of models matching the query. |
||
335 | */ |
||
336 | public function count(): int |
||
343 | |||
344 | /** |
||
345 | * Gets the sum of a property matching the query. |
||
346 | * |
||
347 | * @return number |
||
348 | */ |
||
349 | public function sum(string $property) |
||
356 | |||
357 | /** |
||
358 | * Gets the average of a property matching the query. |
||
359 | * |
||
360 | * @return number |
||
361 | */ |
||
362 | public function average(string $property) |
||
369 | |||
370 | /** |
||
371 | * Gets the max of a property matching the query. |
||
372 | * |
||
373 | * @return number |
||
374 | */ |
||
375 | public function max(string $property) |
||
382 | |||
383 | /** |
||
384 | * Gets the min of a property matching the query. |
||
385 | * |
||
386 | * @return number |
||
387 | */ |
||
388 | public function min(string $property) |
||
395 | |||
396 | /** |
||
397 | * Updates all of the models matched by this query. |
||
398 | * |
||
399 | * @todo should be optimized to be done in a single call to the data layer |
||
400 | * |
||
401 | * @param array $params key-value update parameters |
||
402 | * |
||
403 | * @return int # of models updated |
||
404 | */ |
||
405 | public function set(array $params): int |
||
415 | |||
416 | /** |
||
417 | * Deletes all of the models matched by this query. |
||
418 | * |
||
419 | * @todo should be optimized to be done in a single call to the data layer |
||
420 | * |
||
421 | * @return int # of models deleted |
||
422 | */ |
||
423 | public function delete(): int |
||
433 | } |
||
434 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.