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 query scopes. |
||
93 | * |
||
94 | * @var array |
||
95 | */ |
||
96 | protected $scopes = []; |
||
97 | |||
98 | /** |
||
99 | * The "offset" value of the query. |
||
100 | * |
||
101 | * @var int |
||
102 | */ |
||
103 | protected $offset; |
||
104 | |||
105 | /** |
||
106 | * The "limit" value of the query. |
||
107 | * |
||
108 | * @var int |
||
109 | */ |
||
110 | protected $limit; |
||
111 | |||
112 | /** |
||
113 | * The column to order results by. |
||
114 | * |
||
115 | * @var array |
||
116 | */ |
||
117 | protected $orderBy = []; |
||
118 | |||
119 | /** |
||
120 | * The column to order results by. |
||
121 | * |
||
122 | * @var array |
||
123 | */ |
||
124 | protected $groupBy = []; |
||
125 | |||
126 | /** |
||
127 | * The query having clauses. |
||
128 | * |
||
129 | * @var array |
||
130 | */ |
||
131 | protected $having = []; |
||
132 | |||
133 | /** |
||
134 | * Execute given callback and return the result. |
||
135 | * |
||
136 | * @param string $class |
||
137 | * @param string $method |
||
138 | * @param array $args |
||
139 | * @param \Closure $closure |
||
140 | * |
||
141 | * @return mixed |
||
142 | */ |
||
143 | protected function executeCallback($class, $method, $args, Closure $closure) |
||
160 | |||
161 | /** |
||
162 | * Reset repository to its defaults. |
||
163 | * |
||
164 | * @return $this |
||
165 | */ |
||
166 | protected function resetRepository() |
||
186 | |||
187 | /** |
||
188 | * Prepare query. |
||
189 | * |
||
190 | * @param object $model |
||
191 | * |
||
192 | * @return mixed |
||
193 | */ |
||
194 | protected function prepareQuery($model) |
||
272 | |||
273 | /** |
||
274 | * {@inheritdoc} |
||
275 | */ |
||
276 | public function setContainer(Container $container) |
||
282 | |||
283 | /** |
||
284 | * {@inheritdoc} |
||
285 | */ |
||
286 | public function getContainer($service = null) |
||
290 | |||
291 | /** |
||
292 | * {@inheritdoc} |
||
293 | */ |
||
294 | public function setConnection($name) |
||
300 | |||
301 | /** |
||
302 | * {@inheritdoc} |
||
303 | */ |
||
304 | public function getConnection() |
||
308 | |||
309 | /** |
||
310 | * {@inheritdoc} |
||
311 | */ |
||
312 | public function setRepositoryId($repositoryId) |
||
318 | |||
319 | /** |
||
320 | * {@inheritdoc} |
||
321 | */ |
||
322 | public function getRepositoryId() |
||
326 | |||
327 | /** |
||
328 | * {@inheritdoc} |
||
329 | */ |
||
330 | public function setModel($model) |
||
336 | |||
337 | /** |
||
338 | * {@inheritdoc} |
||
339 | */ |
||
340 | public function getModel() |
||
346 | |||
347 | /** |
||
348 | * {@inheritdoc} |
||
349 | */ |
||
350 | public function with($relations) |
||
360 | |||
361 | /** |
||
362 | * {@inheritdoc} |
||
363 | */ |
||
364 | public function where($attribute, $operator = null, $value = null, $boolean = 'and') |
||
371 | |||
372 | /** |
||
373 | * {@inheritdoc} |
||
374 | */ |
||
375 | public function whereIn($attribute, $values, $boolean = 'and', $not = false) |
||
382 | |||
383 | /** |
||
384 | * {@inheritdoc} |
||
385 | */ |
||
386 | public function whereNotIn($attribute, $values, $boolean = 'and') |
||
393 | |||
394 | /** |
||
395 | * {@inheritdoc} |
||
396 | */ |
||
397 | public function whereHas($relation, Closure $callback = null, $operator = '>=', $count = 1) |
||
404 | |||
405 | /** |
||
406 | * {@inheritdoc} |
||
407 | */ |
||
408 | public function scope($name, array $parameters = []) |
||
414 | |||
415 | /** |
||
416 | * {@inheritdoc} |
||
417 | */ |
||
418 | public function offset($offset) |
||
424 | |||
425 | /** |
||
426 | * {@inheritdoc} |
||
427 | */ |
||
428 | public function limit($limit) |
||
434 | |||
435 | /** |
||
436 | * {@inheritdoc} |
||
437 | */ |
||
438 | public function orderBy($attribute, $direction = 'asc') |
||
444 | |||
445 | /** |
||
446 | * {@inheritdoc} |
||
447 | */ |
||
448 | public function groupBy($column) |
||
454 | |||
455 | /** |
||
456 | * {@inheritdoc} |
||
457 | */ |
||
458 | public function having($column, $operator = null, $value = null, $boolean = 'and') |
||
464 | |||
465 | /** |
||
466 | * {@inheritdoc} |
||
467 | */ |
||
468 | public function orHaving($column, $operator = null, $value = null, $boolean = 'and') |
||
472 | |||
473 | /** |
||
474 | * {@inheritdoc} |
||
475 | */ |
||
476 | public function store($id, array $attributes = []) |
||
480 | |||
481 | /** |
||
482 | * {@inheritdoc} |
||
483 | */ |
||
484 | public static function __callStatic($method, $parameters) |
||
488 | |||
489 | /** |
||
490 | * {@inheritdoc} |
||
491 | */ |
||
492 | public function __call($method, $parameters) |
||
502 | } |
||
503 |
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: