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 actions. |
||
| 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 | * Alias for $this->setTtl($ttl) |
||
| 110 | * |
||
| 111 | * @param $ttl |
||
| 112 | * @return EloquentModel |
||
| 113 | */ |
||
| 114 | public function cache($ttl) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Fire a model event. |
||
| 121 | * |
||
| 122 | * @param $event |
||
| 123 | * @return mixed |
||
| 124 | */ |
||
| 125 | public function fireEvent($event) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Return the entry presenter. |
||
| 132 | * |
||
| 133 | * This is against standards but required |
||
| 134 | * by the presentable interface. |
||
| 135 | * |
||
| 136 | * @return EloquentPresenter |
||
| 137 | */ |
||
| 138 | View Code Duplication | public function getPresenter() |
|
| 148 | |||
| 149 | /** |
||
| 150 | * Return a new collection class with our models. |
||
| 151 | * |
||
| 152 | * @param array $items |
||
| 153 | * @return Collection |
||
| 154 | */ |
||
| 155 | View Code Duplication | public function newCollection(array $items = []) |
|
| 165 | |||
| 166 | /** |
||
| 167 | * Return the translatable flag. |
||
| 168 | * |
||
| 169 | * @return bool |
||
| 170 | */ |
||
| 171 | public function isTranslatable() |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Set the translatable flag. |
||
| 178 | * |
||
| 179 | * @param $translatable |
||
| 180 | * @return $this |
||
| 181 | */ |
||
| 182 | public function setTranslatable($translatable) |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Set the ttl. |
||
| 191 | * |
||
| 192 | * @param $ttl |
||
| 193 | * @return $this |
||
| 194 | */ |
||
| 195 | public function setTtl($ttl) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Get the ttl. |
||
| 204 | * |
||
| 205 | * @return int|mixed |
||
| 206 | */ |
||
| 207 | public function getTtl() |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Get cache collection key. |
||
| 214 | * |
||
| 215 | * @return string |
||
| 216 | */ |
||
| 217 | public function getCacheCollectionKey() |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Get the model title. |
||
| 224 | * |
||
| 225 | * @return mixed |
||
| 226 | */ |
||
| 227 | public function getTitle() |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Get the title key. |
||
| 234 | * |
||
| 235 | * @return string |
||
| 236 | */ |
||
| 237 | public function getTitleName() |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Return if a row is deletable or not. |
||
| 244 | * |
||
| 245 | * @return bool |
||
| 246 | */ |
||
| 247 | public function isDeletable() |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Return if the model is restorable or not. |
||
| 254 | * |
||
| 255 | * @return bool |
||
| 256 | */ |
||
| 257 | public function isRestorable() |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Return whether the model is being |
||
| 264 | * force deleted or not. |
||
| 265 | * |
||
| 266 | * @return bool |
||
| 267 | */ |
||
| 268 | public function isForceDeleting() |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Flush the model's cache. |
||
| 275 | * |
||
| 276 | * @return $this |
||
| 277 | */ |
||
| 278 | public function flushCache() |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Create a new Eloquent query builder for the model. |
||
| 287 | * |
||
| 288 | * @param \Illuminate\Database\Query\Builder $query |
||
| 289 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 290 | */ |
||
| 291 | public function newEloquentBuilder($query) |
||
| 295 | |||
| 296 | /* |
||
| 297 | * Alias for getTranslation() |
||
| 298 | */ |
||
| 299 | public function translate($locale = null, $withFallback = false) |
||
| 303 | |||
| 304 | /* |
||
| 305 | * Alias for getTranslation() |
||
| 306 | */ |
||
| 307 | public function translateOrDefault($locale) |
||
| 311 | |||
| 312 | /* |
||
| 313 | * Alias for getTranslationOrNew() |
||
| 314 | */ |
||
| 315 | public function translateOrNew($locale) |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Get related translations. |
||
| 322 | * |
||
| 323 | * @return EloquentCollection |
||
| 324 | */ |
||
| 325 | public function getTranslations() |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @param null $locale |
||
| 336 | * @param bool|null $withFallback |
||
| 337 | * @return Model|null |
||
| 338 | */ |
||
| 339 | public function getTranslation($locale = null, $withFallback = true) |
||
| 375 | |||
| 376 | public function hasTranslation($locale = null) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Get the translation model. |
||
| 393 | * |
||
| 394 | * @return EloquentModel |
||
| 395 | */ |
||
| 396 | public function getTranslationModel() |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Get the translation model name. |
||
| 403 | * |
||
| 404 | * @return string |
||
| 405 | */ |
||
| 406 | public function getTranslationModelName() |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Get the translation table name. |
||
| 413 | * |
||
| 414 | * @return string |
||
| 415 | */ |
||
| 416 | public function getTranslationTableName() |
||
| 422 | |||
| 423 | public function getTranslationModelNameDefault() |
||
| 427 | |||
| 428 | public function getRelationKey() |
||
| 432 | |||
| 433 | public function getLocaleKey() |
||
| 437 | |||
| 438 | public function translations() |
||
| 442 | |||
| 443 | public function getAttribute($key) |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Set an attribute. |
||
| 461 | * |
||
| 462 | * @param string $key |
||
| 463 | * @param mixed $value |
||
| 464 | * @return $this |
||
| 465 | */ |
||
| 466 | public function setAttribute($key, $value) |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Save the model. |
||
| 479 | * |
||
| 480 | * We have some customization here to |
||
| 481 | * accommodate translations. First sa |
||
| 482 | * then save translations is translatable. |
||
| 483 | * |
||
| 484 | * @param array $options |
||
| 485 | * @return bool |
||
| 486 | */ |
||
| 487 | public function save(array $options = []) |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Save the model to the database. |
||
| 520 | * |
||
| 521 | * This is a direct port from Eloquent |
||
| 522 | * with the only exception being that if |
||
| 523 | * the model is translatable it will NOT |
||
| 524 | * fire the saved event. The saveTranslations |
||
| 525 | * method will do that instead. |
||
| 526 | * |
||
| 527 | * @param array $options |
||
| 528 | * @return bool |
||
| 529 | */ |
||
| 530 | public function saveModel(array $options = []) |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Save translations to the database. |
||
| 564 | * |
||
| 565 | * @return bool |
||
| 566 | */ |
||
| 567 | protected function saveTranslations() |
||
| 592 | |||
| 593 | protected function getTranslationOrNew($locale) |
||
| 601 | |||
| 602 | public function fill(array $attributes) |
||
| 617 | |||
| 618 | private function getTranslationByLocaleKey($key) |
||
| 630 | |||
| 631 | public function isTranslatedAttribute($key) |
||
| 635 | |||
| 636 | protected function isTranslationAttribute($key) |
||
| 640 | |||
| 641 | protected function isKeyALocale($key) |
||
| 645 | |||
| 646 | protected function isTranslationDirty(Model $translation) |
||
| 653 | |||
| 654 | public function getNewTranslation($locale) |
||
| 670 | |||
| 671 | public function scopeTranslatedIn(Builder $query, $locale) |
||
| 680 | |||
| 681 | public function scopeTranslated(Builder $query) |
||
| 685 | |||
| 686 | /** |
||
| 687 | * Return unguarded attributes. |
||
| 688 | * |
||
| 689 | * @return array |
||
| 690 | */ |
||
| 691 | public function getUnguardedAttributes() |
||
| 699 | |||
| 700 | /** |
||
| 701 | * Get the default locale. |
||
| 702 | * |
||
| 703 | * @return string |
||
| 704 | */ |
||
| 705 | protected function getDefaultLocale() |
||
| 713 | |||
| 714 | /** |
||
| 715 | * Get the fallback locale. |
||
| 716 | * |
||
| 717 | * @return string |
||
| 718 | */ |
||
| 719 | protected function getFallbackLocale() |
||
| 727 | |||
| 728 | /** |
||
| 729 | * This is to keep consistency with the |
||
| 730 | * entry interface above us. |
||
| 731 | * |
||
| 732 | * @return string |
||
| 733 | */ |
||
| 734 | public function getTableName() |
||
| 738 | |||
| 739 | /** |
||
| 740 | * Return if the entry is trashed or not. |
||
| 741 | * |
||
| 742 | * @return bool |
||
| 743 | */ |
||
| 744 | public function trashed() |
||
| 748 | |||
| 749 | public function toArray() |
||
| 761 | |||
| 762 | /** |
||
| 763 | * Return the routable array information. |
||
| 764 | * |
||
| 765 | * @return array |
||
| 766 | */ |
||
| 767 | public function toRoutableArray() |
||
| 771 | |||
| 772 | private function alwaysFillable() |
||
| 776 | |||
| 777 | /** |
||
| 778 | * Determine if the given attribute exists. |
||
| 779 | * Make sure to skip where there could be an |
||
| 780 | * issue with relational "looking" properties. |
||
| 781 | * |
||
| 782 | * @param mixed $offset |
||
| 783 | * @return bool |
||
| 784 | */ |
||
| 785 | public function offsetExists($offset) |
||
| 789 | |||
| 790 | /** |
||
| 791 | * Get the criteria class. |
||
| 792 | * |
||
| 793 | * @return string |
||
| 794 | */ |
||
| 795 | public function getCriteriaName() |
||
| 801 | |||
| 802 | /** |
||
| 803 | * Get the cascading actions. |
||
| 804 | * |
||
| 805 | * @return array |
||
| 806 | */ |
||
| 807 | public function getCascades() |
||
| 811 | |||
| 812 | public function __get($key) |
||
| 820 | |||
| 821 | public function __call($method, $parameters) |
||
| 829 | |||
| 830 | /** |
||
| 831 | * Check if an attribute exists. |
||
| 832 | * |
||
| 833 | * @param string $key |
||
| 834 | * @return bool |
||
| 835 | */ |
||
| 836 | public function __isset($key) |
||
| 840 | |||
| 841 | /** |
||
| 842 | * Return the string form of the model. |
||
| 843 | * |
||
| 844 | * @return string |
||
| 845 | */ |
||
| 846 | public function __toString() |
||
| 850 | |||
| 851 | /** |
||
| 852 | * Remove volatile cache from |
||
| 853 | * objects before serialization. |
||
| 854 | * |
||
| 855 | * @return array |
||
| 856 | */ |
||
| 857 | public function __sleep() |
||
| 861 | } |
||
| 862 |
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.