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) |
||
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() |
||
110 | { |
||
111 | return get_class($this).config('translatable.translation_suffix', 'Translation'); |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * @return string |
||
116 | */ |
||
117 | public function getRelationKey() |
||
118 | { |
||
119 | if ($this->translationForeignKey) { |
||
120 | $key = $this->translationForeignKey; |
||
121 | } elseif ($this->primaryKey !== 'id') { |
||
122 | $key = $this->primaryKey; |
||
123 | } else { |
||
124 | $key = $this->getForeignKey(); |
||
125 | } |
||
126 | |||
127 | return $key; |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * @return string |
||
132 | */ |
||
133 | public function getLocaleKey() |
||
134 | { |
||
135 | return $this->localeKey ?: config('translatable.locale_key', 'locale'); |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
140 | */ |
||
141 | public function translations() |
||
142 | { |
||
143 | return $this->hasMany($this->getTranslationModelName(), $this->getRelationKey()); |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * @return bool |
||
148 | */ |
||
149 | private function usePropertyFallback() |
||
150 | { |
||
151 | return config('translatable.use_property_fallback', false); |
||
152 | } |
||
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) |
||
163 | { |
||
164 | $value = $this->getTranslation($locale)->$attribute; |
||
165 | |||
166 | $usePropertyFallback = $this->useFallback() && $this->usePropertyFallback(); |
||
167 | if ( |
||
168 | empty($value) && |
||
169 | $usePropertyFallback && |
||
170 | ($fallback = $this->getTranslation($this->getFallbackLocale(), true)) |
||
171 | ) { |
||
172 | return $fallback->$attribute; |
||
173 | } |
||
174 | |||
175 | return $value; |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * @param string $key |
||
180 | * |
||
181 | * @return mixed |
||
182 | */ |
||
183 | public function getAttribute($key) |
||
206 | |||
207 | /** |
||
208 | * @param string $key |
||
209 | * @param mixed $value |
||
210 | * |
||
211 | * @return $this |
||
212 | */ |
||
213 | public function setAttribute($key, $value) |
||
225 | |||
226 | /** |
||
227 | * @param array $options |
||
228 | * |
||
229 | * @return bool |
||
230 | */ |
||
231 | public function save(array $options = []) |
||
259 | |||
260 | /** |
||
261 | * @param string $locale |
||
262 | * |
||
263 | * @return \Illuminate\Database\Eloquent\Model|null |
||
264 | */ |
||
265 | protected function getTranslationOrNew($locale) |
||
273 | |||
274 | /** |
||
275 | * @param array $attributes |
||
276 | * |
||
277 | * @throws \Illuminate\Database\Eloquent\MassAssignmentException |
||
278 | * @return $this |
||
279 | */ |
||
280 | public function fill(array $attributes) |
||
297 | |||
298 | /** |
||
299 | * @param string $key |
||
300 | */ |
||
301 | private function getTranslationByLocaleKey($key) |
||
311 | |||
312 | /** |
||
313 | * @param null $locale |
||
314 | * |
||
315 | * @return string |
||
316 | */ |
||
317 | private function getFallbackLocale($locale = null) |
||
327 | |||
328 | /** |
||
329 | * @param $locale |
||
330 | * |
||
331 | * @return bool |
||
332 | */ |
||
333 | private function isLocaleCountryBased($locale) |
||
337 | |||
338 | /** |
||
339 | * @param $locale |
||
340 | * |
||
341 | * @return string |
||
342 | */ |
||
343 | private function getLanguageFromCountryBasedLocale($locale) |
||
349 | |||
350 | /** |
||
351 | * @return bool|null |
||
352 | */ |
||
353 | private function useFallback() |
||
361 | |||
362 | /** |
||
363 | * @param string $key |
||
364 | * |
||
365 | * @return bool |
||
366 | */ |
||
367 | public function isTranslationAttribute($key) |
||
371 | |||
372 | /** |
||
373 | * @param string $key |
||
374 | * |
||
375 | * @throws \Dimsav\Translatable\Exception\LocalesNotDefinedException |
||
376 | * @return bool |
||
377 | */ |
||
378 | protected function isKeyALocale($key) |
||
384 | |||
385 | /** |
||
386 | * @throws \Dimsav\Translatable\Exception\LocalesNotDefinedException |
||
387 | * @return array |
||
388 | */ |
||
389 | protected function getLocales() |
||
412 | |||
413 | /** |
||
414 | * @return string |
||
415 | */ |
||
416 | protected function getLocaleSeparator() |
||
420 | |||
421 | /** |
||
422 | * @return bool |
||
423 | */ |
||
424 | protected function saveTranslations() |
||
440 | |||
441 | /** |
||
442 | * @param array |
||
443 | * |
||
444 | * @return \Illuminate\Database\Eloquent\Model |
||
445 | */ |
||
446 | public function replicateWithTranslations(array $except = null) |
||
458 | |||
459 | /** |
||
460 | * @param \Illuminate\Database\Eloquent\Model $translation |
||
461 | * |
||
462 | * @return bool |
||
463 | */ |
||
464 | protected function isTranslationDirty(Model $translation) |
||
471 | |||
472 | /** |
||
473 | * @param string $locale |
||
474 | * |
||
475 | * @return \Illuminate\Database\Eloquent\Model |
||
476 | */ |
||
477 | public function getNewTranslation($locale) |
||
486 | |||
487 | /** |
||
488 | * @param $key |
||
489 | * |
||
490 | * @return bool |
||
491 | */ |
||
492 | public function __isset($key) |
||
496 | |||
497 | /** |
||
498 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
499 | * @param string $locale |
||
500 | * |
||
501 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
502 | */ |
||
503 | View Code Duplication | public function scopeTranslatedIn(Builder $query, $locale = null) |
|
511 | |||
512 | /** |
||
513 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
514 | * @param string $locale |
||
515 | * |
||
516 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
517 | */ |
||
518 | View Code Duplication | public function scopeNotTranslatedIn(Builder $query, $locale = null) |
|
526 | |||
527 | /** |
||
528 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
529 | * |
||
530 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
531 | */ |
||
532 | public function scopeTranslated(Builder $query) |
||
536 | |||
537 | /** |
||
538 | * Adds scope to get a list of translated attributes, using the current locale. |
||
539 | * Example usage: Country::listsTranslations('name')->get()->toArray() |
||
540 | * Will return an array with items: |
||
541 | * [ |
||
542 | * 'id' => '1', // The id of country |
||
543 | * 'name' => 'Griechenland' // The translated name |
||
544 | * ]. |
||
545 | * |
||
546 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
547 | * @param string $translationField |
||
548 | */ |
||
549 | public function scopeListsTranslations(Builder $query, $translationField) |
||
573 | |||
574 | /** |
||
575 | * This scope eager loads the translations for the default and the fallback locale only. |
||
576 | * We can use this as a shortcut to improve performance in our application. |
||
577 | * |
||
578 | * @param Builder $query |
||
579 | */ |
||
580 | public function scopeWithTranslation(Builder $query) |
||
596 | |||
597 | /** |
||
598 | * This scope filters results by checking the translation fields. |
||
599 | * |
||
600 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
601 | * @param string $key |
||
602 | * @param string $value |
||
603 | * @param string $locale |
||
604 | * |
||
605 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
606 | */ |
||
607 | View Code Duplication | public function scopeWhereTranslation(Builder $query, $key, $value, $locale = null) |
|
616 | |||
617 | /** |
||
618 | * This scope filters results by checking the translation fields. |
||
619 | * |
||
620 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
621 | * @param string $key |
||
622 | * @param string $value |
||
623 | * @param string $locale |
||
624 | * |
||
625 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
626 | */ |
||
627 | View Code Duplication | public function scopeOrWhereTranslation(Builder $query, $key, $value, $locale = null) |
|
628 | { |
||
629 | return $query->orWhereHas('translations', function (Builder $query) use ($key, $value, $locale) { |
||
630 | $query->where($this->getTranslationsTable().'.'.$key, $value); |
||
631 | if ($locale) { |
||
632 | $query->where($this->getTranslationsTable().'.'.$this->getLocaleKey(), $locale); |
||
633 | } |
||
634 | }); |
||
635 | } |
||
636 | |||
637 | /** |
||
638 | * This scope filters results by checking the translation fields. |
||
639 | * |
||
640 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
641 | * @param string $key |
||
642 | * @param string $value |
||
643 | * @param string $locale |
||
644 | * |
||
645 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
646 | */ |
||
647 | View Code Duplication | public function scopeWhereTranslationLike(Builder $query, $key, $value, $locale = null) |
|
656 | |||
657 | /** |
||
658 | * This scope filters results by checking the translation fields. |
||
659 | * |
||
660 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
661 | * @param string $key |
||
662 | * @param string $value |
||
663 | * @param string $locale |
||
664 | * |
||
665 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
666 | */ |
||
667 | View Code Duplication | public function scopeOrWhereTranslationLike(Builder $query, $key, $value, $locale = null) |
|
676 | |||
677 | /** |
||
678 | * This scope sorts results by the given translation field. |
||
679 | * |
||
680 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
681 | * @param string $key |
||
682 | * @param string $sortmethod |
||
683 | * |
||
684 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
685 | */ |
||
686 | public function scopeOrderByTranslation(Builder $query, $key, $sortmethod = 'asc') |
||
700 | |||
701 | /** |
||
702 | * @return array |
||
703 | */ |
||
704 | public function attributesToArray() |
||
726 | |||
727 | /** |
||
728 | * @return array |
||
729 | */ |
||
730 | public function getTranslationsArray() |
||
742 | |||
743 | /** |
||
744 | * @return string |
||
745 | */ |
||
746 | private function getTranslationsTable() |
||
750 | |||
751 | /** |
||
752 | * @return string |
||
753 | */ |
||
754 | protected function locale() |
||
763 | |||
764 | /** |
||
765 | * Set the default locale on the model. |
||
766 | * |
||
767 | * @param $locale |
||
768 | * |
||
769 | * @return $this |
||
770 | */ |
||
771 | public function setDefaultLocale($locale) |
||
777 | |||
778 | /** |
||
779 | * Get the default locale on the model. |
||
780 | * |
||
781 | * @return mixed |
||
782 | */ |
||
783 | public function getDefaultLocale() |
||
787 | |||
788 | /** |
||
789 | * Deletes all translations for this model. |
||
790 | * |
||
791 | * @param string|array|null $locales The locales to be deleted (array or single string) |
||
792 | * (e.g., ["en", "de"] would remove these translations). |
||
793 | */ |
||
794 | public function deleteTranslations($locales = null) |
||
810 | |||
811 | /** |
||
812 | * @param $key |
||
813 | * |
||
814 | * @return array |
||
815 | */ |
||
816 | private function getAttributeAndLocale($key) |
||
824 | |||
825 | /** |
||
826 | * @return bool |
||
827 | */ |
||
828 | private function toArrayAlwaysLoadsTranslations() |
||
832 | } |
||
833 |
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: