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 | * @return self |
||
| 18 | */ |
||
| 19 | 68 | public function addField($field, $form = 'both') |
|
| 63 | |||
| 64 | /** |
||
| 65 | * Add multiple fields to the create/update form or both. |
||
| 66 | * |
||
| 67 | * @param array $fields The new fields. |
||
| 68 | * @param string $form The CRUD form. Can be 'create', 'update' or 'both'. Default is 'both'. |
||
| 69 | */ |
||
| 70 | 62 | public function addFields($fields, $form = 'both') |
|
| 78 | |||
| 79 | /** |
||
| 80 | * Move the most recently added field after the given target field. |
||
| 81 | * |
||
| 82 | * @param string $targetFieldName The target field name. |
||
| 83 | * @param string $form The CRUD form. Can be 'create', 'update' or 'both'. Default is 'both'. |
||
| 84 | */ |
||
| 85 | 6 | public function afterField($targetFieldName, $form = 'both') |
|
| 91 | |||
| 92 | /** |
||
| 93 | * Move the most recently added field before the given target field. |
||
| 94 | * |
||
| 95 | * @param string $targetFieldName The target field name. |
||
| 96 | * @param string $form The CRUD form. Can be 'create', 'update' or 'both'. Default is 'both'. |
||
| 97 | */ |
||
| 98 | 7 | public function beforeField($targetFieldName, $form = 'both') |
|
| 104 | |||
| 105 | /** |
||
| 106 | * Move the most recently added field before or after the given target field. Default is before. |
||
| 107 | * |
||
| 108 | * @param array $fields The form fields. |
||
| 109 | * @param string $targetFieldName The target field name. |
||
| 110 | * @param bool $before If true, the field will be moved before the target field, otherwise it will be moved after it. |
||
| 111 | * |
||
| 112 | * @return array |
||
| 113 | */ |
||
| 114 | 13 | private function moveField($fields, $targetFieldName, $before = true) |
|
| 134 | |||
| 135 | /** |
||
| 136 | * Remove a certain field from the create/update/both forms by its name. |
||
| 137 | * |
||
| 138 | * @param string $name Field name (as defined with the addField() procedure) |
||
| 139 | * @param string $form update/create/both |
||
| 140 | */ |
||
| 141 | 4 | public function removeField($name, $form = 'both') |
|
| 149 | |||
| 150 | /** |
||
| 151 | * Remove many fields from the create/update/both forms by their name. |
||
| 152 | * |
||
| 153 | * @param array $array_of_names A simple array of the names of the fields to be removed. |
||
| 154 | * @param string $form update/create/both |
||
| 155 | */ |
||
| 156 | 4 | public function removeFields($array_of_names, $form = 'both') |
|
| 164 | |||
| 165 | /** |
||
| 166 | * Remove all fields from the create/update/both forms. |
||
| 167 | * |
||
| 168 | * @param string $form update/create/both |
||
| 169 | */ |
||
| 170 | public function removeAllFields($form = 'both') |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Update value of a given key for a current field. |
||
| 182 | * |
||
| 183 | * @param string $field The field |
||
| 184 | * @param array $modifications An array of changes to be made. |
||
| 185 | * @param string $form update/create/both |
||
| 186 | */ |
||
| 187 | public function modifyField($field, $modifications, $form = 'both') |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Set label for a specific field. |
||
| 209 | * |
||
| 210 | * @param string $field |
||
| 211 | * @param string $label |
||
| 212 | */ |
||
| 213 | public function setFieldLabel($field, $label) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Check if field is the first of its type in the given fields array. |
||
| 225 | * 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). |
||
| 226 | * |
||
| 227 | * @param array $field The current field being tested if it's the first of its type. |
||
| 228 | * |
||
| 229 | * @return bool true/false |
||
| 230 | */ |
||
| 231 | 3 | public function checkIfFieldIsFirstOfItsType($field) |
|
| 242 | |||
| 243 | /** |
||
| 244 | * Decode attributes that are casted as array/object/json in the model. |
||
| 245 | * So that they are not json_encoded twice before they are stored in the db |
||
| 246 | * (once by Backpack in front-end, once by Laravel Attribute Casting). |
||
| 247 | */ |
||
| 248 | 6 | public function decodeJsonCastedAttributes($data, $form, $id = false) |
|
| 249 | { |
||
| 250 | // get the right fields according to the form type (create/update) |
||
| 251 | 6 | $fields = $this->getFields($form, $id); |
|
| 252 | 5 | $casted_attributes = $this->model->getCastedAttributes(); |
|
| 253 | |||
| 254 | 5 | foreach ($fields as $field) { |
|
| 255 | |||
| 256 | // Test the field is castable |
||
| 257 | 5 | if (isset($field['name']) && array_key_exists($field['name'], $casted_attributes)) { |
|
| 258 | |||
| 259 | // Handle JSON field types |
||
| 260 | 5 | $jsonCastables = ['array', 'object', 'json']; |
|
| 261 | 5 | $fieldCasting = $casted_attributes[$field['name']]; |
|
| 262 | |||
| 263 | 5 | if (in_array($fieldCasting, $jsonCastables) && isset($data[$field['name']]) && ! empty($data[$field['name']]) && ! is_array($data[$field['name']])) { |
|
| 264 | try { |
||
| 265 | $data[$field['name']] = json_decode($data[$field['name']]); |
||
| 266 | } catch (\Exception $e) { |
||
| 267 | 5 | $data[$field['name']] = []; |
|
| 268 | } |
||
| 269 | } |
||
| 270 | } |
||
| 271 | } |
||
| 272 | |||
| 273 | 5 | return $data; |
|
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * @return array |
||
| 278 | */ |
||
| 279 | 12 | public function getCurrentFields() |
|
| 287 | |||
| 288 | /** |
||
| 289 | * Order the CRUD fields in the given form. If certain fields are missing from the given order array, they will be |
||
| 290 | * pushed to the new fields array in the original order. |
||
| 291 | * |
||
| 292 | * @param array $order An array of field names in the desired order. |
||
| 293 | * @param string $form The CRUD form. Can be 'create', 'update' or 'both'. |
||
| 294 | */ |
||
| 295 | 7 | public function orderFields($order, $form = 'both') |
|
| 301 | |||
| 302 | /** |
||
| 303 | * Apply the given order to the fields and return the new array. |
||
| 304 | * |
||
| 305 | * @param array $fields The fields array. |
||
| 306 | * @param array $order The desired field order array. |
||
| 307 | * |
||
| 308 | * @return array The ordered fields array. |
||
| 309 | */ |
||
| 310 | 7 | private function applyOrderToFields($fields, $order) |
|
| 327 | |||
| 328 | /** |
||
| 329 | * Set the order of the CRUD fields. |
||
| 330 | * |
||
| 331 | * @param array $fields Fields order. |
||
| 332 | * |
||
| 333 | * @deprecated This method was not and will not be implemented since its a duplicate of the orderFields method. |
||
| 334 | * @see Fields::orderFields() to order the CRUD fields. |
||
| 335 | */ |
||
| 336 | public function setFieldOrder($fields) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Set the order of the CRUD fields. |
||
| 343 | * |
||
| 344 | * @param array $fields Fields order. |
||
| 345 | * |
||
| 346 | * @deprecated This method was not and will not be implemented since its a duplicate of the orderFields method. |
||
| 347 | * @see Fields::orderFields() to order the CRUD fields. |
||
| 348 | */ |
||
| 349 | public function setFieldsOrder($fields) |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Apply the given callback to the form fields. |
||
| 356 | * |
||
| 357 | * @param string $form The CRUD form. Can be 'create', 'update' or 'both'. |
||
| 358 | * @param callable $callback The callback function to run for the given form fields. |
||
| 359 | */ |
||
| 360 | 67 | private function transformFields($form, callable $callback) |
|
| 377 | } |
||
| 378 |
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.