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) |
||
200 | |||
201 | /** |
||
202 | * Gets the where parameters. |
||
203 | */ |
||
204 | public function getWhere(): array |
||
208 | |||
209 | /** |
||
210 | * Adds a join to the query. Matches a property on this model |
||
211 | * to the ID of the model we are joining. |
||
212 | * |
||
213 | * @param string $model model being joined |
||
214 | * @param string $column name of local property |
||
215 | * |
||
216 | * @return $this |
||
217 | */ |
||
218 | public function join($model, string $column, string $foreignKey) |
||
224 | |||
225 | /** |
||
226 | * Gets the joins. |
||
227 | */ |
||
228 | public function getJoins(): array |
||
232 | |||
233 | /** |
||
234 | * Marks a relationship property on the model that should be eager loaded. |
||
235 | * |
||
236 | * @param string $k local property containing the relationship |
||
237 | * |
||
238 | * @return $this |
||
239 | */ |
||
240 | public function with(string $k) |
||
248 | |||
249 | /** |
||
250 | * Gets the relationship properties that are going to be eager-loaded. |
||
251 | */ |
||
252 | public function getWith(): array |
||
256 | |||
257 | /** |
||
258 | * Executes the query against the model's driver. |
||
259 | * |
||
260 | * @return Model[] results |
||
261 | */ |
||
262 | public function execute(): array |
||
271 | |||
272 | /** |
||
273 | * Creates an iterator for a search. |
||
274 | * |
||
275 | * @return Iterator |
||
276 | */ |
||
277 | public function all() |
||
281 | |||
282 | /** |
||
283 | * Finds exactly one model. If zero or more than one are found |
||
284 | * then this function will fail. |
||
285 | * |
||
286 | * @throws ModelNotFoundException when the result is not exactly one model |
||
287 | */ |
||
288 | public function one(): Model |
||
302 | |||
303 | /** |
||
304 | * Finds exactly one model. If zero or more than one are found |
||
305 | * then this function will return null. |
||
306 | * |
||
307 | * @throws ModelNotFoundException when the result is not exactly one model |
||
308 | */ |
||
309 | public function oneOrNull(): ?Model |
||
315 | |||
316 | /** |
||
317 | * Executes the query against the model's driver and returns the first result. |
||
318 | * |
||
319 | * @param bool $alwaysArray when false (deprecated) returns a single model if 1 result and null if no results |
||
320 | * |
||
321 | * @return Model[]|Model|null when $limit = 1 and $alwaysArray = false, returns a single model or null, otherwise returns an array |
||
322 | */ |
||
323 | public function first(int $limit = 1, bool $alwaysArray = false) |
||
333 | |||
334 | /** |
||
335 | * Gets the number of models matching the query. |
||
336 | */ |
||
337 | public function count(): int |
||
344 | |||
345 | /** |
||
346 | * Gets the sum of a property matching the query. |
||
347 | * |
||
348 | * @return number |
||
349 | */ |
||
350 | public function sum(string $property) |
||
357 | |||
358 | /** |
||
359 | * Gets the average of a property matching the query. |
||
360 | * |
||
361 | * @return number |
||
362 | */ |
||
363 | public function average(string $property) |
||
370 | |||
371 | /** |
||
372 | * Gets the max of a property matching the query. |
||
373 | * |
||
374 | * @return number |
||
375 | */ |
||
376 | public function max(string $property) |
||
383 | |||
384 | /** |
||
385 | * Gets the min of a property matching the query. |
||
386 | * |
||
387 | * @return number |
||
388 | */ |
||
389 | public function min(string $property) |
||
396 | |||
397 | /** |
||
398 | * Updates all of the models matched by this query. |
||
399 | * |
||
400 | * @todo should be optimized to be done in a single call to the data layer |
||
401 | * |
||
402 | * @param array $params key-value update parameters |
||
403 | * |
||
404 | * @return int # of models updated |
||
405 | */ |
||
406 | public function set(array $params): int |
||
416 | |||
417 | /** |
||
418 | * Deletes all of the models matched by this query. |
||
419 | * |
||
420 | * @todo should be optimized to be done in a single call to the data layer |
||
421 | * |
||
422 | * @return int # of models deleted |
||
423 | */ |
||
424 | public function delete(): int |
||
434 | } |
||
435 |
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.