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) |
||
| 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) |
||
| 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) |
||
| 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) |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @param string|null $locale |
||
| 83 | * |
||
| 84 | * @return bool |
||
| 85 | */ |
||
| 86 | public function hasTranslation($locale = null) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @return string |
||
| 101 | */ |
||
| 102 | public function getTranslationModelName() |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @return string |
||
| 109 | */ |
||
| 110 | public function getTranslationModelNameDefault() |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @return string |
||
| 117 | */ |
||
| 118 | public function getRelationKey() |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @return string |
||
| 133 | */ |
||
| 134 | public function getLocaleKey() |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
| 141 | */ |
||
| 142 | public function translations() |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @return bool |
||
| 149 | */ |
||
| 150 | private function usePropertyFallback() |
||
| 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) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @param string $key |
||
| 186 | * |
||
| 187 | * @return mixed |
||
| 188 | */ |
||
| 189 | public function getAttribute($key) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @param string $key |
||
| 215 | * @param mixed $value |
||
| 216 | * |
||
| 217 | * @return $this |
||
| 218 | */ |
||
| 219 | public function setAttribute($key, $value) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @param array $options |
||
| 234 | * |
||
| 235 | * @return bool |
||
| 236 | */ |
||
| 237 | public function save(array $options = []) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @param string $locale |
||
| 272 | * |
||
| 273 | * @return \Illuminate\Database\Eloquent\Model |
||
| 274 | */ |
||
| 275 | protected function getTranslationOrNew($locale) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @param array $attributes |
||
| 286 | * |
||
| 287 | * @throws \Illuminate\Database\Eloquent\MassAssignmentException |
||
| 288 | * @return $this |
||
| 289 | */ |
||
| 290 | public function fill(array $attributes) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @param string $key |
||
| 310 | */ |
||
| 311 | private function getTranslationByLocaleKey($key) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @param null $locale |
||
| 324 | * |
||
| 325 | * @return string |
||
| 326 | */ |
||
| 327 | private function getFallbackLocale($locale = null) |
||
| 337 | |||
| 338 | /** |
||
| 339 | * @param $locale |
||
| 340 | * |
||
| 341 | * @return bool |
||
| 342 | */ |
||
| 343 | private function isLocaleCountryBased($locale) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * @param $locale |
||
| 350 | * |
||
| 351 | * @return string |
||
| 352 | */ |
||
| 353 | private function getLanguageFromCountryBasedLocale($locale) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * @return bool|null |
||
| 362 | */ |
||
| 363 | private function useFallback() |
||
| 371 | |||
| 372 | /** |
||
| 373 | * @param string $key |
||
| 374 | * |
||
| 375 | * @return bool |
||
| 376 | */ |
||
| 377 | public function isTranslationAttribute($key) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * @param string $key |
||
| 384 | * |
||
| 385 | * @throws \Dimsav\Translatable\Exception\LocalesNotDefinedException |
||
| 386 | * @return bool |
||
| 387 | */ |
||
| 388 | protected function isKeyALocale($key) |
||
| 394 | |||
| 395 | /** |
||
| 396 | * @throws \Dimsav\Translatable\Exception\LocalesNotDefinedException |
||
| 397 | * @return array |
||
| 398 | */ |
||
| 399 | protected function getLocales() |
||
| 422 | |||
| 423 | /** |
||
| 424 | * @return string |
||
| 425 | */ |
||
| 426 | protected function getLocaleSeparator() |
||
| 430 | |||
| 431 | /** |
||
| 432 | * @return bool |
||
| 433 | */ |
||
| 434 | protected function saveTranslations() |
||
| 450 | |||
| 451 | /** |
||
| 452 | * @param array |
||
| 453 | * |
||
| 454 | * @return \Illuminate\Database\Eloquent\Model |
||
| 455 | */ |
||
| 456 | public function replicateWithTranslations(array $except = null) |
||
| 468 | |||
| 469 | /** |
||
| 470 | * @param \Illuminate\Database\Eloquent\Model $translation |
||
| 471 | * |
||
| 472 | * @return bool |
||
| 473 | */ |
||
| 474 | protected function isTranslationDirty(Model $translation) |
||
| 481 | |||
| 482 | /** |
||
| 483 | * @param string $locale |
||
| 484 | * |
||
| 485 | * @return \Illuminate\Database\Eloquent\Model |
||
| 486 | */ |
||
| 487 | public function getNewTranslation($locale) |
||
| 496 | |||
| 497 | /** |
||
| 498 | * @param $key |
||
| 499 | * |
||
| 500 | * @return bool |
||
| 501 | */ |
||
| 502 | public function __isset($key) |
||
| 506 | |||
| 507 | /** |
||
| 508 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 509 | * @param string $locale |
||
| 510 | * |
||
| 511 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 512 | */ |
||
| 513 | View Code Duplication | public function scopeTranslatedIn(Builder $query, $locale = null) |
|
| 521 | |||
| 522 | /** |
||
| 523 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 524 | * @param string $locale |
||
| 525 | * |
||
| 526 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 527 | */ |
||
| 528 | View Code Duplication | public function scopeNotTranslatedIn(Builder $query, $locale = null) |
|
| 536 | |||
| 537 | /** |
||
| 538 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 539 | * |
||
| 540 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 541 | */ |
||
| 542 | public function scopeTranslated(Builder $query) |
||
| 546 | |||
| 547 | /** |
||
| 548 | * Adds scope to get a list of translated attributes, using the current locale. |
||
| 549 | * Example usage: Country::listsTranslations('name')->get()->toArray() |
||
| 550 | * Will return an array with items: |
||
| 551 | * [ |
||
| 552 | * 'id' => '1', // The id of country |
||
| 553 | * 'name' => 'Griechenland' // The translated name |
||
| 554 | * ]. |
||
| 555 | * |
||
| 556 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 557 | * @param string $translationField |
||
| 558 | */ |
||
| 559 | public function scopeListsTranslations(Builder $query, $translationField) |
||
| 583 | |||
| 584 | /** |
||
| 585 | * This scope eager loads the translations for the default and the fallback locale only. |
||
| 586 | * We can use this as a shortcut to improve performance in our application. |
||
| 587 | * |
||
| 588 | * @param Builder $query |
||
| 589 | */ |
||
| 590 | public function scopeWithTranslation(Builder $query) |
||
| 606 | |||
| 607 | /** |
||
| 608 | * This scope filters results by checking the translation fields. |
||
| 609 | * |
||
| 610 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 611 | * @param string $key |
||
| 612 | * @param string $value |
||
| 613 | * @param string $locale |
||
| 614 | * |
||
| 615 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 616 | */ |
||
| 617 | View Code Duplication | public function scopeWhereTranslation(Builder $query, $key, $value, $locale = null) |
|
| 626 | |||
| 627 | /** |
||
| 628 | * This scope filters results by checking the translation fields. |
||
| 629 | * |
||
| 630 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 631 | * @param string $key |
||
| 632 | * @param string $value |
||
| 633 | * @param string $locale |
||
| 634 | * |
||
| 635 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 636 | */ |
||
| 637 | View Code Duplication | public function scopeOrWhereTranslation(Builder $query, $key, $value, $locale = null) |
|
| 646 | |||
| 647 | /** |
||
| 648 | * This scope filters results by checking the translation fields. |
||
| 649 | * |
||
| 650 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 651 | * @param string $key |
||
| 652 | * @param string $value |
||
| 653 | * @param string $locale |
||
| 654 | * |
||
| 655 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 656 | */ |
||
| 657 | View Code Duplication | public function scopeWhereTranslationLike(Builder $query, $key, $value, $locale = null) |
|
| 666 | |||
| 667 | /** |
||
| 668 | * This scope filters results by checking the translation fields. |
||
| 669 | * |
||
| 670 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 671 | * @param string $key |
||
| 672 | * @param string $value |
||
| 673 | * @param string $locale |
||
| 674 | * |
||
| 675 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 676 | */ |
||
| 677 | View Code Duplication | public function scopeOrWhereTranslationLike(Builder $query, $key, $value, $locale = null) |
|
| 686 | |||
| 687 | /** |
||
| 688 | * This scope sorts results by the given translation field. |
||
| 689 | * |
||
| 690 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 691 | * @param string $key |
||
| 692 | * @param string $sortmethod |
||
| 693 | * |
||
| 694 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 695 | */ |
||
| 696 | public function scopeOrderByTranslation(Builder $query, $key, $sortmethod = 'asc') |
||
| 713 | |||
| 714 | /** |
||
| 715 | * @return array |
||
| 716 | */ |
||
| 717 | public function attributesToArray() |
||
| 737 | |||
| 738 | /** |
||
| 739 | * @return array |
||
| 740 | */ |
||
| 741 | public function getTranslationsArray() |
||
| 753 | |||
| 754 | /** |
||
| 755 | * @return string |
||
| 756 | */ |
||
| 757 | private function getTranslationsTable() |
||
| 761 | |||
| 762 | /** |
||
| 763 | * @return string |
||
| 764 | */ |
||
| 765 | protected function locale() |
||
| 774 | |||
| 775 | /** |
||
| 776 | * Set the default locale on the model. |
||
| 777 | * |
||
| 778 | * @param $locale |
||
| 779 | * |
||
| 780 | * @return $this |
||
| 781 | */ |
||
| 782 | public function setDefaultLocale($locale) |
||
| 788 | |||
| 789 | /** |
||
| 790 | * Get the default locale on the model. |
||
| 791 | * |
||
| 792 | * @return mixed |
||
| 793 | */ |
||
| 794 | public function getDefaultLocale() |
||
| 798 | |||
| 799 | /** |
||
| 800 | * Deletes all translations for this model. |
||
| 801 | * |
||
| 802 | * @param string|array|null $locales The locales to be deleted (array or single string) |
||
| 803 | * (e.g., ["en", "de"] would remove these translations). |
||
| 804 | */ |
||
| 805 | public function deleteTranslations($locales = null) |
||
| 821 | |||
| 822 | /** |
||
| 823 | * @param $key |
||
| 824 | * |
||
| 825 | * @return array |
||
| 826 | */ |
||
| 827 | private function getAttributeAndLocale($key) |
||
| 835 | |||
| 836 | /** |
||
| 837 | * @return bool |
||
| 838 | */ |
||
| 839 | private function toArrayAlwaysLoadsTranslations() |
||
| 843 | } |
||
| 844 |
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: