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 | * The query where clauses. |
||
| 54 | * |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | protected $where = []; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * The query whereIn clauses. |
||
| 61 | * |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | protected $whereIn = []; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * The query whereNotIn clauses. |
||
| 68 | * |
||
| 69 | * @var array |
||
| 70 | */ |
||
| 71 | protected $whereNotIn = []; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * The query whereHas clauses. |
||
| 75 | * |
||
| 76 | * @var array |
||
| 77 | */ |
||
| 78 | protected $whereHas = []; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * The query scopes. |
||
| 82 | * |
||
| 83 | * @var array |
||
| 84 | */ |
||
| 85 | protected $scopes = []; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * The "offset" value of the query. |
||
| 89 | * |
||
| 90 | * @var int |
||
| 91 | */ |
||
| 92 | protected $offset; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * The "limit" value of the query. |
||
| 96 | * |
||
| 97 | * @var int |
||
| 98 | */ |
||
| 99 | protected $limit; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * The column to order results by. |
||
| 103 | * |
||
| 104 | * @var array |
||
| 105 | */ |
||
| 106 | protected $orderBy = []; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * The column to order results by. |
||
| 110 | * |
||
| 111 | * @var array |
||
| 112 | */ |
||
| 113 | protected $groupBy = []; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * The query having clauses. |
||
| 117 | * |
||
| 118 | * @var array |
||
| 119 | */ |
||
| 120 | protected $having = []; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Execute given callback and return the result. |
||
| 124 | * |
||
| 125 | * @param string $class |
||
| 126 | * @param string $method |
||
| 127 | * @param array $args |
||
| 128 | * @param \Closure $closure |
||
| 129 | * |
||
| 130 | * @return mixed |
||
| 131 | */ |
||
| 132 | protected function executeCallback($class, $method, $args, Closure $closure) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Reset repository to its defaults. |
||
| 152 | * |
||
| 153 | * @return $this |
||
| 154 | */ |
||
| 155 | protected function resetRepository() |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Prepare query. |
||
| 178 | * |
||
| 179 | * @param object $model |
||
| 180 | * |
||
| 181 | * @return mixed |
||
| 182 | */ |
||
| 183 | protected function prepareQuery($model) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * {@inheritdoc} |
||
| 264 | */ |
||
| 265 | public function setContainer(Container $container) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * {@inheritdoc} |
||
| 274 | */ |
||
| 275 | public function getContainer($service = null) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * {@inheritdoc} |
||
| 282 | */ |
||
| 283 | public function setConnection($name) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * {@inheritdoc} |
||
| 292 | */ |
||
| 293 | public function getConnection(): string |
||
| 297 | |||
| 298 | /** |
||
| 299 | * {@inheritdoc} |
||
| 300 | */ |
||
| 301 | public function setRepositoryId($repositoryId) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * {@inheritdoc} |
||
| 310 | */ |
||
| 311 | public function getRepositoryId(): string |
||
| 315 | |||
| 316 | /** |
||
| 317 | * {@inheritdoc} |
||
| 318 | */ |
||
| 319 | public function setModel($model) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * {@inheritdoc} |
||
| 328 | */ |
||
| 329 | public function getModel(): string |
||
| 335 | |||
| 336 | /** |
||
| 337 | * {@inheritdoc} |
||
| 338 | */ |
||
| 339 | public function with($relations) |
||
| 340 | { |
||
| 341 | if (is_string($relations)) { |
||
| 342 | $relations = func_get_args(); |
||
| 343 | } |
||
| 344 | |||
| 345 | $this->relations = $relations; |
||
| 346 | |||
| 347 | return $this; |
||
| 348 | } |
||
| 349 | |||
| 350 | /** |
||
| 351 | * {@inheritdoc} |
||
| 352 | */ |
||
| 353 | public function where($attribute, $operator = null, $value = null, $boolean = 'and') |
||
| 360 | |||
| 361 | /** |
||
| 362 | * {@inheritdoc} |
||
| 363 | */ |
||
| 364 | public function whereIn($attribute, $values, $boolean = 'and', $not = false) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * {@inheritdoc} |
||
| 374 | */ |
||
| 375 | public function whereNotIn($attribute, $values, $boolean = 'and') |
||
| 382 | |||
| 383 | /** |
||
| 384 | * {@inheritdoc} |
||
| 385 | */ |
||
| 386 | public function whereHas($relation, Closure $callback = null, $operator = '>=', $count = 1) |
||
| 393 | |||
| 394 | /** |
||
| 395 | * {@inheritdoc} |
||
| 396 | */ |
||
| 397 | public function scope($name, array $parameters = []) |
||
| 403 | |||
| 404 | /** |
||
| 405 | * {@inheritdoc} |
||
| 406 | */ |
||
| 407 | public function offset($offset) |
||
| 413 | |||
| 414 | /** |
||
| 415 | * {@inheritdoc} |
||
| 416 | */ |
||
| 417 | public function limit($limit) |
||
| 423 | |||
| 424 | /** |
||
| 425 | * {@inheritdoc} |
||
| 426 | */ |
||
| 427 | public function orderBy($attribute, $direction = 'asc') |
||
| 428 | { |
||
| 429 | $this->orderBy[] = [$attribute, $direction ?: 'asc']; |
||
| 430 | |||
| 431 | return $this; |
||
| 432 | } |
||
| 433 | |||
| 434 | /** |
||
| 435 | * {@inheritdoc} |
||
| 436 | */ |
||
| 437 | public function groupBy($column) |
||
| 443 | |||
| 444 | /** |
||
| 445 | * {@inheritdoc} |
||
| 446 | */ |
||
| 447 | public function having($column, $operator = null, $value = null, $boolean = 'and') |
||
| 453 | |||
| 454 | /** |
||
| 455 | * {@inheritdoc} |
||
| 456 | */ |
||
| 457 | public function orHaving($column, $operator = null, $value = null, $boolean = 'and') |
||
| 461 | |||
| 462 | /** |
||
| 463 | * {@inheritdoc} |
||
| 464 | */ |
||
| 465 | public function store($id, array $attributes = [], bool $syncRelations = false) |
||
| 469 | |||
| 470 | /** |
||
| 471 | * {@inheritdoc} |
||
| 472 | */ |
||
| 473 | public static function __callStatic($method, $parameters) |
||
| 477 | |||
| 478 | /** |
||
| 479 | * {@inheritdoc} |
||
| 480 | */ |
||
| 481 | public function __call($method, $parameters) |
||
| 491 | } |
||
| 492 |
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: