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() |
||
90 | |||
91 | /** |
||
92 | * Return the object's ETag fingerprint. |
||
93 | * |
||
94 | * @return string |
||
95 | */ |
||
96 | public function etag() |
||
100 | |||
101 | /** |
||
102 | * Alias for $this->setTtl($ttl) |
||
103 | * |
||
104 | * @param $ttl |
||
105 | * @return EloquentModel |
||
106 | */ |
||
107 | public function cache($ttl) |
||
111 | |||
112 | /** |
||
113 | * Fire a model event. |
||
114 | * |
||
115 | * @param $event |
||
116 | * @return mixed |
||
117 | */ |
||
118 | public function fireEvent($event) |
||
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()) |
|
139 | |||
140 | /** |
||
141 | * Return the translatable flag. |
||
142 | * |
||
143 | * @return bool |
||
144 | */ |
||
145 | public function isTranslatable() |
||
149 | |||
150 | /** |
||
151 | * Set the translatable flag. |
||
152 | * |
||
153 | * @param $translatable |
||
154 | * @return $this |
||
155 | */ |
||
156 | public function setTranslatable($translatable) |
||
162 | |||
163 | /** |
||
164 | * Set the ttl. |
||
165 | * |
||
166 | * @param $ttl |
||
167 | * @return $this |
||
168 | */ |
||
169 | public function setTtl($ttl) |
||
175 | |||
176 | /** |
||
177 | * Get the ttl. |
||
178 | * |
||
179 | * @return int|mixed |
||
180 | */ |
||
181 | public function getTtl() |
||
185 | |||
186 | /** |
||
187 | * Get cache collection key. |
||
188 | * |
||
189 | * @return string |
||
190 | */ |
||
191 | public function getCacheCollectionKey() |
||
195 | |||
196 | /** |
||
197 | * Get the model title. |
||
198 | * |
||
199 | * @return mixed |
||
200 | */ |
||
201 | public function getTitle() |
||
205 | |||
206 | /** |
||
207 | * Get the title key. |
||
208 | * |
||
209 | * @return string |
||
210 | */ |
||
211 | public function getTitleName() |
||
215 | |||
216 | /** |
||
217 | * Return if a row is deletable or not. |
||
218 | * |
||
219 | * @return bool |
||
220 | */ |
||
221 | public function isDeletable() |
||
225 | |||
226 | /** |
||
227 | * Return if the model is restorable or not. |
||
228 | * |
||
229 | * @return bool |
||
230 | */ |
||
231 | public function isRestorable() |
||
235 | |||
236 | /** |
||
237 | * Return whether the model is being |
||
238 | * force deleted or not. |
||
239 | * |
||
240 | * @return bool |
||
241 | */ |
||
242 | public function isForceDeleting() |
||
246 | |||
247 | /** |
||
248 | * Flush the model's cache. |
||
249 | * |
||
250 | * @return $this |
||
251 | */ |
||
252 | public function flushCache() |
||
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() |
|
275 | |||
276 | /* |
||
277 | * Alias for getTranslation() |
||
278 | */ |
||
279 | public function translate($locale = null, $withFallback = false) |
||
283 | |||
284 | /* |
||
285 | * Alias for getTranslation() |
||
286 | */ |
||
287 | public function translateOrDefault($locale) |
||
291 | |||
292 | /* |
||
293 | * Alias for getTranslationOrNew() |
||
294 | */ |
||
295 | public function translateOrNew($locale) |
||
299 | |||
300 | /** |
||
301 | * Get related translations. |
||
302 | * |
||
303 | * @return EloquentCollection |
||
304 | */ |
||
305 | public function getTranslations() |
||
313 | |||
314 | /** |
||
315 | * @param null $locale |
||
316 | * @param bool|null $withFallback |
||
317 | * @return Model|null |
||
318 | */ |
||
319 | public function getTranslation($locale = null, $withFallback = false) |
||
334 | |||
335 | public function hasTranslation($locale = null) |
||
350 | |||
351 | public function getTranslationModel() |
||
355 | |||
356 | public function getTranslationModelName() |
||
360 | |||
361 | public function getTranslationModelNameDefault() |
||
365 | |||
366 | public function getRelationKey() |
||
370 | |||
371 | public function getLocaleKey() |
||
375 | |||
376 | public function translations() |
||
380 | |||
381 | public function getAttribute($key) |
||
397 | |||
398 | public function setAttribute($key, $value) |
||
406 | |||
407 | /** |
||
408 | * Save the model. |
||
409 | * |
||
410 | * We have some customization here to |
||
411 | * accommodate translations. First sa |
||
412 | * then save translations is translatable. |
||
413 | * |
||
414 | * @param array $options |
||
415 | * @return bool |
||
416 | */ |
||
417 | public function save(array $options = array()) |
||
448 | |||
449 | /** |
||
450 | * Save the model to the database. |
||
451 | * |
||
452 | * This is a direct port from Eloquent |
||
453 | * with the only exception being that if |
||
454 | * the model is translatable it will NOT |
||
455 | * fire the saved event. The saveTranslations |
||
456 | * method will do that instead. |
||
457 | * |
||
458 | * @param array $options |
||
459 | * @return bool |
||
460 | */ |
||
461 | public function saveModel(array $options = array()) |
||
492 | |||
493 | /** |
||
494 | * Save translations to the database. |
||
495 | * |
||
496 | * @return bool |
||
497 | */ |
||
498 | View Code Duplication | protected function saveTranslations() |
|
519 | |||
520 | protected function getTranslationOrNew($locale) |
||
528 | |||
529 | public function fill(array $attributes) |
||
544 | |||
545 | private function getTranslationByLocaleKey($key) |
||
558 | |||
559 | public function isTranslatedAttribute($key) |
||
563 | |||
564 | protected function isTranslationAttribute($key) |
||
568 | |||
569 | protected function isKeyALocale($key) |
||
573 | |||
574 | protected function isTranslationDirty(Model $translation) |
||
581 | |||
582 | View Code Duplication | public function getNewTranslation($locale) |
|
598 | |||
599 | public function scopeTranslatedIn(Builder $query, $locale) |
||
608 | |||
609 | public function scopeTranslated(Builder $query) |
||
613 | |||
614 | /** |
||
615 | * Return unguarded attributes. |
||
616 | * |
||
617 | * @return array |
||
618 | */ |
||
619 | public function getUnguardedAttributes() |
||
627 | |||
628 | /** |
||
629 | * Get the fallback locale. |
||
630 | * |
||
631 | * @return string |
||
632 | */ |
||
633 | protected function getFallbackLocale() |
||
641 | |||
642 | /** |
||
643 | * This is to keep consistency with the |
||
644 | * entry interface above us. |
||
645 | * |
||
646 | * @return string |
||
647 | */ |
||
648 | public function getTableName() |
||
652 | |||
653 | /** |
||
654 | * Return if the entry is trashed or not. |
||
655 | * |
||
656 | * @return bool |
||
657 | */ |
||
658 | public function trashed() |
||
662 | |||
663 | public function toArray() |
||
675 | |||
676 | private function alwaysFillable() |
||
680 | |||
681 | public function __get($key) |
||
689 | |||
690 | public function __call($method, $parameters) |
||
698 | |||
699 | /** |
||
700 | * Check if an attribute exists. |
||
701 | * |
||
702 | * @param string $key |
||
703 | * @return bool |
||
704 | */ |
||
705 | public function __isset($key) |
||
709 | |||
710 | /** |
||
711 | * Return the string form of the model. |
||
712 | * |
||
713 | * @return string |
||
714 | */ |
||
715 | function __toString() |
||
719 | } |
||
720 |
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.