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 | 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 | /** |
||
| 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 | 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 | /** |
||
| 717 | * Get the criteria class. |
||
| 718 | * |
||
| 719 | * @return string |
||
| 720 | */ |
||
| 721 | public function getCriteriaName() |
||
| 727 | |||
| 728 | public function __get($key) |
||
| 736 | |||
| 737 | public function __call($method, $parameters) |
||
| 745 | |||
| 746 | /** |
||
| 747 | * Check if an attribute exists. |
||
| 748 | * |
||
| 749 | * @param string $key |
||
| 750 | * @return bool |
||
| 751 | */ |
||
| 752 | public function __isset($key) |
||
| 756 | |||
| 757 | /** |
||
| 758 | * Return the string form of the model. |
||
| 759 | * |
||
| 760 | * @return string |
||
| 761 | */ |
||
| 762 | function __toString() |
||
| 766 | } |
||
| 767 |
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.