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 | /** |
||
| 147 | * @return \Illuminate\Database\Eloquent\Relations\HasOne |
||
| 148 | */ |
||
| 149 | public function translation() |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @return bool |
||
| 164 | */ |
||
| 165 | private function usePropertyFallback() |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Returns the attribute value from fallback translation if value of attribute |
||
| 172 | * is empty and the property fallback is enabled in the configuration. |
||
| 173 | * in model. |
||
| 174 | * @param $locale |
||
| 175 | * @param $attribute |
||
| 176 | * @return mixed |
||
| 177 | */ |
||
| 178 | private function getAttributeOrFallback($locale, $attribute) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @param string $key |
||
| 195 | * |
||
| 196 | * @return mixed |
||
| 197 | */ |
||
| 198 | public function getAttribute($key) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @param string $key |
||
| 224 | * @param mixed $value |
||
| 225 | * |
||
| 226 | * @return $this |
||
| 227 | */ |
||
| 228 | public function setAttribute($key, $value) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @param array $options |
||
| 243 | * |
||
| 244 | * @return bool |
||
| 245 | */ |
||
| 246 | public function save(array $options = []) |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @param string $locale |
||
| 281 | * |
||
| 282 | * @return \Illuminate\Database\Eloquent\Model |
||
| 283 | */ |
||
| 284 | protected function getTranslationOrNew($locale) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @param array $attributes |
||
| 295 | * |
||
| 296 | * @throws \Illuminate\Database\Eloquent\MassAssignmentException |
||
| 297 | * @return $this |
||
| 298 | */ |
||
| 299 | public function fill(array $attributes) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * @param string $key |
||
| 319 | */ |
||
| 320 | private function getTranslationByLocaleKey($key) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * @param null $locale |
||
| 333 | * |
||
| 334 | * @return string |
||
| 335 | */ |
||
| 336 | private function getFallbackLocale($locale = null) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @param $locale |
||
| 349 | * |
||
| 350 | * @return bool |
||
| 351 | */ |
||
| 352 | private function isLocaleCountryBased($locale) |
||
| 356 | |||
| 357 | /** |
||
| 358 | * @param $locale |
||
| 359 | * |
||
| 360 | * @return string |
||
| 361 | */ |
||
| 362 | private function getLanguageFromCountryBasedLocale($locale) |
||
| 368 | |||
| 369 | /** |
||
| 370 | * @return bool|null |
||
| 371 | */ |
||
| 372 | private function useFallback() |
||
| 380 | |||
| 381 | /** |
||
| 382 | * @param string $key |
||
| 383 | * |
||
| 384 | * @return bool |
||
| 385 | */ |
||
| 386 | public function isTranslationAttribute($key) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * @param string $key |
||
| 393 | * |
||
| 394 | * @throws \Dimsav\Translatable\Exception\LocalesNotDefinedException |
||
| 395 | * @return bool |
||
| 396 | */ |
||
| 397 | protected function isKeyALocale($key) |
||
| 403 | |||
| 404 | /** |
||
| 405 | * @throws \Dimsav\Translatable\Exception\LocalesNotDefinedException |
||
| 406 | * @return array |
||
| 407 | */ |
||
| 408 | protected function getLocales() |
||
| 431 | |||
| 432 | /** |
||
| 433 | * @return string |
||
| 434 | */ |
||
| 435 | protected function getLocaleSeparator() |
||
| 439 | |||
| 440 | /** |
||
| 441 | * @return bool |
||
| 442 | */ |
||
| 443 | protected function saveTranslations() |
||
| 459 | |||
| 460 | /** |
||
| 461 | * @param array |
||
| 462 | * |
||
| 463 | * @return \Illuminate\Database\Eloquent\Model |
||
| 464 | */ |
||
| 465 | public function replicateWithTranslations(array $except = null) |
||
| 477 | |||
| 478 | /** |
||
| 479 | * @param \Illuminate\Database\Eloquent\Model $translation |
||
| 480 | * |
||
| 481 | * @return bool |
||
| 482 | */ |
||
| 483 | protected function isTranslationDirty(Model $translation) |
||
| 490 | |||
| 491 | /** |
||
| 492 | * @param string $locale |
||
| 493 | * |
||
| 494 | * @return \Illuminate\Database\Eloquent\Model |
||
| 495 | */ |
||
| 496 | public function getNewTranslation($locale) |
||
| 505 | |||
| 506 | /** |
||
| 507 | * @param $key |
||
| 508 | * |
||
| 509 | * @return bool |
||
| 510 | */ |
||
| 511 | public function __isset($key) |
||
| 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 scopeTranslatedIn(Builder $query, $locale = null) |
|
| 530 | |||
| 531 | /** |
||
| 532 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 533 | * @param string $locale |
||
| 534 | * |
||
| 535 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 536 | */ |
||
| 537 | View Code Duplication | public function scopeNotTranslatedIn(Builder $query, $locale = null) |
|
| 545 | |||
| 546 | /** |
||
| 547 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 548 | * |
||
| 549 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 550 | */ |
||
| 551 | public function scopeTranslated(Builder $query) |
||
| 555 | |||
| 556 | /** |
||
| 557 | * Adds scope to get a list of translated attributes, using the current locale. |
||
| 558 | * Example usage: Country::listsTranslations('name')->get()->toArray() |
||
| 559 | * Will return an array with items: |
||
| 560 | * [ |
||
| 561 | * 'id' => '1', // The id of country |
||
| 562 | * 'name' => 'Griechenland' // The translated name |
||
| 563 | * ]. |
||
| 564 | * |
||
| 565 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 566 | * @param string $translationField |
||
| 567 | */ |
||
| 568 | public function scopeListsTranslations(Builder $query, $translationField) |
||
| 592 | |||
| 593 | /** |
||
| 594 | * This scope eager loads the translations for the default and the fallback locale only. |
||
| 595 | * We can use this as a shortcut to improve performance in our application. |
||
| 596 | * |
||
| 597 | * @param Builder $query |
||
| 598 | */ |
||
| 599 | public function scopeWithTranslation(Builder $query) |
||
| 615 | |||
| 616 | /** |
||
| 617 | * This scope filters results by checking the translation fields. |
||
| 618 | * |
||
| 619 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 620 | * @param string $key |
||
| 621 | * @param string $value |
||
| 622 | * @param string $locale |
||
| 623 | * |
||
| 624 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 625 | */ |
||
| 626 | View Code Duplication | public function scopeWhereTranslation(Builder $query, $key, $value, $locale = null) |
|
| 635 | |||
| 636 | /** |
||
| 637 | * This scope filters results by checking the translation fields. |
||
| 638 | * |
||
| 639 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 640 | * @param string $key |
||
| 641 | * @param string $value |
||
| 642 | * @param string $locale |
||
| 643 | * |
||
| 644 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 645 | */ |
||
| 646 | View Code Duplication | public function scopeOrWhereTranslation(Builder $query, $key, $value, $locale = null) |
|
| 655 | |||
| 656 | /** |
||
| 657 | * This scope filters results by checking the translation fields. |
||
| 658 | * |
||
| 659 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 660 | * @param string $key |
||
| 661 | * @param string $value |
||
| 662 | * @param string $locale |
||
| 663 | * |
||
| 664 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 665 | */ |
||
| 666 | View Code Duplication | public function scopeWhereTranslationLike(Builder $query, $key, $value, $locale = null) |
|
| 675 | |||
| 676 | /** |
||
| 677 | * This scope filters results by checking the translation fields. |
||
| 678 | * |
||
| 679 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 680 | * @param string $key |
||
| 681 | * @param string $value |
||
| 682 | * @param string $locale |
||
| 683 | * |
||
| 684 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 685 | */ |
||
| 686 | View Code Duplication | public function scopeOrWhereTranslationLike(Builder $query, $key, $value, $locale = null) |
|
| 695 | |||
| 696 | /** |
||
| 697 | * @return array |
||
| 698 | */ |
||
| 699 | public function attributesToArray() |
||
| 721 | |||
| 722 | /** |
||
| 723 | * @return array |
||
| 724 | */ |
||
| 725 | public function getTranslationsArray() |
||
| 737 | |||
| 738 | /** |
||
| 739 | * @return string |
||
| 740 | */ |
||
| 741 | private function getTranslationsTable() |
||
| 745 | |||
| 746 | /** |
||
| 747 | * @return string |
||
| 748 | */ |
||
| 749 | protected function locale() |
||
| 758 | |||
| 759 | /** |
||
| 760 | * Set the default locale on the model. |
||
| 761 | * |
||
| 762 | * @param $locale |
||
| 763 | * |
||
| 764 | * @return $this |
||
| 765 | */ |
||
| 766 | public function setDefaultLocale($locale) |
||
| 772 | |||
| 773 | /** |
||
| 774 | * Get the default locale on the model. |
||
| 775 | * |
||
| 776 | * @return mixed |
||
| 777 | */ |
||
| 778 | public function getDefaultLocale() |
||
| 782 | |||
| 783 | /** |
||
| 784 | * Deletes all translations for this model. |
||
| 785 | * |
||
| 786 | * @param string|array|null $locales The locales to be deleted (array or single string) |
||
| 787 | * (e.g., ["en", "de"] would remove these translations). |
||
| 788 | */ |
||
| 789 | public function deleteTranslations($locales = null) |
||
| 805 | |||
| 806 | /** |
||
| 807 | * @param $key |
||
| 808 | * |
||
| 809 | * @return array |
||
| 810 | */ |
||
| 811 | private function getAttributeAndLocale($key) |
||
| 819 | |||
| 820 | /** |
||
| 821 | * @return bool |
||
| 822 | */ |
||
| 823 | private function toArrayAlwaysLoadsTranslations() |
||
| 827 | } |
||
| 828 |
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: