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 | /** |
||
| 15 | * Alias for getTranslation(). |
||
| 16 | * |
||
| 17 | * @param string|null $locale |
||
| 18 | * @param bool $withFallback |
||
| 19 | * |
||
| 20 | * @return \Illuminate\Database\Eloquent\Model|null |
||
| 21 | */ |
||
| 22 | public function translate($locale = null, $withFallback = false) |
||
| 23 | { |
||
| 24 | return $this->getTranslation($locale, $withFallback); |
||
| 25 | } |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Alias for getTranslation(). |
||
| 29 | * |
||
| 30 | * @param string $locale |
||
| 31 | * |
||
| 32 | * @return \Illuminate\Database\Eloquent\Model|null |
||
| 33 | */ |
||
| 34 | public function translateOrDefault($locale) |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Alias for getTranslationOrNew(). |
||
| 41 | * |
||
| 42 | * @param string $locale |
||
| 43 | * |
||
| 44 | * @return \Illuminate\Database\Eloquent\Model|null |
||
| 45 | */ |
||
| 46 | public function translateOrNew($locale) |
||
| 47 | { |
||
| 48 | return $this->getTranslationOrNew($locale); |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @param string|null $locale |
||
| 53 | * @param bool $withFallback |
||
| 54 | * |
||
| 55 | * @return \Illuminate\Database\Eloquent\Model|null |
||
| 56 | */ |
||
| 57 | public function getTranslation($locale = null, $withFallback = null) |
||
| 58 | { |
||
| 59 | $configFallbackLocale = $this->getFallbackLocale(); |
||
| 60 | $locale = $locale ?: $this->locale(); |
||
| 61 | $withFallback = $withFallback === null ? $this->useFallback() : $withFallback; |
||
| 62 | $fallbackLocale = $this->getFallbackLocale($locale); |
||
| 63 | |||
| 64 | if ($translation = $this->getTranslationByLocaleKey($locale)) { |
||
| 65 | return $translation; |
||
| 66 | } |
||
| 67 | if ($withFallback && $fallbackLocale) { |
||
| 68 | if ($translation = $this->getTranslationByLocaleKey($fallbackLocale)) { |
||
| 69 | return $translation; |
||
| 70 | } |
||
| 71 | if ($translation = $this->getTranslationByLocaleKey($configFallbackLocale)) { |
||
| 72 | return $translation; |
||
| 73 | } |
||
| 74 | } |
||
| 75 | |||
| 76 | return null; |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @param string|null $locale |
||
| 81 | * |
||
| 82 | * @return bool |
||
| 83 | */ |
||
| 84 | public function hasTranslation($locale = null) |
||
| 85 | { |
||
| 86 | $locale = $locale ?: $this->locale(); |
||
| 87 | |||
| 88 | foreach ($this->translations as $translation) { |
||
|
|
|||
| 89 | if ($translation->getAttribute($this->getLocaleKey()) == $locale) { |
||
| 90 | return true; |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 94 | return false; |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @return string |
||
| 99 | */ |
||
| 100 | public function getTranslationModelName() |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @return string |
||
| 107 | */ |
||
| 108 | public function getTranslationModelNameDefault() |
||
| 109 | { |
||
| 110 | $config = app()->make('config'); |
||
| 111 | |||
| 112 | return get_class($this).$config->get('translatable.translation_suffix', 'Translation'); |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @return string |
||
| 117 | */ |
||
| 118 | public function getRelationKey() |
||
| 119 | { |
||
| 120 | if ($this->translationForeignKey) { |
||
| 121 | $key = $this->translationForeignKey; |
||
| 122 | } elseif ($this->primaryKey !== 'id') { |
||
| 123 | $key = $this->primaryKey; |
||
| 124 | } else { |
||
| 125 | $key = $this->getForeignKey(); |
||
| 126 | } |
||
| 127 | |||
| 128 | return $key; |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @return string |
||
| 133 | */ |
||
| 134 | public function getLocaleKey() |
||
| 135 | { |
||
| 136 | $config = app()->make('config'); |
||
| 137 | |||
| 138 | return $this->localeKey ?: $config->get('translatable.locale_key', 'locale'); |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
| 143 | */ |
||
| 144 | public function translations() |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @param string $key |
||
| 151 | * |
||
| 152 | * @return mixed |
||
| 153 | */ |
||
| 154 | public function getAttribute($key) |
||
| 155 | { |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @param string $key |
||
| 180 | * @param mixed $value |
||
| 181 | * |
||
| 182 | * @return $this |
||
| 183 | */ |
||
| 184 | public function setAttribute($key, $value) |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @param array $options |
||
| 199 | * |
||
| 200 | * @return bool |
||
| 201 | */ |
||
| 202 | public function save(array $options = []) |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @param string $locale |
||
| 233 | * |
||
| 234 | * @return \Illuminate\Database\Eloquent\Model|null |
||
| 235 | */ |
||
| 236 | protected function getTranslationOrNew($locale) |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @param array $attributes |
||
| 247 | * |
||
| 248 | * @throws \Illuminate\Database\Eloquent\MassAssignmentException |
||
| 249 | * @return $this |
||
| 250 | */ |
||
| 251 | public function fill(array $attributes) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @param string $key |
||
| 271 | */ |
||
| 272 | private function getTranslationByLocaleKey($key) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @param null $locale |
||
| 285 | * |
||
| 286 | * @return string |
||
| 287 | */ |
||
| 288 | private function getFallbackLocale($locale = null) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @param $locale |
||
| 301 | * |
||
| 302 | * @return bool |
||
| 303 | */ |
||
| 304 | private function isLocaleCountryBased($locale) |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @param $locale |
||
| 311 | * |
||
| 312 | * @return string |
||
| 313 | */ |
||
| 314 | private function getLanguageFromCountryBasedLocale($locale) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * @return bool|null |
||
| 323 | */ |
||
| 324 | private function useFallback() |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @param string $key |
||
| 335 | * |
||
| 336 | * @return bool |
||
| 337 | */ |
||
| 338 | public function isTranslationAttribute($key) |
||
| 342 | |||
| 343 | /** |
||
| 344 | * @param string $key |
||
| 345 | * |
||
| 346 | * @throws \Dimsav\Translatable\Exception\LocalesNotDefinedException |
||
| 347 | * @return bool |
||
| 348 | */ |
||
| 349 | protected function isKeyALocale($key) |
||
| 355 | |||
| 356 | /** |
||
| 357 | * @throws \Dimsav\Translatable\Exception\LocalesNotDefinedException |
||
| 358 | * @return array |
||
| 359 | */ |
||
| 360 | protected function getLocales() |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @return string |
||
| 386 | */ |
||
| 387 | protected function getLocaleSeparator() |
||
| 391 | |||
| 392 | /** |
||
| 393 | * @return bool |
||
| 394 | */ |
||
| 395 | protected function saveTranslations() |
||
| 407 | |||
| 408 | /** |
||
| 409 | * @param \Illuminate\Database\Eloquent\Model $translation |
||
| 410 | * |
||
| 411 | * @return bool |
||
| 412 | */ |
||
| 413 | protected function isTranslationDirty(Model $translation) |
||
| 420 | |||
| 421 | /** |
||
| 422 | * @param string $locale |
||
| 423 | * |
||
| 424 | * @return \Illuminate\Database\Eloquent\Model |
||
| 425 | */ |
||
| 426 | public function getNewTranslation($locale) |
||
| 435 | |||
| 436 | /** |
||
| 437 | * @param $key |
||
| 438 | * |
||
| 439 | * @return bool |
||
| 440 | */ |
||
| 441 | public function __isset($key) |
||
| 445 | |||
| 446 | /** |
||
| 447 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 448 | * @param string $locale |
||
| 449 | * |
||
| 450 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 451 | */ |
||
| 452 | View Code Duplication | public function scopeTranslatedIn(Builder $query, $locale = null) |
|
| 460 | |||
| 461 | /** |
||
| 462 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 463 | * @param string $locale |
||
| 464 | * |
||
| 465 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 466 | */ |
||
| 467 | View Code Duplication | public function scopeNotTranslatedIn(Builder $query, $locale = null) |
|
| 475 | |||
| 476 | /** |
||
| 477 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 478 | * |
||
| 479 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 480 | */ |
||
| 481 | public function scopeTranslated(Builder $query) |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Adds scope to get a list of translated attributes, using the current locale. |
||
| 488 | * Example usage: Country::listsTranslations('name')->get()->toArray() |
||
| 489 | * Will return an array with items: |
||
| 490 | * [ |
||
| 491 | * 'id' => '1', // The id of country |
||
| 492 | * 'name' => 'Griechenland' // The translated name |
||
| 493 | * ] |
||
| 494 | * |
||
| 495 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 496 | * @param string $translationField |
||
| 497 | */ |
||
| 498 | public function scopeListsTranslations(Builder $query, $translationField) |
||
| 522 | |||
| 523 | /** |
||
| 524 | * This scope eager loads the translations for the default and the fallback locale only. |
||
| 525 | * We can use this as a shortcut to improve performance in our application. |
||
| 526 | * |
||
| 527 | * @param Builder $query |
||
| 528 | */ |
||
| 529 | public function scopeWithTranslation(Builder $query) |
||
| 541 | |||
| 542 | /** |
||
| 543 | * This scope filters results by checking the translation fields. |
||
| 544 | * |
||
| 545 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 546 | * @param string $key |
||
| 547 | * @param string $value |
||
| 548 | * @param string $locale |
||
| 549 | * |
||
| 550 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 551 | */ |
||
| 552 | View Code Duplication | public function scopeWhereTranslation(Builder $query, $key, $value, $locale = null) |
|
| 561 | |||
| 562 | /** |
||
| 563 | * This scope filters results by checking the translation fields. |
||
| 564 | * |
||
| 565 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 566 | * @param string $key |
||
| 567 | * @param string $value |
||
| 568 | * @param string $locale |
||
| 569 | * |
||
| 570 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 571 | */ |
||
| 572 | View Code Duplication | public function scopeWhereTranslationLike(Builder $query, $key, $value, $locale = null) |
|
| 581 | |||
| 582 | /** |
||
| 583 | * @return array |
||
| 584 | */ |
||
| 585 | public function toArray() |
||
| 603 | |||
| 604 | /** |
||
| 605 | * @return bool |
||
| 606 | */ |
||
| 607 | private function alwaysFillable() |
||
| 611 | |||
| 612 | /** |
||
| 613 | * @return string |
||
| 614 | */ |
||
| 615 | private function getTranslationsTable() |
||
| 619 | |||
| 620 | /** |
||
| 621 | * @return string |
||
| 622 | */ |
||
| 623 | protected function locale() |
||
| 628 | |||
| 629 | /** |
||
| 630 | * Deletes all translations for this model. |
||
| 631 | * |
||
| 632 | * @param string|array|null $locales The locales to be deleted (array or single string) |
||
| 633 | * (e.g., ["en", "de"] would remove these translations). |
||
| 634 | */ |
||
| 635 | public function deleteTranslations($locales = null) |
||
| 648 | |||
| 649 | /** |
||
| 650 | * @param $key |
||
| 651 | * |
||
| 652 | * @return array |
||
| 653 | */ |
||
| 654 | private function getAttributeAndLocale($key) |
||
| 662 | } |
||
| 663 |
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: