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 = []) |
||
263 | |||
264 | /** |
||
265 | * @param string $locale |
||
266 | * |
||
267 | * @return \Illuminate\Database\Eloquent\Model|null |
||
268 | */ |
||
269 | protected function getTranslationOrNew($locale) |
||
277 | |||
278 | /** |
||
279 | * @param array $attributes |
||
280 | * |
||
281 | * @throws \Illuminate\Database\Eloquent\MassAssignmentException |
||
282 | * @return $this |
||
283 | */ |
||
284 | public function fill(array $attributes) |
||
301 | |||
302 | /** |
||
303 | * @param string $key |
||
304 | */ |
||
305 | private function getTranslationByLocaleKey($key) |
||
315 | |||
316 | /** |
||
317 | * @param null $locale |
||
318 | * |
||
319 | * @return string |
||
320 | */ |
||
321 | private function getFallbackLocale($locale = null) |
||
331 | |||
332 | /** |
||
333 | * @param $locale |
||
334 | * |
||
335 | * @return bool |
||
336 | */ |
||
337 | private function isLocaleCountryBased($locale) |
||
341 | |||
342 | /** |
||
343 | * @param $locale |
||
344 | * |
||
345 | * @return string |
||
346 | */ |
||
347 | private function getLanguageFromCountryBasedLocale($locale) |
||
353 | |||
354 | /** |
||
355 | * @return bool|null |
||
356 | */ |
||
357 | private function useFallback() |
||
365 | |||
366 | /** |
||
367 | * @param string $key |
||
368 | * |
||
369 | * @return bool |
||
370 | */ |
||
371 | public function isTranslationAttribute($key) |
||
375 | |||
376 | /** |
||
377 | * @param string $key |
||
378 | * |
||
379 | * @throws \Dimsav\Translatable\Exception\LocalesNotDefinedException |
||
380 | * @return bool |
||
381 | */ |
||
382 | protected function isKeyALocale($key) |
||
388 | |||
389 | /** |
||
390 | * @throws \Dimsav\Translatable\Exception\LocalesNotDefinedException |
||
391 | * @return array |
||
392 | */ |
||
393 | protected function getLocales() |
||
416 | |||
417 | /** |
||
418 | * @return string |
||
419 | */ |
||
420 | protected function getLocaleSeparator() |
||
424 | |||
425 | /** |
||
426 | * @return bool |
||
427 | */ |
||
428 | protected function saveTranslations() |
||
444 | |||
445 | /** |
||
446 | * @param array |
||
447 | * |
||
448 | * @return \Illuminate\Database\Eloquent\Model |
||
449 | */ |
||
450 | public function replicateWithTranslations(array $except = null) |
||
462 | |||
463 | /** |
||
464 | * @param \Illuminate\Database\Eloquent\Model $translation |
||
465 | * |
||
466 | * @return bool |
||
467 | */ |
||
468 | protected function isTranslationDirty(Model $translation) |
||
475 | |||
476 | /** |
||
477 | * @param string $locale |
||
478 | * |
||
479 | * @return \Illuminate\Database\Eloquent\Model |
||
480 | */ |
||
481 | public function getNewTranslation($locale) |
||
490 | |||
491 | /** |
||
492 | * @param $key |
||
493 | * |
||
494 | * @return bool |
||
495 | */ |
||
496 | public function __isset($key) |
||
500 | |||
501 | /** |
||
502 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
503 | * @param string $locale |
||
504 | * |
||
505 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
506 | */ |
||
507 | View Code Duplication | public function scopeTranslatedIn(Builder $query, $locale = null) |
|
515 | |||
516 | /** |
||
517 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
518 | * @param string $locale |
||
519 | * |
||
520 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
521 | */ |
||
522 | View Code Duplication | public function scopeNotTranslatedIn(Builder $query, $locale = null) |
|
530 | |||
531 | /** |
||
532 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
533 | * |
||
534 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
535 | */ |
||
536 | public function scopeTranslated(Builder $query) |
||
540 | |||
541 | /** |
||
542 | * Adds scope to get a list of translated attributes, using the current locale. |
||
543 | * Example usage: Country::listsTranslations('name')->get()->toArray() |
||
544 | * Will return an array with items: |
||
545 | * [ |
||
546 | * 'id' => '1', // The id of country |
||
547 | * 'name' => 'Griechenland' // The translated name |
||
548 | * ]. |
||
549 | * |
||
550 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
551 | * @param string $translationField |
||
552 | */ |
||
553 | public function scopeListsTranslations(Builder $query, $translationField) |
||
577 | |||
578 | /** |
||
579 | * This scope eager loads the translations for the default and the fallback locale only. |
||
580 | * We can use this as a shortcut to improve performance in our application. |
||
581 | * |
||
582 | * @param Builder $query |
||
583 | */ |
||
584 | public function scopeWithTranslation(Builder $query) |
||
600 | |||
601 | /** |
||
602 | * This scope filters results by checking the translation fields. |
||
603 | * |
||
604 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
605 | * @param string $key |
||
606 | * @param string $value |
||
607 | * @param string $locale |
||
608 | * |
||
609 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
610 | */ |
||
611 | View Code Duplication | public function scopeWhereTranslation(Builder $query, $key, $value, $locale = null) |
|
620 | |||
621 | /** |
||
622 | * This scope filters results by checking the translation fields. |
||
623 | * |
||
624 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
625 | * @param string $key |
||
626 | * @param string $value |
||
627 | * @param string $locale |
||
628 | * |
||
629 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
630 | */ |
||
631 | View Code Duplication | public function scopeOrWhereTranslation(Builder $query, $key, $value, $locale = null) |
|
640 | |||
641 | /** |
||
642 | * This scope filters results by checking the translation fields. |
||
643 | * |
||
644 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
645 | * @param string $key |
||
646 | * @param string $value |
||
647 | * @param string $locale |
||
648 | * |
||
649 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
650 | */ |
||
651 | View Code Duplication | public function scopeWhereTranslationLike(Builder $query, $key, $value, $locale = null) |
|
660 | |||
661 | /** |
||
662 | * This scope filters results by checking the translation fields. |
||
663 | * |
||
664 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
665 | * @param string $key |
||
666 | * @param string $value |
||
667 | * @param string $locale |
||
668 | * |
||
669 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
670 | */ |
||
671 | View Code Duplication | public function scopeOrWhereTranslationLike(Builder $query, $key, $value, $locale = null) |
|
680 | |||
681 | /** |
||
682 | * @return array |
||
683 | */ |
||
684 | public function attributesToArray() |
||
706 | |||
707 | /** |
||
708 | * @return array |
||
709 | */ |
||
710 | public function getTranslationsArray() |
||
722 | |||
723 | /** |
||
724 | * @return string |
||
725 | */ |
||
726 | private function getTranslationsTable() |
||
730 | |||
731 | /** |
||
732 | * @return string |
||
733 | */ |
||
734 | protected function locale() |
||
743 | |||
744 | /** |
||
745 | * Set the default locale on the model. |
||
746 | * |
||
747 | * @param $locale |
||
748 | * |
||
749 | * @return $this |
||
750 | */ |
||
751 | public function setDefaultLocale($locale) |
||
757 | |||
758 | /** |
||
759 | * Get the default locale on the model. |
||
760 | * |
||
761 | * @return mixed |
||
762 | */ |
||
763 | public function getDefaultLocale() |
||
767 | |||
768 | /** |
||
769 | * Deletes all translations for this model. |
||
770 | * |
||
771 | * @param string|array|null $locales The locales to be deleted (array or single string) |
||
772 | * (e.g., ["en", "de"] would remove these translations). |
||
773 | */ |
||
774 | public function deleteTranslations($locales = null) |
||
790 | |||
791 | /** |
||
792 | * @param $key |
||
793 | * |
||
794 | * @return array |
||
795 | */ |
||
796 | private function getAttributeAndLocale($key) |
||
804 | |||
805 | /** |
||
806 | * @return bool |
||
807 | */ |
||
808 | private function toArrayAlwaysLoadsTranslations() |
||
812 | } |
||
813 |
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: