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 |
||
22 | abstract class BaseRepository implements RepositoryContract |
||
23 | { |
||
24 | /** |
||
25 | * The IoC container instance. |
||
26 | * |
||
27 | * @var \Illuminate\Contracts\Container\Container |
||
28 | */ |
||
29 | protected $container; |
||
30 | |||
31 | /** |
||
32 | * The repository model. |
||
33 | * |
||
34 | * @var object |
||
35 | */ |
||
36 | protected $model; |
||
37 | |||
38 | /** |
||
39 | * The repository identifier. |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $repositoryId; |
||
44 | |||
45 | /** |
||
46 | * Indicate if the repository cache is enabled. |
||
47 | * |
||
48 | * @var bool |
||
49 | */ |
||
50 | protected $cacheEnabled = true; |
||
51 | |||
52 | /** |
||
53 | * Indicate if the repository cache clear is enabled. |
||
54 | * |
||
55 | * @var bool |
||
56 | */ |
||
57 | protected $cacheClearEnabled = true; |
||
58 | |||
59 | /** |
||
60 | * The repository cache lifetime. |
||
61 | * |
||
62 | * @var int |
||
63 | */ |
||
64 | protected $cacheLifetime; |
||
65 | |||
66 | /** |
||
67 | * The repository cache driver. |
||
68 | * |
||
69 | * @var string |
||
70 | */ |
||
71 | protected $cacheDriver; |
||
72 | |||
73 | /** |
||
74 | * Execute given callback and cache result set. |
||
75 | * |
||
76 | * @param string $class |
||
77 | * @param string $method |
||
78 | * @param array $args |
||
79 | * @param \Closure $closure |
||
80 | * |
||
81 | * @return mixed |
||
82 | */ |
||
83 | protected function executeCallback($class, $method, $args, Closure $closure) |
||
112 | |||
113 | /** |
||
114 | * Set the IoC container instance. |
||
115 | * |
||
116 | * @param \Illuminate\Contracts\Container\Container $container |
||
117 | * |
||
118 | * @return $this |
||
119 | */ |
||
120 | public function setContainer(Container $container) |
||
126 | |||
127 | /** |
||
128 | * Get the IoC container instance or any of it's services. |
||
129 | * |
||
130 | * @param string|null $service |
||
131 | * |
||
132 | * @return mixed |
||
133 | */ |
||
134 | public function getContainer($service = null) |
||
138 | |||
139 | /** |
||
140 | * Set the repository identifier. |
||
141 | * |
||
142 | * @param string $repositoryId |
||
143 | * |
||
144 | * @return $this |
||
145 | */ |
||
146 | public function setRepositoryId($repositoryId) |
||
152 | |||
153 | /** |
||
154 | * Get the repository identifier. |
||
155 | * |
||
156 | * @return string |
||
157 | */ |
||
158 | public function getRepositoryId() |
||
162 | |||
163 | /** |
||
164 | * Set the repository cache lifetime. |
||
165 | * |
||
166 | * @param int $cacheLifetime |
||
167 | * |
||
168 | * @return $this |
||
169 | */ |
||
170 | public function setCacheLifetime($cacheLifetime) |
||
176 | |||
177 | /** |
||
178 | * Get the repository cache lifetime. |
||
179 | * |
||
180 | * @return int |
||
181 | */ |
||
182 | public function getCacheLifetime() |
||
188 | |||
189 | /** |
||
190 | * Set the repository cache driver. |
||
191 | * |
||
192 | * @param string $cacheDriver |
||
193 | * |
||
194 | * @return $this |
||
195 | */ |
||
196 | public function setCacheDriver($cacheDriver) |
||
202 | |||
203 | /** |
||
204 | * Get the repository cache driver. |
||
205 | * |
||
206 | * @return string |
||
207 | */ |
||
208 | public function getCacheDriver() |
||
212 | |||
213 | /** |
||
214 | * Enable repository cache. |
||
215 | * |
||
216 | * @param bool $status |
||
217 | * |
||
218 | * @return $this |
||
219 | */ |
||
220 | public function enableCache($status = true) |
||
226 | |||
227 | /** |
||
228 | * Determine if repository cache is enabled. |
||
229 | * |
||
230 | * @return bool |
||
231 | */ |
||
232 | public function isCacheEnabled() |
||
236 | |||
237 | /** |
||
238 | * Enable repository cache clear. |
||
239 | * |
||
240 | * @param bool $status |
||
241 | * |
||
242 | * @return $this |
||
243 | */ |
||
244 | public function enableCacheClear($status = true) |
||
250 | |||
251 | /** |
||
252 | * Determine if repository cache clear is enabled. |
||
253 | * |
||
254 | * @return bool |
||
255 | */ |
||
256 | public function isCacheClearEnabled() |
||
260 | |||
261 | /** |
||
262 | * Forget the repository cache. |
||
263 | * |
||
264 | * @return $this |
||
265 | */ |
||
266 | public function forgetCache() |
||
283 | |||
284 | /** |
||
285 | * Set the relationships that should be eager loaded. |
||
286 | * |
||
287 | * @param mixed $relations |
||
288 | * |
||
289 | * @return $this |
||
290 | */ |
||
291 | public function with($relations) |
||
297 | |||
298 | /** |
||
299 | * Add an "order by" clause to the repository. |
||
300 | * |
||
301 | * @param string $column |
||
302 | * @param string $direction |
||
303 | * |
||
304 | * @return $this |
||
305 | */ |
||
306 | public function orderBy($column, $direction = 'asc') |
||
312 | |||
313 | /** |
||
314 | * Register a new global scope. |
||
315 | * |
||
316 | * @param \Illuminate\Database\Eloquent\Scope|\Closure|string $scope |
||
317 | * @param \Closure|null $implementation |
||
318 | * |
||
319 | * @throws \InvalidArgumentException |
||
320 | * |
||
321 | * @return mixed |
||
322 | */ |
||
323 | public function addGlobalScope($scope, Closure $implementation = null) |
||
327 | |||
328 | /** |
||
329 | * Remove all or passed registered global scopes. |
||
330 | * |
||
331 | * @param array|null $scopes |
||
332 | * |
||
333 | * @return $this |
||
334 | */ |
||
335 | public function withoutGlobalScopes(array $scopes = null) |
||
341 | |||
342 | /** |
||
343 | * Dynamically pass missing static methods to the model. |
||
344 | * |
||
345 | * @param $method |
||
346 | * @param $parameters |
||
347 | * |
||
348 | * @return mixed |
||
349 | */ |
||
350 | public static function __callStatic($method, $parameters) |
||
354 | |||
355 | /** |
||
356 | * Dynamically pass missing methods to the model. |
||
357 | * |
||
358 | * @param string $method |
||
359 | * @param array $parameters |
||
360 | * |
||
361 | * @return mixed |
||
362 | */ |
||
363 | public function __call($method, $parameters) |
||
369 | |||
370 | /** |
||
371 | * Store cache keys by mimicking cache tags. |
||
372 | * |
||
373 | * @param string $class |
||
374 | * @param string $method |
||
375 | * @param string $hash |
||
376 | * |
||
377 | * @return void |
||
378 | */ |
||
379 | protected function storeCacheKeys($class, $method, $hash) |
||
389 | |||
390 | /** |
||
391 | * Flush cache keys by mimicking cache tags. |
||
392 | * |
||
393 | * @return array |
||
394 | */ |
||
395 | protected function flushCacheKeys() |
||
413 | |||
414 | /** |
||
415 | * Get cache keys. |
||
416 | * |
||
417 | * @param string $file |
||
418 | * |
||
419 | * @return array |
||
420 | */ |
||
421 | protected function getCacheKeys($file) |
||
425 | |||
426 | /** |
||
427 | * Determine if repository is cacheable. |
||
428 | * |
||
429 | * @return bool |
||
430 | */ |
||
431 | protected function isCacheable() |
||
438 | } |
||
439 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.