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 |
||
12 | trait Translatable |
||
13 | { |
||
14 | protected $defaultLocale; |
||
15 | |||
16 | /** |
||
17 | * Alias for getTranslation(). |
||
18 | * |
||
19 | * @param string|null $locale |
||
20 | * @param bool $withFallback |
||
21 | * |
||
22 | * @return \Illuminate\Database\Eloquent\Model|null |
||
23 | */ |
||
24 | public function translate($locale = null, $withFallback = false) |
||
25 | { |
||
26 | return $this->getTranslation($locale, $withFallback); |
||
27 | } |
||
28 | |||
29 | /** |
||
30 | * Alias for getTranslation(). |
||
31 | * |
||
32 | * @param string $locale |
||
33 | * |
||
34 | * @return \Illuminate\Database\Eloquent\Model|null |
||
35 | */ |
||
36 | public function translateOrDefault($locale) |
||
40 | |||
41 | /** |
||
42 | * Alias for getTranslationOrNew(). |
||
43 | * |
||
44 | * @param string $locale |
||
45 | * |
||
46 | * @return \Illuminate\Database\Eloquent\Model|null |
||
47 | */ |
||
48 | public function translateOrNew($locale) |
||
52 | |||
53 | /** |
||
54 | * @param string|null $locale |
||
55 | * @param bool $withFallback |
||
56 | * |
||
57 | * @return \Illuminate\Database\Eloquent\Model|null |
||
58 | */ |
||
59 | public function getTranslation($locale = null, $withFallback = null) |
||
60 | { |
||
61 | $configFallbackLocale = $this->getFallbackLocale(); |
||
62 | $locale = $locale ?: $this->locale(); |
||
63 | $withFallback = $withFallback === null ? $this->useFallback() : $withFallback; |
||
64 | $fallbackLocale = $this->getFallbackLocale($locale); |
||
65 | |||
66 | if ($translation = $this->getTranslationByLocaleKey($locale)) { |
||
67 | return $translation; |
||
68 | } |
||
69 | if ($withFallback && $fallbackLocale) { |
||
70 | if ($translation = $this->getTranslationByLocaleKey($fallbackLocale)) { |
||
71 | return $translation; |
||
72 | } |
||
73 | if ($translation = $this->getTranslationByLocaleKey($configFallbackLocale)) { |
||
74 | return $translation; |
||
75 | } |
||
76 | } |
||
77 | |||
78 | return null; |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * @param string|null $locale |
||
83 | * |
||
84 | * @return bool |
||
85 | */ |
||
86 | public function hasTranslation($locale = null) |
||
87 | { |
||
88 | $locale = $locale ?: $this->locale(); |
||
89 | |||
90 | foreach ($this->translations as $translation) { |
||
|
|||
91 | if ($translation->getAttribute($this->getLocaleKey()) == $locale) { |
||
92 | return true; |
||
93 | } |
||
94 | } |
||
95 | |||
96 | return false; |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * @return string |
||
101 | */ |
||
102 | public function getTranslationModelName() |
||
106 | |||
107 | /** |
||
108 | * @return string |
||
109 | */ |
||
110 | public function getTranslationModelNameDefault() |
||
111 | { |
||
112 | $config = app()->make('config'); |
||
113 | |||
114 | return get_class($this).$config->get('translatable.translation_suffix', 'Translation'); |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * @return string |
||
119 | */ |
||
120 | public function getRelationKey() |
||
121 | { |
||
122 | if ($this->translationForeignKey) { |
||
123 | $key = $this->translationForeignKey; |
||
124 | } elseif ($this->primaryKey !== 'id') { |
||
125 | $key = $this->primaryKey; |
||
126 | } else { |
||
127 | $key = $this->getForeignKey(); |
||
128 | } |
||
129 | |||
130 | return $key; |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * @return string |
||
135 | */ |
||
136 | public function getLocaleKey() |
||
137 | { |
||
138 | $config = app()->make('config'); |
||
139 | |||
140 | return $this->localeKey ?: $config->get('translatable.locale_key', 'locale'); |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
145 | */ |
||
146 | public function translations() |
||
150 | |||
151 | /** |
||
152 | * @param string $key |
||
153 | * |
||
154 | * @return mixed |
||
155 | */ |
||
156 | public function getAttribute($key) |
||
157 | { |
||
158 | list($attribute, $locale) = $this->getAttributeAndLocale($key); |
||
159 | |||
160 | if ($this->isTranslationAttribute($attribute)) { |
||
161 | if ($this->getTranslation($locale) === null) { |
||
162 | return null; |
||
163 | } |
||
164 | |||
165 | // If the given $attribute has a mutator, we push it to $attributes and then call getAttributeValue |
||
166 | // on it. This way, we can use Eloquent's checking for Mutation, type casting, and |
||
167 | // Date fields. |
||
168 | if ($this->hasGetMutator($attribute)) { |
||
169 | $this->attributes[$attribute] = $this->getTranslation($locale)->$attribute; |
||
170 | |||
171 | return $this->getAttributeValue($attribute); |
||
172 | } |
||
173 | |||
174 | return $this->getTranslation($locale)->$attribute; |
||
175 | } |
||
176 | |||
177 | return parent::getAttribute($key); |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * @param string $key |
||
182 | * @param mixed $value |
||
183 | * |
||
184 | * @return $this |
||
185 | */ |
||
186 | public function setAttribute($key, $value) |
||
187 | { |
||
188 | list($attribute, $locale) = $this->getAttributeAndLocale($key); |
||
189 | |||
190 | if ($this->isTranslationAttribute($attribute)) { |
||
191 | $this->getTranslationOrNew($locale)->$attribute = $value; |
||
192 | } else { |
||
193 | return parent::setAttribute($key, $value); |
||
194 | } |
||
195 | |||
196 | return $this; |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * @param array $options |
||
201 | * |
||
202 | * @return bool |
||
203 | */ |
||
204 | public function save(array $options = []) |
||
205 | { |
||
206 | if ($this->exists) { |
||
207 | if (count($this->getDirty()) > 0) { |
||
208 | // If $this->exists and dirty, parent::save() has to return true. If not, |
||
209 | // an error has occurred. Therefore we shouldn't save the translations. |
||
210 | if (parent::save($options)) { |
||
211 | return $this->saveTranslations(); |
||
212 | } |
||
213 | |||
214 | return false; |
||
215 | } else { |
||
216 | // If $this->exists and not dirty, parent::save() skips saving and returns |
||
217 | // false. So we have to save the translations |
||
218 | if ($saved = $this->saveTranslations()) { |
||
219 | $this->fireModelEvent('saved', false); |
||
220 | $this->fireModelEvent('updated', false); |
||
221 | } |
||
222 | |||
223 | return $saved; |
||
224 | } |
||
225 | } elseif (parent::save($options)) { |
||
226 | // We save the translations only if the instance is saved in the database. |
||
227 | return $this->saveTranslations(); |
||
228 | } |
||
229 | |||
230 | return false; |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * @param string $locale |
||
235 | * |
||
236 | * @return \Illuminate\Database\Eloquent\Model|null |
||
237 | */ |
||
238 | protected function getTranslationOrNew($locale) |
||
239 | { |
||
240 | if (($translation = $this->getTranslation($locale, false)) === null) { |
||
241 | $translation = $this->getNewTranslation($locale); |
||
242 | } |
||
243 | |||
244 | return $translation; |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * @param array $attributes |
||
249 | * |
||
250 | * @throws \Illuminate\Database\Eloquent\MassAssignmentException |
||
251 | * @return $this |
||
252 | */ |
||
253 | public function fill(array $attributes) |
||
254 | { |
||
255 | foreach ($attributes as $key => $values) { |
||
256 | if ($this->isKeyALocale($key)) { |
||
257 | $this->getTranslationOrNew($key)->fill($values); |
||
258 | unset($attributes[$key]); |
||
259 | } else { |
||
260 | list($attribute, $locale) = $this->getAttributeAndLocale($key); |
||
261 | if ($this->isTranslationAttribute($attribute) and $this->isKeyALocale($locale)) { |
||
262 | $this->getTranslationOrNew($locale)->fill([$attribute => $values]); |
||
263 | unset($attributes[$key]); |
||
264 | } |
||
265 | } |
||
266 | } |
||
267 | |||
268 | return parent::fill($attributes); |
||
269 | } |
||
270 | |||
271 | /** |
||
272 | * @param string $key |
||
273 | */ |
||
274 | private function getTranslationByLocaleKey($key) |
||
275 | { |
||
276 | foreach ($this->translations as $translation) { |
||
277 | if ($translation->getAttribute($this->getLocaleKey()) == $key) { |
||
278 | return $translation; |
||
279 | } |
||
280 | } |
||
281 | |||
282 | return null; |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * @param null $locale |
||
287 | * |
||
288 | * @return string |
||
289 | */ |
||
290 | private function getFallbackLocale($locale = null) |
||
291 | { |
||
292 | if ($locale && $this->isLocaleCountryBased($locale)) { |
||
293 | if ($fallback = $this->getLanguageFromCountryBasedLocale($locale)) { |
||
294 | return $fallback; |
||
295 | } |
||
296 | } |
||
297 | |||
298 | return app()->make('config')->get('translatable.fallback_locale'); |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * @param $locale |
||
303 | * |
||
304 | * @return bool |
||
305 | */ |
||
306 | private function isLocaleCountryBased($locale) |
||
307 | { |
||
308 | return strpos($locale, $this->getLocaleSeparator()) !== false; |
||
309 | } |
||
310 | |||
311 | /** |
||
312 | * @param $locale |
||
313 | * |
||
314 | * @return string |
||
315 | */ |
||
316 | private function getLanguageFromCountryBasedLocale($locale) |
||
317 | { |
||
318 | $parts = explode($this->getLocaleSeparator(), $locale); |
||
319 | |||
320 | return array_get($parts, 0); |
||
321 | } |
||
322 | |||
323 | /** |
||
324 | * @return bool|null |
||
325 | */ |
||
326 | private function useFallback() |
||
327 | { |
||
328 | if (isset($this->useTranslationFallback) && $this->useTranslationFallback !== null) { |
||
329 | return $this->useTranslationFallback; |
||
330 | } |
||
331 | |||
332 | return app()->make('config')->get('translatable.use_fallback'); |
||
333 | } |
||
334 | |||
335 | /** |
||
336 | * @param string $key |
||
337 | * |
||
338 | * @return bool |
||
339 | */ |
||
340 | public function isTranslationAttribute($key) |
||
341 | { |
||
342 | return in_array($key, $this->translatedAttributes); |
||
343 | } |
||
344 | |||
345 | /** |
||
346 | * @param string $key |
||
347 | * |
||
348 | * @throws \Dimsav\Translatable\Exception\LocalesNotDefinedException |
||
349 | * @return bool |
||
350 | */ |
||
351 | protected function isKeyALocale($key) |
||
352 | { |
||
353 | $locales = $this->getLocales(); |
||
354 | |||
355 | return in_array($key, $locales); |
||
356 | } |
||
357 | |||
358 | /** |
||
359 | * @throws \Dimsav\Translatable\Exception\LocalesNotDefinedException |
||
360 | * @return array |
||
361 | */ |
||
362 | protected function getLocales() |
||
363 | { |
||
364 | $localesConfig = (array) app()->make('config')->get('translatable.locales'); |
||
365 | |||
366 | if (empty($localesConfig)) { |
||
367 | throw new LocalesNotDefinedException('Please make sure you have run "php artisan config:publish dimsav/laravel-translatable" '. |
||
368 | ' and that the locales configuration is defined.'); |
||
369 | } |
||
370 | |||
371 | $locales = []; |
||
372 | foreach ($localesConfig as $key => $locale) { |
||
373 | if (is_array($locale)) { |
||
374 | $locales[] = $key; |
||
375 | foreach ($locale as $countryLocale) { |
||
376 | $locales[] = $key.$this->getLocaleSeparator().$countryLocale; |
||
377 | } |
||
378 | } else { |
||
379 | $locales[] = $locale; |
||
380 | } |
||
381 | } |
||
382 | |||
383 | return $locales; |
||
384 | } |
||
385 | |||
386 | /** |
||
387 | * @return string |
||
388 | */ |
||
389 | protected function getLocaleSeparator() |
||
390 | { |
||
391 | return app()->make('config')->get('translatable.locale_separator', '-'); |
||
392 | } |
||
393 | |||
394 | /** |
||
395 | * @return bool |
||
396 | */ |
||
397 | protected function saveTranslations() |
||
398 | { |
||
399 | $saved = true; |
||
400 | foreach ($this->translations as $translation) { |
||
401 | if ($saved && $this->isTranslationDirty($translation)) { |
||
402 | $translation->setAttribute($this->getRelationKey(), $this->getKey()); |
||
403 | $saved = $translation->save(); |
||
404 | } |
||
405 | } |
||
406 | |||
407 | return $saved; |
||
408 | } |
||
409 | |||
410 | /** |
||
411 | * @param \Illuminate\Database\Eloquent\Model $translation |
||
412 | * |
||
413 | * @return bool |
||
414 | */ |
||
415 | protected function isTranslationDirty(Model $translation) |
||
416 | { |
||
417 | $dirtyAttributes = $translation->getDirty(); |
||
418 | unset($dirtyAttributes[$this->getLocaleKey()]); |
||
419 | |||
420 | return count($dirtyAttributes) > 0; |
||
421 | } |
||
422 | |||
423 | /** |
||
424 | * @param string $locale |
||
425 | * |
||
426 | * @return \Illuminate\Database\Eloquent\Model |
||
427 | */ |
||
428 | public function getNewTranslation($locale) |
||
429 | { |
||
430 | $modelName = $this->getTranslationModelName(); |
||
431 | $translation = new $modelName(); |
||
432 | $translation->setAttribute($this->getLocaleKey(), $locale); |
||
433 | $this->translations->add($translation); |
||
434 | |||
435 | return $translation; |
||
436 | } |
||
437 | |||
438 | /** |
||
439 | * @param $key |
||
440 | * |
||
441 | * @return bool |
||
442 | */ |
||
443 | public function __isset($key) |
||
444 | { |
||
445 | return $this->isTranslationAttribute($key) || parent::__isset($key); |
||
446 | } |
||
447 | |||
448 | /** |
||
449 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
450 | * @param string $locale |
||
451 | * |
||
452 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
453 | */ |
||
454 | View Code Duplication | public function scopeTranslatedIn(Builder $query, $locale = null) |
|
455 | { |
||
456 | $locale = $locale ?: $this->locale(); |
||
457 | |||
458 | return $query->whereHas('translations', function (Builder $q) use ($locale) { |
||
459 | $q->where($this->getLocaleKey(), '=', $locale); |
||
460 | }); |
||
461 | } |
||
462 | |||
463 | /** |
||
464 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
465 | * @param string $locale |
||
466 | * |
||
467 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
468 | */ |
||
469 | View Code Duplication | public function scopeNotTranslatedIn(Builder $query, $locale = null) |
|
470 | { |
||
471 | $locale = $locale ?: $this->locale(); |
||
472 | |||
473 | return $query->whereDoesntHave('translations', function (Builder $q) use ($locale) { |
||
474 | $q->where($this->getLocaleKey(), '=', $locale); |
||
475 | }); |
||
476 | } |
||
477 | |||
478 | /** |
||
479 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
480 | * |
||
481 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
482 | */ |
||
483 | public function scopeTranslated(Builder $query) |
||
484 | { |
||
485 | return $query->has('translations'); |
||
486 | } |
||
487 | |||
488 | /** |
||
489 | * Adds scope to get a list of translated attributes, using the current locale. |
||
490 | * Example usage: Country::listsTranslations('name')->get()->toArray() |
||
491 | * Will return an array with items: |
||
492 | * [ |
||
493 | * 'id' => '1', // The id of country |
||
494 | * 'name' => 'Griechenland' // The translated name |
||
495 | * ]. |
||
496 | * |
||
497 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
498 | * @param string $translationField |
||
499 | */ |
||
500 | public function scopeListsTranslations(Builder $query, $translationField) |
||
501 | { |
||
502 | $withFallback = $this->useFallback(); |
||
503 | $translationTable = $this->getTranslationsTable(); |
||
504 | $localeKey = $this->getLocaleKey(); |
||
505 | |||
506 | $query |
||
507 | ->select($this->getTable().'.'.$this->getKeyName(), $translationTable.'.'.$translationField) |
||
508 | ->leftJoin($translationTable, $translationTable.'.'.$this->getRelationKey(), '=', $this->getTable().'.'.$this->getKeyName()) |
||
509 | ->where($translationTable.'.'.$localeKey, $this->locale()); |
||
510 | if ($withFallback) { |
||
511 | $query->orWhere(function (Builder $q) use ($translationTable, $localeKey) { |
||
512 | $q->where($translationTable.'.'.$localeKey, $this->getFallbackLocale()) |
||
513 | ->whereNotIn($translationTable.'.'.$this->getRelationKey(), function (QueryBuilder $q) use ( |
||
514 | $translationTable, |
||
515 | $localeKey |
||
516 | ) { |
||
517 | $q->select($translationTable.'.'.$this->getRelationKey()) |
||
518 | ->from($translationTable) |
||
519 | ->where($translationTable.'.'.$localeKey, $this->locale()); |
||
520 | }); |
||
521 | }); |
||
522 | } |
||
523 | } |
||
524 | |||
525 | /** |
||
526 | * This scope eager loads the translations for the default and the fallback locale only. |
||
527 | * We can use this as a shortcut to improve performance in our application. |
||
528 | * |
||
529 | * @param Builder $query |
||
530 | */ |
||
531 | public function scopeWithTranslation(Builder $query) |
||
532 | { |
||
533 | $query->with([ |
||
534 | 'translations' => function (Relation $query) { |
||
535 | $query->where($this->getTranslationsTable().'.'.$this->getLocaleKey(), $this->locale()); |
||
536 | |||
537 | if ($this->useFallback()) { |
||
538 | return $query->orWhere($this->getTranslationsTable().'.'.$this->getLocaleKey(), $this->getFallbackLocale()); |
||
539 | } |
||
540 | }, |
||
541 | ]); |
||
542 | } |
||
543 | |||
544 | /** |
||
545 | * This scope filters results by checking the translation fields. |
||
546 | * |
||
547 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
548 | * @param string $key |
||
549 | * @param string $value |
||
550 | * @param string $locale |
||
551 | * |
||
552 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
553 | */ |
||
554 | View Code Duplication | public function scopeWhereTranslation(Builder $query, $key, $value, $locale = null) |
|
563 | |||
564 | /** |
||
565 | * This scope filters results by checking the translation fields. |
||
566 | * |
||
567 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
568 | * @param string $key |
||
569 | * @param string $value |
||
570 | * @param string $locale |
||
571 | * |
||
572 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
573 | */ |
||
574 | View Code Duplication | public function scopeWhereTranslationLike(Builder $query, $key, $value, $locale = null) |
|
583 | |||
584 | /** |
||
585 | * @return array |
||
586 | */ |
||
587 | public function toArray() |
||
588 | { |
||
589 | $attributes = parent::toArray(); |
||
590 | |||
591 | if ($this->relationLoaded('translations') || $this->toArrayAlwaysLoadsTranslations()) { |
||
592 | // continue |
||
593 | } else { |
||
594 | return $attributes; |
||
595 | } |
||
596 | |||
597 | $hiddenAttributes = $this->getHidden(); |
||
598 | |||
599 | foreach ($this->translatedAttributes as $field) { |
||
600 | if (in_array($field, $hiddenAttributes)) { |
||
601 | continue; |
||
602 | } |
||
603 | |||
604 | if ($translations = $this->getTranslation()) { |
||
605 | $attributes[$field] = $translations->$field; |
||
606 | } |
||
607 | } |
||
608 | |||
609 | return $attributes; |
||
610 | } |
||
611 | |||
612 | /** |
||
613 | * @return string |
||
614 | */ |
||
615 | private function getTranslationsTable() |
||
619 | |||
620 | /** |
||
621 | * @return string |
||
622 | */ |
||
623 | protected function locale() |
||
624 | { |
||
625 | if ($this->defaultLocale) { |
||
626 | return $this->defaultLocale; |
||
627 | } |
||
628 | |||
629 | return app()->make('config')->get('translatable.locale') |
||
630 | ?: app()->make('translator')->getLocale(); |
||
631 | } |
||
632 | |||
633 | /** |
||
634 | * Set the default locale on the model. |
||
635 | * |
||
636 | * @param $locale |
||
637 | * |
||
638 | * @return $this |
||
639 | */ |
||
640 | public function setDefaultLocale($locale) |
||
641 | { |
||
642 | $this->defaultLocale = $locale; |
||
643 | |||
644 | return $this; |
||
645 | } |
||
646 | |||
647 | /** |
||
648 | * Get the default locale on the model. |
||
649 | * |
||
650 | * @return mixed |
||
651 | */ |
||
652 | public function getDefaultLocale() |
||
656 | |||
657 | /** |
||
658 | * Deletes all translations for this model. |
||
659 | * |
||
660 | * @param string|array|null $locales The locales to be deleted (array or single string) |
||
661 | * (e.g., ["en", "de"] would remove these translations). |
||
662 | */ |
||
663 | public function deleteTranslations($locales = null) |
||
676 | |||
677 | /** |
||
678 | * @param $key |
||
679 | * |
||
680 | * @return array |
||
681 | */ |
||
682 | private function getAttributeAndLocale($key) |
||
690 | |||
691 | /** |
||
692 | * @return bool |
||
693 | */ |
||
694 | private function toArrayAlwaysLoadsTranslations() |
||
698 | } |
||
699 |
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: