We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
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:
1 | <?php |
||
7 | trait HasTranslations |
||
8 | { |
||
9 | use OriginalHasTranslations; |
||
10 | |||
11 | /** |
||
12 | * @var bool |
||
13 | */ |
||
14 | public $locale = false; |
||
15 | |||
16 | /* |
||
17 | |-------------------------------------------------------------------------- |
||
18 | | SPATIE/LARAVEL-TRANSLATABLE OVERWRITES |
||
19 | |-------------------------------------------------------------------------- |
||
20 | */ |
||
21 | |||
22 | /** |
||
23 | * Use the forced locale if present. |
||
24 | * |
||
25 | * @param string $key |
||
26 | * @return mixed |
||
27 | */ |
||
28 | public function getAttributeValue($key) |
||
43 | |||
44 | /* |
||
45 | |-------------------------------------------------------------------------- |
||
46 | | ELOQUENT OVERWRITES |
||
47 | |-------------------------------------------------------------------------- |
||
48 | */ |
||
49 | |||
50 | /** |
||
51 | * Create translated items as json. |
||
52 | * |
||
53 | * @param array $attributes |
||
54 | * @return static |
||
55 | */ |
||
56 | public static function create(array $attributes = []) |
||
57 | { |
||
58 | $locale = $attributes['locale'] ?? \App::getLocale(); |
||
59 | $attributes = array_except($attributes, ['locale']); |
||
60 | $non_translatable = []; |
||
61 | |||
62 | $model = new static(); |
||
63 | |||
64 | // do the actual saving |
||
65 | View Code Duplication | foreach ($attributes as $attribute => $value) { |
|
|
|||
66 | if ($model->isTranslatableAttribute($attribute)) { // the attribute is translatable |
||
67 | $model->setTranslation($attribute, $locale, $value); |
||
68 | } else { // the attribute is NOT translatable |
||
69 | $non_translatable[$attribute] = $value; |
||
70 | } |
||
71 | } |
||
72 | $model->fill($non_translatable)->save(); |
||
73 | |||
74 | return $model; |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * Update translated items as json. |
||
79 | * |
||
80 | * @param array $attributes |
||
81 | * @param array $options |
||
82 | * @return bool |
||
83 | */ |
||
84 | public function update(array $attributes = [], array $options = []) |
||
85 | { |
||
86 | if (! $this->exists) { |
||
87 | return false; |
||
88 | } |
||
89 | |||
90 | $locale = $attributes['locale'] ?? \App::getLocale(); |
||
91 | $attributes = array_except($attributes, ['locale']); |
||
92 | $non_translatable = []; |
||
93 | |||
94 | // do the actual saving |
||
95 | View Code Duplication | foreach ($attributes as $attribute => $value) { |
|
96 | if ($this->isTranslatableAttribute($attribute)) { // the attribute is translatable |
||
97 | $this->setTranslation($attribute, $locale, $value); |
||
98 | } else { // the attribute is NOT translatable |
||
99 | $non_translatable[$attribute] = $value; |
||
100 | } |
||
101 | } |
||
102 | |||
103 | return $this->fill($non_translatable)->save($options); |
||
104 | } |
||
105 | |||
106 | /* |
||
107 | |-------------------------------------------------------------------------- |
||
108 | | CUSTOM METHODS |
||
109 | |-------------------------------------------------------------------------- |
||
110 | */ |
||
111 | |||
112 | /** |
||
113 | * Check if a model is translatable, by the adapter's standards. |
||
114 | * |
||
115 | * @return bool |
||
116 | */ |
||
117 | public function translationEnabledForModel() |
||
121 | |||
122 | /** |
||
123 | * Get all locales the admin is allowed to use. |
||
124 | * |
||
125 | * @return array |
||
126 | */ |
||
127 | public function getAvailableLocales() |
||
131 | |||
132 | /** |
||
133 | * Set the locale property. Used in normalizeLocale() to force the translation |
||
134 | * to a different language that the one set in app()->getLocale();. |
||
135 | * |
||
136 | * @param string |
||
137 | */ |
||
138 | public function setLocale($locale) |
||
142 | |||
143 | /** |
||
144 | * Get the locale property. Used in SpatieTranslatableSluggableService |
||
145 | * to save the slug for the appropriate language. |
||
146 | * |
||
147 | * @param string |
||
148 | */ |
||
149 | public function getLocale() |
||
157 | |||
158 | /** |
||
159 | * Magic method to get the db entries already translated in the wanted locale. |
||
160 | * |
||
161 | * @param string $method |
||
162 | * @param array $parameters |
||
163 | * @return |
||
164 | */ |
||
165 | public function __call($method, $parameters) |
||
200 | } |
||
201 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.