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 Translatable 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 Translatable, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | trait Translatable |
||
| 13 | { |
||
| 14 | protected $defaultLocale; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Alias for getTranslation(). |
||
| 18 | * |
||
| 19 | * @param string|null $locale |
||
| 20 | * @param bool $withFallback |
||
| 21 | * |
||
| 22 | * @return \Illuminate\Database\Eloquent\Model|null |
||
| 23 | */ |
||
| 24 | public function translate($locale = null, $withFallback = false) |
||
| 25 | { |
||
| 26 | return $this->getTranslation($locale, $withFallback); |
||
| 27 | } |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Alias for getTranslation(). |
||
| 31 | * |
||
| 32 | * @param string $locale |
||
| 33 | * |
||
| 34 | * @return \Illuminate\Database\Eloquent\Model|null |
||
| 35 | */ |
||
| 36 | public function translateOrDefault($locale) |
||
| 37 | { |
||
| 38 | return $this->getTranslation($locale, true); |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Alias for getTranslationOrNew(). |
||
| 43 | * |
||
| 44 | * @param string $locale |
||
| 45 | * |
||
| 46 | * @return \Illuminate\Database\Eloquent\Model|null |
||
| 47 | */ |
||
| 48 | public function translateOrNew($locale) |
||
| 49 | { |
||
| 50 | return $this->getTranslationOrNew($locale); |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @param string|null $locale |
||
| 55 | * @param bool $withFallback |
||
| 56 | * |
||
| 57 | * @return \Illuminate\Database\Eloquent\Model|null |
||
| 58 | */ |
||
| 59 | public function getTranslation($locale = null, $withFallback = null) |
||
| 60 | { |
||
| 61 | $configFallbackLocale = $this->getFallbackLocale(); |
||
| 62 | $locale = $locale ?: $this->locale(); |
||
| 63 | $withFallback = $withFallback === null ? $this->useFallback() : $withFallback; |
||
| 64 | $fallbackLocale = $this->getFallbackLocale($locale); |
||
| 65 | |||
| 66 | if ($translation = $this->getTranslationByLocaleKey($locale)) { |
||
| 67 | return $translation; |
||
| 68 | } |
||
| 69 | if ($withFallback && $fallbackLocale) { |
||
| 70 | if ($translation = $this->getTranslationByLocaleKey($fallbackLocale)) { |
||
| 71 | return $translation; |
||
| 72 | } |
||
| 73 | if ($fallbackLocale !== $configFallbackLocale && $translation = $this->getTranslationByLocaleKey($configFallbackLocale)) { |
||
| 74 | return $translation; |
||
| 75 | } |
||
| 76 | } |
||
| 77 | |||
| 78 | return null; |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @param string|null $locale |
||
| 83 | * |
||
| 84 | * @return bool |
||
| 85 | */ |
||
| 86 | public function hasTranslation($locale = null) |
||
| 87 | { |
||
| 88 | $locale = $locale ?: $this->locale(); |
||
| 89 | |||
| 90 | foreach ($this->translations as $translation) { |
||
|
|
|||
| 91 | if ($translation->getAttribute($this->getLocaleKey()) == $locale) { |
||
| 92 | return true; |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | return false; |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @return string |
||
| 101 | */ |
||
| 102 | public function getTranslationModelName() |
||
| 103 | { |
||
| 104 | return $this->translationModel ?: $this->getTranslationModelNameDefault(); |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @return string |
||
| 109 | */ |
||
| 110 | public function getTranslationModelNameDefault() |
||
| 111 | { |
||
| 112 | return get_class($this).config('translatable.translation_suffix', 'Translation'); |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @return string |
||
| 117 | */ |
||
| 118 | public function getRelationKey() |
||
| 119 | { |
||
| 120 | if ($this->translationForeignKey) { |
||
| 121 | $key = $this->translationForeignKey; |
||
| 122 | } elseif ($this->primaryKey !== 'id') { |
||
| 123 | $key = $this->primaryKey; |
||
| 124 | } else { |
||
| 125 | $key = $this->getForeignKey(); |
||
| 126 | } |
||
| 127 | |||
| 128 | return $key; |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @return string |
||
| 133 | */ |
||
| 134 | public function getLocaleKey() |
||
| 135 | { |
||
| 136 | return $this->localeKey ?: config('translatable.locale_key', 'locale'); |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
| 141 | */ |
||
| 142 | public function translations() |
||
| 143 | { |
||
| 144 | return $this->hasMany($this->getTranslationModelName(), $this->getRelationKey()); |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @return bool |
||
| 149 | */ |
||
| 150 | private function usePropertyFallback() |
||
| 151 | { |
||
| 152 | return $this->useFallback() && config('translatable.use_property_fallback', false); |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Returns the attribute value from fallback translation if value of attribute |
||
| 157 | * is empty and the property fallback is enabled in the configuration. |
||
| 158 | * in model. |
||
| 159 | * @param $locale |
||
| 160 | * @param $attribute |
||
| 161 | * @return mixed |
||
| 162 | */ |
||
| 163 | private function getAttributeOrFallback($locale, $attribute) |
||
| 164 | { |
||
| 165 | $value = $this->getTranslation($locale)->$attribute; |
||
| 166 | |||
| 167 | if ( |
||
| 168 | empty($value) && |
||
| 169 | $this->usePropertyFallback() && |
||
| 170 | ($fallback = $this->getTranslation($this->getFallbackLocale(), true)) |
||
| 171 | ) { |
||
| 172 | return $fallback->$attribute; |
||
| 173 | } |
||
| 174 | |||
| 175 | return $value; |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @param string $key |
||
| 180 | * |
||
| 181 | * @return mixed |
||
| 182 | */ |
||
| 183 | public function getAttribute($key) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @param string $key |
||
| 209 | * @param mixed $value |
||
| 210 | * |
||
| 211 | * @return $this |
||
| 212 | */ |
||
| 213 | public function setAttribute($key, $value) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @param array $options |
||
| 228 | * |
||
| 229 | * @return bool |
||
| 230 | */ |
||
| 231 | public function save(array $options = []) |
||
| 232 | { |
||
| 233 | if ($this->exists) { |
||
| 234 | if ($this->isDirty()) { |
||
| 235 | // If $this->exists and dirty, parent::save() has to return true. If not, |
||
| 236 | // an error has occurred. Therefore we shouldn't save the translations. |
||
| 237 | if (parent::save($options)) { |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @param string $locale |
||
| 266 | * |
||
| 267 | * @return \Illuminate\Database\Eloquent\Model |
||
| 268 | */ |
||
| 269 | protected function getTranslationOrNew($locale) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @param array $attributes |
||
| 280 | * |
||
| 281 | * @throws \Illuminate\Database\Eloquent\MassAssignmentException |
||
| 282 | * @return $this |
||
| 283 | */ |
||
| 284 | public function fill(array $attributes) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @param string $key |
||
| 304 | */ |
||
| 305 | private function getTranslationByLocaleKey($key) |
||
| 315 | |||
| 316 | /** |
||
| 317 | * @param null $locale |
||
| 318 | * |
||
| 319 | * @return string |
||
| 320 | */ |
||
| 321 | private function getFallbackLocale($locale = null) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @param $locale |
||
| 334 | * |
||
| 335 | * @return bool |
||
| 336 | */ |
||
| 337 | private function isLocaleCountryBased($locale) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * @param $locale |
||
| 344 | * |
||
| 345 | * @return string |
||
| 346 | */ |
||
| 347 | private function getLanguageFromCountryBasedLocale($locale) |
||
| 353 | |||
| 354 | /** |
||
| 355 | * @return bool|null |
||
| 356 | */ |
||
| 357 | private function useFallback() |
||
| 365 | |||
| 366 | /** |
||
| 367 | * @param string $key |
||
| 368 | * |
||
| 369 | * @return bool |
||
| 370 | */ |
||
| 371 | public function isTranslationAttribute($key) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * @param string $key |
||
| 378 | * |
||
| 379 | * @throws \Dimsav\Translatable\Exception\LocalesNotDefinedException |
||
| 380 | * @return bool |
||
| 381 | */ |
||
| 382 | protected function isKeyALocale($key) |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @throws \Dimsav\Translatable\Exception\LocalesNotDefinedException |
||
| 391 | * @return array |
||
| 392 | */ |
||
| 393 | protected function getLocales() |
||
| 416 | |||
| 417 | /** |
||
| 418 | * @return string |
||
| 419 | */ |
||
| 420 | protected function getLocaleSeparator() |
||
| 424 | |||
| 425 | /** |
||
| 426 | * @return bool |
||
| 427 | */ |
||
| 428 | protected function saveTranslations() |
||
| 444 | |||
| 445 | /** |
||
| 446 | * @param array |
||
| 447 | * |
||
| 448 | * @return \Illuminate\Database\Eloquent\Model |
||
| 449 | */ |
||
| 450 | public function replicateWithTranslations(array $except = null) |
||
| 462 | |||
| 463 | /** |
||
| 464 | * @param \Illuminate\Database\Eloquent\Model $translation |
||
| 465 | * |
||
| 466 | * @return bool |
||
| 467 | */ |
||
| 468 | protected function isTranslationDirty(Model $translation) |
||
| 475 | |||
| 476 | /** |
||
| 477 | * @param string $locale |
||
| 478 | * |
||
| 479 | * @return \Illuminate\Database\Eloquent\Model |
||
| 480 | */ |
||
| 481 | public function getNewTranslation($locale) |
||
| 490 | |||
| 491 | /** |
||
| 492 | * @param $key |
||
| 493 | * |
||
| 494 | * @return bool |
||
| 495 | */ |
||
| 496 | public function __isset($key) |
||
| 500 | |||
| 501 | /** |
||
| 502 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 503 | * @param string $locale |
||
| 504 | * |
||
| 505 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 506 | */ |
||
| 507 | View Code Duplication | public function scopeTranslatedIn(Builder $query, $locale = null) |
|
| 515 | |||
| 516 | /** |
||
| 517 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 518 | * @param string $locale |
||
| 519 | * |
||
| 520 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 521 | */ |
||
| 522 | View Code Duplication | public function scopeNotTranslatedIn(Builder $query, $locale = null) |
|
| 530 | |||
| 531 | /** |
||
| 532 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 533 | * |
||
| 534 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 535 | */ |
||
| 536 | public function scopeTranslated(Builder $query) |
||
| 540 | |||
| 541 | /** |
||
| 542 | * Adds scope to get a list of translated attributes, using the current locale. |
||
| 543 | * Example usage: Country::listsTranslations('name')->get()->toArray() |
||
| 544 | * Will return an array with items: |
||
| 545 | * [ |
||
| 546 | * 'id' => '1', // The id of country |
||
| 547 | * 'name' => 'Griechenland' // The translated name |
||
| 548 | * ]. |
||
| 549 | * |
||
| 550 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 551 | * @param string $translationField |
||
| 552 | */ |
||
| 553 | public function scopeListsTranslations(Builder $query, $translationField) |
||
| 577 | |||
| 578 | /** |
||
| 579 | * This scope eager loads the translations for the default and the fallback locale only. |
||
| 580 | * We can use this as a shortcut to improve performance in our application. |
||
| 581 | * |
||
| 582 | * @param Builder $query |
||
| 583 | */ |
||
| 584 | public function scopeWithTranslation(Builder $query) |
||
| 600 | |||
| 601 | /** |
||
| 602 | * This scope filters results by checking the translation fields. |
||
| 603 | * |
||
| 604 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 605 | * @param string $key |
||
| 606 | * @param string $value |
||
| 607 | * @param string $locale |
||
| 608 | * |
||
| 609 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 610 | */ |
||
| 611 | View Code Duplication | public function scopeWhereTranslation(Builder $query, $key, $value, $locale = null) |
|
| 620 | |||
| 621 | /** |
||
| 622 | * This scope filters results by checking the translation fields. |
||
| 623 | * |
||
| 624 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 625 | * @param string $key |
||
| 626 | * @param string $value |
||
| 627 | * @param string $locale |
||
| 628 | * |
||
| 629 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 630 | */ |
||
| 631 | View Code Duplication | public function scopeOrWhereTranslation(Builder $query, $key, $value, $locale = null) |
|
| 640 | |||
| 641 | /** |
||
| 642 | * This scope filters results by checking the translation fields. |
||
| 643 | * |
||
| 644 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 645 | * @param string $key |
||
| 646 | * @param string $value |
||
| 647 | * @param string $locale |
||
| 648 | * |
||
| 649 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 650 | */ |
||
| 651 | View Code Duplication | public function scopeWhereTranslationLike(Builder $query, $key, $value, $locale = null) |
|
| 660 | |||
| 661 | /** |
||
| 662 | * This scope filters results by checking the translation fields. |
||
| 663 | * |
||
| 664 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 665 | * @param string $key |
||
| 666 | * @param string $value |
||
| 667 | * @param string $locale |
||
| 668 | * |
||
| 669 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 670 | */ |
||
| 671 | View Code Duplication | public function scopeOrWhereTranslationLike(Builder $query, $key, $value, $locale = null) |
|
| 680 | |||
| 681 | /** |
||
| 682 | * This scope sorts results by the given translation field. |
||
| 683 | * |
||
| 684 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 685 | * @param string $key |
||
| 686 | * @param string $sortmethod |
||
| 687 | * |
||
| 688 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 689 | */ |
||
| 690 | public function scopeOrderByTranslation(Builder $query, $key, $sortmethod = 'asc') |
||
| 707 | |||
| 708 | /** |
||
| 709 | * @return array |
||
| 710 | */ |
||
| 711 | public function attributesToArray() |
||
| 733 | |||
| 734 | /** |
||
| 735 | * @return array |
||
| 736 | */ |
||
| 737 | public function getTranslationsArray() |
||
| 749 | |||
| 750 | /** |
||
| 751 | * @return string |
||
| 752 | */ |
||
| 753 | private function getTranslationsTable() |
||
| 757 | |||
| 758 | /** |
||
| 759 | * @return string |
||
| 760 | */ |
||
| 761 | protected function locale() |
||
| 770 | |||
| 771 | /** |
||
| 772 | * Set the default locale on the model. |
||
| 773 | * |
||
| 774 | * @param $locale |
||
| 775 | * |
||
| 776 | * @return $this |
||
| 777 | */ |
||
| 778 | public function setDefaultLocale($locale) |
||
| 784 | |||
| 785 | /** |
||
| 786 | * Get the default locale on the model. |
||
| 787 | * |
||
| 788 | * @return mixed |
||
| 789 | */ |
||
| 790 | public function getDefaultLocale() |
||
| 794 | |||
| 795 | /** |
||
| 796 | * Deletes all translations for this model. |
||
| 797 | * |
||
| 798 | * @param string|array|null $locales The locales to be deleted (array or single string) |
||
| 799 | * (e.g., ["en", "de"] would remove these translations). |
||
| 800 | */ |
||
| 801 | public function deleteTranslations($locales = null) |
||
| 817 | |||
| 818 | /** |
||
| 819 | * @param $key |
||
| 820 | * |
||
| 821 | * @return array |
||
| 822 | */ |
||
| 823 | private function getAttributeAndLocale($key) |
||
| 831 | |||
| 832 | /** |
||
| 833 | * @return bool |
||
| 834 | */ |
||
| 835 | private function toArrayAlwaysLoadsTranslations() |
||
| 839 | } |
||
| 840 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: