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 | * The column to order results by. |
||
114 | * |
||
115 | * @var array |
||
116 | */ |
||
117 | protected $groupBy = []; |
||
118 | |||
119 | /** |
||
120 | * The query having clauses. |
||
121 | * |
||
122 | * @var array |
||
123 | */ |
||
124 | protected $having = []; |
||
125 | |||
126 | /** |
||
127 | * Execute given callback and return the result. |
||
128 | * |
||
129 | * @param string $class |
||
130 | * @param string $method |
||
131 | * @param array $args |
||
132 | * @param \Closure $closure |
||
133 | * |
||
134 | * @return mixed |
||
135 | */ |
||
136 | protected function executeCallback($class, $method, $args, Closure $closure) |
||
153 | |||
154 | /** |
||
155 | * Reset repository to it's defaults. |
||
156 | * |
||
157 | * @return $this |
||
158 | */ |
||
159 | protected function resetRepository() |
||
178 | |||
179 | /** |
||
180 | * Prepare query. |
||
181 | * |
||
182 | * @param object $model |
||
183 | * |
||
184 | * @return object |
||
185 | */ |
||
186 | protected function prepareQuery($model) |
||
259 | |||
260 | /** |
||
261 | * {@inheritdoc} |
||
262 | */ |
||
263 | public function setContainer(Container $container) |
||
269 | |||
270 | /** |
||
271 | * {@inheritdoc} |
||
272 | */ |
||
273 | public function getContainer($service = null) |
||
277 | |||
278 | /** |
||
279 | * {@inheritdoc} |
||
280 | */ |
||
281 | public function setConnection($name) |
||
287 | |||
288 | /** |
||
289 | * {@inheritdoc} |
||
290 | */ |
||
291 | public function getConnection() |
||
295 | |||
296 | /** |
||
297 | * {@inheritdoc} |
||
298 | */ |
||
299 | public function setRepositoryId($repositoryId) |
||
305 | |||
306 | /** |
||
307 | * {@inheritdoc} |
||
308 | */ |
||
309 | public function getRepositoryId() |
||
313 | |||
314 | /** |
||
315 | * {@inheritdoc} |
||
316 | */ |
||
317 | public function setModel($model) |
||
323 | |||
324 | /** |
||
325 | * {@inheritdoc} |
||
326 | */ |
||
327 | public function getModel() |
||
333 | |||
334 | /** |
||
335 | * {@inheritdoc} |
||
336 | */ |
||
337 | public function getModelInstance($id) |
||
341 | |||
342 | /** |
||
343 | * {@inheritdoc} |
||
344 | */ |
||
345 | public function with($relations) |
||
355 | |||
356 | /** |
||
357 | * {@inheritdoc} |
||
358 | */ |
||
359 | public function where($attribute, $operator = null, $value = null, $boolean = 'and') |
||
366 | |||
367 | /** |
||
368 | * {@inheritdoc} |
||
369 | */ |
||
370 | public function whereIn($attribute, $values, $boolean = 'and', $not = false) |
||
377 | |||
378 | /** |
||
379 | * {@inheritdoc} |
||
380 | */ |
||
381 | public function whereNotIn($attribute, $values, $boolean = 'and') |
||
388 | |||
389 | /** |
||
390 | * {@inheritdoc} |
||
391 | */ |
||
392 | public function whereHas($relation, Closure $callback, $operator = '>=', $count = 1) |
||
399 | |||
400 | /** |
||
401 | * {@inheritdoc} |
||
402 | */ |
||
403 | public function offset($offset) |
||
409 | |||
410 | /** |
||
411 | * {@inheritdoc} |
||
412 | */ |
||
413 | public function limit($limit) |
||
419 | |||
420 | /** |
||
421 | * {@inheritdoc} |
||
422 | */ |
||
423 | public function orderBy($attribute, $direction = 'asc') |
||
429 | |||
430 | /** |
||
431 | * {@inheritdoc} |
||
432 | */ |
||
433 | public function groupBy($column) |
||
439 | |||
440 | /** |
||
441 | * {@inheritdoc} |
||
442 | */ |
||
443 | public function having($column, $operator = null, $value = null, $boolean = 'and') |
||
449 | |||
450 | /** |
||
451 | * {@inheritdoc} |
||
452 | */ |
||
453 | public function orHaving($column, $operator = null, $value = null, $boolean = 'and') |
||
457 | |||
458 | /** |
||
459 | * {@inheritdoc} |
||
460 | */ |
||
461 | public function store($id, array $attributes = []) |
||
465 | |||
466 | /** |
||
467 | * {@inheritdoc} |
||
468 | */ |
||
469 | public static function __callStatic($method, $parameters) |
||
473 | |||
474 | /** |
||
475 | * {@inheritdoc} |
||
476 | */ |
||
477 | public function __call($method, $parameters) |
||
481 | } |
||
482 |
If you implement
__call
and 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
__call
is implemented by a parent class and only the child class knows which methods exist: