We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Complex classes like Fields 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 Fields, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | trait Fields |
||
6 | { |
||
7 | // ------------ |
||
8 | // FIELDS |
||
9 | // ------------ |
||
10 | |||
11 | /** |
||
12 | * Add a field to the create/update form or both. |
||
13 | * |
||
14 | * @param string|array $field The new field. |
||
15 | * @param string $form The CRUD form. Can be 'create', 'update' or 'both'. Default is 'both'. |
||
16 | */ |
||
17 | 67 | public function addField($field, $form = 'both') |
|
58 | |||
59 | /** |
||
60 | * Add multiple fields to the create/update form or both. |
||
61 | * |
||
62 | * @param array $fields The new fields. |
||
63 | * @param string $form The CRUD form. Can be 'create', 'update' or 'both'. Default is 'both'. |
||
64 | */ |
||
65 | 61 | public function addFields($fields, $form = 'both') |
|
73 | |||
74 | /** |
||
75 | * Move the most recently added field after the given target field. |
||
76 | * |
||
77 | * @param string $targetFieldName The target field name. |
||
78 | * @param string $form The CRUD form. Can be 'create', 'update' or 'both'. Default is 'both'. |
||
79 | */ |
||
80 | public function afterField($targetFieldName, $form = 'both') |
||
86 | |||
87 | /** |
||
88 | * Move the most recently added field before the given target field. |
||
89 | * |
||
90 | * @param string $targetFieldName The target field name. |
||
91 | * @param string $form The CRUD form. Can be 'create', 'update' or 'both'. Default is 'both'. |
||
92 | */ |
||
93 | public function beforeField($targetFieldName, $form = 'both') |
||
99 | |||
100 | /** |
||
101 | * Move the most recently added field before or after the given target field. Default is before. |
||
102 | * |
||
103 | * @param array $fields The form fields. |
||
104 | * @param string $targetFieldName The target field name. |
||
105 | * @param bool $before If true, the field will be moved before the target field, otherwise it will be moved after it. |
||
106 | * @return array |
||
107 | */ |
||
108 | 13 | private function moveField($fields, $targetFieldName, $before = true) |
|
128 | |||
129 | /** |
||
130 | * Remove a certain field from the create/update/both forms by its name. |
||
131 | * |
||
132 | * @param string $name Field name (as defined with the addField() procedure) |
||
133 | * @param string $form update/create/both |
||
134 | */ |
||
135 | public function removeField($name, $form = 'both') |
||
143 | |||
144 | /** |
||
145 | * Remove many fields from the create/update/both forms by their name. |
||
146 | * |
||
147 | * @param array $array_of_names A simple array of the names of the fields to be removed. |
||
148 | * @param string $form update/create/both |
||
149 | */ |
||
150 | 4 | public function removeFields($array_of_names, $form = 'both') |
|
158 | |||
159 | /** |
||
160 | * Remove all fields from the create/update/both forms. |
||
161 | * |
||
162 | * @param string $form update/create/both |
||
163 | */ |
||
164 | public function removeAllFields($form = 'both') |
||
173 | |||
174 | /** |
||
175 | * Update value of a given key for a current field. |
||
176 | * |
||
177 | * @param string $field The field |
||
178 | * @param array $modifications An array of changes to be made. |
||
179 | * @param string $form update/create/both |
||
180 | */ |
||
181 | public function modifyField($field, $modifications, $form = 'both') |
||
182 | { |
||
183 | foreach ($modifications as $key => $newValue) { |
||
184 | switch (strtolower($form)) { |
||
185 | case 'create': |
||
186 | $this->create_fields[$field][$key] = $newValue; |
||
187 | break; |
||
188 | |||
189 | case 'update': |
||
190 | $this->update_fields[$field][$key] = $newValue; |
||
191 | break; |
||
192 | |||
193 | default: |
||
194 | $this->create_fields[$field][$key] = $newValue; |
||
195 | $this->update_fields[$field][$key] = $newValue; |
||
196 | break; |
||
197 | } |
||
198 | } |
||
199 | 3 | } |
|
200 | |||
201 | 3 | /** |
|
202 | * Set label for a specific field. |
||
203 | 2 | * |
|
204 | 1 | * @param string $field |
|
205 | * @param string $label |
||
206 | */ |
||
207 | 2 | public function setFieldLabel($field, $label) |
|
208 | { |
||
209 | if (isset($this->create_fields[$field])) { |
||
210 | $this->create_fields[$field]['label'] = $label; |
||
211 | } |
||
212 | if (isset($this->update_fields[$field])) { |
||
213 | $this->update_fields[$field]['label'] = $label; |
||
214 | } |
||
215 | 6 | } |
|
216 | |||
217 | /** |
||
218 | 6 | * Check if field is the first of its type in the given fields array. |
|
219 | 5 | * It's used in each field_type.blade.php to determine wether to push the css and js content or not (we only need to push the js and css for a field the first time it's loaded in the form, not any subsequent times). |
|
220 | * |
||
221 | 5 | * @param array $field The current field being tested if it's the first of its type. |
|
222 | * @param array $fields_array All the fields in that particular form. |
||
223 | * |
||
224 | 5 | * @return bool true/false |
|
225 | */ |
||
226 | public function checkIfFieldIsFirstOfItsType($field, $fields_array) |
||
227 | 5 | { |
|
228 | 5 | $first_field = $this->getFirstOfItsTypeInArray($field['type'], $fields_array); |
|
229 | |||
230 | 5 | if ($field['name'] == $first_field['name']) { |
|
231 | return true; |
||
232 | } |
||
233 | |||
234 | 5 | return false; |
|
235 | } |
||
236 | |||
237 | /** |
||
238 | * Decode attributes that are casted as array/object/json in the model. |
||
239 | * So that they are not json_encoded twice before they are stored in the db |
||
240 | 5 | * (once by Backpack in front-end, once by Laravel Attribute Casting). |
|
241 | */ |
||
242 | public function decodeJsonCastedAttributes($data, $form, $id = false) |
||
243 | 9 | { |
|
244 | // get the right fields according to the form type (create/update) |
||
245 | 9 | $fields = $this->getFields($form, $id); |
|
246 | 1 | $casted_attributes = $this->model->getCastedAttributes(); |
|
247 | |||
248 | foreach ($fields as $field) { |
||
249 | 8 | ||
250 | // Test the field is castable |
||
251 | if (isset($field['name']) && array_key_exists($field['name'], $casted_attributes)) { |
||
252 | |||
253 | // Handle JSON field types |
||
254 | $jsonCastables = ['array', 'object', 'json']; |
||
255 | $fieldCasting = $casted_attributes[$field['name']]; |
||
256 | |||
257 | if (in_array($fieldCasting, $jsonCastables) && isset($data[$field['name']]) && ! empty($data[$field['name']]) && ! is_array($data[$field['name']])) { |
||
258 | try { |
||
259 | $data[$field['name']] = json_decode($data[$field['name']]); |
||
260 | } catch (\Exception $e) { |
||
261 | 7 | $data[$field['name']] = []; |
|
262 | 7 | } |
|
263 | 7 | } |
|
264 | 7 | } |
|
265 | } |
||
266 | |||
267 | return $data; |
||
268 | } |
||
269 | |||
270 | public function getCurrentFields() |
||
278 | 6 | ||
279 | /** |
||
280 | * Order the CRUD fields in the given form. If certain fields are missing from the given order array, they will be |
||
281 | * pushed to the new fields array in the original order. |
||
282 | 7 | * |
|
283 | 2 | * @param array $order An array of field names in the desired order. |
|
284 | * @param string $form The CRUD form. Can be 'create', 'update' or 'both'. |
||
285 | */ |
||
286 | 5 | public function orderFields($order, $form = 'both') |
|
292 | |||
293 | /** |
||
294 | * Apply the given order to the fields and return the new array. |
||
295 | * |
||
296 | * @param array $fields The fields array. |
||
297 | * @param array $order The desired field order array. |
||
298 | * @return array The ordered fields array. |
||
299 | */ |
||
300 | private function applyOrderToFields($fields, $order) |
||
317 | |||
318 | /** |
||
319 | * Set the order of the CRUD fields. |
||
320 | * |
||
321 | * @param array $fields Fields order. |
||
322 | * |
||
323 | 66 | * @deprecated This method was not and will not be implemented since its a duplicate of the orderFields method. |
|
324 | * @see Fields::orderFields() to order the CRUD fields. |
||
325 | 66 | */ |
|
326 | 66 | public function setFieldOrder($fields) |
|
330 | 58 | ||
331 | 15 | /** |
|
332 | 15 | * Set the order of the CRUD fields. |
|
333 | * |
||
334 | * @param array $fields Fields order. |
||
335 | 49 | * |
|
336 | 49 | * @deprecated This method was not and will not be implemented since its a duplicate of the orderFields method. |
|
337 | 49 | * @see Fields::orderFields() to order the CRUD fields. |
|
338 | */ |
||
339 | 66 | public function setFieldsOrder($fields) |
|
343 | |||
344 | /** |
||
345 | * Apply the given callback to the form fields. |
||
346 | * |
||
347 | * @param string $form The CRUD form. Can be 'create', 'update' or 'both'. |
||
348 | * @param callable $callback The callback function to run for the given form fields. |
||
349 | */ |
||
350 | private function transformFields($form, callable $callback) |
||
367 | } |
||
368 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.