Complex classes like TranslateableTrait 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 TranslateableTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | trait TranslateableTrait |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * Fallback translation object. The model will use |
||
| 18 | * the value from this object, if there was no |
||
| 19 | * translated attribute value for current locale. |
||
| 20 | * |
||
| 21 | * @var \RichanFongdasen\I18n\Eloquent\TranslationModel |
||
| 22 | */ |
||
| 23 | protected $fallbackTranslation; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Current selected locale. |
||
| 27 | * |
||
| 28 | * @var \RichanFongdasen\I18n\Locale |
||
| 29 | */ |
||
| 30 | protected $locale; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Default language key. |
||
| 34 | * |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | protected static $localeKey; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Translation object for the current selected |
||
| 41 | * locale. |
||
| 42 | * |
||
| 43 | * @var \RichanFongdasen\I18n\Eloquent\TranslationModel |
||
| 44 | */ |
||
| 45 | protected $translation; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Convert the model's attributes to an array. |
||
| 49 | * |
||
| 50 | * @return array |
||
| 51 | */ |
||
| 52 | public function attributesToArray() |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Boot the TranslateableTrait model extension. |
||
| 65 | * |
||
| 66 | * @return void |
||
| 67 | */ |
||
| 68 | public static function bootTranslateableTrait() |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Create a new translation for the given locale. |
||
| 77 | * |
||
| 78 | * @param \RichanFongdasen\I18n\Locale $locale |
||
| 79 | * |
||
| 80 | * @return \RichanFongdasen\I18n\Eloquent\TranslationModel |
||
| 81 | */ |
||
| 82 | protected function createTranslation(Locale $locale) |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Fill the model with an array of attributes. |
||
| 103 | * |
||
| 104 | * @param array $attributes |
||
| 105 | * |
||
| 106 | * @return $this |
||
| 107 | */ |
||
| 108 | public function fill(array $attributes) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Get an attribute from the model. |
||
| 121 | * |
||
| 122 | * @param string $key |
||
| 123 | * |
||
| 124 | * @return mixed |
||
| 125 | */ |
||
| 126 | public function getAttribute($key) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Get join table attributes. |
||
| 137 | * |
||
| 138 | * @return string[] |
||
| 139 | */ |
||
| 140 | protected function getJoinAttributes() |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Get all of translateable attributes. |
||
| 153 | * |
||
| 154 | * @return array |
||
| 155 | */ |
||
| 156 | public function getTranslateableAttributes() |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Get a translated attribute value from |
||
| 163 | * the model. |
||
| 164 | * |
||
| 165 | * @param string $key |
||
| 166 | * |
||
| 167 | * @return mixed |
||
| 168 | */ |
||
| 169 | protected function getTranslated($key) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Get a translated attribute value from |
||
| 184 | * the given translation model. |
||
| 185 | * |
||
| 186 | * @param mixed $translation |
||
| 187 | * @param string $key |
||
| 188 | * |
||
| 189 | * @return mixed |
||
| 190 | */ |
||
| 191 | protected function getTranslatedValue($translation, $key) |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Get existing translation or create a |
||
| 202 | * new one. |
||
| 203 | * |
||
| 204 | * @param \RichanFongdasen\I18n\Locale $locale |
||
| 205 | * |
||
| 206 | * @return \RichanFongdasen\I18n\Eloquent\TranslationModel |
||
| 207 | */ |
||
| 208 | protected function getTranslation(Locale $locale) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Find locale object based on the given |
||
| 221 | * key value. |
||
| 222 | * |
||
| 223 | * @param mixed $key |
||
| 224 | * |
||
| 225 | * @return \RichanFongdasen\I18n\Locale |
||
| 226 | */ |
||
| 227 | protected function getTranslationLocale($key = null) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Get translation table. |
||
| 246 | * |
||
| 247 | * @return string |
||
| 248 | */ |
||
| 249 | public function getTranslationTable() |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Check whether the given attribute key is |
||
| 262 | * translateable. |
||
| 263 | * |
||
| 264 | * @param string $key |
||
| 265 | * |
||
| 266 | * @return bool |
||
| 267 | */ |
||
| 268 | protected function isTranslateableAttribute($key) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Add and additional scope to join the translation table |
||
| 277 | * and make the translation content more easier to search. |
||
| 278 | * |
||
| 279 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
| 280 | * |
||
| 281 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 282 | */ |
||
| 283 | public function scopeJoinTranslation(Builder $query) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Set a given attribute on the model. |
||
| 298 | * |
||
| 299 | * @param string $key |
||
| 300 | * @param mixed $value |
||
| 301 | * |
||
| 302 | * @return $this |
||
| 303 | */ |
||
| 304 | public function setAttribute($key, $value) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Set fallback translation model. |
||
| 325 | * |
||
| 326 | * @return void |
||
| 327 | */ |
||
| 328 | protected function setFallbackTranslation() |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Set translateable attribute based on the |
||
| 336 | * given key. |
||
| 337 | * |
||
| 338 | * @param string $key |
||
| 339 | * @param mixed $data |
||
| 340 | * @param mixed $locale |
||
| 341 | * |
||
| 342 | * @return $this |
||
| 343 | */ |
||
| 344 | protected function setTranslateableAttribute($key, $data, $locale = null) |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Translate current model. |
||
| 364 | * |
||
| 365 | * @param mixed $key |
||
| 366 | * |
||
| 367 | * @return $this |
||
| 368 | */ |
||
| 369 | public function translate($key = null) |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Define HasMany model relationship |
||
| 385 | * with its translation model. |
||
| 386 | * |
||
| 387 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
| 388 | */ |
||
| 389 | public function translations() |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Get the default foreign key name for the model. |
||
| 404 | * |
||
| 405 | * @return string |
||
| 406 | */ |
||
| 407 | abstract public function getForeignKey(); |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Get the value of the model's primary key. |
||
| 411 | * |
||
| 412 | * @return mixed |
||
| 413 | */ |
||
| 414 | abstract public function getKey(); |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Get the primary key for the model. |
||
| 418 | * |
||
| 419 | * @return string |
||
| 420 | */ |
||
| 421 | abstract public function getKeyName(); |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Get the table associated with the model. |
||
| 425 | * |
||
| 426 | * @return string |
||
| 427 | */ |
||
| 428 | abstract public function getTable(); |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Update the creation and update timestamps. |
||
| 432 | * |
||
| 433 | * @return void |
||
| 434 | */ |
||
| 435 | abstract protected function updateTimestamps(); |
||
| 436 | } |
||
| 437 |