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 | 60 | */ |
|
17 | public function addField($field, $form = 'both') |
||
58 | |||
59 | /** |
||
60 | 40 | * Add multiple fields to the create/update form or both. |
|
61 | 40 | * |
|
62 | 40 | * @param array $fields The new fields. |
|
63 | * @param string $form The CRUD form. Can be 'create', 'update' or 'both'. Default is 'both'. |
||
64 | */ |
||
65 | 59 | public function addFields($fields, $form = 'both') |
|
73 | |||
74 | /** |
||
75 | 53 | * 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 | 6 | ||
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 | 13 | * @return array |
|
107 | */ |
||
108 | private function moveField($fields, $targetFieldName, $before = true) |
||
128 | |||
129 | 13 | /** |
|
130 | * Remove a certain field from the create/update/both forms by its name. |
||
131 | 13 | * |
|
132 | 11 | * @param string $name Field name (as defined with the addField() procedure) |
|
133 | 11 | * @param string $form update/create/both |
|
134 | */ |
||
135 | 11 | public function removeField($name, $form = 'both') |
|
143 | |||
144 | 9 | /** |
|
145 | * Remove many fields from the create/update/both forms by their name. |
||
146 | 11 | * |
|
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 | public function removeFields($array_of_names, $form = 'both') |
||
158 | 1 | ||
159 | 1 | /** |
|
160 | * Set label for a specific field. |
||
161 | 3 | * |
|
162 | 1 | * @param string $field |
|
163 | 1 | * @param string $label |
|
164 | */ |
||
165 | public function setFieldLabel($field, $label) |
||
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 | 4 | * |
|
179 | * @param array $field The current field being tested if it's the first of its type. |
||
180 | 4 | * @param array $fields_array All the fields in that particular form. |
|
181 | 4 | * |
|
182 | 4 | * @return bool true/false |
|
183 | */ |
||
184 | 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 | public function decodeJsonCastedAttributes($data, $form, $id = false) |
||
227 | |||
228 | 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 | 5 | /** |
|
252 | * Apply the given order to the fields and return the new array. |
||
253 | * |
||
254 | 5 | * @param array $fields The fields array. |
|
255 | * @param array $order The desired field order array. |
||
256 | * @return array The ordered fields array. |
||
257 | 5 | */ |
|
258 | 5 | private function applyOrderToFields($fields, $order) |
|
275 | 9 | ||
276 | 1 | /** |
|
277 | * Set the order of the CRUD fields. |
||
278 | * |
||
279 | 8 | * @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) |
||
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) |
||
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 | 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.