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') |
|
18 | { |
||
19 | // if the field_definition_array array is a string, it means the programmer was lazy and has only passed the name |
||
|
|||
20 | // set some default values, so the field will still work |
||
21 | 67 | if (is_string($field)) { |
|
22 | 4 | $completeFieldsArray['name'] = $field; |
|
23 | } else { |
||
24 | 63 | $completeFieldsArray = $field; |
|
25 | } |
||
26 | |||
27 | // if this is a relation type field and no corresponding model was specified, get it from the relation method |
||
28 | // defined in the main model |
||
29 | 67 | if (isset($completeFieldsArray['entity']) && ! isset($completeFieldsArray['model'])) { |
|
30 | 8 | $completeFieldsArray['model'] = $this->getRelationModel($completeFieldsArray['entity']); |
|
31 | } |
||
32 | |||
33 | // if the label is missing, we should set it |
||
34 | 67 | if (! isset($completeFieldsArray['label'])) { |
|
35 | 49 | $completeFieldsArray['label'] = ucfirst($completeFieldsArray['name']); |
|
36 | } |
||
37 | |||
38 | // if the field type is missing, we should set it |
||
39 | 66 | if (! isset($completeFieldsArray['type'])) { |
|
40 | 62 | $completeFieldsArray['type'] = $this->getFieldTypeFromDbColumnType($completeFieldsArray['name']); |
|
41 | } |
||
42 | |||
43 | // if a tab was mentioned, we should enable it |
||
44 | 66 | if (isset($completeFieldsArray['tab'])) { |
|
45 | 7 | if (! $this->tabsEnabled()) { |
|
46 | 7 | $this->enableTabs(); |
|
47 | } |
||
48 | } |
||
49 | |||
50 | 66 | $this->transformFields($form, function ($fields) use ($completeFieldsArray) { |
|
51 | 66 | $fields[$completeFieldsArray['name']] = $completeFieldsArray; |
|
52 | |||
53 | 66 | return $fields; |
|
54 | 66 | }); |
|
55 | |||
56 | 66 | return $this; |
|
57 | } |
||
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') |
||
81 | { |
||
82 | 6 | $this->transformFields($form, function ($fields) use ($targetFieldName) { |
|
83 | 6 | return $this->moveField($fields, $targetFieldName, false); |
|
84 | 6 | }); |
|
85 | 6 | } |
|
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') |
||
94 | { |
||
95 | 7 | $this->transformFields($form, function ($fields) use ($targetFieldName) { |
|
96 | 7 | return $this->moveField($fields, $targetFieldName, true); |
|
97 | 7 | }); |
|
98 | 7 | } |
|
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) |
|
109 | { |
||
110 | 13 | if (array_key_exists($targetFieldName, $fields)) { |
|
111 | 11 | $targetFieldPosition = $before ? array_search($targetFieldName, array_keys($fields)) |
|
112 | 11 | : array_search($targetFieldName, array_keys($fields)) + 1; |
|
113 | |||
114 | 11 | if ($targetFieldPosition >= (count($fields) - 1)) { |
|
115 | // target field name is same as element |
||
116 | 3 | return $fields; |
|
117 | } |
||
118 | |||
119 | 9 | $element = array_pop($fields); |
|
120 | 9 | $beginningArrayPart = array_slice($fields, 0, $targetFieldPosition, true); |
|
121 | 9 | $endingArrayPart = array_slice($fields, $targetFieldPosition, null, true); |
|
122 | |||
123 | 9 | $fields = array_merge($beginningArrayPart, [$element['name'] => $element], $endingArrayPart); |
|
124 | } |
||
125 | |||
126 | 11 | return $fields; |
|
127 | } |
||
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') |
|
151 | { |
||
152 | 4 | if (! empty($array_of_names)) { |
|
153 | 4 | foreach ($array_of_names as $name) { |
|
154 | 4 | $this->removeField($name, $form); |
|
155 | } |
||
156 | } |
||
157 | 4 | } |
|
158 | |||
159 | /** |
||
160 | * Set label for a specific field. |
||
161 | * |
||
162 | * @param string $field |
||
163 | * @param string $label |
||
164 | */ |
||
165 | public function setFieldLabel($field, $label) |
||
166 | { |
||
167 | if (isset($this->create_fields[$field])) { |
||
168 | $this->create_fields[$field]['label'] = $label; |
||
169 | } |
||
170 | if (isset($this->update_fields[$field])) { |
||
171 | $this->update_fields[$field]['label'] = $label; |
||
172 | } |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * Check if field is the first of its type in the given fields array. |
||
177 | * 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). |
||
178 | * |
||
179 | * @param array $field The current field being tested if it's the first of its type. |
||
180 | * @param array $fields_array All the fields in that particular form. |
||
181 | * |
||
182 | * @return bool true/false |
||
183 | */ |
||
184 | 3 | public function checkIfFieldIsFirstOfItsType($field, $fields_array) |
|
194 | |||
195 | /** |
||
196 | * Decode attributes that are casted as array/object/json in the model. |
||
197 | * So that they are not json_encoded twice before they are stored in the db |
||
198 | * (once by Backpack in front-end, once by Laravel Attribute Casting). |
||
199 | */ |
||
200 | 6 | public function decodeJsonCastedAttributes($data, $form, $id = false) |
|
201 | { |
||
202 | // get the right fields according to the form type (create/update) |
||
203 | 6 | $fields = $this->getFields($form, $id); |
|
204 | 5 | $casted_attributes = $this->model->getCastedAttributes(); |
|
205 | |||
206 | 5 | foreach ($fields as $field) { |
|
207 | |||
208 | // Test the field is castable |
||
209 | 5 | if (isset($field['name']) && array_key_exists($field['name'], $casted_attributes)) { |
|
210 | |||
211 | // Handle JSON field types |
||
212 | 5 | $jsonCastables = ['array', 'object', 'json']; |
|
213 | 5 | $fieldCasting = $casted_attributes[$field['name']]; |
|
214 | |||
215 | 5 | if (in_array($fieldCasting, $jsonCastables) && isset($data[$field['name']]) && ! empty($data[$field['name']]) && ! is_array($data[$field['name']])) { |
|
216 | try { |
||
217 | $data[$field['name']] = json_decode($data[$field['name']]); |
||
218 | } catch (\Exception $e) { |
||
219 | 5 | $data[$field['name']] = []; |
|
220 | } |
||
221 | } |
||
222 | } |
||
223 | } |
||
224 | |||
225 | 5 | return $data; |
|
226 | } |
||
227 | |||
228 | 9 | public function getCurrentFields() |
|
236 | |||
237 | /** |
||
238 | * Order the CRUD fields in the given form. If certain fields are missing from the given order array, they will be |
||
239 | * pushed to the new fields array in the original order. |
||
240 | * |
||
241 | * @param array $order An array of field names in the desired order. |
||
242 | * @param string $form The CRUD form. Can be 'create', 'update' or 'both'. |
||
243 | */ |
||
244 | public function orderFields($order, $form = 'both') |
||
250 | |||
251 | /** |
||
252 | * Apply the given order to the fields and return the new array. |
||
253 | * |
||
254 | * @param array $fields The fields array. |
||
255 | * @param array $order The desired field order array. |
||
256 | * @return array The ordered fields array. |
||
257 | */ |
||
258 | 7 | private function applyOrderToFields($fields, $order) |
|
275 | |||
276 | /** |
||
277 | * Set the order of the CRUD fields. |
||
278 | * |
||
279 | * @param array $fields Fields order. |
||
280 | * |
||
281 | * @deprecated This method was not and will not be implemented since its a duplicate of the orderFields method. |
||
282 | * @see Fields::orderFields() to order the CRUD fields. |
||
283 | */ |
||
284 | public function setFieldOrder($fields) |
||
285 | { |
||
286 | // not implemented |
||
287 | } |
||
288 | |||
289 | /** |
||
290 | * Set the order of the CRUD fields. |
||
291 | * |
||
292 | * @param array $fields Fields order. |
||
293 | * |
||
294 | * @deprecated This method was not and will not be implemented since its a duplicate of the orderFields method. |
||
295 | * @see Fields::orderFields() to order the CRUD fields. |
||
296 | */ |
||
297 | public function setFieldsOrder($fields) |
||
298 | { |
||
299 | $this->setFieldOrder($fields); |
||
300 | } |
||
301 | |||
302 | /** |
||
303 | * Apply the given callback to the form fields. |
||
304 | * |
||
305 | * @param string $form The CRUD form. Can be 'create', 'update' or 'both'. |
||
306 | * @param callable $callback The callback function to run for the given form fields. |
||
307 | */ |
||
308 | 66 | private function transformFields($form, callable $callback) |
|
325 | } |
||
326 |
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.