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){ |
||
| 61 | |||
| 62 | public function getTranslation(string $key, string $locale, bool $useFallbackLocale = true) |
||
| 72 | |||
| 73 | public function getTranslationWithFallback(string $key, string $locale): string |
||
| 77 | |||
| 78 | public function getTranslationWithoutFallback(string $key, string $locale) |
||
| 82 | |||
| 83 | public function getTranslations(string $key = null): array |
||
| 99 | |||
| 100 | public function setTranslation(string $key, string $locale, $value): self |
||
| 124 | |||
| 125 | public function setTranslations(string $key, array $translations): self |
||
| 135 | |||
| 136 | public function forgetTranslation(string $key, string $locale): self |
||
| 149 | |||
| 150 | public function forgetAllTranslations(string $locale): self |
||
| 158 | |||
| 159 | public function getTranslatedLocales(string $key): array |
||
| 163 | |||
| 164 | public function isTranslatableAttribute(string $key): bool |
||
| 168 | |||
| 169 | public function hasTranslation(string $key, string $locale = null): bool |
||
| 175 | |||
| 176 | protected function guardAgainstNonTranslatableAttribute(string $key) |
||
| 182 | |||
| 183 | protected function normalizeLocale(string $key, string $locale, bool $useFallbackLocale): string |
||
| 203 | |||
| 204 | protected function getLocale(): string |
||
| 208 | |||
| 209 | public function getTranslatableAttributes(): array |
||
| 215 | |||
| 216 | public function getTranslationsAttribute(): array |
||
| 224 | |||
| 225 | public function getCasts(): array |
||
| 232 | } |
||
| 233 |
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: