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