Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like EloquentModel 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 EloquentModel, and based on these observations, apply Extract Interface, too.
1 | <?php namespace Anomaly\Streams\Platform\Model; |
||
19 | class EloquentModel extends Model implements Arrayable |
||
20 | { |
||
21 | |||
22 | use DispatchesJobs; |
||
23 | use Hookable; |
||
24 | |||
25 | /** |
||
26 | * Disable timestamps for this model. |
||
27 | * |
||
28 | * @var bool |
||
29 | */ |
||
30 | public $timestamps = false; |
||
31 | |||
32 | /** |
||
33 | * Translatable attributes. |
||
34 | * |
||
35 | * @var array |
||
36 | */ |
||
37 | protected $translatedAttributes = []; |
||
38 | |||
39 | /** |
||
40 | * The number of minutes to cache query results. |
||
41 | * |
||
42 | * @var null|false|int |
||
43 | */ |
||
44 | protected $ttl = false; |
||
45 | |||
46 | /** |
||
47 | * The attributes that are |
||
48 | * not mass assignable. Let upper |
||
49 | * models handle this themselves. |
||
50 | * |
||
51 | * @var array |
||
52 | */ |
||
53 | protected $guarded = []; |
||
54 | |||
55 | /** |
||
56 | * The title key. |
||
57 | * |
||
58 | * @var string |
||
59 | */ |
||
60 | protected $titleKey = 'id'; |
||
61 | |||
62 | /** |
||
63 | * Observable model events. |
||
64 | * |
||
65 | * @var array |
||
66 | */ |
||
67 | protected $observables = [ |
||
68 | 'updatingMultiple', |
||
69 | 'updatedMultiple', |
||
70 | 'deletingMultiple', |
||
71 | 'deletedMultiple' |
||
72 | ]; |
||
73 | |||
74 | /** |
||
75 | * Runtime cache. |
||
76 | * |
||
77 | * @var array |
||
78 | */ |
||
79 | protected $cache = []; |
||
80 | |||
81 | /** |
||
82 | * Get the ID. |
||
83 | * |
||
84 | * @return integer |
||
85 | */ |
||
86 | public function getId() |
||
87 | { |
||
88 | return $this->id; |
||
|
|||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Return the object's ETag fingerprint. |
||
93 | * |
||
94 | * @return string |
||
95 | */ |
||
96 | public function etag() |
||
97 | { |
||
98 | return md5(get_class($this) . json_encode($this->toArray())); |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Alias for $this->setTtl($ttl) |
||
103 | * |
||
104 | * @param $ttl |
||
105 | * @return EloquentModel |
||
106 | */ |
||
107 | public function cache($ttl) |
||
108 | { |
||
109 | return $this->setTtl($ttl); |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Fire a model event. |
||
114 | * |
||
115 | * @param $event |
||
116 | * @return mixed |
||
117 | */ |
||
118 | public function fireEvent($event) |
||
119 | { |
||
120 | return $this->fireModelEvent($event); |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * Return a new collection class with our models. |
||
125 | * |
||
126 | * @param array $items |
||
127 | * @return Collection |
||
128 | */ |
||
129 | View Code Duplication | public function newCollection(array $items = array()) |
|
130 | { |
||
131 | $collection = substr(get_class($this), 0, -5) . 'Collection'; |
||
132 | |||
133 | if (class_exists($collection)) { |
||
134 | return new $collection($items); |
||
135 | } |
||
136 | |||
137 | return new EloquentCollection($items); |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * Return the translatable flag. |
||
142 | * |
||
143 | * @return bool |
||
144 | */ |
||
145 | public function isTranslatable() |
||
146 | { |
||
147 | return isset($this->translationModel); |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * Set the translatable flag. |
||
152 | * |
||
153 | * @param $translatable |
||
154 | * @return $this |
||
155 | */ |
||
156 | public function setTranslatable($translatable) |
||
157 | { |
||
158 | $this->translatable = $translatable; |
||
159 | |||
160 | return $this; |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * Set the ttl. |
||
165 | * |
||
166 | * @param $ttl |
||
167 | * @return $this |
||
168 | */ |
||
169 | public function setTtl($ttl) |
||
170 | { |
||
171 | $this->ttl = $ttl; |
||
172 | |||
173 | return $this; |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * Get the ttl. |
||
178 | * |
||
179 | * @return int|mixed |
||
180 | */ |
||
181 | public function getTtl() |
||
182 | { |
||
183 | return $this->ttl; |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * Get cache collection key. |
||
188 | * |
||
189 | * @return string |
||
190 | */ |
||
191 | public function getCacheCollectionKey() |
||
192 | { |
||
193 | return get_called_class(); |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * Get the model title. |
||
198 | * |
||
199 | * @return mixed |
||
200 | */ |
||
201 | public function getTitle() |
||
202 | { |
||
203 | return $this->{$this->getTitleName()}; |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * Get the title key. |
||
208 | * |
||
209 | * @return string |
||
210 | */ |
||
211 | public function getTitleName() |
||
212 | { |
||
213 | return $this->titleName ?: 'id'; |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * Return if a row is deletable or not. |
||
218 | * |
||
219 | * @return bool |
||
220 | */ |
||
221 | public function isDeletable() |
||
222 | { |
||
223 | return true; |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * Return if the model is restorable or not. |
||
228 | * |
||
229 | * @return bool |
||
230 | */ |
||
231 | public function isRestorable() |
||
232 | { |
||
233 | return true; |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * Return whether the model is being |
||
238 | * force deleted or not. |
||
239 | * |
||
240 | * @return bool |
||
241 | */ |
||
242 | public function isForceDeleting() |
||
243 | { |
||
244 | return isset($this->forceDeleting) && $this->forceDeleting == true; |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * Flush the model's cache. |
||
249 | * |
||
250 | * @return $this |
||
251 | */ |
||
252 | public function flushCache() |
||
253 | { |
||
254 | (new CacheCollection())->setKey($this->getCacheCollectionKey())->flush(); |
||
255 | |||
256 | return $this; |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * Get a new query builder for the model's table. |
||
261 | * |
||
262 | * @return \Illuminate\Database\Eloquent\Builder |
||
263 | */ |
||
264 | View Code Duplication | public function newQuery() |
|
265 | { |
||
266 | $builder = new EloquentQueryBuilder($this->newBaseQueryBuilder()); |
||
267 | |||
268 | // Once we have the query builders, we will set the model instances so the |
||
269 | // builder can easily access any information it may need from the model |
||
270 | // while it is constructing and executing various queries against it. |
||
271 | $builder->setModel($this)->with($this->with); |
||
272 | |||
273 | return $this->applyGlobalScopes($builder); |
||
274 | } |
||
275 | |||
276 | /* |
||
277 | * Alias for getTranslation() |
||
278 | */ |
||
279 | public function translate($locale = null, $withFallback = false) |
||
280 | { |
||
281 | return $this->getTranslation($locale, $withFallback); |
||
282 | } |
||
283 | |||
284 | /* |
||
285 | * Alias for getTranslation() |
||
286 | */ |
||
287 | public function translateOrDefault($locale) |
||
288 | { |
||
289 | return $this->getTranslation($locale, true) ?: $this; |
||
290 | } |
||
291 | |||
292 | /* |
||
293 | * Alias for getTranslationOrNew() |
||
294 | */ |
||
295 | public function translateOrNew($locale) |
||
296 | { |
||
297 | return $this->getTranslationOrNew($locale); |
||
298 | } |
||
299 | |||
300 | /** |
||
301 | * Get related translations. |
||
302 | * |
||
303 | * @return EloquentCollection |
||
304 | */ |
||
305 | public function getTranslations() |
||
306 | { |
||
307 | foreach ($translations = $this->translations()->get() as $translation) { |
||
308 | $translation->setRelation('parent', $this); |
||
309 | } |
||
310 | |||
311 | return $translations; |
||
312 | } |
||
313 | |||
314 | /** |
||
315 | * @param null $locale |
||
316 | * @param bool|null $withFallback |
||
317 | * @return Model|null |
||
318 | */ |
||
319 | public function getTranslation($locale = null, $withFallback = false) |
||
320 | { |
||
321 | $locale = $locale ?: $this->getFallbackLocale(); |
||
322 | |||
323 | if ($translation = $this->getTranslationByLocaleKey($locale)) { |
||
324 | return $translation; |
||
325 | } elseif ($withFallback |
||
326 | && $this->getFallbackLocale() |
||
327 | && $this->getTranslationByLocaleKey($this->getFallbackLocale()) |
||
328 | ) { |
||
329 | return $this->getTranslationByLocaleKey($this->getFallbackLocale()); |
||
330 | } |
||
331 | |||
332 | return null; |
||
333 | } |
||
334 | |||
335 | public function hasTranslation($locale = null) |
||
336 | { |
||
337 | $locale = $locale ?: $this->getFallbackLocale(); |
||
338 | |||
339 | View Code Duplication | foreach ($this->translations as $translation) { |
|
340 | |||
341 | $translation->setRelation('parent', $this); |
||
342 | |||
343 | if ($translation->getAttribute($this->getLocaleKey()) == $locale) { |
||
344 | return true; |
||
345 | } |
||
346 | } |
||
347 | |||
348 | return false; |
||
349 | } |
||
350 | |||
351 | /** |
||
352 | * Get the translation model. |
||
353 | * |
||
354 | * @return EloquentModel |
||
355 | */ |
||
356 | public function getTranslationModel() |
||
357 | { |
||
358 | return new $this->translationModel; |
||
359 | } |
||
360 | |||
361 | /** |
||
362 | * Get the translation model name. |
||
363 | * |
||
364 | * @return string |
||
365 | */ |
||
366 | public function getTranslationModelName() |
||
367 | { |
||
368 | return $this->translationModel; |
||
369 | } |
||
370 | |||
371 | /** |
||
372 | * Get the translation table name. |
||
373 | * |
||
374 | * @return string |
||
375 | */ |
||
376 | public function getTranslationTableName() |
||
382 | |||
383 | public function getTranslationModelNameDefault() |
||
384 | { |
||
385 | return get_class($this) . 'Translation'; |
||
386 | } |
||
387 | |||
388 | public function getRelationKey() |
||
389 | { |
||
390 | return $this->translationForeignKey ?: $this->getForeignKey(); |
||
391 | } |
||
392 | |||
393 | public function getLocaleKey() |
||
394 | { |
||
395 | return $this->localeKey ?: 'locale'; |
||
396 | } |
||
397 | |||
398 | public function translations() |
||
399 | { |
||
400 | return $this->hasMany($this->getTranslationModelName(), $this->getRelationKey()); |
||
401 | } |
||
402 | |||
403 | public function getAttribute($key) |
||
404 | { |
||
405 | if ($this->isTranslatedAttribute($key)) { |
||
406 | if ($this->getTranslation() === null) { |
||
407 | return null; |
||
408 | } |
||
409 | |||
410 | $translation = $this->getTranslation(); |
||
419 | |||
420 | public function setAttribute($key, $value) |
||
428 | |||
429 | /** |
||
430 | * Save the model. |
||
431 | * |
||
432 | * We have some customization here to |
||
433 | * accommodate translations. First sa |
||
434 | * then save translations is translatable. |
||
435 | * |
||
436 | * @param array $options |
||
437 | * @return bool |
||
438 | */ |
||
439 | public function save(array $options = array()) |
||
470 | |||
471 | /** |
||
472 | * Save the model to the database. |
||
473 | * |
||
474 | * This is a direct port from Eloquent |
||
475 | * with the only exception being that if |
||
476 | * the model is translatable it will NOT |
||
477 | * fire the saved event. The saveTranslations |
||
478 | * method will do that instead. |
||
479 | * |
||
480 | * @param array $options |
||
481 | * @return bool |
||
482 | */ |
||
483 | public function saveModel(array $options = array()) |
||
514 | |||
515 | /** |
||
516 | * Save translations to the database. |
||
517 | * |
||
518 | * @return bool |
||
519 | */ |
||
520 | View Code Duplication | protected function saveTranslations() |
|
541 | |||
542 | protected function getTranslationOrNew($locale) |
||
550 | |||
551 | public function fill(array $attributes) |
||
566 | |||
567 | private function getTranslationByLocaleKey($key) |
||
580 | |||
581 | public function isTranslatedAttribute($key) |
||
585 | |||
586 | protected function isTranslationAttribute($key) |
||
590 | |||
591 | protected function isKeyALocale($key) |
||
595 | |||
596 | protected function isTranslationDirty(Model $translation) |
||
603 | |||
604 | View Code Duplication | public function getNewTranslation($locale) |
|
620 | |||
621 | public function scopeTranslatedIn(Builder $query, $locale) |
||
630 | |||
631 | public function scopeTranslated(Builder $query) |
||
635 | |||
636 | /** |
||
637 | * Return unguarded attributes. |
||
638 | * |
||
639 | * @return array |
||
640 | */ |
||
641 | public function getUnguardedAttributes() |
||
649 | |||
650 | /** |
||
651 | * Get the fallback locale. |
||
652 | * |
||
653 | * @return string |
||
654 | */ |
||
655 | protected function getFallbackLocale() |
||
663 | |||
664 | /** |
||
665 | * This is to keep consistency with the |
||
666 | * entry interface above us. |
||
667 | * |
||
668 | * @return string |
||
669 | */ |
||
670 | public function getTableName() |
||
674 | |||
675 | /** |
||
676 | * Return if the entry is trashed or not. |
||
677 | * |
||
678 | * @return bool |
||
679 | */ |
||
680 | public function trashed() |
||
684 | |||
685 | public function toArray() |
||
697 | |||
698 | private function alwaysFillable() |
||
702 | |||
703 | /** |
||
704 | * Determine if the given attribute exists. |
||
705 | * Make sure to skip where there could be an |
||
706 | * issue with relational "looking" properties. |
||
707 | * |
||
708 | * @param mixed $offset |
||
709 | * @return bool |
||
710 | */ |
||
711 | public function offsetExists($offset) |
||
715 | |||
716 | public function __get($key) |
||
724 | |||
725 | public function __call($method, $parameters) |
||
733 | |||
734 | /** |
||
735 | * Check if an attribute exists. |
||
736 | * |
||
737 | * @param string $key |
||
738 | * @return bool |
||
739 | */ |
||
740 | public function __isset($key) |
||
744 | |||
745 | /** |
||
746 | * Return the string form of the model. |
||
747 | * |
||
748 | * @return string |
||
749 | */ |
||
750 | function __toString() |
||
754 | } |
||
755 |
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.