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) |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Get the attributes that have been changed since last sync. |
||
| 485 | * |
||
| 486 | * @return array |
||
| 487 | */ |
||
| 488 | public function getDirtyWithTranslations() |
||
| 506 | |||
| 507 | /** |
||
| 508 | * @param string $locale |
||
| 509 | * |
||
| 510 | * @return \Illuminate\Database\Eloquent\Model |
||
| 511 | */ |
||
| 512 | public function getNewTranslation($locale) |
||
| 521 | |||
| 522 | /** |
||
| 523 | * @param $key |
||
| 524 | * |
||
| 525 | * @return bool |
||
| 526 | */ |
||
| 527 | public function __isset($key) |
||
| 531 | |||
| 532 | /** |
||
| 533 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 534 | * @param string $locale |
||
| 535 | * |
||
| 536 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 537 | */ |
||
| 538 | View Code Duplication | public function scopeTranslatedIn(Builder $query, $locale = null) |
|
| 546 | |||
| 547 | /** |
||
| 548 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 549 | * @param string $locale |
||
| 550 | * |
||
| 551 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 552 | */ |
||
| 553 | View Code Duplication | public function scopeNotTranslatedIn(Builder $query, $locale = null) |
|
| 561 | |||
| 562 | /** |
||
| 563 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 564 | * |
||
| 565 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 566 | */ |
||
| 567 | public function scopeTranslated(Builder $query) |
||
| 571 | |||
| 572 | /** |
||
| 573 | * Adds scope to get a list of translated attributes, using the current locale. |
||
| 574 | * Example usage: Country::listsTranslations('name')->get()->toArray() |
||
| 575 | * Will return an array with items: |
||
| 576 | * [ |
||
| 577 | * 'id' => '1', // The id of country |
||
| 578 | * 'name' => 'Griechenland' // The translated name |
||
| 579 | * ]. |
||
| 580 | * |
||
| 581 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 582 | * @param string $translationField |
||
| 583 | */ |
||
| 584 | public function scopeListsTranslations(Builder $query, $translationField) |
||
| 608 | |||
| 609 | /** |
||
| 610 | * This scope eager loads the translations for the default and the fallback locale only. |
||
| 611 | * We can use this as a shortcut to improve performance in our application. |
||
| 612 | * |
||
| 613 | * @param Builder $query |
||
| 614 | */ |
||
| 615 | public function scopeWithTranslation(Builder $query) |
||
| 627 | |||
| 628 | /** |
||
| 629 | * This scope filters results by checking the translation fields. |
||
| 630 | * |
||
| 631 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 632 | * @param string $key |
||
| 633 | * @param string $value |
||
| 634 | * @param string $locale |
||
| 635 | * |
||
| 636 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 637 | */ |
||
| 638 | View Code Duplication | public function scopeWhereTranslation(Builder $query, $key, $value, $locale = null) |
|
| 647 | |||
| 648 | /** |
||
| 649 | * This scope filters results by checking the translation fields. |
||
| 650 | * |
||
| 651 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 652 | * @param string $key |
||
| 653 | * @param string $value |
||
| 654 | * @param string $locale |
||
| 655 | * |
||
| 656 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 657 | */ |
||
| 658 | public function scopeOrWhereTranslation(Builder $query, $key, $value, $locale = null) |
||
| 667 | |||
| 668 | /** |
||
| 669 | * This scope filters results by checking the translation fields. |
||
| 670 | * |
||
| 671 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 672 | * @param string $key |
||
| 673 | * @param string $value |
||
| 674 | * @param string $locale |
||
| 675 | * |
||
| 676 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 677 | */ |
||
| 678 | View Code Duplication | public function scopeWhereTranslationLike(Builder $query, $key, $value, $locale = null) |
|
| 687 | |||
| 688 | /** |
||
| 689 | * This scope filters results by checking the translation fields. |
||
| 690 | * |
||
| 691 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 692 | * @param string $key |
||
| 693 | * @param string $value |
||
| 694 | * @param string $locale |
||
| 695 | * |
||
| 696 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 697 | */ |
||
| 698 | public function scopeOrWhereTranslationLike(Builder $query, $key, $value, $locale = null) |
||
| 707 | |||
| 708 | /** |
||
| 709 | * @return array |
||
| 710 | */ |
||
| 711 | public function toArray() |
||
| 735 | |||
| 736 | /** |
||
| 737 | * @return array |
||
| 738 | */ |
||
| 739 | public function getTranslationsArray() |
||
| 751 | |||
| 752 | /** |
||
| 753 | * @return string |
||
| 754 | */ |
||
| 755 | private function getTranslationsTable() |
||
| 759 | |||
| 760 | /** |
||
| 761 | * @return string |
||
| 762 | */ |
||
| 763 | protected function locale() |
||
| 772 | |||
| 773 | /** |
||
| 774 | * Set the default locale on the model. |
||
| 775 | * |
||
| 776 | * @param $locale |
||
| 777 | * |
||
| 778 | * @return $this |
||
| 779 | */ |
||
| 780 | public function setDefaultLocale($locale) |
||
| 786 | |||
| 787 | /** |
||
| 788 | * Get the default locale on the model. |
||
| 789 | * |
||
| 790 | * @return mixed |
||
| 791 | */ |
||
| 792 | public function getDefaultLocale() |
||
| 796 | |||
| 797 | /** |
||
| 798 | * Deletes all translations for this model. |
||
| 799 | * |
||
| 800 | * @param string|array|null $locales The locales to be deleted (array or single string) |
||
| 801 | * (e.g., ["en", "de"] would remove these translations). |
||
| 802 | */ |
||
| 803 | public function deleteTranslations($locales = null) |
||
| 819 | |||
| 820 | /** |
||
| 821 | * @param $key |
||
| 822 | * |
||
| 823 | * @return array |
||
| 824 | */ |
||
| 825 | private function getAttributeAndLocale($key) |
||
| 833 | |||
| 834 | /** |
||
| 835 | * @return bool |
||
| 836 | */ |
||
| 837 | private function toArrayAlwaysLoadsTranslations() |
||
| 841 | } |
||
| 842 |
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: