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()) |
|
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() |
||
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 | /** |
||
352 | * Get the translation model. |
||
353 | * |
||
354 | * @return EloquentModel |
||
355 | */ |
||
356 | public function getTranslationModel() |
||
360 | |||
361 | /** |
||
362 | * Get the translation model name. |
||
363 | * |
||
364 | * @return string |
||
365 | */ |
||
366 | public function getTranslationModelName() |
||
370 | |||
371 | /** |
||
372 | * Get the translation table name. |
||
373 | * |
||
374 | * @return string |
||
375 | */ |
||
376 | public function getTranslationTableName() |
||
377 | { |
||
378 | $model = $this->getTranslationModel(); |
||
379 | |||
380 | return $model->getTableName(); |
||
381 | } |
||
382 | |||
383 | public function getTranslationModelNameDefault() |
||
387 | |||
388 | public function getRelationKey() |
||
392 | |||
393 | public function getLocaleKey() |
||
397 | |||
398 | public function translations() |
||
402 | |||
403 | public function getAttribute($key) |
||
419 | |||
420 | /** |
||
421 | * Set an attribute. |
||
422 | * |
||
423 | * @param string $key |
||
424 | * @param mixed $value |
||
425 | * @return $this |
||
426 | */ |
||
427 | public function setAttribute($key, $value) |
||
437 | |||
438 | /** |
||
439 | * Save the model. |
||
440 | * |
||
441 | * We have some customization here to |
||
442 | * accommodate translations. First sa |
||
443 | * then save translations is translatable. |
||
444 | * |
||
445 | * @param array $options |
||
446 | * @return bool |
||
447 | */ |
||
448 | public function save(array $options = array()) |
||
479 | |||
480 | /** |
||
481 | * Save the model to the database. |
||
482 | * |
||
483 | * This is a direct port from Eloquent |
||
484 | * with the only exception being that if |
||
485 | * the model is translatable it will NOT |
||
486 | * fire the saved event. The saveTranslations |
||
487 | * method will do that instead. |
||
488 | * |
||
489 | * @param array $options |
||
490 | * @return bool |
||
491 | */ |
||
492 | public function saveModel(array $options = array()) |
||
523 | |||
524 | /** |
||
525 | * Save translations to the database. |
||
526 | * |
||
527 | * @return bool |
||
528 | */ |
||
529 | protected function saveTranslations() |
||
557 | |||
558 | protected function getTranslationOrNew($locale) |
||
566 | |||
567 | public function fill(array $attributes) |
||
582 | |||
583 | private function getTranslationByLocaleKey($key) |
||
596 | |||
597 | public function isTranslatedAttribute($key) |
||
601 | |||
602 | protected function isTranslationAttribute($key) |
||
606 | |||
607 | protected function isKeyALocale($key) |
||
611 | |||
612 | protected function isTranslationDirty(Model $translation) |
||
619 | |||
620 | public function getNewTranslation($locale) |
||
636 | |||
637 | public function scopeTranslatedIn(Builder $query, $locale) |
||
646 | |||
647 | public function scopeTranslated(Builder $query) |
||
651 | |||
652 | /** |
||
653 | * Return unguarded attributes. |
||
654 | * |
||
655 | * @return array |
||
656 | */ |
||
657 | public function getUnguardedAttributes() |
||
665 | |||
666 | /** |
||
667 | * Get the fallback locale. |
||
668 | * |
||
669 | * @return string |
||
670 | */ |
||
671 | protected function getFallbackLocale() |
||
679 | |||
680 | /** |
||
681 | * This is to keep consistency with the |
||
682 | * entry interface above us. |
||
683 | * |
||
684 | * @return string |
||
685 | */ |
||
686 | public function getTableName() |
||
690 | |||
691 | /** |
||
692 | * Return if the entry is trashed or not. |
||
693 | * |
||
694 | * @return bool |
||
695 | */ |
||
696 | public function trashed() |
||
700 | |||
701 | public function toArray() |
||
713 | |||
714 | /** |
||
715 | * Return the routable array information. |
||
716 | * |
||
717 | * @return array |
||
718 | */ |
||
719 | public function toRoutable() |
||
723 | |||
724 | private function alwaysFillable() |
||
728 | |||
729 | /** |
||
730 | * Determine if the given attribute exists. |
||
731 | * Make sure to skip where there could be an |
||
732 | * issue with relational "looking" properties. |
||
733 | * |
||
734 | * @param mixed $offset |
||
735 | * @return bool |
||
736 | */ |
||
737 | public function offsetExists($offset) |
||
741 | |||
742 | /** |
||
743 | * Get the criteria class. |
||
744 | * |
||
745 | * @return string |
||
746 | */ |
||
747 | public function getCriteriaName() |
||
753 | |||
754 | public function __get($key) |
||
762 | |||
763 | public function __call($method, $parameters) |
||
771 | |||
772 | /** |
||
773 | * Check if an attribute exists. |
||
774 | * |
||
775 | * @param string $key |
||
776 | * @return bool |
||
777 | */ |
||
778 | public function __isset($key) |
||
782 | |||
783 | /** |
||
784 | * Return the string form of the model. |
||
785 | * |
||
786 | * @return string |
||
787 | */ |
||
788 | function __toString() |
||
792 | } |
||
793 |
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.