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 |
||
| 13 | abstract class BaseRepository implements RepositoryContract, CacheableContract |
||
| 14 | { |
||
| 15 | use Cacheable; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * The IoC container instance. |
||
| 19 | * |
||
| 20 | * @var \Illuminate\Contracts\Container\Container |
||
| 21 | */ |
||
| 22 | protected $container; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * The connection name for the repository. |
||
| 26 | * |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | protected $connection; |
||
| 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 relations to eager load on query execution. |
||
| 47 | * |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | protected $relations = []; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Count relations to eager load on query execution. |
||
| 54 | * |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | protected $countRelations = []; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * The query where clauses. |
||
| 61 | * |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | protected $where = []; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * The query whereIn clauses. |
||
| 68 | * |
||
| 69 | * @var array |
||
| 70 | */ |
||
| 71 | protected $whereIn = []; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * The query whereNotIn clauses. |
||
| 75 | * |
||
| 76 | * @var array |
||
| 77 | */ |
||
| 78 | protected $whereNotIn = []; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * The query whereHas clauses. |
||
| 82 | * |
||
| 83 | * @var array |
||
| 84 | */ |
||
| 85 | protected $whereHas = []; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * The query scopes. |
||
| 89 | * |
||
| 90 | * @var array |
||
| 91 | */ |
||
| 92 | protected $scopes = []; |
||
| 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 | * The column to order results by. |
||
| 117 | * |
||
| 118 | * @var array |
||
| 119 | */ |
||
| 120 | protected $groupBy = []; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * The query having clauses. |
||
| 124 | * |
||
| 125 | * @var array |
||
| 126 | */ |
||
| 127 | protected $having = []; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Execute given callback and return the result. |
||
| 131 | * |
||
| 132 | * @param string $class |
||
| 133 | * @param string $method |
||
| 134 | * @param array $args |
||
| 135 | * @param \Closure $closure |
||
| 136 | * |
||
| 137 | * @return mixed |
||
| 138 | */ |
||
| 139 | protected function executeCallback($class, $method, $args, Closure $closure) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Reset repository to its defaults. |
||
| 159 | * |
||
| 160 | * @return $this |
||
| 161 | */ |
||
| 162 | protected function resetRepository() |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Prepare query. |
||
| 186 | * |
||
| 187 | * @param object $model |
||
| 188 | * |
||
| 189 | * @return mixed |
||
| 190 | */ |
||
| 191 | protected function prepareQuery($model) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * {@inheritdoc} |
||
| 277 | */ |
||
| 278 | public function setContainer(Container $container) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * {@inheritdoc} |
||
| 287 | */ |
||
| 288 | public function getContainer($service = null) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * {@inheritdoc} |
||
| 295 | */ |
||
| 296 | public function setConnection($name) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * {@inheritdoc} |
||
| 305 | */ |
||
| 306 | public function getConnection(): string |
||
| 310 | |||
| 311 | /** |
||
| 312 | * {@inheritdoc} |
||
| 313 | */ |
||
| 314 | public function setRepositoryId($repositoryId) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * {@inheritdoc} |
||
| 323 | */ |
||
| 324 | public function getRepositoryId(): string |
||
| 328 | |||
| 329 | /** |
||
| 330 | * {@inheritdoc} |
||
| 331 | */ |
||
| 332 | public function setModel($model) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * {@inheritdoc} |
||
| 341 | */ |
||
| 342 | public function getModel(): string |
||
| 348 | |||
| 349 | /** |
||
| 350 | * {@inheritdoc} |
||
| 351 | */ |
||
| 352 | public function with($relations) |
||
| 362 | |||
| 363 | /** |
||
| 364 | * {@inheritdoc} |
||
| 365 | */ |
||
| 366 | public function withCount($countRelations) |
||
| 376 | |||
| 377 | /** |
||
| 378 | * {@inheritdoc} |
||
| 379 | */ |
||
| 380 | public function where($attribute, $operator = null, $value = null, $boolean = 'and') |
||
| 387 | |||
| 388 | /** |
||
| 389 | * {@inheritdoc} |
||
| 390 | */ |
||
| 391 | public function whereIn($attribute, $values, $boolean = 'and', $not = false) |
||
| 398 | |||
| 399 | /** |
||
| 400 | * {@inheritdoc} |
||
| 401 | */ |
||
| 402 | public function whereNotIn($attribute, $values, $boolean = 'and') |
||
| 409 | |||
| 410 | /** |
||
| 411 | * {@inheritdoc} |
||
| 412 | */ |
||
| 413 | public function whereHas($relation, Closure $callback = null, $operator = '>=', $count = 1) |
||
| 420 | |||
| 421 | /** |
||
| 422 | * {@inheritdoc} |
||
| 423 | */ |
||
| 424 | public function scope($name, array $parameters = []) |
||
| 430 | |||
| 431 | /** |
||
| 432 | * {@inheritdoc} |
||
| 433 | */ |
||
| 434 | public function offset($offset) |
||
| 440 | |||
| 441 | /** |
||
| 442 | * {@inheritdoc} |
||
| 443 | */ |
||
| 444 | public function limit($limit) |
||
| 450 | |||
| 451 | /** |
||
| 452 | * {@inheritdoc} |
||
| 453 | */ |
||
| 454 | public function orderBy($attribute, $direction = 'asc') |
||
| 460 | |||
| 461 | /** |
||
| 462 | * {@inheritdoc} |
||
| 463 | */ |
||
| 464 | public function groupBy($column) |
||
| 470 | |||
| 471 | /** |
||
| 472 | * {@inheritdoc} |
||
| 473 | */ |
||
| 474 | public function having($column, $operator = null, $value = null, $boolean = 'and') |
||
| 480 | |||
| 481 | /** |
||
| 482 | * {@inheritdoc} |
||
| 483 | */ |
||
| 484 | public function orHaving($column, $operator = null, $value = null, $boolean = 'and') |
||
| 488 | |||
| 489 | /** |
||
| 490 | * {@inheritdoc} |
||
| 491 | */ |
||
| 492 | public function store($id, array $attributes = [], bool $syncRelations = false) |
||
| 496 | |||
| 497 | /** |
||
| 498 | * {@inheritdoc} |
||
| 499 | */ |
||
| 500 | public static function __callStatic($method, $parameters) |
||
| 504 | |||
| 505 | /** |
||
| 506 | * {@inheritdoc} |
||
| 507 | */ |
||
| 508 | public function __call($method, $parameters) |
||
| 518 | } |
||
| 519 |
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: