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 |
||
| 11 | trait Translatable |
||
| 12 | { |
||
| 13 | protected $defaultLocale; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Alias for getTranslation(). |
||
| 17 | * |
||
| 18 | * @param string|null $locale |
||
| 19 | * @param bool $withFallback |
||
| 20 | * |
||
| 21 | * @return \Illuminate\Database\Eloquent\Model|null |
||
| 22 | */ |
||
| 23 | public function translate($locale = null, $withFallback = false) |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Alias for getTranslation(). |
||
| 30 | * |
||
| 31 | * @param string $locale |
||
| 32 | * |
||
| 33 | * @return \Illuminate\Database\Eloquent\Model|null |
||
| 34 | */ |
||
| 35 | public function translateOrDefault($locale) |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Alias for getTranslationOrNew(). |
||
| 42 | * |
||
| 43 | * @param string $locale |
||
| 44 | * |
||
| 45 | * @return \Illuminate\Database\Eloquent\Model|null |
||
| 46 | */ |
||
| 47 | public function translateOrNew($locale) |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @param string|null $locale |
||
| 54 | * @param bool $withFallback |
||
| 55 | * |
||
| 56 | * @return \Illuminate\Database\Eloquent\Model|null |
||
| 57 | */ |
||
| 58 | public function getTranslation($locale = null, $withFallback = null) |
||
| 59 | { |
||
| 60 | $configFallbackLocale = $this->getFallbackLocale(); |
||
| 61 | $locale = $locale ?: $this->locale(); |
||
| 62 | $withFallback = $withFallback === null ? $this->useFallback() : $withFallback; |
||
| 63 | $fallbackLocale = $this->getFallbackLocale($locale); |
||
| 64 | |||
| 65 | if ($translation = $this->getTranslationByLocaleKey($locale)) { |
||
| 66 | return $translation; |
||
| 67 | } |
||
| 68 | if ($withFallback && $fallbackLocale) { |
||
| 69 | if ($translation = $this->getTranslationByLocaleKey($fallbackLocale)) { |
||
| 70 | return $translation; |
||
| 71 | } |
||
| 72 | if ($fallbackLocale !== $configFallbackLocale && $translation = $this->getTranslationByLocaleKey($configFallbackLocale)) { |
||
| 73 | return $translation; |
||
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 77 | return null; |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @param string|null $locale |
||
| 82 | * |
||
| 83 | * @return bool |
||
| 84 | */ |
||
| 85 | public function hasTranslation($locale = null) |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @return string |
||
| 100 | */ |
||
| 101 | public function getTranslationModelName() |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @return string |
||
| 108 | */ |
||
| 109 | public function getTranslationModelNameDefault() |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @return string |
||
| 116 | */ |
||
| 117 | public function getRelationKey() |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @return string |
||
| 132 | */ |
||
| 133 | public function getLocaleKey() |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
| 140 | */ |
||
| 141 | public function translations() |
||
| 145 | |||
| 146 | public function translation() |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @return bool |
||
| 161 | */ |
||
| 162 | private function usePropertyFallback() |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Returns the attribute value from fallback translation if value of attribute |
||
| 169 | * is empty and the property fallback is enabled in the configuration. |
||
| 170 | * in model. |
||
| 171 | * @param $locale |
||
| 172 | * @param $attribute |
||
| 173 | * @return mixed |
||
| 174 | */ |
||
| 175 | private function getAttributeOrFallback($locale, $attribute) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @param string $key |
||
| 192 | * |
||
| 193 | * @return mixed |
||
| 194 | */ |
||
| 195 | public function getAttribute($key) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @param string $key |
||
| 221 | * @param mixed $value |
||
| 222 | * |
||
| 223 | * @return $this |
||
| 224 | */ |
||
| 225 | public function setAttribute($key, $value) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @param array $options |
||
| 240 | * |
||
| 241 | * @return bool |
||
| 242 | */ |
||
| 243 | public function save(array $options = []) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * @param string $locale |
||
| 278 | * |
||
| 279 | * @return \Illuminate\Database\Eloquent\Model |
||
| 280 | */ |
||
| 281 | protected function getTranslationOrNew($locale) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @param array $attributes |
||
| 292 | * |
||
| 293 | * @throws \Illuminate\Database\Eloquent\MassAssignmentException |
||
| 294 | * @return $this |
||
| 295 | */ |
||
| 296 | public function fill(array $attributes) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @param string $key |
||
| 316 | */ |
||
| 317 | private function getTranslationByLocaleKey($key) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * @param null $locale |
||
| 330 | * |
||
| 331 | * @return string |
||
| 332 | */ |
||
| 333 | private function getFallbackLocale($locale = null) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @param $locale |
||
| 346 | * |
||
| 347 | * @return bool |
||
| 348 | */ |
||
| 349 | private function isLocaleCountryBased($locale) |
||
| 353 | |||
| 354 | /** |
||
| 355 | * @param $locale |
||
| 356 | * |
||
| 357 | * @return string |
||
| 358 | */ |
||
| 359 | private function getLanguageFromCountryBasedLocale($locale) |
||
| 365 | |||
| 366 | /** |
||
| 367 | * @return bool|null |
||
| 368 | */ |
||
| 369 | private function useFallback() |
||
| 377 | |||
| 378 | /** |
||
| 379 | * @param string $key |
||
| 380 | * |
||
| 381 | * @return bool |
||
| 382 | */ |
||
| 383 | public function isTranslationAttribute($key) |
||
| 387 | |||
| 388 | /** |
||
| 389 | * @param string $key |
||
| 390 | * |
||
| 391 | * @throws \Dimsav\Translatable\Exception\LocalesNotDefinedException |
||
| 392 | * @return bool |
||
| 393 | */ |
||
| 394 | protected function isKeyALocale($key) |
||
| 400 | |||
| 401 | /** |
||
| 402 | * @throws \Dimsav\Translatable\Exception\LocalesNotDefinedException |
||
| 403 | * @return array |
||
| 404 | */ |
||
| 405 | protected function getLocales() |
||
| 428 | |||
| 429 | /** |
||
| 430 | * @return string |
||
| 431 | */ |
||
| 432 | protected function getLocaleSeparator() |
||
| 436 | |||
| 437 | /** |
||
| 438 | * @return bool |
||
| 439 | */ |
||
| 440 | protected function saveTranslations() |
||
| 456 | |||
| 457 | /** |
||
| 458 | * @param array |
||
| 459 | * |
||
| 460 | * @return \Illuminate\Database\Eloquent\Model |
||
| 461 | */ |
||
| 462 | public function replicateWithTranslations(array $except = null) |
||
| 474 | |||
| 475 | /** |
||
| 476 | * @param \Illuminate\Database\Eloquent\Model $translation |
||
| 477 | * |
||
| 478 | * @return bool |
||
| 479 | */ |
||
| 480 | protected function isTranslationDirty(Model $translation) |
||
| 487 | |||
| 488 | /** |
||
| 489 | * @param string $locale |
||
| 490 | * |
||
| 491 | * @return \Illuminate\Database\Eloquent\Model |
||
| 492 | */ |
||
| 493 | public function getNewTranslation($locale) |
||
| 502 | |||
| 503 | /** |
||
| 504 | * @param $key |
||
| 505 | * |
||
| 506 | * @return bool |
||
| 507 | */ |
||
| 508 | public function __isset($key) |
||
| 512 | |||
| 513 | /** |
||
| 514 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 515 | * @param string $locale |
||
| 516 | * |
||
| 517 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 518 | */ |
||
| 519 | View Code Duplication | public function scopeTranslatedIn(Builder $query, $locale = null) |
|
| 527 | |||
| 528 | /** |
||
| 529 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 530 | * @param string $locale |
||
| 531 | * |
||
| 532 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 533 | */ |
||
| 534 | View Code Duplication | public function scopeNotTranslatedIn(Builder $query, $locale = null) |
|
| 542 | |||
| 543 | /** |
||
| 544 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 545 | * |
||
| 546 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 547 | */ |
||
| 548 | public function scopeTranslated(Builder $query) |
||
| 552 | |||
| 553 | /** |
||
| 554 | * Adds scope to get a list of translated attributes, using the current locale. |
||
| 555 | * Example usage: Country::listsTranslations('name')->get()->toArray() |
||
| 556 | * Will return an array with items: |
||
| 557 | * [ |
||
| 558 | * 'id' => '1', // The id of country |
||
| 559 | * 'name' => 'Griechenland' // The translated name |
||
| 560 | * ]. |
||
| 561 | * |
||
| 562 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 563 | * @param string $translationField |
||
| 564 | */ |
||
| 565 | public function scopeListsTranslations(Builder $query, $translationField) |
||
| 589 | |||
| 590 | /** |
||
| 591 | * This scope eager loads the translations for the default and the fallback locale only. |
||
| 592 | * We can use this as a shortcut to improve performance in our application. |
||
| 593 | * |
||
| 594 | * @param Builder $query |
||
| 595 | */ |
||
| 596 | public function scopeWithTranslation(Builder $query) |
||
| 612 | |||
| 613 | /** |
||
| 614 | * This scope filters results by checking the translation fields. |
||
| 615 | * |
||
| 616 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 617 | * @param string $key |
||
| 618 | * @param string $value |
||
| 619 | * @param string $locale |
||
| 620 | * |
||
| 621 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 622 | */ |
||
| 623 | View Code Duplication | public function scopeWhereTranslation(Builder $query, $key, $value, $locale = null) |
|
| 632 | |||
| 633 | /** |
||
| 634 | * This scope filters results by checking the translation fields. |
||
| 635 | * |
||
| 636 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 637 | * @param string $key |
||
| 638 | * @param string $value |
||
| 639 | * @param string $locale |
||
| 640 | * |
||
| 641 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 642 | */ |
||
| 643 | View Code Duplication | public function scopeOrWhereTranslation(Builder $query, $key, $value, $locale = null) |
|
| 652 | |||
| 653 | /** |
||
| 654 | * This scope filters results by checking the translation fields. |
||
| 655 | * |
||
| 656 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 657 | * @param string $key |
||
| 658 | * @param string $value |
||
| 659 | * @param string $locale |
||
| 660 | * |
||
| 661 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 662 | */ |
||
| 663 | View Code Duplication | public function scopeWhereTranslationLike(Builder $query, $key, $value, $locale = null) |
|
| 672 | |||
| 673 | /** |
||
| 674 | * This scope filters results by checking the translation fields. |
||
| 675 | * |
||
| 676 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 677 | * @param string $key |
||
| 678 | * @param string $value |
||
| 679 | * @param string $locale |
||
| 680 | * |
||
| 681 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 682 | */ |
||
| 683 | View Code Duplication | public function scopeOrWhereTranslationLike(Builder $query, $key, $value, $locale = null) |
|
| 692 | |||
| 693 | /** |
||
| 694 | * @return array |
||
| 695 | */ |
||
| 696 | public function attributesToArray() |
||
| 718 | |||
| 719 | /** |
||
| 720 | * @return array |
||
| 721 | */ |
||
| 722 | public function getTranslationsArray() |
||
| 734 | |||
| 735 | /** |
||
| 736 | * @return string |
||
| 737 | */ |
||
| 738 | private function getTranslationsTable() |
||
| 742 | |||
| 743 | /** |
||
| 744 | * @return string |
||
| 745 | */ |
||
| 746 | protected function locale() |
||
| 755 | |||
| 756 | /** |
||
| 757 | * Set the default locale on the model. |
||
| 758 | * |
||
| 759 | * @param $locale |
||
| 760 | * |
||
| 761 | * @return $this |
||
| 762 | */ |
||
| 763 | public function setDefaultLocale($locale) |
||
| 769 | |||
| 770 | /** |
||
| 771 | * Get the default locale on the model. |
||
| 772 | * |
||
| 773 | * @return mixed |
||
| 774 | */ |
||
| 775 | public function getDefaultLocale() |
||
| 779 | |||
| 780 | /** |
||
| 781 | * Deletes all translations for this model. |
||
| 782 | * |
||
| 783 | * @param string|array|null $locales The locales to be deleted (array or single string) |
||
| 784 | * (e.g., ["en", "de"] would remove these translations). |
||
| 785 | */ |
||
| 786 | public function deleteTranslations($locales = null) |
||
| 802 | |||
| 803 | /** |
||
| 804 | * @param $key |
||
| 805 | * |
||
| 806 | * @return array |
||
| 807 | */ |
||
| 808 | private function getAttributeAndLocale($key) |
||
| 816 | |||
| 817 | /** |
||
| 818 | * @return bool |
||
| 819 | */ |
||
| 820 | private function toArrayAlwaysLoadsTranslations() |
||
| 824 | } |
||
| 825 |
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: