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, PresentableInterface |
||
| 20 | { |
||
| 21 | |||
| 22 | use Hookable; |
||
| 23 | use DispatchesJobs; |
||
| 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 | * The cascading delete-able relations. |
||
| 76 | * |
||
| 77 | * @var array |
||
| 78 | */ |
||
| 79 | protected $cascades = []; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Runtime cache. |
||
| 83 | * |
||
| 84 | * @var array |
||
| 85 | */ |
||
| 86 | protected $cache = []; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Get the ID. |
||
| 90 | * |
||
| 91 | * @return integer |
||
| 92 | */ |
||
| 93 | public function getId() |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Return the object's ETag fingerprint. |
||
| 100 | * |
||
| 101 | * @return string |
||
| 102 | */ |
||
| 103 | public function etag() |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Cache a value in the |
||
| 110 | * model's cache collection. |
||
| 111 | * |
||
| 112 | * @param $key |
||
| 113 | * @param $ttl |
||
| 114 | * @param $value |
||
| 115 | * @return mixed |
||
| 116 | */ |
||
| 117 | public function cache($key, $ttl, $value) |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Fire a model event. |
||
| 133 | * |
||
| 134 | * @param $event |
||
| 135 | * @return mixed |
||
| 136 | */ |
||
| 137 | public function fireEvent($event) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Return the entry presenter. |
||
| 144 | * |
||
| 145 | * This is against standards but required |
||
| 146 | * by the presentable interface. |
||
| 147 | * |
||
| 148 | * @return EloquentPresenter |
||
| 149 | */ |
||
| 150 | View Code Duplication | public function getPresenter() |
|
| 160 | |||
| 161 | /** |
||
| 162 | * Return a new collection class with our models. |
||
| 163 | * |
||
| 164 | * @param array $items |
||
| 165 | * @return Collection |
||
| 166 | */ |
||
| 167 | View Code Duplication | public function newCollection(array $items = []) |
|
| 177 | |||
| 178 | /** |
||
| 179 | * Return the translatable flag. |
||
| 180 | * |
||
| 181 | * @return bool |
||
| 182 | */ |
||
| 183 | public function isTranslatable() |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Set the translatable flag. |
||
| 190 | * |
||
| 191 | * @param $translatable |
||
| 192 | * @return $this |
||
| 193 | */ |
||
| 194 | public function setTranslatable($translatable) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Set the ttl. |
||
| 203 | * |
||
| 204 | * @param $ttl |
||
| 205 | * @return $this |
||
| 206 | */ |
||
| 207 | public function setTtl($ttl) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Get the ttl. |
||
| 216 | * |
||
| 217 | * @return int|mixed |
||
| 218 | */ |
||
| 219 | public function getTtl() |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Get cache collection key. |
||
| 226 | * |
||
| 227 | * @return string |
||
| 228 | */ |
||
| 229 | public function getCacheCollectionKey() |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Get the model title. |
||
| 236 | * |
||
| 237 | * @return mixed |
||
| 238 | */ |
||
| 239 | public function getTitle() |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Get the title key. |
||
| 246 | * |
||
| 247 | * @return string |
||
| 248 | */ |
||
| 249 | public function getTitleName() |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Return if a row is deletable or not. |
||
| 256 | * |
||
| 257 | * @return bool |
||
| 258 | */ |
||
| 259 | public function isDeletable() |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Return if the model is restorable or not. |
||
| 266 | * |
||
| 267 | * @return bool |
||
| 268 | */ |
||
| 269 | public function isRestorable() |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Return whether the model is being |
||
| 276 | * force deleted or not. |
||
| 277 | * |
||
| 278 | * @return bool |
||
| 279 | */ |
||
| 280 | public function isForceDeleting() |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Flush the model's cache. |
||
| 287 | * |
||
| 288 | * @return $this |
||
| 289 | */ |
||
| 290 | public function flushCache() |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Create a new Eloquent query builder for the model. |
||
| 301 | * |
||
| 302 | * @param \Illuminate\Database\Query\Builder $query |
||
| 303 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 304 | */ |
||
| 305 | View Code Duplication | public function newEloquentBuilder($query) |
|
| 315 | |||
| 316 | /* |
||
| 317 | * Alias for getTranslation() |
||
| 318 | */ |
||
| 319 | public function translate($locale = null, $withFallback = false) |
||
| 323 | |||
| 324 | /* |
||
| 325 | * Alias for getTranslation() |
||
| 326 | */ |
||
| 327 | public function translateOrDefault($locale) |
||
| 331 | |||
| 332 | /* |
||
| 333 | * Alias for getTranslationOrNew() |
||
| 334 | */ |
||
| 335 | public function translateOrNew($locale) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Get related translations. |
||
| 342 | * |
||
| 343 | * @return EloquentCollection |
||
| 344 | */ |
||
| 345 | public function getTranslations() |
||
| 353 | |||
| 354 | /** |
||
| 355 | * @param null $locale |
||
| 356 | * @param bool|null $withFallback |
||
| 357 | * @return Model|null |
||
| 358 | */ |
||
| 359 | public function getTranslation($locale = null, $withFallback = true) |
||
| 395 | |||
| 396 | public function hasTranslation($locale = null) |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Get the translation model. |
||
| 413 | * |
||
| 414 | * @return EloquentModel |
||
| 415 | */ |
||
| 416 | public function getTranslationModel() |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Get the translation model name. |
||
| 423 | * |
||
| 424 | * @return string |
||
| 425 | */ |
||
| 426 | public function getTranslationModelName() |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Get the translation table name. |
||
| 433 | * |
||
| 434 | * @return string |
||
| 435 | */ |
||
| 436 | public function getTranslationTableName() |
||
| 442 | |||
| 443 | public function getTranslationModelNameDefault() |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Return translated attributes. |
||
| 450 | * |
||
| 451 | * @return array |
||
| 452 | */ |
||
| 453 | public function getTranslatedAttributes() |
||
| 457 | |||
| 458 | public function getRelationKey() |
||
| 462 | |||
| 463 | public function getLocaleKey() |
||
| 467 | |||
| 468 | public function translations() |
||
| 472 | |||
| 473 | public function getAttribute($key) |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Set an attribute. |
||
| 491 | * |
||
| 492 | * @param string $key |
||
| 493 | * @param mixed $value |
||
| 494 | * @return $this |
||
| 495 | */ |
||
| 496 | public function setAttribute($key, $value) |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Save the model. |
||
| 509 | * |
||
| 510 | * We have some customization here to |
||
| 511 | * accommodate translations. First sa |
||
| 512 | * then save translations is translatable. |
||
| 513 | * |
||
| 514 | * @param array $options |
||
| 515 | * @return bool |
||
| 516 | */ |
||
| 517 | public function save(array $options = []) |
||
| 547 | |||
| 548 | /** |
||
| 549 | * Save the model to the database. |
||
| 550 | * |
||
| 551 | * This is a direct port from Eloquent |
||
| 552 | * with the only exception being that if |
||
| 553 | * the model is translatable it will NOT |
||
| 554 | * fire the saved event. The saveTranslations |
||
| 555 | * method will do that instead. |
||
| 556 | * |
||
| 557 | * @param array $options |
||
| 558 | * @return bool |
||
| 559 | */ |
||
| 560 | public function saveModel(array $options = []) |
||
| 591 | |||
| 592 | /** |
||
| 593 | * Save translations to the database. |
||
| 594 | * |
||
| 595 | * @return bool |
||
| 596 | */ |
||
| 597 | protected function saveTranslations() |
||
| 622 | |||
| 623 | protected function getTranslationOrNew($locale) |
||
| 631 | |||
| 632 | public function fill(array $attributes) |
||
| 647 | |||
| 648 | private function getTranslationByLocaleKey($key) |
||
| 660 | |||
| 661 | public function isTranslatedAttribute($key) |
||
| 665 | |||
| 666 | protected function isTranslationAttribute($key) |
||
| 670 | |||
| 671 | protected function isKeyALocale($key) |
||
| 675 | |||
| 676 | protected function isTranslationDirty(Model $translation) |
||
| 683 | |||
| 684 | public function getNewTranslation($locale) |
||
| 700 | |||
| 701 | public function scopeTranslatedIn(Builder $query, $locale) |
||
| 710 | |||
| 711 | public function scopeTranslated(Builder $query) |
||
| 715 | |||
| 716 | /** |
||
| 717 | * Return unguarded attributes. |
||
| 718 | * |
||
| 719 | * @return array |
||
| 720 | */ |
||
| 721 | public function getUnguardedAttributes() |
||
| 729 | |||
| 730 | /** |
||
| 731 | * Get the default locale. |
||
| 732 | * |
||
| 733 | * @return string |
||
| 734 | */ |
||
| 735 | protected function getDefaultLocale() |
||
| 743 | |||
| 744 | /** |
||
| 745 | * Get the fallback locale. |
||
| 746 | * |
||
| 747 | * @return string |
||
| 748 | */ |
||
| 749 | protected function getFallbackLocale() |
||
| 757 | |||
| 758 | /** |
||
| 759 | * This is to keep consistency with the |
||
| 760 | * entry interface above us. |
||
| 761 | * |
||
| 762 | * @return string |
||
| 763 | */ |
||
| 764 | public function getTableName() |
||
| 768 | |||
| 769 | /** |
||
| 770 | * Return if the entry is trashed or not. |
||
| 771 | * |
||
| 772 | * @return bool |
||
| 773 | */ |
||
| 774 | public function trashed() |
||
| 778 | |||
| 779 | public function toArray() |
||
| 791 | |||
| 792 | /** |
||
| 793 | * Return the routable array information. |
||
| 794 | * |
||
| 795 | * @return array |
||
| 796 | */ |
||
| 797 | public function toRoutableArray() |
||
| 801 | |||
| 802 | private function alwaysFillable() |
||
| 806 | |||
| 807 | /** |
||
| 808 | * Determine if the given attribute exists. |
||
| 809 | * Make sure to skip where there could be an |
||
| 810 | * issue with relational "looking" properties. |
||
| 811 | * |
||
| 812 | * @param mixed $offset |
||
| 813 | * @return bool |
||
| 814 | */ |
||
| 815 | public function offsetExists($offset) |
||
| 819 | |||
| 820 | /** |
||
| 821 | * Get the criteria class. |
||
| 822 | * |
||
| 823 | * @return string |
||
| 824 | */ |
||
| 825 | public function getCriteriaName() |
||
| 831 | |||
| 832 | /** |
||
| 833 | * Get the cascading actions. |
||
| 834 | * |
||
| 835 | * @return array |
||
| 836 | */ |
||
| 837 | public function getCascades() |
||
| 841 | |||
| 842 | public function __get($key) |
||
| 850 | |||
| 851 | public function __call($method, $parameters) |
||
| 859 | |||
| 860 | /** |
||
| 861 | * Check if an attribute exists. |
||
| 862 | * |
||
| 863 | * @param string $key |
||
| 864 | * @return bool |
||
| 865 | */ |
||
| 866 | public function __isset($key) |
||
| 870 | |||
| 871 | /** |
||
| 872 | * Return the string form of the model. |
||
| 873 | * |
||
| 874 | * @return string |
||
| 875 | */ |
||
| 876 | public function __toString() |
||
| 880 | |||
| 881 | /** |
||
| 882 | * Remove volatile cache from |
||
| 883 | * objects before serialization. |
||
| 884 | * |
||
| 885 | * @return array |
||
| 886 | */ |
||
| 887 | public function __sleep() |
||
| 891 | } |
||
| 892 |
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@propertyannotation 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.