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 $form The form to add the field to (create/update/both) |
||
15 | */ |
||
16 | 60 | public function addField($field, $form = 'both') |
|
67 | |||
68 | 54 | public function addFields($fields, $form = 'both') |
|
76 | |||
77 | /** |
||
78 | * Move the most recently added field after the given target field. |
||
79 | * |
||
80 | * @param string $targetFieldName The target field name. |
||
81 | * @param string $form The CRUD form. Can be 'create', 'update' or 'both'. Default is 'both'. |
||
82 | */ |
||
83 | 6 | public function afterField($targetFieldName, $form = 'both') |
|
87 | |||
88 | /** |
||
89 | * Move the most recently added field before the given target field. |
||
90 | * |
||
91 | * @param string $targetFieldName The target field name. |
||
92 | * @param string $form The CRUD form. Can be 'create', 'update' or 'both'. Default is 'both'. |
||
93 | */ |
||
94 | 7 | public function beforeField($targetFieldName, $form = 'both') |
|
98 | |||
99 | /** |
||
100 | * Move the most recently added field from a given form before or after the given target field. Default is before. |
||
101 | * |
||
102 | * @param string $targetFieldName The target field name. |
||
103 | * @param string $form The CRUD form. Can be 'create', 'update' or 'both'. Default is 'both'. |
||
104 | * @param bool $before If true, the field will be moved before the target field, otherwise it will be moved after it. |
||
105 | */ |
||
106 | 13 | protected function moveFieldInForm($targetFieldName, $form = 'both', $before = true) |
|
121 | |||
122 | /** |
||
123 | * Move the most recently added field before or after the given target field. Default is before. |
||
124 | * |
||
125 | * @param array $fields The form fields. |
||
126 | * @param string $targetFieldName The target field name. |
||
127 | * @param bool $before If true, the field will be moved before the target field, otherwise it will be moved after it. |
||
128 | */ |
||
129 | 13 | private function moveField(&$fields, $targetFieldName, $before = true) |
|
147 | |||
148 | /** |
||
149 | * Remove a certain field from the create/update/both forms by its name. |
||
150 | * |
||
151 | * @param string $name Field name (as defined with the addField() procedure) |
||
152 | * @param string $form update/create/both |
||
153 | */ |
||
154 | 4 | public function removeField($name, $form = 'both') |
|
171 | |||
172 | /** |
||
173 | * Remove many fields from the create/update/both forms by their name. |
||
174 | * |
||
175 | * @param array $array_of_names A simple array of the names of the fields to be removed. |
||
176 | * @param string $form update/create/both |
||
177 | */ |
||
178 | 4 | public function removeFields($array_of_names, $form = 'both') |
|
186 | |||
187 | /** |
||
188 | * Set label for a specific field. |
||
189 | * |
||
190 | * @param string $field |
||
191 | * @param string $label |
||
192 | */ |
||
193 | public function setFieldLabel($field, $label) |
||
202 | |||
203 | /** |
||
204 | * Check if field is the first of its type in the given fields array. |
||
205 | * 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). |
||
206 | * |
||
207 | * @param array $field The current field being tested if it's the first of its type. |
||
208 | * @param array $fields_array All the fields in that particular form. |
||
209 | * |
||
210 | * @return bool true/false |
||
211 | */ |
||
212 | 3 | public function checkIfFieldIsFirstOfItsType($field, $fields_array) |
|
222 | |||
223 | /** |
||
224 | * Order the fields in a certain way. |
||
225 | * |
||
226 | * @param [string] Column name. |
||
227 | * @param [attributes and values array] |
||
228 | */ |
||
229 | public function setFieldOrder($fields) |
||
233 | |||
234 | // ALIAS of setFieldOrder($fields) |
||
235 | public function setFieldsOrder($fields) |
||
239 | |||
240 | /** |
||
241 | * Decode attributes that are casted as array/object/json in the model. |
||
242 | * So that they are not json_encoded twice before they are stored in the db |
||
243 | * (once by Backpack in front-end, once by Laravel Attribute Casting). |
||
244 | */ |
||
245 | 6 | public function decodeJsonCastedAttributes($data, $form, $id = false) |
|
272 | |||
273 | 9 | public function getCurrentFields() |
|
281 | |||
282 | // ------------ |
||
283 | // TONE FUNCTIONS - UNDOCUMENTED, UNTESTED, SOME MAY BE USED |
||
284 | // ------------ |
||
285 | // TODO: check them |
||
286 | |||
287 | public function orderFields($order) |
||
291 | } |
||
292 |
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.