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() |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Get list of items. |
||
| 128 | * |
||
| 129 | * @return Collection |
||
| 130 | */ |
||
| 131 | abstract protected function loadModels(); |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Constructor. |
||
| 135 | * |
||
| 136 | * @param object|string $bxObject |
||
| 137 | * @param string $modelName |
||
| 138 | */ |
||
| 139 | public function __construct($bxObject, $modelName) |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Get the first item that matches query params. |
||
| 148 | * |
||
| 149 | * @return mixed |
||
| 150 | */ |
||
| 151 | public function first() |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Get item by its id. |
||
| 158 | * |
||
| 159 | * @param int $id |
||
| 160 | * |
||
| 161 | * @return mixed |
||
| 162 | */ |
||
| 163 | public function getById($id) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Setter for sort. |
||
| 177 | * |
||
| 178 | * @param mixed $by |
||
| 179 | * @param string $order |
||
| 180 | * |
||
| 181 | * @return $this |
||
| 182 | */ |
||
| 183 | public function sort($by, $order = 'ASC') |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Another setter for sort. |
||
| 192 | * |
||
| 193 | * @param mixed $by |
||
| 194 | * @param string $order |
||
| 195 | * |
||
| 196 | * @return $this |
||
| 197 | */ |
||
| 198 | public function order($by, $order = 'ASC') |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Setter for filter. |
||
| 205 | * |
||
| 206 | * @param array $filter |
||
| 207 | * |
||
| 208 | * @return $this |
||
| 209 | */ |
||
| 210 | public function filter($filter) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Reset filter. |
||
| 219 | * |
||
| 220 | * @return $this |
||
| 221 | */ |
||
| 222 | public function resetFilter() |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Add another filter to filters array. |
||
| 231 | * |
||
| 232 | * @param array $filters |
||
| 233 | * |
||
| 234 | * @return $this |
||
| 235 | */ |
||
| 236 | public function addFilter($filters) |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Setter for navigation. |
||
| 247 | * |
||
| 248 | * @param $value |
||
| 249 | * |
||
| 250 | * @return $this |
||
| 251 | */ |
||
| 252 | public function navigation($value) |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Setter for select. |
||
| 261 | * |
||
| 262 | * @param $value |
||
| 263 | * |
||
| 264 | * @return $this |
||
| 265 | */ |
||
| 266 | public function select($value) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Setter for cache ttl. |
||
| 275 | * |
||
| 276 | * @param float|int $minutes |
||
| 277 | * |
||
| 278 | * @return $this |
||
| 279 | */ |
||
| 280 | public function cache($minutes) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Setter for keyBy. |
||
| 289 | * |
||
| 290 | * @param string $value |
||
| 291 | * |
||
| 292 | * @return $this |
||
| 293 | */ |
||
| 294 | public function keyBy($value) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Set the "limit" value of the query. |
||
| 303 | * |
||
| 304 | * @param int $value |
||
| 305 | * |
||
| 306 | * @return $this |
||
| 307 | */ |
||
| 308 | public function limit($value) |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Set the "page number" value of the query. |
||
| 317 | * |
||
| 318 | * @param int $num |
||
| 319 | * |
||
| 320 | * @return $this |
||
| 321 | */ |
||
| 322 | public function page($num) |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Alias for "limit". |
||
| 331 | * |
||
| 332 | * @param int $value |
||
| 333 | * |
||
| 334 | * @return $this |
||
| 335 | */ |
||
| 336 | public function take($value) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Set the limit and offset for a given page. |
||
| 343 | * |
||
| 344 | * @param int $page |
||
| 345 | * @param int $perPage |
||
| 346 | * @return $this |
||
| 347 | */ |
||
| 348 | public function forPage($page, $perPage = 15) |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Paginate the given query into a paginator. |
||
| 355 | * |
||
| 356 | * @param int $perPage |
||
| 357 | * @param string $pageName |
||
| 358 | * |
||
| 359 | * @return \Illuminate\Pagination\LengthAwarePaginator |
||
| 360 | */ |
||
| 361 | View Code Duplication | public function paginate($perPage = 15, $pageName = 'page') |
|
| 372 | |||
| 373 | /** |
||
| 374 | * Get a paginator only supporting simple next and previous links. |
||
| 375 | * |
||
| 376 | * This is more efficient on larger data-sets, etc. |
||
| 377 | * |
||
| 378 | * @param int $perPage |
||
| 379 | * @param string $pageName |
||
| 380 | * |
||
| 381 | * @return \Illuminate\Pagination\Paginator |
||
| 382 | */ |
||
| 383 | View Code Duplication | public function simplePaginate($perPage = 15, $pageName = 'page') |
|
| 393 | |||
| 394 | /** |
||
| 395 | * Stop the query from touching DB. |
||
| 396 | * |
||
| 397 | * @return $this |
||
| 398 | */ |
||
| 399 | public function stopQuery() |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Adds $item to $results using keyBy value. |
||
| 408 | * |
||
| 409 | * @param $results |
||
| 410 | * @param BaseBitrixModel $object |
||
| 411 | * |
||
| 412 | * @return void |
||
| 413 | */ |
||
| 414 | protected function addItemToResultsUsingKeyBy(&$results, BaseBitrixModel $object) |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Determine if all fields must be selected. |
||
| 459 | * |
||
| 460 | * @return bool |
||
| 461 | */ |
||
| 462 | protected function fieldsMustBeSelected() |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Determine if all fields must be selected. |
||
| 469 | * |
||
| 470 | * @return bool |
||
| 471 | */ |
||
| 472 | protected function propsMustBeSelected() |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Set $array[$new] as $array[$old] and delete $array[$old]. |
||
| 481 | * |
||
| 482 | * @param array $array |
||
| 483 | * @param $old |
||
| 484 | * @param $new |
||
| 485 | * |
||
| 486 | * return null |
||
| 487 | */ |
||
| 488 | protected function substituteField(&$array, $old, $new) |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Clear select array from duplication and additional fields. |
||
| 499 | * |
||
| 500 | * @return array |
||
| 501 | */ |
||
| 502 | protected function clearSelectArray() |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Store closure's result in the cache for a given number of minutes. |
||
| 511 | * |
||
| 512 | * @param string $key |
||
| 513 | * @param double $minutes |
||
| 514 | * @param Closure $callback |
||
| 515 | * @return mixed |
||
| 516 | */ |
||
| 517 | protected function rememberInCache($key, $minutes, Closure $callback) |
||
| 536 | |||
| 537 | protected function handleCacheIfNeeded($cacheKeyParams, Closure $callback) |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Handle dynamic method calls into the method. |
||
| 546 | * |
||
| 547 | * @param string $method |
||
| 548 | * @param array $parameters |
||
| 549 | * |
||
| 550 | * @throws BadMethodCallException |
||
| 551 | * |
||
| 552 | * @return $this |
||
| 553 | */ |
||
| 554 | public function __call($method, $parameters) |
||
| 572 | } |
||
| 573 |