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 |
||
| 24 | abstract class BaseRepository implements RepositoryContract, CacheableContract |
||
| 25 | { |
||
| 26 | use Cacheable; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * The IoC container instance. |
||
| 30 | * |
||
| 31 | * @var \Illuminate\Contracts\Container\Container |
||
| 32 | */ |
||
| 33 | protected $container; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * The connection name for the repository. |
||
| 37 | * |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | protected $connection; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * The repository identifier. |
||
| 44 | * |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | protected $repositoryId; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * The repository model. |
||
| 51 | * |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | protected $model; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * The relations to eager load on query execution. |
||
| 58 | * |
||
| 59 | * @var array |
||
| 60 | */ |
||
| 61 | protected $relations = []; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * The query where clauses. |
||
| 65 | * |
||
| 66 | * @var array |
||
| 67 | */ |
||
| 68 | protected $where = []; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * The query whereIn clauses. |
||
| 72 | * |
||
| 73 | * @var array |
||
| 74 | */ |
||
| 75 | protected $whereIn = []; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * The query whereNotIn clauses. |
||
| 79 | * |
||
| 80 | * @var array |
||
| 81 | */ |
||
| 82 | protected $whereNotIn = []; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * The query whereHas clauses. |
||
| 86 | * |
||
| 87 | * @var array |
||
| 88 | */ |
||
| 89 | protected $whereHas = []; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * The "offset" value of the query. |
||
| 93 | * |
||
| 94 | * @var int |
||
| 95 | */ |
||
| 96 | protected $offset; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * The "limit" value of the query. |
||
| 100 | * |
||
| 101 | * @var int |
||
| 102 | */ |
||
| 103 | protected $limit; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * The column to order results by. |
||
| 107 | * |
||
| 108 | * @var array |
||
| 109 | */ |
||
| 110 | protected $orderBy = []; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Execute given callback and return the result. |
||
| 114 | * |
||
| 115 | * @param string $class |
||
| 116 | * @param string $method |
||
| 117 | * @param array $args |
||
| 118 | * @param \Closure $closure |
||
| 119 | * |
||
| 120 | * @return mixed |
||
| 121 | */ |
||
| 122 | protected function executeCallback($class, $method, $args, Closure $closure) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Reset repository to it's defaults. |
||
| 142 | * |
||
| 143 | * @return $this |
||
| 144 | */ |
||
| 145 | protected function resetRepository() |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Prepare query. |
||
| 165 | * |
||
| 166 | * @param object $model |
||
| 167 | * |
||
| 168 | * @return object |
||
| 169 | */ |
||
| 170 | protected function prepareQuery($model) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * {@inheritdoc} |
||
| 232 | */ |
||
| 233 | public function setContainer(Container $container) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * {@inheritdoc} |
||
| 242 | */ |
||
| 243 | public function getContainer($service = null) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * {@inheritdoc} |
||
| 250 | */ |
||
| 251 | public function setConnection($name) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * {@inheritdoc} |
||
| 260 | */ |
||
| 261 | public function getConnection() |
||
| 265 | |||
| 266 | /** |
||
| 267 | * {@inheritdoc} |
||
| 268 | */ |
||
| 269 | public function setRepositoryId($repositoryId) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * {@inheritdoc} |
||
| 278 | */ |
||
| 279 | public function getRepositoryId() |
||
| 283 | |||
| 284 | /** |
||
| 285 | * {@inheritdoc} |
||
| 286 | */ |
||
| 287 | public function setModel($model) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * {@inheritdoc} |
||
| 296 | */ |
||
| 297 | public function getModel() |
||
| 303 | |||
| 304 | /** |
||
| 305 | * {@inheritdoc} |
||
| 306 | */ |
||
| 307 | public function getModelInstance($id) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * {@inheritdoc} |
||
| 314 | */ |
||
| 315 | public function with($relations) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * {@inheritdoc} |
||
| 328 | */ |
||
| 329 | public function where($attribute, $operator = null, $value = null, $boolean = 'and') |
||
| 336 | |||
| 337 | /** |
||
| 338 | * {@inheritdoc} |
||
| 339 | */ |
||
| 340 | public function whereIn($attribute, $values, $boolean = 'and', $not = false) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * {@inheritdoc} |
||
| 350 | */ |
||
| 351 | public function whereNotIn($attribute, $values, $boolean = 'and') |
||
| 358 | |||
| 359 | /** |
||
| 360 | * {@inheritdoc} |
||
| 361 | */ |
||
| 362 | public function whereHas($relation, Closure $callback, $operator = '>=', $count = 1) |
||
| 369 | |||
| 370 | /** |
||
| 371 | * {@inheritdoc} |
||
| 372 | */ |
||
| 373 | public function offset($offset) |
||
| 379 | |||
| 380 | /** |
||
| 381 | * {@inheritdoc} |
||
| 382 | */ |
||
| 383 | public function limit($limit) |
||
| 389 | |||
| 390 | /** |
||
| 391 | * {@inheritdoc} |
||
| 392 | */ |
||
| 393 | public function orderBy($attribute, $direction = 'asc') |
||
| 399 | |||
| 400 | /** |
||
| 401 | * {@inheritdoc} |
||
| 402 | */ |
||
| 403 | public function store($id, array $attributes = []) |
||
| 407 | |||
| 408 | /** |
||
| 409 | * {@inheritdoc} |
||
| 410 | */ |
||
| 411 | public static function __callStatic($method, $parameters) |
||
| 415 | |||
| 416 | /** |
||
| 417 | * {@inheritdoc} |
||
| 418 | */ |
||
| 419 | public function __call($method, $parameters) |
||
| 423 | } |
||
| 424 |
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: