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 | /** |
||
| 20 | * Query select. |
||
| 21 | * |
||
| 22 | * @var array |
||
| 23 | */ |
||
| 24 | public $select = []; |
||
| 25 | /** |
||
| 26 | * Bitrix object to be queried. |
||
| 27 | * |
||
| 28 | * @var object|string |
||
| 29 | */ |
||
| 30 | protected $bxObject; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Name of the model that calls the query. |
||
| 34 | * |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | protected $modelName; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Model that calls the query. |
||
| 41 | * |
||
| 42 | * @var object |
||
| 43 | */ |
||
| 44 | protected $model; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Query sort. |
||
| 48 | * |
||
| 49 | * @var array |
||
| 50 | */ |
||
| 51 | public $sort = []; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Query filter. |
||
| 55 | * |
||
| 56 | * @var array |
||
| 57 | */ |
||
| 58 | public $filter = []; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Query navigation. |
||
| 62 | * |
||
| 63 | * @var array|bool |
||
| 64 | */ |
||
| 65 | public $navigation = false; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * The key to list items in array of results. |
||
| 69 | * Set to false to have auto incrementing integer. |
||
| 70 | * |
||
| 71 | * @var string|bool |
||
| 72 | */ |
||
| 73 | public $keyBy = 'ID'; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Number of minutes to cache a query |
||
| 77 | * |
||
| 78 | * @var double|int |
||
| 79 | */ |
||
| 80 | public $cacheTtl = 0; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Indicates that the query should be stopped instead of touching the DB. |
||
| 84 | * Can be set in query scopes or manually. |
||
| 85 | * |
||
| 86 | * @var bool |
||
| 87 | */ |
||
| 88 | protected $queryShouldBeStopped = false; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Get count of users that match $filter. |
||
| 92 | * |
||
| 93 | * @return int |
||
| 94 | */ |
||
| 95 | abstract public function count(); |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Подготавливает запрос и вызывает loadModels() |
||
| 99 | * |
||
| 100 | * @return Collection |
||
| 101 | */ |
||
| 102 | public function getList() |
||
| 103 | { |
||
| 104 | if (!is_null($this->primaryModel)) { |
||
| 105 | // Запрос - подгрузка релейшена. Надо добавить filter |
||
| 106 | $this->filterByModels([$this->primaryModel]); |
||
| 107 | } |
||
| 108 | |||
| 109 | if ($this->queryShouldBeStopped) { |
||
| 110 | return new Collection(); |
||
| 111 | } |
||
| 112 | |||
| 113 | $models = $this->loadModels(); |
||
| 114 | |||
| 115 | if (!empty($this->with)) { |
||
| 116 | $this->findWith($this->with, $models); |
||
| 117 | } |
||
| 118 | |||
| 119 | return $models; |
||
|
|
|||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Get list of items. |
||
| 124 | * |
||
| 125 | * @return Collection |
||
| 126 | */ |
||
| 127 | abstract protected function loadModels(); |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Constructor. |
||
| 131 | * |
||
| 132 | * @param object|string $bxObject |
||
| 133 | * @param string $modelName |
||
| 134 | */ |
||
| 135 | public function __construct($bxObject, $modelName) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Get the first item that matches query params. |
||
| 144 | * |
||
| 145 | * @return mixed |
||
| 146 | */ |
||
| 147 | public function first() |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Get item by its id. |
||
| 154 | * |
||
| 155 | * @param int $id |
||
| 156 | * |
||
| 157 | * @return mixed |
||
| 158 | */ |
||
| 159 | public function getById($id) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Setter for sort. |
||
| 173 | * |
||
| 174 | * @param mixed $by |
||
| 175 | * @param string $order |
||
| 176 | * |
||
| 177 | * @return $this |
||
| 178 | */ |
||
| 179 | public function sort($by, $order = 'ASC') |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Another setter for sort. |
||
| 188 | * |
||
| 189 | * @param mixed $by |
||
| 190 | * @param string $order |
||
| 191 | * |
||
| 192 | * @return $this |
||
| 193 | */ |
||
| 194 | public function order($by, $order = 'ASC') |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Setter for filter. |
||
| 201 | * |
||
| 202 | * @param array $filter |
||
| 203 | * |
||
| 204 | * @return $this |
||
| 205 | */ |
||
| 206 | public function filter($filter) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Reset filter. |
||
| 215 | * |
||
| 216 | * @return $this |
||
| 217 | */ |
||
| 218 | public function resetFilter() |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Add another filter to filters array. |
||
| 227 | * |
||
| 228 | * @param array $filters |
||
| 229 | * |
||
| 230 | * @return $this |
||
| 231 | */ |
||
| 232 | public function addFilter($filters) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Setter for navigation. |
||
| 243 | * |
||
| 244 | * @param $value |
||
| 245 | * |
||
| 246 | * @return $this |
||
| 247 | */ |
||
| 248 | public function navigation($value) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Setter for select. |
||
| 257 | * |
||
| 258 | * @param $value |
||
| 259 | * |
||
| 260 | * @return $this |
||
| 261 | */ |
||
| 262 | public function select($value) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Setter for cache ttl. |
||
| 271 | * |
||
| 272 | * @param float|int $minutes |
||
| 273 | * |
||
| 274 | * @return $this |
||
| 275 | */ |
||
| 276 | public function cache($minutes) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Setter for keyBy. |
||
| 285 | * |
||
| 286 | * @param string $value |
||
| 287 | * |
||
| 288 | * @return $this |
||
| 289 | */ |
||
| 290 | public function keyBy($value) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Set the "limit" value of the query. |
||
| 299 | * |
||
| 300 | * @param int $value |
||
| 301 | * |
||
| 302 | * @return $this |
||
| 303 | */ |
||
| 304 | public function limit($value) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Set the "page number" value of the query. |
||
| 313 | * |
||
| 314 | * @param int $num |
||
| 315 | * |
||
| 316 | * @return $this |
||
| 317 | */ |
||
| 318 | public function page($num) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Alias for "limit". |
||
| 327 | * |
||
| 328 | * @param int $value |
||
| 329 | * |
||
| 330 | * @return $this |
||
| 331 | */ |
||
| 332 | public function take($value) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Set the limit and offset for a given page. |
||
| 339 | * |
||
| 340 | * @param int $page |
||
| 341 | * @param int $perPage |
||
| 342 | * @return $this |
||
| 343 | */ |
||
| 344 | public function forPage($page, $perPage = 15) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Paginate the given query into a paginator. |
||
| 351 | * |
||
| 352 | * @param int $perPage |
||
| 353 | * @param string $pageName |
||
| 354 | * |
||
| 355 | * @return \Illuminate\Pagination\LengthAwarePaginator |
||
| 356 | */ |
||
| 357 | View Code Duplication | public function paginate($perPage = 15, $pageName = 'page') |
|
| 368 | |||
| 369 | /** |
||
| 370 | * Get a paginator only supporting simple next and previous links. |
||
| 371 | * |
||
| 372 | * This is more efficient on larger data-sets, etc. |
||
| 373 | * |
||
| 374 | * @param int $perPage |
||
| 375 | * @param string $pageName |
||
| 376 | * |
||
| 377 | * @return \Illuminate\Pagination\Paginator |
||
| 378 | */ |
||
| 379 | View Code Duplication | public function simplePaginate($perPage = 15, $pageName = 'page') |
|
| 389 | |||
| 390 | /** |
||
| 391 | * Stop the query from touching DB. |
||
| 392 | * |
||
| 393 | * @return $this |
||
| 394 | */ |
||
| 395 | public function stopQuery() |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Adds $item to $results using keyBy value. |
||
| 404 | * |
||
| 405 | * @param $results |
||
| 406 | * @param BaseBitrixModel $object |
||
| 407 | * |
||
| 408 | * @return void |
||
| 409 | */ |
||
| 410 | protected function addItemToResultsUsingKeyBy(&$results, BaseBitrixModel $object) |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Determine if all fields must be selected. |
||
| 455 | * |
||
| 456 | * @return bool |
||
| 457 | */ |
||
| 458 | protected function fieldsMustBeSelected() |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Determine if all fields must be selected. |
||
| 465 | * |
||
| 466 | * @return bool |
||
| 467 | */ |
||
| 468 | protected function propsMustBeSelected() |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Set $array[$new] as $array[$old] and delete $array[$old]. |
||
| 477 | * |
||
| 478 | * @param array $array |
||
| 479 | * @param $old |
||
| 480 | * @param $new |
||
| 481 | * |
||
| 482 | * return null |
||
| 483 | */ |
||
| 484 | protected function substituteField(&$array, $old, $new) |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Clear select array from duplication and additional fields. |
||
| 495 | * |
||
| 496 | * @return array |
||
| 497 | */ |
||
| 498 | protected function clearSelectArray() |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Store closure's result in the cache for a given number of minutes. |
||
| 507 | * |
||
| 508 | * @param string $key |
||
| 509 | * @param double $minutes |
||
| 510 | * @param Closure $callback |
||
| 511 | * @return mixed |
||
| 512 | */ |
||
| 513 | protected function rememberInCache($key, $minutes, Closure $callback) |
||
| 539 | |||
| 540 | protected function handleCacheIfNeeded($cacheKeyParams, Closure $callback) |
||
| 546 | |||
| 547 | /** |
||
| 548 | * Handle dynamic method calls into the method. |
||
| 549 | * |
||
| 550 | * @param string $method |
||
| 551 | * @param array $parameters |
||
| 552 | * |
||
| 553 | * @throws BadMethodCallException |
||
| 554 | * |
||
| 555 | * @return $this |
||
| 556 | */ |
||
| 557 | public function __call($method, $parameters) |
||
| 575 | |||
| 576 | protected function prepareMultiFilter(&$key, &$value) |
||
| 580 | } |
||
| 581 |