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() |
||
115 | |||
116 | /** |
||
117 | * @return string |
||
118 | */ |
||
119 | public function getRelationKey() |
||
131 | |||
132 | /** |
||
133 | * @return string |
||
134 | */ |
||
135 | public function getLocaleKey() |
||
141 | |||
142 | /** |
||
143 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
144 | */ |
||
145 | public function translations() |
||
149 | |||
150 | /** |
||
151 | * @return bool |
||
152 | */ |
||
153 | private function usePropertyFallback() |
||
157 | |||
158 | /** |
||
159 | * Returns the attribute value from fallback translation if value of attribute |
||
160 | * is empty and the property fallback is enabled in the configuration. |
||
161 | * in model. |
||
162 | * @param $locale |
||
163 | * @param $attribute |
||
164 | * @return mixed |
||
165 | */ |
||
166 | private function getAttributeOrFallback($locale, $attribute) |
||
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 = []) |
||
250 | |||
251 | /** |
||
252 | * @param string $locale |
||
253 | * |
||
254 | * @return \Illuminate\Database\Eloquent\Model|null |
||
255 | */ |
||
256 | protected function getTranslationOrNew($locale) |
||
264 | |||
265 | /** |
||
266 | * @param array $attributes |
||
267 | * |
||
268 | * @throws \Illuminate\Database\Eloquent\MassAssignmentException |
||
269 | * @return $this |
||
270 | */ |
||
271 | public function fill(array $attributes) |
||
288 | |||
289 | /** |
||
290 | * @param string $key |
||
291 | */ |
||
292 | private function getTranslationByLocaleKey($key) |
||
302 | |||
303 | /** |
||
304 | * @param null $locale |
||
305 | * |
||
306 | * @return string |
||
307 | */ |
||
308 | private function getFallbackLocale($locale = null) |
||
318 | |||
319 | /** |
||
320 | * @param $locale |
||
321 | * |
||
322 | * @return bool |
||
323 | */ |
||
324 | private function isLocaleCountryBased($locale) |
||
328 | |||
329 | /** |
||
330 | * @param $locale |
||
331 | * |
||
332 | * @return string |
||
333 | */ |
||
334 | private function getLanguageFromCountryBasedLocale($locale) |
||
340 | |||
341 | /** |
||
342 | * @return bool|null |
||
343 | */ |
||
344 | private function useFallback() |
||
352 | |||
353 | /** |
||
354 | * @param string $key |
||
355 | * |
||
356 | * @return bool |
||
357 | */ |
||
358 | public function isTranslationAttribute($key) |
||
362 | |||
363 | /** |
||
364 | * @param string $key |
||
365 | * |
||
366 | * @throws \Dimsav\Translatable\Exception\LocalesNotDefinedException |
||
367 | * @return bool |
||
368 | */ |
||
369 | protected function isKeyALocale($key) |
||
375 | |||
376 | /** |
||
377 | * @throws \Dimsav\Translatable\Exception\LocalesNotDefinedException |
||
378 | * @return array |
||
379 | */ |
||
380 | protected function getLocales() |
||
403 | |||
404 | /** |
||
405 | * @return string |
||
406 | */ |
||
407 | protected function getLocaleSeparator() |
||
411 | |||
412 | /** |
||
413 | * @return bool |
||
414 | */ |
||
415 | protected function saveTranslations() |
||
431 | |||
432 | /** |
||
433 | * @param array |
||
434 | * |
||
435 | * @return \Illuminate\Database\Eloquent\Model |
||
436 | */ |
||
437 | public function replicateWithTranslations(array $except = null) |
||
449 | |||
450 | /** |
||
451 | * @param \Illuminate\Database\Eloquent\Model $translation |
||
452 | * |
||
453 | * @return bool |
||
454 | */ |
||
455 | protected function isTranslationDirty(Model $translation) |
||
479 | |||
480 | /** |
||
481 | * Get the attributes that have been changed since last sync. |
||
482 | * |
||
483 | * @return array |
||
484 | */ |
||
485 | public function getDirtyWithTranslations() |
||
509 | |||
510 | /** |
||
511 | * @param string $locale |
||
512 | * |
||
513 | * @return \Illuminate\Database\Eloquent\Model |
||
514 | */ |
||
515 | public function getNewTranslation($locale) |
||
524 | |||
525 | /** |
||
526 | * @param $key |
||
527 | * |
||
528 | * @return bool |
||
529 | */ |
||
530 | public function __isset($key) |
||
534 | |||
535 | /** |
||
536 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
537 | * @param string $locale |
||
538 | * |
||
539 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
540 | */ |
||
541 | View Code Duplication | public function scopeTranslatedIn(Builder $query, $locale = null) |
|
549 | |||
550 | /** |
||
551 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
552 | * @param string $locale |
||
553 | * |
||
554 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
555 | */ |
||
556 | View Code Duplication | public function scopeNotTranslatedIn(Builder $query, $locale = null) |
|
564 | |||
565 | /** |
||
566 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
567 | * |
||
568 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
569 | */ |
||
570 | public function scopeTranslated(Builder $query) |
||
574 | |||
575 | /** |
||
576 | * Adds scope to get a list of translated attributes, using the current locale. |
||
577 | * Example usage: Country::listsTranslations('name')->get()->toArray() |
||
578 | * Will return an array with items: |
||
579 | * [ |
||
580 | * 'id' => '1', // The id of country |
||
581 | * 'name' => 'Griechenland' // The translated name |
||
582 | * ]. |
||
583 | * |
||
584 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
585 | * @param string $translationField |
||
586 | */ |
||
587 | public function scopeListsTranslations(Builder $query, $translationField) |
||
611 | |||
612 | /** |
||
613 | * This scope eager loads the translations for the default and the fallback locale only. |
||
614 | * We can use this as a shortcut to improve performance in our application. |
||
615 | * |
||
616 | * @param Builder $query |
||
617 | */ |
||
618 | public function scopeWithTranslation(Builder $query) |
||
630 | |||
631 | /** |
||
632 | * This scope filters results by checking the translation fields. |
||
633 | * |
||
634 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
635 | * @param string $key |
||
636 | * @param string $value |
||
637 | * @param string $locale |
||
638 | * |
||
639 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
640 | */ |
||
641 | View Code Duplication | public function scopeWhereTranslation(Builder $query, $key, $value, $locale = null) |
|
650 | |||
651 | /** |
||
652 | * This scope filters results by checking the translation fields. |
||
653 | * |
||
654 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
655 | * @param string $key |
||
656 | * @param string $value |
||
657 | * @param string $locale |
||
658 | * |
||
659 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
660 | */ |
||
661 | public function scopeOrWhereTranslation(Builder $query, $key, $value, $locale = null) |
||
670 | |||
671 | /** |
||
672 | * This scope filters results by checking the translation fields. |
||
673 | * |
||
674 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
675 | * @param string $key |
||
676 | * @param string $value |
||
677 | * @param string $locale |
||
678 | * |
||
679 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
680 | */ |
||
681 | View Code Duplication | public function scopeWhereTranslationLike(Builder $query, $key, $value, $locale = null) |
|
690 | |||
691 | /** |
||
692 | * This scope filters results by checking the translation fields. |
||
693 | * |
||
694 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
695 | * @param string $key |
||
696 | * @param string $value |
||
697 | * @param string $locale |
||
698 | * |
||
699 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
700 | */ |
||
701 | public function scopeOrWhereTranslationLike(Builder $query, $key, $value, $locale = null) |
||
710 | |||
711 | /** |
||
712 | * @return array |
||
713 | */ |
||
714 | public function toArray() |
||
738 | |||
739 | /** |
||
740 | * @return array |
||
741 | */ |
||
742 | public function getTranslationsArray() |
||
754 | |||
755 | /** |
||
756 | * @return string |
||
757 | */ |
||
758 | private function getTranslationsTable() |
||
762 | |||
763 | /** |
||
764 | * @return string |
||
765 | */ |
||
766 | protected function locale() |
||
775 | |||
776 | /** |
||
777 | * Set the default locale on the model. |
||
778 | * |
||
779 | * @param $locale |
||
780 | * |
||
781 | * @return $this |
||
782 | */ |
||
783 | public function setDefaultLocale($locale) |
||
789 | |||
790 | /** |
||
791 | * Get the default locale on the model. |
||
792 | * |
||
793 | * @return mixed |
||
794 | */ |
||
795 | public function getDefaultLocale() |
||
799 | |||
800 | /** |
||
801 | * Deletes all translations for this model. |
||
802 | * |
||
803 | * @param string|array|null $locales The locales to be deleted (array or single string) |
||
804 | * (e.g., ["en", "de"] would remove these translations). |
||
805 | */ |
||
806 | public function deleteTranslations($locales = null) |
||
822 | |||
823 | /** |
||
824 | * @param $key |
||
825 | * |
||
826 | * @return array |
||
827 | */ |
||
828 | private function getAttributeAndLocale($key) |
||
836 | |||
837 | /** |
||
838 | * @return bool |
||
839 | */ |
||
840 | private function toArrayAlwaysLoadsTranslations() |
||
844 | } |
||
845 |
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: