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) |
||
176 | |||
177 | /** |
||
178 | * @param string $key |
||
179 | * |
||
180 | * @return mixed |
||
181 | */ |
||
182 | public function getAttribute($key) |
||
205 | |||
206 | /** |
||
207 | * @param string $key |
||
208 | * @param mixed $value |
||
209 | * |
||
210 | * @return $this |
||
211 | */ |
||
212 | public function setAttribute($key, $value) |
||
224 | |||
225 | /** |
||
226 | * @param array $options |
||
227 | * |
||
228 | * @return bool |
||
229 | */ |
||
230 | public function save(array $options = []) |
||
262 | |||
263 | /** |
||
264 | * @param string $locale |
||
265 | * |
||
266 | * @return \Illuminate\Database\Eloquent\Model |
||
267 | */ |
||
268 | protected function getTranslationOrNew($locale) |
||
276 | |||
277 | /** |
||
278 | * @param array $attributes |
||
279 | * |
||
280 | * @throws \Illuminate\Database\Eloquent\MassAssignmentException |
||
281 | * @return $this |
||
282 | */ |
||
283 | public function fill(array $attributes) |
||
300 | |||
301 | /** |
||
302 | * @param string $key |
||
303 | */ |
||
304 | private function getTranslationByLocaleKey($key) |
||
314 | |||
315 | /** |
||
316 | * @param null $locale |
||
317 | * |
||
318 | * @return string |
||
319 | */ |
||
320 | private function getFallbackLocale($locale = null) |
||
330 | |||
331 | /** |
||
332 | * @param $locale |
||
333 | * |
||
334 | * @return bool |
||
335 | */ |
||
336 | private function isLocaleCountryBased($locale) |
||
340 | |||
341 | /** |
||
342 | * @param $locale |
||
343 | * |
||
344 | * @return string |
||
345 | */ |
||
346 | private function getLanguageFromCountryBasedLocale($locale) |
||
352 | |||
353 | /** |
||
354 | * @return bool|null |
||
355 | */ |
||
356 | private function useFallback() |
||
364 | |||
365 | /** |
||
366 | * @param string $key |
||
367 | * |
||
368 | * @return bool |
||
369 | */ |
||
370 | public function isTranslationAttribute($key) |
||
374 | |||
375 | /** |
||
376 | * @param string $key |
||
377 | * |
||
378 | * @throws \Dimsav\Translatable\Exception\LocalesNotDefinedException |
||
379 | * @return bool |
||
380 | */ |
||
381 | protected function isKeyALocale($key) |
||
387 | |||
388 | /** |
||
389 | * @throws \Dimsav\Translatable\Exception\LocalesNotDefinedException |
||
390 | * @return array |
||
391 | */ |
||
392 | protected function getLocales() |
||
415 | |||
416 | /** |
||
417 | * @return string |
||
418 | */ |
||
419 | protected function getLocaleSeparator() |
||
423 | |||
424 | /** |
||
425 | * @return bool |
||
426 | */ |
||
427 | protected function saveTranslations() |
||
443 | |||
444 | /** |
||
445 | * @param array |
||
446 | * |
||
447 | * @return \Illuminate\Database\Eloquent\Model |
||
448 | */ |
||
449 | public function replicateWithTranslations(array $except = null) |
||
461 | |||
462 | /** |
||
463 | * @param \Illuminate\Database\Eloquent\Model $translation |
||
464 | * |
||
465 | * @return bool |
||
466 | */ |
||
467 | protected function isTranslationDirty(Model $translation) |
||
474 | |||
475 | /** |
||
476 | * @param string $locale |
||
477 | * |
||
478 | * @return \Illuminate\Database\Eloquent\Model |
||
479 | */ |
||
480 | public function getNewTranslation($locale) |
||
489 | |||
490 | /** |
||
491 | * @param $key |
||
492 | * |
||
493 | * @return bool |
||
494 | */ |
||
495 | public function __isset($key) |
||
499 | |||
500 | /** |
||
501 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
502 | * @param string $locale |
||
503 | * |
||
504 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
505 | */ |
||
506 | View Code Duplication | public function scopeTranslatedIn(Builder $query, $locale = null) |
|
514 | |||
515 | /** |
||
516 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
517 | * @param string $locale |
||
518 | * |
||
519 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
520 | */ |
||
521 | View Code Duplication | public function scopeNotTranslatedIn(Builder $query, $locale = null) |
|
529 | |||
530 | /** |
||
531 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
532 | * |
||
533 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
534 | */ |
||
535 | public function scopeTranslated(Builder $query) |
||
539 | |||
540 | /** |
||
541 | * Adds scope to get a list of translated attributes, using the current locale. |
||
542 | * Example usage: Country::listsTranslations('name')->get()->toArray() |
||
543 | * Will return an array with items: |
||
544 | * [ |
||
545 | * 'id' => '1', // The id of country |
||
546 | * 'name' => 'Griechenland' // The translated name |
||
547 | * ]. |
||
548 | * |
||
549 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
550 | * @param string $translationField |
||
551 | */ |
||
552 | public function scopeListsTranslations(Builder $query, $translationField) |
||
576 | |||
577 | /** |
||
578 | * This scope eager loads the translations for the default and the fallback locale only. |
||
579 | * We can use this as a shortcut to improve performance in our application. |
||
580 | * |
||
581 | * @param Builder $query |
||
582 | */ |
||
583 | public function scopeWithTranslation(Builder $query) |
||
599 | |||
600 | /** |
||
601 | * This scope filters results by checking the translation fields. |
||
602 | * |
||
603 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
604 | * @param string $key |
||
605 | * @param string $value |
||
606 | * @param string $locale |
||
607 | * |
||
608 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
609 | */ |
||
610 | View Code Duplication | public function scopeWhereTranslation(Builder $query, $key, $value, $locale = null) |
|
619 | |||
620 | /** |
||
621 | * This scope filters results by checking the translation fields. |
||
622 | * |
||
623 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
624 | * @param string $key |
||
625 | * @param string $value |
||
626 | * @param string $locale |
||
627 | * |
||
628 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
629 | */ |
||
630 | View Code Duplication | public function scopeOrWhereTranslation(Builder $query, $key, $value, $locale = null) |
|
639 | |||
640 | /** |
||
641 | * This scope filters results by checking the translation fields. |
||
642 | * |
||
643 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
644 | * @param string $key |
||
645 | * @param string $value |
||
646 | * @param string $locale |
||
647 | * |
||
648 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
649 | */ |
||
650 | View Code Duplication | public function scopeWhereTranslationLike(Builder $query, $key, $value, $locale = null) |
|
659 | |||
660 | /** |
||
661 | * This scope filters results by checking the translation fields. |
||
662 | * |
||
663 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
664 | * @param string $key |
||
665 | * @param string $value |
||
666 | * @param string $locale |
||
667 | * |
||
668 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
669 | */ |
||
670 | View Code Duplication | public function scopeOrWhereTranslationLike(Builder $query, $key, $value, $locale = null) |
|
679 | |||
680 | /** |
||
681 | * This scope sorts results by the given translation field. |
||
682 | * |
||
683 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
684 | * @param string $key |
||
685 | * @param string $sortmethod |
||
686 | * |
||
687 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
688 | */ |
||
689 | public function scopeOrderByTranslation(Builder $query, $key, $sortmethod = 'asc') |
||
706 | |||
707 | /** |
||
708 | * @return array |
||
709 | */ |
||
710 | public function attributesToArray() |
||
732 | |||
733 | /** |
||
734 | * @return array |
||
735 | */ |
||
736 | public function getTranslationsArray() |
||
748 | |||
749 | /** |
||
750 | * @return string |
||
751 | */ |
||
752 | private function getTranslationsTable() |
||
756 | |||
757 | /** |
||
758 | * @return string |
||
759 | */ |
||
760 | protected function locale() |
||
769 | |||
770 | /** |
||
771 | * Set the default locale on the model. |
||
772 | * |
||
773 | * @param $locale |
||
774 | * |
||
775 | * @return $this |
||
776 | */ |
||
777 | public function setDefaultLocale($locale) |
||
783 | |||
784 | /** |
||
785 | * Get the default locale on the model. |
||
786 | * |
||
787 | * @return mixed |
||
788 | */ |
||
789 | public function getDefaultLocale() |
||
793 | |||
794 | /** |
||
795 | * Deletes all translations for this model. |
||
796 | * |
||
797 | * @param string|array|null $locales The locales to be deleted (array or single string) |
||
798 | * (e.g., ["en", "de"] would remove these translations). |
||
799 | */ |
||
800 | public function deleteTranslations($locales = null) |
||
816 | |||
817 | /** |
||
818 | * @param $key |
||
819 | * |
||
820 | * @return array |
||
821 | */ |
||
822 | private function getAttributeAndLocale($key) |
||
830 | |||
831 | /** |
||
832 | * @return bool |
||
833 | */ |
||
834 | private function toArrayAlwaysLoadsTranslations() |
||
838 | } |
||
839 |
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: