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 |
||
| 153 | */ |
||
| 154 | private function usePropertyFallback() |
||
| 155 | { |
||
| 156 | return app()->make('config')->get('translatable.use_property_fallback', false); |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Returns the attribute value from fallback translation if value of attribute |
||
| 161 | * is empty and the property fallback is enabled in the configuration. |
||
| 162 | * in model. |
||
| 163 | * @param $locale |
||
| 164 | * @param $attribute |
||
| 165 | * @return mixed |
||
| 166 | */ |
||
| 167 | private function getAttributeOrFallback($locale, $attribute) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @param string $key |
||
| 181 | * |
||
| 182 | * @return mixed |
||
| 183 | */ |
||
| 184 | public function getAttribute($key) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @param string $key |
||
| 210 | * @param mixed $value |
||
| 211 | * |
||
| 212 | * @return $this |
||
| 213 | */ |
||
| 214 | public function setAttribute($key, $value) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @param array $options |
||
| 229 | * |
||
| 230 | * @return bool |
||
| 231 | */ |
||
| 232 | public function save(array $options = []) |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @param string $locale |
||
| 263 | * |
||
| 264 | * @return \Illuminate\Database\Eloquent\Model|null |
||
| 265 | */ |
||
| 266 | protected function getTranslationOrNew($locale) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @param array $attributes |
||
| 277 | * |
||
| 278 | * @throws \Illuminate\Database\Eloquent\MassAssignmentException |
||
| 279 | * @return $this |
||
| 280 | */ |
||
| 281 | public function fill(array $attributes) |
||
| 282 | { |
||
| 283 | foreach ($attributes as $key => $values) { |
||
| 284 | if ($this->isKeyALocale($key)) { |
||
| 285 | $this->getTranslationOrNew($key)->fill($values); |
||
| 286 | unset($attributes[$key]); |
||
| 287 | } else { |
||
| 288 | list($attribute, $locale) = $this->getAttributeAndLocale($key); |
||
| 289 | if ($this->isTranslationAttribute($attribute) and $this->isKeyALocale($locale)) { |
||
| 290 | $this->getTranslationOrNew($locale)->fill([$attribute => $values]); |
||
| 291 | unset($attributes[$key]); |
||
| 292 | } |
||
| 293 | } |
||
| 294 | } |
||
| 295 | |||
| 296 | return parent::fill($attributes); |
||
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @param string $key |
||
| 301 | */ |
||
| 302 | private function getTranslationByLocaleKey($key) |
||
| 312 | |||
| 313 | /** |
||
| 314 | * @param null $locale |
||
| 315 | * |
||
| 316 | * @return string |
||
| 317 | */ |
||
| 318 | private function getFallbackLocale($locale = null) |
||
| 328 | |||
| 329 | /** |
||
| 330 | * @param $locale |
||
| 331 | * |
||
| 332 | * @return bool |
||
| 333 | */ |
||
| 334 | private function isLocaleCountryBased($locale) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @param $locale |
||
| 341 | * |
||
| 342 | * @return string |
||
| 343 | */ |
||
| 344 | private function getLanguageFromCountryBasedLocale($locale) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * @return bool|null |
||
| 353 | */ |
||
| 354 | private function useFallback() |
||
| 362 | |||
| 363 | /** |
||
| 364 | * @param string $key |
||
| 365 | * |
||
| 366 | * @return bool |
||
| 367 | */ |
||
| 368 | public function isTranslationAttribute($key) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * @param string $key |
||
| 375 | * |
||
| 376 | * @throws \Dimsav\Translatable\Exception\LocalesNotDefinedException |
||
| 377 | * @return bool |
||
| 378 | */ |
||
| 379 | protected function isKeyALocale($key) |
||
| 385 | |||
| 386 | /** |
||
| 387 | * @throws \Dimsav\Translatable\Exception\LocalesNotDefinedException |
||
| 388 | * @return array |
||
| 389 | */ |
||
| 390 | protected function getLocales() |
||
| 413 | |||
| 414 | /** |
||
| 415 | * @return string |
||
| 416 | */ |
||
| 417 | protected function getLocaleSeparator() |
||
| 421 | |||
| 422 | /** |
||
| 423 | * @return bool |
||
| 424 | */ |
||
| 425 | protected function saveTranslations() |
||
| 437 | |||
| 438 | /** |
||
| 439 | * @param \Illuminate\Database\Eloquent\Model $translation |
||
| 440 | * |
||
| 441 | * @return bool |
||
| 442 | */ |
||
| 443 | protected function isTranslationDirty(Model $translation) |
||
| 450 | |||
| 451 | /** |
||
| 452 | * @param string $locale |
||
| 453 | * |
||
| 454 | * @return \Illuminate\Database\Eloquent\Model |
||
| 455 | */ |
||
| 456 | public function getNewTranslation($locale) |
||
| 465 | |||
| 466 | /** |
||
| 467 | * @param $key |
||
| 468 | * |
||
| 469 | * @return bool |
||
| 470 | */ |
||
| 471 | public function __isset($key) |
||
| 475 | |||
| 476 | /** |
||
| 477 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 478 | * @param string $locale |
||
| 479 | * |
||
| 480 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 481 | */ |
||
| 482 | View Code Duplication | public function scopeTranslatedIn(Builder $query, $locale = null) |
|
| 490 | |||
| 491 | /** |
||
| 492 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 493 | * @param string $locale |
||
| 494 | * |
||
| 495 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 496 | */ |
||
| 497 | View Code Duplication | public function scopeNotTranslatedIn(Builder $query, $locale = null) |
|
| 505 | |||
| 506 | /** |
||
| 507 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 508 | * |
||
| 509 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 510 | */ |
||
| 511 | public function scopeTranslated(Builder $query) |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Adds scope to get a list of translated attributes, using the current locale. |
||
| 518 | * Example usage: Country::listsTranslations('name')->get()->toArray() |
||
| 519 | * Will return an array with items: |
||
| 520 | * [ |
||
| 521 | * 'id' => '1', // The id of country |
||
| 522 | * 'name' => 'Griechenland' // The translated name |
||
| 523 | * ]. |
||
| 524 | * |
||
| 525 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 526 | * @param string $translationField |
||
| 527 | */ |
||
| 528 | public function scopeListsTranslations(Builder $query, $translationField) |
||
| 552 | |||
| 553 | /** |
||
| 554 | * This scope eager loads the translations for the default and the fallback locale only. |
||
| 555 | * We can use this as a shortcut to improve performance in our application. |
||
| 556 | * |
||
| 557 | * @param Builder $query |
||
| 558 | */ |
||
| 559 | public function scopeWithTranslation(Builder $query) |
||
| 571 | |||
| 572 | /** |
||
| 573 | * This scope filters results by checking the translation fields. |
||
| 574 | * |
||
| 575 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 576 | * @param string $key |
||
| 577 | * @param string $value |
||
| 578 | * @param string $locale |
||
| 579 | * |
||
| 580 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 581 | */ |
||
| 582 | View Code Duplication | public function scopeWhereTranslation(Builder $query, $key, $value, $locale = null) |
|
| 591 | |||
| 592 | /** |
||
| 593 | * This scope filters results by checking the translation fields. |
||
| 594 | * |
||
| 595 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 596 | * @param string $key |
||
| 597 | * @param string $value |
||
| 598 | * @param string $locale |
||
| 599 | * |
||
| 600 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 601 | */ |
||
| 602 | View Code Duplication | public function scopeWhereTranslationLike(Builder $query, $key, $value, $locale = null) |
|
| 611 | |||
| 612 | /** |
||
| 613 | * @return array |
||
| 614 | */ |
||
| 615 | public function toArray() |
||
| 639 | |||
| 640 | /** |
||
| 641 | * @return string |
||
| 642 | */ |
||
| 643 | private function getTranslationsTable() |
||
| 647 | |||
| 648 | /** |
||
| 649 | * @return string |
||
| 650 | */ |
||
| 651 | protected function locale() |
||
| 660 | |||
| 661 | /** |
||
| 662 | * Set the default locale on the model. |
||
| 663 | * |
||
| 664 | * @param $locale |
||
| 665 | * |
||
| 666 | * @return $this |
||
| 667 | */ |
||
| 668 | public function setDefaultLocale($locale) |
||
| 674 | |||
| 675 | /** |
||
| 676 | * Get the default locale on the model. |
||
| 677 | * |
||
| 678 | * @return mixed |
||
| 679 | */ |
||
| 680 | public function getDefaultLocale() |
||
| 684 | |||
| 685 | /** |
||
| 686 | * Deletes all translations for this model. |
||
| 687 | * |
||
| 688 | * @param string|array|null $locales The locales to be deleted (array or single string) |
||
| 689 | * (e.g., ["en", "de"] would remove these translations). |
||
| 690 | */ |
||
| 691 | public function deleteTranslations($locales = null) |
||
| 704 | |||
| 705 | /** |
||
| 706 | * @param $key |
||
| 707 | * |
||
| 708 | * @return array |
||
| 709 | */ |
||
| 710 | private function getAttributeAndLocale($key) |
||
| 718 | |||
| 719 | /** |
||
| 720 | * @return bool |
||
| 721 | */ |
||
| 722 | private function toArrayAlwaysLoadsTranslations() |
||
| 726 | } |
||
| 727 |
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: