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 query having clauses. |
||
114 | * |
||
115 | * @var array |
||
116 | */ |
||
117 | protected $having = []; |
||
118 | |||
119 | /** |
||
120 | * Execute given callback and return the result. |
||
121 | * |
||
122 | * @param string $class |
||
123 | * @param string $method |
||
124 | * @param array $args |
||
125 | * @param \Closure $closure |
||
126 | * |
||
127 | * @return mixed |
||
128 | */ |
||
129 | protected function executeCallback($class, $method, $args, Closure $closure) |
||
146 | |||
147 | /** |
||
148 | * Reset repository to it's defaults. |
||
149 | * |
||
150 | * @return $this |
||
151 | */ |
||
152 | protected function resetRepository() |
||
170 | |||
171 | /** |
||
172 | * Prepare query. |
||
173 | * |
||
174 | * @param object $model |
||
175 | * |
||
176 | * @return object |
||
177 | */ |
||
178 | protected function prepareQuery($model) |
||
244 | |||
245 | /** |
||
246 | * {@inheritdoc} |
||
247 | */ |
||
248 | public function setContainer(Container $container) |
||
254 | |||
255 | /** |
||
256 | * {@inheritdoc} |
||
257 | */ |
||
258 | public function getContainer($service = null) |
||
262 | |||
263 | /** |
||
264 | * {@inheritdoc} |
||
265 | */ |
||
266 | public function setConnection($name) |
||
272 | |||
273 | /** |
||
274 | * {@inheritdoc} |
||
275 | */ |
||
276 | public function getConnection() |
||
280 | |||
281 | /** |
||
282 | * {@inheritdoc} |
||
283 | */ |
||
284 | public function setRepositoryId($repositoryId) |
||
290 | |||
291 | /** |
||
292 | * {@inheritdoc} |
||
293 | */ |
||
294 | public function getRepositoryId() |
||
298 | |||
299 | /** |
||
300 | * {@inheritdoc} |
||
301 | */ |
||
302 | public function setModel($model) |
||
308 | |||
309 | /** |
||
310 | * {@inheritdoc} |
||
311 | */ |
||
312 | public function getModel() |
||
318 | |||
319 | /** |
||
320 | * {@inheritdoc} |
||
321 | */ |
||
322 | public function getModelInstance($id) |
||
326 | |||
327 | /** |
||
328 | * {@inheritdoc} |
||
329 | */ |
||
330 | public function with($relations) |
||
340 | |||
341 | /** |
||
342 | * {@inheritdoc} |
||
343 | */ |
||
344 | public function where($attribute, $operator = null, $value = null, $boolean = 'and') |
||
351 | |||
352 | /** |
||
353 | * {@inheritdoc} |
||
354 | */ |
||
355 | public function whereIn($attribute, $values, $boolean = 'and', $not = false) |
||
362 | |||
363 | /** |
||
364 | * {@inheritdoc} |
||
365 | */ |
||
366 | public function whereNotIn($attribute, $values, $boolean = 'and') |
||
373 | |||
374 | /** |
||
375 | * {@inheritdoc} |
||
376 | */ |
||
377 | public function whereHas($relation, Closure $callback, $operator = '>=', $count = 1) |
||
384 | |||
385 | /** |
||
386 | * {@inheritdoc} |
||
387 | */ |
||
388 | public function offset($offset) |
||
394 | |||
395 | /** |
||
396 | * {@inheritdoc} |
||
397 | */ |
||
398 | public function limit($limit) |
||
404 | |||
405 | /** |
||
406 | * {@inheritdoc} |
||
407 | */ |
||
408 | public function orderBy($attribute, $direction = 'asc') |
||
414 | |||
415 | /** |
||
416 | * {@inheritdoc} |
||
417 | */ |
||
418 | public function having($column, $operator = null, $value = null, $boolean = 'and') |
||
424 | |||
425 | /** |
||
426 | * {@inheritdoc} |
||
427 | */ |
||
428 | public function orHaving($column, $operator = null, $value = null, $boolean = 'and') |
||
432 | |||
433 | /** |
||
434 | * {@inheritdoc} |
||
435 | */ |
||
436 | public function store($id, array $attributes = []) |
||
440 | |||
441 | /** |
||
442 | * {@inheritdoc} |
||
443 | */ |
||
444 | public static function __callStatic($method, $parameters) |
||
448 | |||
449 | /** |
||
450 | * {@inheritdoc} |
||
451 | */ |
||
452 | public function __call($method, $parameters) |
||
456 | } |
||
457 |
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: