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 = []) |
||
253 | |||
254 | /** |
||
255 | * @param string $locale |
||
256 | * |
||
257 | * @return \Illuminate\Database\Eloquent\Model|null |
||
258 | */ |
||
259 | protected function getTranslationOrNew($locale) |
||
267 | |||
268 | /** |
||
269 | * @param array $attributes |
||
270 | * |
||
271 | * @throws \Illuminate\Database\Eloquent\MassAssignmentException |
||
272 | * @return $this |
||
273 | */ |
||
274 | public function fill(array $attributes) |
||
291 | |||
292 | /** |
||
293 | * @param string $key |
||
294 | */ |
||
295 | private function getTranslationByLocaleKey($key) |
||
305 | |||
306 | /** |
||
307 | * @param null $locale |
||
308 | * |
||
309 | * @return string |
||
310 | */ |
||
311 | private function getFallbackLocale($locale = null) |
||
321 | |||
322 | /** |
||
323 | * @param $locale |
||
324 | * |
||
325 | * @return bool |
||
326 | */ |
||
327 | private function isLocaleCountryBased($locale) |
||
331 | |||
332 | /** |
||
333 | * @param $locale |
||
334 | * |
||
335 | * @return string |
||
336 | */ |
||
337 | private function getLanguageFromCountryBasedLocale($locale) |
||
343 | |||
344 | /** |
||
345 | * @return bool|null |
||
346 | */ |
||
347 | private function useFallback() |
||
355 | |||
356 | /** |
||
357 | * @param string $key |
||
358 | * |
||
359 | * @return bool |
||
360 | */ |
||
361 | public function isTranslationAttribute($key) |
||
365 | |||
366 | /** |
||
367 | * @param string $key |
||
368 | * |
||
369 | * @throws \Dimsav\Translatable\Exception\LocalesNotDefinedException |
||
370 | * @return bool |
||
371 | */ |
||
372 | protected function isKeyALocale($key) |
||
378 | |||
379 | /** |
||
380 | * @throws \Dimsav\Translatable\Exception\LocalesNotDefinedException |
||
381 | * @return array |
||
382 | */ |
||
383 | protected function getLocales() |
||
406 | |||
407 | /** |
||
408 | * @return string |
||
409 | */ |
||
410 | protected function getLocaleSeparator() |
||
414 | |||
415 | /** |
||
416 | * @return bool |
||
417 | */ |
||
418 | protected function saveTranslations() |
||
434 | |||
435 | /** |
||
436 | * @param array |
||
437 | * |
||
438 | * @return \Illuminate\Database\Eloquent\Model |
||
439 | */ |
||
440 | public function replicateWithTranslations(array $except = null) |
||
452 | |||
453 | /** |
||
454 | * @param \Illuminate\Database\Eloquent\Model $translation |
||
455 | * |
||
456 | * @return bool |
||
457 | */ |
||
458 | protected function isTranslationDirty(Model $translation) |
||
482 | |||
483 | /** |
||
484 | * Get the attributes that have been changed since last sync. |
||
485 | * |
||
486 | * @return array |
||
487 | */ |
||
488 | public function getDirtyWithTranslations() |
||
512 | |||
513 | /** |
||
514 | * @param string $locale |
||
515 | * |
||
516 | * @return \Illuminate\Database\Eloquent\Model |
||
517 | */ |
||
518 | public function getNewTranslation($locale) |
||
527 | |||
528 | /** |
||
529 | * @param $key |
||
530 | * |
||
531 | * @return bool |
||
532 | */ |
||
533 | public function __isset($key) |
||
537 | |||
538 | /** |
||
539 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
540 | * @param string $locale |
||
541 | * |
||
542 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
543 | */ |
||
544 | View Code Duplication | public function scopeTranslatedIn(Builder $query, $locale = null) |
|
552 | |||
553 | /** |
||
554 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
555 | * @param string $locale |
||
556 | * |
||
557 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
558 | */ |
||
559 | View Code Duplication | public function scopeNotTranslatedIn(Builder $query, $locale = null) |
|
567 | |||
568 | /** |
||
569 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
570 | * |
||
571 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
572 | */ |
||
573 | public function scopeTranslated(Builder $query) |
||
577 | |||
578 | /** |
||
579 | * Adds scope to get a list of translated attributes, using the current locale. |
||
580 | * Example usage: Country::listsTranslations('name')->get()->toArray() |
||
581 | * Will return an array with items: |
||
582 | * [ |
||
583 | * 'id' => '1', // The id of country |
||
584 | * 'name' => 'Griechenland' // The translated name |
||
585 | * ]. |
||
586 | * |
||
587 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
588 | * @param string $translationField |
||
589 | */ |
||
590 | public function scopeListsTranslations(Builder $query, $translationField) |
||
614 | |||
615 | /** |
||
616 | * This scope eager loads the translations for the default and the fallback locale only. |
||
617 | * We can use this as a shortcut to improve performance in our application. |
||
618 | * |
||
619 | * @param Builder $query |
||
620 | */ |
||
621 | public function scopeWithTranslation(Builder $query) |
||
633 | |||
634 | /** |
||
635 | * This scope filters results by checking the translation fields. |
||
636 | * |
||
637 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
638 | * @param string $key |
||
639 | * @param string $value |
||
640 | * @param string $locale |
||
641 | * |
||
642 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
643 | */ |
||
644 | View Code Duplication | public function scopeWhereTranslation(Builder $query, $key, $value, $locale = null) |
|
653 | |||
654 | /** |
||
655 | * This scope filters results by checking the translation fields. |
||
656 | * |
||
657 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
658 | * @param string $key |
||
659 | * @param string $value |
||
660 | * @param string $locale |
||
661 | * |
||
662 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
663 | */ |
||
664 | public function scopeOrWhereTranslation(Builder $query, $key, $value, $locale = null) |
||
673 | |||
674 | /** |
||
675 | * This scope filters results by checking the translation fields. |
||
676 | * |
||
677 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
678 | * @param string $key |
||
679 | * @param string $value |
||
680 | * @param string $locale |
||
681 | * |
||
682 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
683 | */ |
||
684 | View Code Duplication | public function scopeWhereTranslationLike(Builder $query, $key, $value, $locale = null) |
|
693 | |||
694 | /** |
||
695 | * This scope filters results by checking the translation fields. |
||
696 | * |
||
697 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
698 | * @param string $key |
||
699 | * @param string $value |
||
700 | * @param string $locale |
||
701 | * |
||
702 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
703 | */ |
||
704 | public function scopeOrWhereTranslationLike(Builder $query, $key, $value, $locale = null) |
||
713 | |||
714 | /** |
||
715 | * @return array |
||
716 | */ |
||
717 | public function toArray() |
||
741 | |||
742 | /** |
||
743 | * @return array |
||
744 | */ |
||
745 | public function getTranslationsArray() |
||
757 | |||
758 | /** |
||
759 | * @return string |
||
760 | */ |
||
761 | private function getTranslationsTable() |
||
765 | |||
766 | /** |
||
767 | * @return string |
||
768 | */ |
||
769 | protected function locale() |
||
778 | |||
779 | /** |
||
780 | * Set the default locale on the model. |
||
781 | * |
||
782 | * @param $locale |
||
783 | * |
||
784 | * @return $this |
||
785 | */ |
||
786 | public function setDefaultLocale($locale) |
||
792 | |||
793 | /** |
||
794 | * Get the default locale on the model. |
||
795 | * |
||
796 | * @return mixed |
||
797 | */ |
||
798 | public function getDefaultLocale() |
||
802 | |||
803 | /** |
||
804 | * Deletes all translations for this model. |
||
805 | * |
||
806 | * @param string|array|null $locales The locales to be deleted (array or single string) |
||
807 | * (e.g., ["en", "de"] would remove these translations). |
||
808 | */ |
||
809 | public function deleteTranslations($locales = null) |
||
825 | |||
826 | /** |
||
827 | * @param $key |
||
828 | * |
||
829 | * @return array |
||
830 | */ |
||
831 | private function getAttributeAndLocale($key) |
||
839 | |||
840 | /** |
||
841 | * @return bool |
||
842 | */ |
||
843 | private function toArrayAlwaysLoadsTranslations() |
||
847 | } |
||
848 |
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: