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) |
||
| 25 | { |
||
| 26 | return $this->getTranslation($locale, $withFallback); |
||
| 27 | } |
||
| 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) |
||
| 60 | { |
||
| 61 | $configFallbackLocale = $this->getFallbackLocale(); |
||
| 62 | $locale = $locale ?: $this->locale(); |
||
| 63 | $withFallback = $withFallback === null ? $this->useFallback() : $withFallback; |
||
| 64 | $fallbackLocale = $this->getFallbackLocale($locale); |
||
| 65 | |||
| 66 | if ($translation = $this->getTranslationByLocaleKey($locale)) { |
||
| 67 | return $translation; |
||
| 68 | } |
||
| 69 | if ($withFallback && $fallbackLocale) { |
||
| 70 | if ($translation = $this->getTranslationByLocaleKey($fallbackLocale)) { |
||
| 71 | return $translation; |
||
| 72 | } |
||
| 73 | if ($translation = $this->getTranslationByLocaleKey($configFallbackLocale)) { |
||
| 74 | return $translation; |
||
| 75 | } |
||
| 76 | } |
||
| 77 | |||
| 78 | return null; |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @param string|null $locale |
||
| 83 | * |
||
| 84 | * @return bool |
||
| 85 | */ |
||
| 86 | public function hasTranslation($locale = null) |
||
| 87 | { |
||
| 88 | $locale = $locale ?: $this->locale(); |
||
| 89 | |||
| 90 | foreach ($this->translations as $translation) { |
||
|
|
|||
| 91 | if ($translation->getAttribute($this->getLocaleKey()) == $locale) { |
||
| 92 | return true; |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | return false; |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @return string |
||
| 101 | */ |
||
| 102 | public function getTranslationModelName() |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @return string |
||
| 109 | */ |
||
| 110 | public function getTranslationModelNameDefault() |
||
| 111 | { |
||
| 112 | $config = app()->make('config'); |
||
| 113 | |||
| 114 | return get_class($this).$config->get('translatable.translation_suffix', 'Translation'); |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @return string |
||
| 119 | */ |
||
| 120 | public function getRelationKey() |
||
| 121 | { |
||
| 122 | if ($this->translationForeignKey) { |
||
| 123 | $key = $this->translationForeignKey; |
||
| 124 | } elseif ($this->primaryKey !== 'id') { |
||
| 125 | $key = $this->primaryKey; |
||
| 126 | } else { |
||
| 127 | $key = $this->getForeignKey(); |
||
| 128 | } |
||
| 129 | |||
| 130 | return $key; |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @return string |
||
| 135 | */ |
||
| 136 | public function getLocaleKey() |
||
| 137 | { |
||
| 138 | $config = app()->make('config'); |
||
| 139 | |||
| 140 | return $this->localeKey ?: $config->get('translatable.locale_key', 'locale'); |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
| 145 | */ |
||
| 146 | public function translations() |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @return bool|null |
||
| 153 | */ |
||
| 154 | private function useFallbackWhenValueIsNull() |
||
| 155 | { |
||
| 156 | return ! empty($this->useFallbackWhenNull) ? $this->useFallbackWhenNull : |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Returns attribute value from fallback translation if |
||
| 162 | * value of attribute is empty and options use_fallback, use_fallback_when_null is enabled in config file or in model |
||
| 163 | * in model. |
||
| 164 | * @param $locale |
||
| 165 | * @param $attribute |
||
| 166 | * @return mixed |
||
| 167 | */ |
||
| 168 | private function getAttributeWithFallback($locale, $attribute) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @param string $key |
||
| 178 | * |
||
| 179 | * @return mixed |
||
| 180 | */ |
||
| 181 | public function getAttribute($key) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @param string $key |
||
| 207 | * @param mixed $value |
||
| 208 | * |
||
| 209 | * @return $this |
||
| 210 | */ |
||
| 211 | public function setAttribute($key, $value) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @param array $options |
||
| 226 | * |
||
| 227 | * @return bool |
||
| 228 | */ |
||
| 229 | public function save(array $options = []) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @param string $locale |
||
| 260 | * |
||
| 261 | * @return \Illuminate\Database\Eloquent\Model|null |
||
| 262 | */ |
||
| 263 | protected function getTranslationOrNew($locale) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @param array $attributes |
||
| 274 | * |
||
| 275 | * @throws \Illuminate\Database\Eloquent\MassAssignmentException |
||
| 276 | * @return $this |
||
| 277 | */ |
||
| 278 | public function fill(array $attributes) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * @param string $key |
||
| 298 | */ |
||
| 299 | private function getTranslationByLocaleKey($key) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @param null $locale |
||
| 312 | * |
||
| 313 | * @return string |
||
| 314 | */ |
||
| 315 | private function getFallbackLocale($locale = null) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @param $locale |
||
| 328 | * |
||
| 329 | * @return bool |
||
| 330 | */ |
||
| 331 | private function isLocaleCountryBased($locale) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @param $locale |
||
| 338 | * |
||
| 339 | * @return string |
||
| 340 | */ |
||
| 341 | private function getLanguageFromCountryBasedLocale($locale) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * @return bool|null |
||
| 350 | */ |
||
| 351 | private function useFallback() |
||
| 359 | |||
| 360 | /** |
||
| 361 | * @param string $key |
||
| 362 | * |
||
| 363 | * @return bool |
||
| 364 | */ |
||
| 365 | public function isTranslationAttribute($key) |
||
| 369 | |||
| 370 | /** |
||
| 371 | * @param string $key |
||
| 372 | * |
||
| 373 | * @throws \Dimsav\Translatable\Exception\LocalesNotDefinedException |
||
| 374 | * @return bool |
||
| 375 | */ |
||
| 376 | protected function isKeyALocale($key) |
||
| 382 | |||
| 383 | /** |
||
| 384 | * @throws \Dimsav\Translatable\Exception\LocalesNotDefinedException |
||
| 385 | * @return array |
||
| 386 | */ |
||
| 387 | protected function getLocales() |
||
| 410 | |||
| 411 | /** |
||
| 412 | * @return string |
||
| 413 | */ |
||
| 414 | protected function getLocaleSeparator() |
||
| 418 | |||
| 419 | /** |
||
| 420 | * @return bool |
||
| 421 | */ |
||
| 422 | protected function saveTranslations() |
||
| 434 | |||
| 435 | /** |
||
| 436 | * @param \Illuminate\Database\Eloquent\Model $translation |
||
| 437 | * |
||
| 438 | * @return bool |
||
| 439 | */ |
||
| 440 | protected function isTranslationDirty(Model $translation) |
||
| 447 | |||
| 448 | /** |
||
| 449 | * @param string $locale |
||
| 450 | * |
||
| 451 | * @return \Illuminate\Database\Eloquent\Model |
||
| 452 | */ |
||
| 453 | public function getNewTranslation($locale) |
||
| 462 | |||
| 463 | /** |
||
| 464 | * @param $key |
||
| 465 | * |
||
| 466 | * @return bool |
||
| 467 | */ |
||
| 468 | public function __isset($key) |
||
| 472 | |||
| 473 | /** |
||
| 474 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 475 | * @param string $locale |
||
| 476 | * |
||
| 477 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 478 | */ |
||
| 479 | View Code Duplication | public function scopeTranslatedIn(Builder $query, $locale = null) |
|
| 487 | |||
| 488 | /** |
||
| 489 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 490 | * @param string $locale |
||
| 491 | * |
||
| 492 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 493 | */ |
||
| 494 | View Code Duplication | public function scopeNotTranslatedIn(Builder $query, $locale = null) |
|
| 502 | |||
| 503 | /** |
||
| 504 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 505 | * |
||
| 506 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 507 | */ |
||
| 508 | public function scopeTranslated(Builder $query) |
||
| 512 | |||
| 513 | /** |
||
| 514 | * Adds scope to get a list of translated attributes, using the current locale. |
||
| 515 | * Example usage: Country::listsTranslations('name')->get()->toArray() |
||
| 516 | * Will return an array with items: |
||
| 517 | * [ |
||
| 518 | * 'id' => '1', // The id of country |
||
| 519 | * 'name' => 'Griechenland' // The translated name |
||
| 520 | * ]. |
||
| 521 | * |
||
| 522 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 523 | * @param string $translationField |
||
| 524 | */ |
||
| 525 | public function scopeListsTranslations(Builder $query, $translationField) |
||
| 549 | |||
| 550 | /** |
||
| 551 | * This scope eager loads the translations for the default and the fallback locale only. |
||
| 552 | * We can use this as a shortcut to improve performance in our application. |
||
| 553 | * |
||
| 554 | * @param Builder $query |
||
| 555 | */ |
||
| 556 | public function scopeWithTranslation(Builder $query) |
||
| 568 | |||
| 569 | /** |
||
| 570 | * This scope filters results by checking the translation fields. |
||
| 571 | * |
||
| 572 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 573 | * @param string $key |
||
| 574 | * @param string $value |
||
| 575 | * @param string $locale |
||
| 576 | * |
||
| 577 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 578 | */ |
||
| 579 | View Code Duplication | public function scopeWhereTranslation(Builder $query, $key, $value, $locale = null) |
|
| 588 | |||
| 589 | /** |
||
| 590 | * This scope filters results by checking the translation fields. |
||
| 591 | * |
||
| 592 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 593 | * @param string $key |
||
| 594 | * @param string $value |
||
| 595 | * @param string $locale |
||
| 596 | * |
||
| 597 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 598 | */ |
||
| 599 | View Code Duplication | public function scopeWhereTranslationLike(Builder $query, $key, $value, $locale = null) |
|
| 608 | |||
| 609 | /** |
||
| 610 | * @return array |
||
| 611 | */ |
||
| 612 | public function toArray() |
||
| 636 | |||
| 637 | /** |
||
| 638 | * @return string |
||
| 639 | */ |
||
| 640 | private function getTranslationsTable() |
||
| 644 | |||
| 645 | /** |
||
| 646 | * @return string |
||
| 647 | */ |
||
| 648 | protected function locale() |
||
| 657 | |||
| 658 | /** |
||
| 659 | * Set the default locale on the model. |
||
| 660 | * |
||
| 661 | * @param $locale |
||
| 662 | * |
||
| 663 | * @return $this |
||
| 664 | */ |
||
| 665 | public function setDefaultLocale($locale) |
||
| 671 | |||
| 672 | /** |
||
| 673 | * Get the default locale on the model. |
||
| 674 | * |
||
| 675 | * @return mixed |
||
| 676 | */ |
||
| 677 | public function getDefaultLocale() |
||
| 681 | |||
| 682 | /** |
||
| 683 | * Deletes all translations for this model. |
||
| 684 | * |
||
| 685 | * @param string|array|null $locales The locales to be deleted (array or single string) |
||
| 686 | * (e.g., ["en", "de"] would remove these translations). |
||
| 687 | */ |
||
| 688 | public function deleteTranslations($locales = null) |
||
| 701 | |||
| 702 | /** |
||
| 703 | * @param $key |
||
| 704 | * |
||
| 705 | * @return array |
||
| 706 | */ |
||
| 707 | private function getAttributeAndLocale($key) |
||
| 715 | |||
| 716 | /** |
||
| 717 | * @return bool |
||
| 718 | */ |
||
| 719 | private function toArrayAlwaysLoadsTranslations() |
||
| 723 | } |
||
| 724 |
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: