Complex classes like CacheableEloquent 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 CacheableEloquent, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | trait CacheableEloquent |
||
12 | { |
||
13 | /** |
||
14 | * Indicate if the model cache clear is enabled. |
||
15 | * |
||
16 | * @var bool |
||
17 | */ |
||
18 | protected static $cacheClearEnabled = true; |
||
19 | |||
20 | /** |
||
21 | * The model cache driver. |
||
22 | * |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $cacheDriver; |
||
26 | |||
27 | /** |
||
28 | * The model cache lifetime. |
||
29 | * |
||
30 | * @var float|int |
||
31 | */ |
||
32 | protected $cacheLifetime = -1; |
||
33 | |||
34 | /** |
||
35 | * Register an updated model event with the dispatcher. |
||
36 | * |
||
37 | * @param \Closure|string $callback |
||
38 | * |
||
39 | * @return void |
||
40 | */ |
||
41 | abstract public static function updated($callback); |
||
42 | |||
43 | /** |
||
44 | * Register a created model event with the dispatcher. |
||
45 | * |
||
46 | * @param \Closure|string $callback |
||
47 | * |
||
48 | * @return void |
||
49 | */ |
||
50 | abstract public static function created($callback); |
||
51 | |||
52 | /** |
||
53 | * Register a deleted model event with the dispatcher. |
||
54 | * |
||
55 | * @param \Closure|string $callback |
||
56 | * |
||
57 | * @return void |
||
58 | */ |
||
59 | abstract public static function deleted($callback); |
||
60 | |||
61 | /** |
||
62 | * Boot the cacheable eloquent trait for a model. |
||
63 | * |
||
64 | * @return void |
||
65 | */ |
||
66 | public static function bootCacheableEloquent() |
||
70 | |||
71 | /** |
||
72 | * Store the given cache key for the given model by mimicking cache tags. |
||
73 | * |
||
74 | * @param string $modelName |
||
75 | * @param string $cacheKey |
||
76 | * |
||
77 | * @return void |
||
78 | */ |
||
79 | protected static function storeCacheKey(string $modelName, string $cacheKey) |
||
89 | |||
90 | /** |
||
91 | * Get cache keys from the given file. |
||
92 | * |
||
93 | * @param string $file |
||
94 | * |
||
95 | * @return array |
||
96 | */ |
||
97 | protected static function getCacheKeys($file) |
||
105 | |||
106 | /** |
||
107 | * Flush cache keys of the given model by mimicking cache tags. |
||
108 | * |
||
109 | * @param string $modelName |
||
110 | * |
||
111 | * @return array |
||
112 | */ |
||
113 | protected static function flushCacheKeys(string $modelName): array |
||
129 | |||
130 | /** |
||
131 | * Set the model cache lifetime. |
||
132 | * |
||
133 | * @param float|int $cacheLifetime |
||
134 | * |
||
135 | * @return $this |
||
136 | */ |
||
137 | public function setCacheLifetime($cacheLifetime) |
||
143 | |||
144 | /** |
||
145 | * Get the model cache lifetime. |
||
146 | * |
||
147 | * @return float|int |
||
148 | */ |
||
149 | public function getCacheLifetime() |
||
153 | |||
154 | /** |
||
155 | * Set the model cache driver. |
||
156 | * |
||
157 | * @param string $cacheDriver |
||
158 | * |
||
159 | * @return $this |
||
160 | */ |
||
161 | public function setCacheDriver($cacheDriver) |
||
167 | |||
168 | /** |
||
169 | * Get the model cache driver. |
||
170 | * |
||
171 | * @return string |
||
172 | */ |
||
173 | public function getCacheDriver() |
||
177 | |||
178 | /** |
||
179 | * Determine if model cache clear is enabled. |
||
180 | * |
||
181 | * @return bool |
||
182 | */ |
||
183 | public static function isCacheClearEnabled() |
||
187 | |||
188 | /** |
||
189 | * Forget the model cache. |
||
190 | * |
||
191 | * @return void |
||
192 | */ |
||
193 | public static function forgetCache() |
||
209 | |||
210 | /** |
||
211 | * Fire the given event for the model. |
||
212 | * |
||
213 | * @param string $event |
||
214 | * @param bool $halt |
||
215 | * |
||
216 | * @return mixed |
||
217 | */ |
||
218 | protected static function fireCacheFlushEvent($event, $halt = true) |
||
233 | |||
234 | /** |
||
235 | * Reset cached model to its defaults. |
||
236 | * |
||
237 | * @return $this |
||
238 | */ |
||
239 | public function resetCacheConfig() |
||
246 | |||
247 | /** |
||
248 | * Generate unique cache key. |
||
249 | * |
||
250 | * @param \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder $builder |
||
251 | * @param array $columns |
||
252 | * |
||
253 | * @return string |
||
254 | */ |
||
255 | protected function generateCacheKey($builder, array $columns) |
||
256 | { |
||
257 | $query = $builder instanceof Builder ? $builder->getQuery() : $builder; |
||
258 | $vars = [ |
||
259 | 'aggregate' => $query->aggregate, |
||
260 | 'columns' => $query->columns, |
||
261 | 'distinct' => $query->distinct, |
||
262 | 'from' => $query->from, |
||
263 | 'joins' => $query->joins, |
||
264 | 'wheres' => $query->wheres, |
||
265 | 'groups' => $query->groups, |
||
266 | 'havings' => $query->havings, |
||
267 | 'orders' => $query->orders, |
||
268 | 'limit' => $query->limit, |
||
269 | 'offset' => $query->offset, |
||
270 | 'unions' => $query->unions, |
||
271 | 'unionLimit' => $query->unionLimit, |
||
272 | 'unionOffset' => $query->unionOffset, |
||
273 | 'unionOrders' => $query->unionOrders, |
||
274 | 'lock' => $query->lock, |
||
275 | ]; |
||
276 | |||
277 | return md5(json_encode([ |
||
278 | $vars, |
||
279 | $columns, |
||
280 | static::class, |
||
281 | $this->getCacheDriver(), |
||
282 | $this->getCacheLifetime(), |
||
283 | $builder instanceof Builder ? $builder->getEagerLoads() : null, |
||
284 | $builder->getBindings(), |
||
|
|||
285 | $builder->toSql(), |
||
286 | ])); |
||
287 | } |
||
288 | |||
289 | /** |
||
290 | * Cache given callback. |
||
291 | * |
||
292 | * @param \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder $builder |
||
293 | * @param array $columns |
||
294 | * @param \Closure $closure |
||
295 | * |
||
296 | * @return mixed |
||
297 | */ |
||
298 | public function cacheQuery($builder, array $columns, Closure $closure) |
||
326 | |||
327 | /** |
||
328 | * Create a new Eloquent query builder for the model. |
||
329 | * |
||
330 | * @param \Illuminate\Database\Query\Builder $query |
||
331 | * |
||
332 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
333 | */ |
||
334 | public function newEloquentBuilder($query) |
||
338 | |||
339 | /** |
||
340 | * Attach events to the model. |
||
341 | * |
||
342 | * @return void |
||
343 | */ |
||
344 | protected static function attacheEvents() |
||
364 | |||
365 | /** |
||
366 | * Define a many-to-many relationship. |
||
367 | * |
||
368 | * @param string $related |
||
369 | * @param string $table |
||
370 | * @param string $foreignPivotKey |
||
371 | * @param string $relatedPivotKey |
||
372 | * @param string $parentKey |
||
373 | * @param string $relatedKey |
||
374 | * @param string $relation |
||
375 | * |
||
376 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
377 | */ |
||
378 | public function belongsToMany($related, $table = null, $foreignPivotKey = null, $relatedPivotKey = null, |
||
410 | } |
||
411 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: