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) |
||
| 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() |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @return string |
||
| 118 | */ |
||
| 119 | public function getRelationKey() |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @return string |
||
| 134 | */ |
||
| 135 | public function getLocaleKey() |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
| 144 | */ |
||
| 145 | public function translations() |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @return bool |
||
| 152 | */ |
||
| 153 | private function usePropertyFallback() |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Returns the attribute value from fallback translation if value of attribute |
||
| 160 | * is empty and the property fallback is enabled in the configuration. |
||
| 161 | * in model. |
||
| 162 | * @param $locale |
||
| 163 | * @param $attribute |
||
| 164 | * @return mixed |
||
| 165 | */ |
||
| 166 | private function getAttributeOrFallback($locale, $attribute) |
||
| 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 = []) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @param string $locale |
||
| 256 | * |
||
| 257 | * @return \Illuminate\Database\Eloquent\Model|null |
||
| 258 | */ |
||
| 259 | protected function getTranslationOrNew($locale) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @param array $attributes |
||
| 270 | * |
||
| 271 | * @throws \Illuminate\Database\Eloquent\MassAssignmentException |
||
| 272 | * @return $this |
||
| 273 | */ |
||
| 274 | public function fill(array $attributes) |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @param string $key |
||
| 294 | */ |
||
| 295 | private function getTranslationByLocaleKey($key) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * @param null $locale |
||
| 308 | * |
||
| 309 | * @return string |
||
| 310 | */ |
||
| 311 | private function getFallbackLocale($locale = null) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @param $locale |
||
| 324 | * |
||
| 325 | * @return bool |
||
| 326 | */ |
||
| 327 | private function isLocaleCountryBased($locale) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @param $locale |
||
| 334 | * |
||
| 335 | * @return string |
||
| 336 | */ |
||
| 337 | private function getLanguageFromCountryBasedLocale($locale) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @return bool|null |
||
| 346 | */ |
||
| 347 | private function useFallback() |
||
| 355 | |||
| 356 | /** |
||
| 357 | * @param string $key |
||
| 358 | * |
||
| 359 | * @return bool |
||
| 360 | */ |
||
| 361 | public function isTranslationAttribute($key) |
||
| 365 | |||
| 366 | /** |
||
| 367 | * @param string $key |
||
| 368 | * |
||
| 369 | * @throws \Dimsav\Translatable\Exception\LocalesNotDefinedException |
||
| 370 | * @return bool |
||
| 371 | */ |
||
| 372 | protected function isKeyALocale($key) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * @throws \Dimsav\Translatable\Exception\LocalesNotDefinedException |
||
| 381 | * @return array |
||
| 382 | */ |
||
| 383 | protected function getLocales() |
||
| 406 | |||
| 407 | /** |
||
| 408 | * @return string |
||
| 409 | */ |
||
| 410 | protected function getLocaleSeparator() |
||
| 414 | |||
| 415 | /** |
||
| 416 | * @return bool |
||
| 417 | */ |
||
| 418 | protected function saveTranslations() |
||
| 434 | |||
| 435 | /** |
||
| 436 | * @param array |
||
| 437 | * |
||
| 438 | * @return \Illuminate\Database\Eloquent\Model |
||
| 439 | */ |
||
| 440 | public function replicateWithTranslations(array $except = null) |
||
| 452 | |||
| 453 | /** |
||
| 454 | * @param \Illuminate\Database\Eloquent\Model $translation |
||
| 455 | * |
||
| 456 | * @return bool |
||
| 457 | */ |
||
| 458 | protected function isTranslationDirty(Model $translation) |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Get the attributes that have been changed since last sync. |
||
| 484 | * |
||
| 485 | * @return array |
||
| 486 | */ |
||
| 487 | public function getDirtyWithTranslations() |
||
| 505 | |||
| 506 | /** |
||
| 507 | * @param string $locale |
||
| 508 | * |
||
| 509 | * @return \Illuminate\Database\Eloquent\Model |
||
| 510 | */ |
||
| 511 | public function getNewTranslation($locale) |
||
| 520 | |||
| 521 | /** |
||
| 522 | * @param $key |
||
| 523 | * |
||
| 524 | * @return bool |
||
| 525 | */ |
||
| 526 | public function __isset($key) |
||
| 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 scopeTranslatedIn(Builder $query, $locale = null) |
|
| 545 | |||
| 546 | /** |
||
| 547 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 548 | * @param string $locale |
||
| 549 | * |
||
| 550 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 551 | */ |
||
| 552 | View Code Duplication | public function scopeNotTranslatedIn(Builder $query, $locale = null) |
|
| 560 | |||
| 561 | /** |
||
| 562 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 563 | * |
||
| 564 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 565 | */ |
||
| 566 | public function scopeTranslated(Builder $query) |
||
| 570 | |||
| 571 | /** |
||
| 572 | * Adds scope to get a list of translated attributes, using the current locale. |
||
| 573 | * Example usage: Country::listsTranslations('name')->get()->toArray() |
||
| 574 | * Will return an array with items: |
||
| 575 | * [ |
||
| 576 | * 'id' => '1', // The id of country |
||
| 577 | * 'name' => 'Griechenland' // The translated name |
||
| 578 | * ]. |
||
| 579 | * |
||
| 580 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 581 | * @param string $translationField |
||
| 582 | */ |
||
| 583 | public function scopeListsTranslations(Builder $query, $translationField) |
||
| 607 | |||
| 608 | /** |
||
| 609 | * This scope eager loads the translations for the default and the fallback locale only. |
||
| 610 | * We can use this as a shortcut to improve performance in our application. |
||
| 611 | * |
||
| 612 | * @param Builder $query |
||
| 613 | */ |
||
| 614 | public function scopeWithTranslation(Builder $query) |
||
| 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 scopeWhereTranslation(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 | public function scopeOrWhereTranslation(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 scopeWhereTranslationLike(Builder $query, $key, $value, $locale = null) |
|
| 686 | |||
| 687 | /** |
||
| 688 | * This scope filters results by checking the translation fields. |
||
| 689 | * |
||
| 690 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 691 | * @param string $key |
||
| 692 | * @param string $value |
||
| 693 | * @param string $locale |
||
| 694 | * |
||
| 695 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 696 | */ |
||
| 697 | public function scopeOrWhereTranslationLike(Builder $query, $key, $value, $locale = null) |
||
| 706 | |||
| 707 | /** |
||
| 708 | * @return array |
||
| 709 | */ |
||
| 710 | public function toArray() |
||
| 734 | |||
| 735 | /** |
||
| 736 | * @return array |
||
| 737 | */ |
||
| 738 | public function getTranslationsArray() |
||
| 750 | |||
| 751 | /** |
||
| 752 | * @return string |
||
| 753 | */ |
||
| 754 | private function getTranslationsTable() |
||
| 758 | |||
| 759 | /** |
||
| 760 | * @return string |
||
| 761 | */ |
||
| 762 | protected function locale() |
||
| 771 | |||
| 772 | /** |
||
| 773 | * Set the default locale on the model. |
||
| 774 | * |
||
| 775 | * @param $locale |
||
| 776 | * |
||
| 777 | * @return $this |
||
| 778 | */ |
||
| 779 | public function setDefaultLocale($locale) |
||
| 785 | |||
| 786 | /** |
||
| 787 | * Get the default locale on the model. |
||
| 788 | * |
||
| 789 | * @return mixed |
||
| 790 | */ |
||
| 791 | public function getDefaultLocale() |
||
| 795 | |||
| 796 | /** |
||
| 797 | * Deletes all translations for this model. |
||
| 798 | * |
||
| 799 | * @param string|array|null $locales The locales to be deleted (array or single string) |
||
| 800 | * (e.g., ["en", "de"] would remove these translations). |
||
| 801 | */ |
||
| 802 | public function deleteTranslations($locales = null) |
||
| 818 | |||
| 819 | /** |
||
| 820 | * @param $key |
||
| 821 | * |
||
| 822 | * @return array |
||
| 823 | */ |
||
| 824 | private function getAttributeAndLocale($key) |
||
| 832 | |||
| 833 | /** |
||
| 834 | * @return bool |
||
| 835 | */ |
||
| 836 | private function toArrayAlwaysLoadsTranslations() |
||
| 840 | } |
||
| 841 |
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: