Complex classes like HasTranslations 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 HasTranslations, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | trait HasTranslations |
||
11 | { |
||
12 | public function getAttributeValue($key) |
||
20 | |||
21 | public function getRawOriginal($key = null, $default = null) |
||
22 | { |
||
23 | if (is_null($key)) { |
||
24 | return Arr::get($this->original, $key, $default); |
||
|
|||
25 | } |
||
26 | |||
27 | if (! $this->isTranslatableAttribute($key)) { |
||
28 | return Arr::get($this->original, $key, $default); |
||
29 | } |
||
30 | |||
31 | return $this->getRawOriginalTranslation($key, $this->getLocale()); |
||
32 | } |
||
33 | |||
34 | public function setAttribute($key, $value) |
||
45 | |||
46 | public function translate(string $key, string $locale = '', bool $useFallbackLocale = true): string |
||
50 | |||
51 | public function getRawOriginalTranslation(string $key, string $locale, bool $useFallbackLocale = true) |
||
62 | |||
63 | public function getTranslation(string $key, string $locale, bool $useFallbackLocale = true) |
||
73 | |||
74 | public function getTranslationWithFallback(string $key, string $locale): string |
||
78 | |||
79 | public function getTranslationWithoutFallback(string $key, string $locale) |
||
83 | |||
84 | public function getTranslations(string $key = null): array |
||
100 | |||
101 | public function setTranslation(string $key, string $locale, $value): self |
||
125 | |||
126 | public function setTranslations(string $key, array $translations): self |
||
136 | |||
137 | public function forgetTranslation(string $key, string $locale): self |
||
150 | |||
151 | public function forgetAllTranslations(string $locale): self |
||
159 | |||
160 | public function getTranslatedLocales(string $key): array |
||
164 | |||
165 | public function isTranslatableAttribute(string $key): bool |
||
169 | |||
170 | public function hasTranslation(string $key, string $locale = null): bool |
||
176 | |||
177 | protected function guardAgainstNonTranslatableAttribute(string $key) |
||
183 | |||
184 | protected function normalizeLocale(string $key, string $locale, bool $useFallbackLocale): string |
||
204 | |||
205 | protected function getLocale(): string |
||
209 | |||
210 | public function getTranslatableAttributes(): array |
||
216 | |||
217 | public function getTranslationsAttribute(): array |
||
225 | |||
226 | public function getCasts(): array |
||
233 | } |
||
234 |
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: