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() |
||
158 | |||
159 | /** |
||
160 | * Prepare query. |
||
161 | * |
||
162 | * @param object $model |
||
163 | * |
||
164 | * @return object |
||
165 | */ |
||
166 | protected function prepareQuery($model) |
||
220 | |||
221 | /** |
||
222 | * {@inheritdoc} |
||
223 | */ |
||
224 | public function setContainer(Container $container) |
||
230 | |||
231 | /** |
||
232 | * {@inheritdoc} |
||
233 | */ |
||
234 | public function getContainer($service = null) |
||
238 | |||
239 | /** |
||
240 | * {@inheritdoc} |
||
241 | */ |
||
242 | public function setConnection($name) |
||
248 | |||
249 | /** |
||
250 | * {@inheritdoc} |
||
251 | */ |
||
252 | public function getConnection() |
||
256 | |||
257 | /** |
||
258 | * {@inheritdoc} |
||
259 | */ |
||
260 | public function setRepositoryId($repositoryId) |
||
266 | |||
267 | /** |
||
268 | * {@inheritdoc} |
||
269 | */ |
||
270 | public function getRepositoryId() |
||
274 | |||
275 | /** |
||
276 | * {@inheritdoc} |
||
277 | */ |
||
278 | public function setModel($model) |
||
284 | |||
285 | /** |
||
286 | * {@inheritdoc} |
||
287 | */ |
||
288 | public function getModel() |
||
294 | |||
295 | /** |
||
296 | * {@inheritdoc} |
||
297 | */ |
||
298 | public function with(array $relations) |
||
304 | |||
305 | /** |
||
306 | * {@inheritdoc} |
||
307 | */ |
||
308 | public function where($attribute, $operator = null, $value = null, $boolean = 'and') |
||
315 | |||
316 | /** |
||
317 | * {@inheritdoc} |
||
318 | */ |
||
319 | public function whereIn($attribute, $values, $boolean = 'and', $not = false) |
||
326 | |||
327 | /** |
||
328 | * {@inheritdoc} |
||
329 | */ |
||
330 | public function whereNotIn($attribute, $values, $boolean = 'and') |
||
337 | |||
338 | /** |
||
339 | * {@inheritdoc} |
||
340 | */ |
||
341 | public function whereHas($relation, Closure $callback, $operator = '>=', $count = 1) |
||
348 | |||
349 | /** |
||
350 | * {@inheritdoc} |
||
351 | */ |
||
352 | public function offset($offset) |
||
358 | |||
359 | /** |
||
360 | * {@inheritdoc} |
||
361 | */ |
||
362 | public function limit($limit) |
||
368 | |||
369 | /** |
||
370 | * {@inheritdoc} |
||
371 | */ |
||
372 | public function orderBy($attribute, $direction = 'asc') |
||
378 | |||
379 | /** |
||
380 | * Start a new database transaction. |
||
381 | * |
||
382 | * @throws \Exception |
||
383 | * |
||
384 | * @return void |
||
385 | */ |
||
386 | public function beginTransaction() |
||
390 | |||
391 | /** |
||
392 | * Commit the active database transaction. |
||
393 | * |
||
394 | * @return void |
||
395 | */ |
||
396 | public function commit() |
||
400 | |||
401 | /** |
||
402 | * Rollback the active database transaction. |
||
403 | * |
||
404 | * @return void |
||
405 | */ |
||
406 | public function rollBack() |
||
410 | |||
411 | /** |
||
412 | * {@inheritdoc} |
||
413 | */ |
||
414 | public static function __callStatic($method, $parameters) |
||
418 | |||
419 | /** |
||
420 | * {@inheritdoc} |
||
421 | */ |
||
422 | public function __call($method, $parameters) |
||
426 | } |
||
427 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.