Complex classes like BaseRepository 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 BaseRepository, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | abstract class BaseRepository implements RepositoryContract |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * The IoC container instance. |
||
| 26 | * |
||
| 27 | * @var \Illuminate\Contracts\Container\Container |
||
| 28 | */ |
||
| 29 | protected $container; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * The repository identifier. |
||
| 33 | * |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | protected $repositoryId; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * The repository model. |
||
| 40 | * |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | protected $model; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * The repository cache lifetime. |
||
| 47 | * |
||
| 48 | * @var float|int |
||
| 49 | */ |
||
| 50 | protected $cacheLifetime; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * The repository cache driver. |
||
| 54 | * |
||
| 55 | * @var string |
||
| 56 | */ |
||
| 57 | protected $cacheDriver; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Indicate if the repository cache clear is enabled. |
||
| 61 | * |
||
| 62 | * @var bool |
||
| 63 | */ |
||
| 64 | protected $cacheClearEnabled = true; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * The relations to eager load on query execution. |
||
| 68 | * |
||
| 69 | * @var array |
||
| 70 | */ |
||
| 71 | protected $relations = []; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * The query where clauses. |
||
| 75 | * |
||
| 76 | * @var array |
||
| 77 | */ |
||
| 78 | protected $where = []; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * The query whereIn clauses. |
||
| 82 | * |
||
| 83 | * @var array |
||
| 84 | */ |
||
| 85 | protected $whereIn = []; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * The query whereNotIn clauses. |
||
| 89 | * |
||
| 90 | * @var array |
||
| 91 | */ |
||
| 92 | protected $whereNotIn = []; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * The "offset" value of the query. |
||
| 96 | * |
||
| 97 | * @var int |
||
| 98 | */ |
||
| 99 | protected $offset; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * The "limit" value of the query. |
||
| 103 | * |
||
| 104 | * @var int |
||
| 105 | */ |
||
| 106 | protected $limit; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * The column to order results by. |
||
| 110 | * |
||
| 111 | * @var array |
||
| 112 | */ |
||
| 113 | protected $orderBy = []; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Execute given callback and cache the result. |
||
| 117 | * |
||
| 118 | * @param string $class |
||
| 119 | * @param string $method |
||
| 120 | * @param array $args |
||
| 121 | * @param \Closure $closure |
||
| 122 | * |
||
| 123 | * @return mixed |
||
| 124 | */ |
||
| 125 | protected function executeCallback($class, $method, $args, Closure $closure) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Generate unique query hash. |
||
| 177 | * |
||
| 178 | * @param $args |
||
| 179 | * |
||
| 180 | * @return string |
||
| 181 | */ |
||
| 182 | protected function generateHash($args) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Reset repository to it's defaults. |
||
| 201 | * |
||
| 202 | * @return $this |
||
| 203 | */ |
||
| 204 | protected function resetRepository() |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Prepare query. |
||
| 221 | * |
||
| 222 | * @param object $model |
||
| 223 | * |
||
| 224 | * @return object |
||
| 225 | */ |
||
| 226 | protected function prepareQuery($model) |
||
| 227 | { |
||
| 228 | // Set the relationships that should be eager loaded |
||
| 229 | if (! empty($this->relations)) { |
||
| 230 | $model = $model->with($this->relations); |
||
| 231 | } |
||
| 232 | |||
| 233 | // Add a basic where clause to the query |
||
| 234 | foreach ($this->where as $where) { |
||
| 235 | list($attribute, $operator, $value, $boolean) = array_pad($where, 4, null); |
||
| 236 | |||
| 237 | $model = $model->where($attribute, $operator, $value, $boolean); |
||
| 238 | } |
||
| 239 | |||
| 240 | // Add a "where in" clause to the query |
||
| 241 | foreach ($this->whereIn as $whereIn) { |
||
| 242 | list($attribute, $values, $boolean, $not) = array_pad($whereIn, 4, null); |
||
| 243 | |||
| 244 | $model = $model->whereIn($attribute, $values, $boolean, $not); |
||
| 245 | } |
||
| 246 | |||
| 247 | // Add a "where not in" clause to the query |
||
| 248 | foreach ($this->whereNotIn as $whereNotIn) { |
||
| 249 | list($attribute, $values, $boolean) = array_pad($whereNotIn, 3, null); |
||
| 250 | |||
| 251 | $model = $model->whereNotIn($attribute, $values, $boolean); |
||
| 252 | } |
||
| 253 | |||
| 254 | // Set the "offset" value of the query |
||
| 255 | if ($this->offset > 0) { |
||
| 256 | $model = $model->offset($this->offset); |
||
| 257 | } |
||
| 258 | |||
| 259 | // Set the "limit" value of the query |
||
| 260 | if ($this->limit > 0) { |
||
| 261 | $model = $model->limit($this->limit); |
||
| 262 | } |
||
| 263 | |||
| 264 | // Add an "order by" clause to the query. |
||
| 265 | if (! empty($this->orderBy)) { |
||
| 266 | list($attribute, $direction) = $this->orderBy; |
||
| 267 | |||
| 268 | $model = $model->orderBy($attribute, $direction); |
||
| 269 | } |
||
| 270 | |||
| 271 | return $model; |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Store cache keys by mimicking cache tags. |
||
| 276 | * |
||
| 277 | * @param string $class |
||
| 278 | * @param string $method |
||
| 279 | * @param string $hash |
||
| 280 | * |
||
| 281 | * @return void |
||
| 282 | */ |
||
| 283 | protected function storeCacheKeys($class, $method, $hash) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Get cache keys. |
||
| 296 | * |
||
| 297 | * @param string $file |
||
| 298 | * |
||
| 299 | * @return array |
||
| 300 | */ |
||
| 301 | protected function getCacheKeys($file) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Flush cache keys by mimicking cache tags. |
||
| 312 | * |
||
| 313 | * @return array |
||
| 314 | */ |
||
| 315 | protected function flushCacheKeys() |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Set the IoC container instance. |
||
| 336 | * |
||
| 337 | * @param \Illuminate\Contracts\Container\Container $container |
||
| 338 | * |
||
| 339 | * @return $this |
||
| 340 | */ |
||
| 341 | public function setContainer(Container $container) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Get the IoC container instance or any of it's services. |
||
| 350 | * |
||
| 351 | * @param string|null $service |
||
| 352 | * |
||
| 353 | * @return object |
||
| 354 | */ |
||
| 355 | public function getContainer($service = null) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Set the repository identifier. |
||
| 362 | * |
||
| 363 | * @param string $repositoryId |
||
| 364 | * |
||
| 365 | * @return $this |
||
| 366 | */ |
||
| 367 | public function setRepositoryId($repositoryId) |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Get the repository identifier. |
||
| 376 | * |
||
| 377 | * @return string |
||
| 378 | */ |
||
| 379 | public function getRepositoryId() |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Set the repository model. |
||
| 386 | * |
||
| 387 | * @param string $model |
||
| 388 | * |
||
| 389 | * @return $this |
||
| 390 | */ |
||
| 391 | public function setModel($model) |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Get the repository model. |
||
| 400 | * |
||
| 401 | * @return string |
||
| 402 | */ |
||
| 403 | public function getModel() |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Set the repository cache lifetime. |
||
| 410 | * |
||
| 411 | * @param float|int $cacheLifetime |
||
| 412 | * |
||
| 413 | * @return $this |
||
| 414 | */ |
||
| 415 | public function setCacheLifetime($cacheLifetime) |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Get the repository cache lifetime. |
||
| 424 | * |
||
| 425 | * @return float|int |
||
| 426 | */ |
||
| 427 | public function getCacheLifetime() |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Set the repository cache driver. |
||
| 437 | * |
||
| 438 | * @param string $cacheDriver |
||
| 439 | * |
||
| 440 | * @return $this |
||
| 441 | */ |
||
| 442 | public function setCacheDriver($cacheDriver) |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Get the repository cache driver. |
||
| 451 | * |
||
| 452 | * @return string |
||
| 453 | */ |
||
| 454 | public function getCacheDriver() |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Enable repository cache clear. |
||
| 461 | * |
||
| 462 | * @param bool $status |
||
| 463 | * |
||
| 464 | * @return $this |
||
| 465 | */ |
||
| 466 | public function enableCacheClear($status = true) |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Determine if repository cache clear is enabled. |
||
| 475 | * |
||
| 476 | * @return bool |
||
| 477 | */ |
||
| 478 | public function isCacheClearEnabled() |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Forget the repository cache. |
||
| 485 | * |
||
| 486 | * @return $this |
||
| 487 | */ |
||
| 488 | public function forgetCache() |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Set the relationships that should be eager loaded. |
||
| 509 | * |
||
| 510 | * @param array $relations |
||
| 511 | * |
||
| 512 | * @return $this |
||
| 513 | */ |
||
| 514 | public function with(array $relations) |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Add a basic where clause to the query. |
||
| 523 | * |
||
| 524 | * @param string $attribute |
||
| 525 | * @param string $operator |
||
| 526 | * @param mixed $value |
||
| 527 | * @param string $boolean |
||
| 528 | * |
||
| 529 | * @return $this |
||
| 530 | */ |
||
| 531 | public function where($attribute, $operator = null, $value = null, $boolean = 'and') |
||
| 538 | |||
| 539 | /** |
||
| 540 | * Add a "where in" clause to the query. |
||
| 541 | * |
||
| 542 | * @param string $attribute |
||
| 543 | * @param mixed $values |
||
| 544 | * @param string $boolean |
||
| 545 | * @param bool $not |
||
| 546 | * |
||
| 547 | * @return $this |
||
| 548 | */ |
||
| 549 | public function whereIn($attribute, $values, $boolean = 'and', $not = false) |
||
| 556 | |||
| 557 | /** |
||
| 558 | * Add a "where not in" clause to the query. |
||
| 559 | * |
||
| 560 | * @param string $attribute |
||
| 561 | * @param mixed $values |
||
| 562 | * @param string $boolean |
||
| 563 | * |
||
| 564 | * @return $this |
||
| 565 | */ |
||
| 566 | public function whereNotIn($attribute, $values, $boolean = 'and') |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Set the "offset" value of the query. |
||
| 576 | * |
||
| 577 | * @param int $offset |
||
| 578 | * |
||
| 579 | * @return $this |
||
| 580 | */ |
||
| 581 | public function offset($offset) |
||
| 587 | |||
| 588 | /** |
||
| 589 | * Set the "limit" value of the query. |
||
| 590 | * |
||
| 591 | * @param int $limit |
||
| 592 | * |
||
| 593 | * @return $this |
||
| 594 | */ |
||
| 595 | public function limit($limit) |
||
| 601 | |||
| 602 | /** |
||
| 603 | * Add an "order by" clause to the query. |
||
| 604 | * |
||
| 605 | * @param string $attribute |
||
| 606 | * @param string $direction |
||
| 607 | * |
||
| 608 | * @return $this |
||
| 609 | */ |
||
| 610 | public function orderBy($attribute, $direction = 'asc') |
||
| 616 | |||
| 617 | /** |
||
| 618 | * Dynamically pass missing static methods to the model. |
||
| 619 | * |
||
| 620 | * @param $method |
||
| 621 | * @param $parameters |
||
| 622 | * |
||
| 623 | * @return mixed |
||
| 624 | */ |
||
| 625 | public static function __callStatic($method, $parameters) |
||
| 629 | |||
| 630 | /** |
||
| 631 | * Dynamically pass missing methods to the model. |
||
| 632 | * |
||
| 633 | * @param string $method |
||
| 634 | * @param array $parameters |
||
| 635 | * |
||
| 636 | * @return mixed |
||
| 637 | */ |
||
| 638 | public function __call($method, $parameters) |
||
| 644 | } |
||
| 645 |