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 | * Execute given callback and cache result set. |
||
61 | * |
||
62 | * @param string $class |
||
63 | * @param string $method |
||
64 | * @param string $hash |
||
65 | * @param \Closure $closure |
||
66 | * @param int $lifetime |
||
|
|||
67 | * @param string $driver |
||
68 | * |
||
69 | * @return mixed |
||
70 | */ |
||
71 | protected function executeCallback($class, $method, $hash, $lifetime = null, $driver = null, Closure $closure) |
||
99 | |||
100 | /** |
||
101 | * Set the IoC container instance. |
||
102 | * |
||
103 | * @param \Illuminate\Contracts\Container\Container $container |
||
104 | * |
||
105 | * @return $this |
||
106 | */ |
||
107 | public function setContainer(Container $container) |
||
113 | |||
114 | /** |
||
115 | * Get the IoC container instance or any of it's services. |
||
116 | * |
||
117 | * @param string|null $service |
||
118 | * |
||
119 | * @return mixed |
||
120 | */ |
||
121 | public function getContainer($service = null) |
||
125 | |||
126 | /** |
||
127 | * Set the repository identifier. |
||
128 | * |
||
129 | * @param string $repositoryId |
||
130 | * |
||
131 | * @return $this |
||
132 | */ |
||
133 | public function setRepositoryId($repositoryId) |
||
139 | |||
140 | /** |
||
141 | * Get the repository identifier. |
||
142 | * |
||
143 | * @return string |
||
144 | */ |
||
145 | public function getRepositoryId() |
||
149 | |||
150 | /** |
||
151 | * Enable repository cache. |
||
152 | * |
||
153 | * @param bool $status |
||
154 | * |
||
155 | * @return $this |
||
156 | */ |
||
157 | public function enableCache($status = true) |
||
163 | |||
164 | /** |
||
165 | * Determine if repository cache is enabled. |
||
166 | * |
||
167 | * @return bool |
||
168 | */ |
||
169 | public function isCacheEnabled() |
||
173 | |||
174 | /** |
||
175 | * Enable repository cache clear. |
||
176 | * |
||
177 | * @param bool $status |
||
178 | * |
||
179 | * @return $this |
||
180 | */ |
||
181 | public function enableCacheClear($status = true) |
||
187 | |||
188 | /** |
||
189 | * Determine if repository cache clear is enabled. |
||
190 | * |
||
191 | * @return bool |
||
192 | */ |
||
193 | public function isCacheClearEnabled() |
||
197 | |||
198 | /** |
||
199 | * Forget the repository cache. |
||
200 | * |
||
201 | * @return $this |
||
202 | */ |
||
203 | public function forgetCache() |
||
222 | |||
223 | /** |
||
224 | * Set the relationships that should be eager loaded. |
||
225 | * |
||
226 | * @param mixed $relations |
||
227 | * |
||
228 | * @return $this |
||
229 | */ |
||
230 | public function with($relations) |
||
236 | |||
237 | /** |
||
238 | * Add an "order by" clause to the repository. |
||
239 | * |
||
240 | * @param string $column |
||
241 | * @param string $direction |
||
242 | * |
||
243 | * @return $this |
||
244 | */ |
||
245 | public function orderBy($column, $direction = 'asc') |
||
251 | |||
252 | /** |
||
253 | * Register a new global scope. |
||
254 | * |
||
255 | * @param \Illuminate\Database\Eloquent\Scope|\Closure|string $scope |
||
256 | * @param \Closure|null $implementation |
||
257 | * |
||
258 | * @throws \InvalidArgumentException |
||
259 | * |
||
260 | * @return mixed |
||
261 | */ |
||
262 | public function addGlobalScope($scope, Closure $implementation = null) |
||
266 | |||
267 | /** |
||
268 | * Remove all or passed registered global scopes. |
||
269 | * |
||
270 | * @param array|null $scopes |
||
271 | * |
||
272 | * @return $this |
||
273 | */ |
||
274 | public function withoutGlobalScopes(array $scopes = null) |
||
280 | |||
281 | /** |
||
282 | * Dynamically pass missing static methods to the model. |
||
283 | * |
||
284 | * @param $method |
||
285 | * @param $parameters |
||
286 | * |
||
287 | * @return mixed |
||
288 | */ |
||
289 | public static function __callStatic($method, $parameters) |
||
293 | |||
294 | /** |
||
295 | * Dynamically pass missing methods to the model. |
||
296 | * |
||
297 | * @param string $method |
||
298 | * @param array $parameters |
||
299 | * |
||
300 | * @return mixed |
||
301 | */ |
||
302 | public function __call($method, $parameters) |
||
308 | |||
309 | /** |
||
310 | * Store cache keys by mimicking cache tags. |
||
311 | * |
||
312 | * @param string $class |
||
313 | * @param string $method |
||
314 | * @param string $hash |
||
315 | * @param string $file |
||
316 | * |
||
317 | * @return void |
||
318 | */ |
||
319 | protected function storeCacheKeys($class, $method, $hash, $file) |
||
328 | |||
329 | /** |
||
330 | * Flush cache keys by mimicking cache tags. |
||
331 | * |
||
332 | * @return array |
||
333 | */ |
||
334 | protected function flushCacheKeys() |
||
352 | |||
353 | /** |
||
354 | * Get cache keys file. |
||
355 | * |
||
356 | * @param string $file |
||
357 | * |
||
358 | * @return array |
||
359 | */ |
||
360 | protected function getCacheKeys($file) |
||
364 | |||
365 | /** |
||
366 | * Determine if repository method is cacheable. |
||
367 | * |
||
368 | * @param array $config |
||
369 | * @param string $method |
||
370 | * @param int $lifetime |
||
371 | * |
||
372 | * @return bool |
||
373 | */ |
||
374 | protected function isCacheableMethod($config, $method, $lifetime) |
||
380 | } |
||
381 |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.