Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like BaseQuery 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 BaseQuery, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | abstract class BaseQuery |
||
16 | { |
||
17 | use BaseRelationQuery; |
||
18 | |||
19 | public static $cacheDir = '/bitrix-models'; |
||
20 | |||
21 | /** |
||
22 | * Query select. |
||
23 | * |
||
24 | * @var array |
||
25 | */ |
||
26 | public $select = []; |
||
27 | /** |
||
28 | * Bitrix object to be queried. |
||
29 | * |
||
30 | * @var object|string |
||
31 | */ |
||
32 | protected $bxObject; |
||
33 | |||
34 | /** |
||
35 | * Name of the model that calls the query. |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | protected $modelName; |
||
40 | |||
41 | /** |
||
42 | * Model that calls the query. |
||
43 | * |
||
44 | * @var object |
||
45 | */ |
||
46 | protected $model; |
||
47 | |||
48 | /** |
||
49 | * Query sort. |
||
50 | * |
||
51 | * @var array |
||
52 | */ |
||
53 | public $sort = []; |
||
54 | |||
55 | /** |
||
56 | * Query filter. |
||
57 | * |
||
58 | * @var array |
||
59 | */ |
||
60 | public $filter = []; |
||
61 | |||
62 | /** |
||
63 | * Query navigation. |
||
64 | * |
||
65 | * @var array|bool |
||
66 | */ |
||
67 | public $navigation = false; |
||
68 | |||
69 | /** |
||
70 | * The key to list items in array of results. |
||
71 | * Set to false to have auto incrementing integer. |
||
72 | * |
||
73 | * @var string|bool |
||
74 | */ |
||
75 | public $keyBy = 'ID'; |
||
76 | |||
77 | /** |
||
78 | * Number of minutes to cache a query |
||
79 | * |
||
80 | * @var double|int |
||
81 | */ |
||
82 | public $cacheTtl = 0; |
||
83 | |||
84 | /** |
||
85 | * Indicates that the query should be stopped instead of touching the DB. |
||
86 | * Can be set in query scopes or manually. |
||
87 | * |
||
88 | * @var bool |
||
89 | */ |
||
90 | protected $queryShouldBeStopped = false; |
||
91 | |||
92 | /** |
||
93 | * Get count of users that match $filter. |
||
94 | * |
||
95 | * @return int |
||
96 | */ |
||
97 | abstract public function count(); |
||
98 | |||
99 | /** |
||
100 | * Подготавливает запрос и вызывает loadModels() |
||
101 | * |
||
102 | * @return Collection |
||
103 | */ |
||
104 | public function getList() |
||
123 | |||
124 | /** |
||
125 | * Get list of items. |
||
126 | * |
||
127 | * @return Collection |
||
128 | */ |
||
129 | abstract protected function loadModels(); |
||
130 | |||
131 | /** |
||
132 | * Constructor. |
||
133 | * |
||
134 | * @param object|string $bxObject |
||
135 | * @param string $modelName |
||
136 | */ |
||
137 | public function __construct($bxObject, $modelName) |
||
143 | |||
144 | /** |
||
145 | * Get the first item that matches query params. |
||
146 | * |
||
147 | * @return mixed |
||
148 | */ |
||
149 | public function first() |
||
153 | |||
154 | /** |
||
155 | * Get item by its id. |
||
156 | * |
||
157 | * @param int $id |
||
158 | * |
||
159 | * @return mixed |
||
160 | */ |
||
161 | public function getById($id) |
||
172 | |||
173 | /** |
||
174 | * Setter for sort. |
||
175 | * |
||
176 | * @param mixed $by |
||
177 | * @param string $order |
||
178 | * |
||
179 | * @return $this |
||
180 | */ |
||
181 | public function sort($by, $order = 'ASC') |
||
187 | |||
188 | /** |
||
189 | * Another setter for sort. |
||
190 | * |
||
191 | * @param mixed $by |
||
192 | * @param string $order |
||
193 | * |
||
194 | * @return $this |
||
195 | */ |
||
196 | public function order($by, $order = 'ASC') |
||
200 | |||
201 | /** |
||
202 | * Setter for filter. |
||
203 | * |
||
204 | * @param array $filter |
||
205 | * |
||
206 | * @return $this |
||
207 | */ |
||
208 | public function filter($filter) |
||
214 | |||
215 | /** |
||
216 | * Reset filter. |
||
217 | * |
||
218 | * @return $this |
||
219 | */ |
||
220 | public function resetFilter() |
||
226 | |||
227 | /** |
||
228 | * Add another filter to filters array. |
||
229 | * |
||
230 | * @param array $filters |
||
231 | * |
||
232 | * @return $this |
||
233 | */ |
||
234 | public function addFilter($filters) |
||
242 | |||
243 | /** |
||
244 | * Setter for navigation. |
||
245 | * |
||
246 | * @param $value |
||
247 | * |
||
248 | * @return $this |
||
249 | */ |
||
250 | public function navigation($value) |
||
256 | |||
257 | /** |
||
258 | * Setter for select. |
||
259 | * |
||
260 | * @param $value |
||
261 | * |
||
262 | * @return $this |
||
263 | */ |
||
264 | public function select($value) |
||
270 | |||
271 | /** |
||
272 | * Setter for cache ttl. |
||
273 | * |
||
274 | * @param float|int $minutes |
||
275 | * |
||
276 | * @return $this |
||
277 | */ |
||
278 | public function cache($minutes) |
||
284 | |||
285 | /** |
||
286 | * Setter for keyBy. |
||
287 | * |
||
288 | * @param string $value |
||
289 | * |
||
290 | * @return $this |
||
291 | */ |
||
292 | public function keyBy($value) |
||
298 | |||
299 | /** |
||
300 | * Set the "limit" value of the query. |
||
301 | * |
||
302 | * @param int $value |
||
303 | * |
||
304 | * @return $this |
||
305 | */ |
||
306 | public function limit($value) |
||
312 | |||
313 | /** |
||
314 | * Set the "page number" value of the query. |
||
315 | * |
||
316 | * @param int $num |
||
317 | * |
||
318 | * @return $this |
||
319 | */ |
||
320 | public function page($num) |
||
326 | |||
327 | /** |
||
328 | * Alias for "limit". |
||
329 | * |
||
330 | * @param int $value |
||
331 | * |
||
332 | * @return $this |
||
333 | */ |
||
334 | public function take($value) |
||
338 | |||
339 | /** |
||
340 | * Set the limit and offset for a given page. |
||
341 | * |
||
342 | * @param int $page |
||
343 | * @param int $perPage |
||
344 | * @return $this |
||
345 | */ |
||
346 | public function forPage($page, $perPage = 15) |
||
350 | |||
351 | /** |
||
352 | * Paginate the given query into a paginator. |
||
353 | * |
||
354 | * @param int $perPage |
||
355 | * @param string $pageName |
||
356 | * |
||
357 | * @return \Illuminate\Pagination\LengthAwarePaginator |
||
358 | */ |
||
359 | View Code Duplication | public function paginate($perPage = 15, $pageName = 'page') |
|
370 | |||
371 | /** |
||
372 | * Get a paginator only supporting simple next and previous links. |
||
373 | * |
||
374 | * This is more efficient on larger data-sets, etc. |
||
375 | * |
||
376 | * @param int $perPage |
||
377 | * @param string $pageName |
||
378 | * |
||
379 | * @return \Illuminate\Pagination\Paginator |
||
380 | */ |
||
381 | View Code Duplication | public function simplePaginate($perPage = 15, $pageName = 'page') |
|
391 | |||
392 | /** |
||
393 | * Stop the query from touching DB. |
||
394 | * |
||
395 | * @return $this |
||
396 | */ |
||
397 | public function stopQuery() |
||
403 | |||
404 | /** |
||
405 | * Adds $item to $results using keyBy value. |
||
406 | * |
||
407 | * @param $results |
||
408 | * @param BaseBitrixModel $object |
||
409 | * |
||
410 | * @return void |
||
411 | */ |
||
412 | protected function addItemToResultsUsingKeyBy(&$results, BaseBitrixModel $object) |
||
454 | |||
455 | /** |
||
456 | * Determine if all fields must be selected. |
||
457 | * |
||
458 | * @return bool |
||
459 | */ |
||
460 | protected function fieldsMustBeSelected() |
||
464 | |||
465 | /** |
||
466 | * Determine if all fields must be selected. |
||
467 | * |
||
468 | * @return bool |
||
469 | */ |
||
470 | protected function propsMustBeSelected() |
||
476 | |||
477 | /** |
||
478 | * Set $array[$new] as $array[$old] and delete $array[$old]. |
||
479 | * |
||
480 | * @param array $array |
||
481 | * @param $old |
||
482 | * @param $new |
||
483 | * |
||
484 | * return null |
||
485 | */ |
||
486 | protected function substituteField(&$array, $old, $new) |
||
494 | |||
495 | /** |
||
496 | * Clear select array from duplication and additional fields. |
||
497 | * |
||
498 | * @return array |
||
499 | */ |
||
500 | protected function clearSelectArray() |
||
506 | |||
507 | /** |
||
508 | * Store closure's result in the cache for a given number of minutes. |
||
509 | * |
||
510 | * @param string $key |
||
511 | * @param double $minutes |
||
512 | * @param Closure $callback |
||
513 | * @return mixed |
||
514 | */ |
||
515 | protected function rememberInCache($key, $minutes, Closure $callback) |
||
541 | |||
542 | protected function handleCacheIfNeeded($cacheKeyParams, Closure $callback) |
||
548 | |||
549 | /** |
||
550 | * Handle dynamic method calls into the method. |
||
551 | * |
||
552 | * @param string $method |
||
553 | * @param array $parameters |
||
554 | * |
||
555 | * @throws BadMethodCallException |
||
556 | * |
||
557 | * @return $this |
||
558 | */ |
||
559 | public function __call($method, $parameters) |
||
577 | |||
578 | protected function prepareMultiFilter(&$key, &$value) |
||
582 | |||
583 | /** |
||
584 | * Проверка включен ли тегированный кеш |
||
585 | * @return bool |
||
586 | */ |
||
587 | protected function isManagedCacheOn() |
||
591 | |||
592 | public function exec($query) |
||
610 | } |
||
611 |