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) |
||
59 | { |
||
60 | $configFallbackLocale = $this->getFallbackLocale(); |
||
61 | $locale = $locale ?: $this->locale(); |
||
62 | $withFallback = $withFallback === null ? $this->useFallback() : $withFallback; |
||
63 | $fallbackLocale = $this->getFallbackLocale($locale); |
||
64 | |||
65 | if ($translation = $this->getTranslationByLocaleKey($locale)) { |
||
66 | return $translation; |
||
67 | } |
||
68 | if ($withFallback && $fallbackLocale) { |
||
69 | if ($translation = $this->getTranslationByLocaleKey($fallbackLocale)) { |
||
70 | return $translation; |
||
71 | } |
||
72 | if ($fallbackLocale !== $configFallbackLocale && $translation = $this->getTranslationByLocaleKey($configFallbackLocale)) { |
||
73 | return $translation; |
||
74 | } |
||
75 | } |
||
76 | |||
77 | return null; |
||
78 | } |
||
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() |
||
113 | |||
114 | /** |
||
115 | * @return string |
||
116 | */ |
||
117 | public function getRelationKey() |
||
129 | |||
130 | /** |
||
131 | * @return string |
||
132 | */ |
||
133 | public function getLocaleKey() |
||
137 | |||
138 | /** |
||
139 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
140 | */ |
||
141 | public function translations() |
||
145 | |||
146 | /** |
||
147 | * @return bool |
||
148 | */ |
||
149 | private function usePropertyFallback() |
||
153 | |||
154 | /** |
||
155 | * Returns the attribute value from fallback translation if value of attribute |
||
156 | * is empty and the property fallback is enabled in the configuration. |
||
157 | * in model. |
||
158 | * @param $locale |
||
159 | * @param $attribute |
||
160 | * @return mixed |
||
161 | */ |
||
162 | private function getAttributeOrFallback($locale, $attribute) |
||
182 | |||
183 | /** |
||
184 | * @param string $key |
||
185 | * |
||
186 | * @return mixed |
||
187 | */ |
||
188 | public function getAttribute($key) |
||
211 | |||
212 | /** |
||
213 | * @param string $key |
||
214 | * @param mixed $value |
||
215 | * |
||
216 | * @return $this |
||
217 | */ |
||
218 | public function setAttribute($key, $value) |
||
230 | |||
231 | /** |
||
232 | * @param array $options |
||
233 | * |
||
234 | * @return bool |
||
235 | */ |
||
236 | public function save(array $options = []) |
||
268 | |||
269 | /** |
||
270 | * @param string $locale |
||
271 | * |
||
272 | * @return \Illuminate\Database\Eloquent\Model |
||
273 | */ |
||
274 | protected function getTranslationOrNew($locale) |
||
282 | |||
283 | /** |
||
284 | * @param array $attributes |
||
285 | * |
||
286 | * @throws \Illuminate\Database\Eloquent\MassAssignmentException |
||
287 | * @return $this |
||
288 | */ |
||
289 | public function fill(array $attributes) |
||
306 | |||
307 | /** |
||
308 | * @param string $key |
||
309 | */ |
||
310 | private function getTranslationByLocaleKey($key) |
||
320 | |||
321 | /** |
||
322 | * @param null $locale |
||
323 | * |
||
324 | * @return string |
||
325 | */ |
||
326 | private function getFallbackLocale($locale = null) |
||
336 | |||
337 | /** |
||
338 | * @param $locale |
||
339 | * |
||
340 | * @return bool |
||
341 | */ |
||
342 | private function isLocaleCountryBased($locale) |
||
346 | |||
347 | /** |
||
348 | * @param $locale |
||
349 | * |
||
350 | * @return string |
||
351 | */ |
||
352 | private function getLanguageFromCountryBasedLocale($locale) |
||
358 | |||
359 | /** |
||
360 | * @return bool|null |
||
361 | */ |
||
362 | private function useFallback() |
||
370 | |||
371 | /** |
||
372 | * @param string $key |
||
373 | * |
||
374 | * @return bool |
||
375 | */ |
||
376 | public function isTranslationAttribute($key) |
||
380 | |||
381 | /** |
||
382 | * @param string $key |
||
383 | * |
||
384 | * @throws \Dimsav\Translatable\Exception\LocalesNotDefinedException |
||
385 | * @return bool |
||
386 | */ |
||
387 | protected function isKeyALocale($key) |
||
393 | |||
394 | /** |
||
395 | * @throws \Dimsav\Translatable\Exception\LocalesNotDefinedException |
||
396 | * @return array |
||
397 | */ |
||
398 | protected function getLocales() |
||
421 | |||
422 | /** |
||
423 | * @return string |
||
424 | */ |
||
425 | protected function getLocaleSeparator() |
||
429 | |||
430 | /** |
||
431 | * @return bool |
||
432 | */ |
||
433 | protected function saveTranslations() |
||
449 | |||
450 | /** |
||
451 | * @param array |
||
452 | * |
||
453 | * @return \Illuminate\Database\Eloquent\Model |
||
454 | */ |
||
455 | public function replicateWithTranslations(array $except = null) |
||
467 | |||
468 | /** |
||
469 | * @param \Illuminate\Database\Eloquent\Model $translation |
||
470 | * |
||
471 | * @return bool |
||
472 | */ |
||
473 | protected function isTranslationDirty(Model $translation) |
||
480 | |||
481 | /** |
||
482 | * @param string $locale |
||
483 | * |
||
484 | * @return \Illuminate\Database\Eloquent\Model |
||
485 | */ |
||
486 | public function getNewTranslation($locale) |
||
495 | |||
496 | /** |
||
497 | * @param $key |
||
498 | * |
||
499 | * @return bool |
||
500 | */ |
||
501 | public function __isset($key) |
||
505 | |||
506 | /** |
||
507 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
508 | * @param string $locale |
||
509 | * |
||
510 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
511 | */ |
||
512 | View Code Duplication | public function scopeTranslatedIn(Builder $query, $locale = null) |
|
520 | |||
521 | /** |
||
522 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
523 | * @param string $locale |
||
524 | * |
||
525 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
526 | */ |
||
527 | View Code Duplication | public function scopeNotTranslatedIn(Builder $query, $locale = null) |
|
535 | |||
536 | /** |
||
537 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
538 | * |
||
539 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
540 | */ |
||
541 | public function scopeTranslated(Builder $query) |
||
545 | |||
546 | /** |
||
547 | * Adds scope to get a list of translated attributes, using the current locale. |
||
548 | * Example usage: Country::listsTranslations('name')->get()->toArray() |
||
549 | * Will return an array with items: |
||
550 | * [ |
||
551 | * 'id' => '1', // The id of country |
||
552 | * 'name' => 'Griechenland' // The translated name |
||
553 | * ]. |
||
554 | * |
||
555 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
556 | * @param string $translationField |
||
557 | */ |
||
558 | public function scopeListsTranslations(Builder $query, $translationField) |
||
582 | |||
583 | /** |
||
584 | * This scope eager loads the translations for the default and the fallback locale only. |
||
585 | * We can use this as a shortcut to improve performance in our application. |
||
586 | * |
||
587 | * @param Builder $query |
||
588 | */ |
||
589 | public function scopeWithTranslation(Builder $query) |
||
605 | |||
606 | /** |
||
607 | * This scope filters results by checking the translation fields. |
||
608 | * |
||
609 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
610 | * @param string $key |
||
611 | * @param string $value |
||
612 | * @param string $locale |
||
613 | * |
||
614 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
615 | */ |
||
616 | View Code Duplication | public function scopeWhereTranslation(Builder $query, $key, $value, $locale = null) |
|
625 | |||
626 | /** |
||
627 | * This scope filters results by checking the translation fields. |
||
628 | * |
||
629 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
630 | * @param string $key |
||
631 | * @param string $value |
||
632 | * @param string $locale |
||
633 | * |
||
634 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
635 | */ |
||
636 | View Code Duplication | public function scopeOrWhereTranslation(Builder $query, $key, $value, $locale = null) |
|
645 | |||
646 | /** |
||
647 | * This scope filters results by checking the translation fields. |
||
648 | * |
||
649 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
650 | * @param string $key |
||
651 | * @param string $value |
||
652 | * @param string $locale |
||
653 | * |
||
654 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
655 | */ |
||
656 | View Code Duplication | public function scopeWhereTranslationLike(Builder $query, $key, $value, $locale = null) |
|
665 | |||
666 | /** |
||
667 | * This scope filters results by checking the translation fields. |
||
668 | * |
||
669 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
670 | * @param string $key |
||
671 | * @param string $value |
||
672 | * @param string $locale |
||
673 | * |
||
674 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
675 | */ |
||
676 | View Code Duplication | public function scopeOrWhereTranslationLike(Builder $query, $key, $value, $locale = null) |
|
685 | |||
686 | /** |
||
687 | * @return array |
||
688 | */ |
||
689 | public function attributesToArray() |
||
709 | |||
710 | /** |
||
711 | * @return array |
||
712 | */ |
||
713 | public function getTranslationsArray() |
||
725 | |||
726 | /** |
||
727 | * @return string |
||
728 | */ |
||
729 | private function getTranslationsTable() |
||
733 | |||
734 | /** |
||
735 | * @return string |
||
736 | */ |
||
737 | protected function locale() |
||
746 | |||
747 | /** |
||
748 | * Set the default locale on the model. |
||
749 | * |
||
750 | * @param $locale |
||
751 | * |
||
752 | * @return $this |
||
753 | */ |
||
754 | public function setDefaultLocale($locale) |
||
760 | |||
761 | /** |
||
762 | * Get the default locale on the model. |
||
763 | * |
||
764 | * @return mixed |
||
765 | */ |
||
766 | public function getDefaultLocale() |
||
770 | |||
771 | /** |
||
772 | * Deletes all translations for this model. |
||
773 | * |
||
774 | * @param string|array|null $locales The locales to be deleted (array or single string) |
||
775 | * (e.g., ["en", "de"] would remove these translations). |
||
776 | */ |
||
777 | public function deleteTranslations($locales = null) |
||
793 | |||
794 | /** |
||
795 | * @param $key |
||
796 | * |
||
797 | * @return array |
||
798 | */ |
||
799 | private function getAttributeAndLocale($key) |
||
807 | |||
808 | /** |
||
809 | * @return bool |
||
810 | */ |
||
811 | private function toArrayAlwaysLoadsTranslations() |
||
815 | } |
||
816 |
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: