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 | * |
||
67 | * @return mixed |
||
68 | */ |
||
69 | protected function executeCallback($class, $method, $hash, Closure $closure) |
||
87 | |||
88 | /** |
||
89 | * Set the IoC container instance. |
||
90 | * |
||
91 | * @param \Illuminate\Contracts\Container\Container $container |
||
92 | * |
||
93 | * @return $this |
||
94 | */ |
||
95 | public function setContainer(Container $container) |
||
101 | |||
102 | /** |
||
103 | * Return the IoC container instance or any of it's services. |
||
104 | * |
||
105 | * @param string|null $service |
||
106 | * |
||
107 | * @return mixed |
||
108 | */ |
||
109 | public function getContainer($service = null) |
||
113 | |||
114 | /** |
||
115 | * Set the repository identifier. |
||
116 | * |
||
117 | * @param string $repositoryId |
||
118 | * |
||
119 | * @return $this |
||
120 | */ |
||
121 | public function setRepositoryId($repositoryId) |
||
127 | |||
128 | /** |
||
129 | * Get the repository identifier. |
||
130 | * |
||
131 | * @return string |
||
132 | */ |
||
133 | public function getRepositoryId() |
||
137 | |||
138 | /** |
||
139 | * Enable repository cache. |
||
140 | * |
||
141 | * @param bool $status |
||
142 | * |
||
143 | * @return $this |
||
144 | */ |
||
145 | public function enableCache($status = true) |
||
146 | { |
||
147 | $this->cacheEnabled = $status; |
||
148 | |||
149 | return $this; |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Determine if repository cache is enabled. |
||
154 | * |
||
155 | * @return bool |
||
156 | */ |
||
157 | public function isCacheEnabled() |
||
158 | { |
||
159 | return $this->cacheEnabled; |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * Enable repository cache clear. |
||
164 | * |
||
165 | * @param bool $status |
||
166 | * |
||
167 | * @return $this |
||
168 | */ |
||
169 | public function enableCacheClear($status = true) |
||
170 | { |
||
171 | $this->cacheClearEnabled = $status; |
||
172 | |||
173 | return $this; |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * Determine if repository cache clear is enabled. |
||
178 | * |
||
179 | * @return bool |
||
180 | */ |
||
181 | public function isCacheClearEnabled() |
||
182 | { |
||
183 | return $this->cacheClearEnabled; |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * Forget the repository cache. |
||
188 | * |
||
189 | * @return $this |
||
190 | */ |
||
191 | public function forgetCache() |
||
210 | |||
211 | /** |
||
212 | * Set the relationships that should be eager loaded. |
||
213 | * |
||
214 | * @param mixed $relations |
||
215 | * |
||
216 | * @return $this |
||
217 | */ |
||
218 | public function with($relations) |
||
224 | |||
225 | /** |
||
226 | * Add an "order by" clause to the repository. |
||
227 | * |
||
228 | * @param string $column |
||
229 | * @param string $direction |
||
230 | * |
||
231 | * @return $this |
||
232 | */ |
||
233 | public function orderBy($column, $direction = 'asc') |
||
239 | |||
240 | /** |
||
241 | * Register a new global scope. |
||
242 | * |
||
243 | * @param \Illuminate\Database\Eloquent\Scope|\Closure|string $scope |
||
244 | * @param \Closure|null $implementation |
||
245 | * |
||
246 | * @throws \InvalidArgumentException |
||
247 | * |
||
248 | * @return mixed |
||
249 | */ |
||
250 | public function addGlobalScope($scope, Closure $implementation = null) |
||
254 | |||
255 | /** |
||
256 | * Remove all or passed registered global scopes. |
||
257 | * |
||
258 | * @param array|null $scopes |
||
259 | * |
||
260 | * @return $this |
||
261 | */ |
||
262 | public function withoutGlobalScopes(array $scopes = null) |
||
268 | |||
269 | /** |
||
270 | * Dynamically pass missing static methods to the model. |
||
271 | * |
||
272 | * @param $method |
||
273 | * @param $parameters |
||
274 | * |
||
275 | * @return mixed |
||
276 | */ |
||
277 | public static function __callStatic($method, $parameters) |
||
281 | |||
282 | /** |
||
283 | * Dynamically pass missing methods to the model. |
||
284 | * |
||
285 | * @param string $method |
||
286 | * @param array $parameters |
||
287 | * |
||
288 | * @return mixed |
||
289 | */ |
||
290 | public function __call($method, $parameters) |
||
296 | |||
297 | /** |
||
298 | * Store cache keys by mimicking cache tags. |
||
299 | * |
||
300 | * @param string $class |
||
301 | * @param string $method |
||
302 | * @param string $hash |
||
303 | * @param string $file |
||
304 | * |
||
305 | * @return void |
||
306 | */ |
||
307 | protected function storeCacheKeys($class, $method, $hash, $file) |
||
316 | |||
317 | /** |
||
318 | * Flush cache keys by mimicking cache tags. |
||
319 | * |
||
320 | * @return array |
||
321 | */ |
||
322 | protected function flushCacheKeys() |
||
340 | |||
341 | /** |
||
342 | * Get cache keys file. |
||
343 | * |
||
344 | * @param string $file |
||
345 | * |
||
346 | * @return array |
||
347 | */ |
||
348 | protected function getCacheKeys($file) |
||
352 | } |
||
353 |
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.