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') |
|
| 60 | |||
| 61 | /** |
||
| 62 | * Add multiple fields to the create/update form or both. |
||
| 63 | * |
||
| 64 | * @param array $fields The new fields. |
||
| 65 | * @param string $form The CRUD form. Can be 'create', 'update' or 'both'. Default is 'both'. |
||
| 66 | */ |
||
| 67 | 62 | public function addFields($fields, $form = 'both') |
|
| 68 | { |
||
| 69 | 62 | if (count($fields)) { |
|
| 70 | 62 | foreach ($fields as $field) { |
|
| 71 | 62 | $this->addField($field, $form); |
|
| 72 | } |
||
| 73 | } |
||
| 74 | 61 | } |
|
| 75 | |||
| 76 | /** |
||
| 77 | * Move the most recently added field after the given target field. |
||
| 78 | * |
||
| 79 | * @param string $targetFieldName The target field name. |
||
| 80 | * @param string $form The CRUD form. Can be 'create', 'update' or 'both'. Default is 'both'. |
||
| 81 | */ |
||
| 82 | 6 | public function afterField($targetFieldName, $form = 'both') |
|
| 88 | |||
| 89 | /** |
||
| 90 | * Move the most recently added field before the given target field. |
||
| 91 | * |
||
| 92 | * @param string $targetFieldName The target field name. |
||
| 93 | * @param string $form The CRUD form. Can be 'create', 'update' or 'both'. Default is 'both'. |
||
| 94 | */ |
||
| 95 | 7 | public function beforeField($targetFieldName, $form = 'both') |
|
| 101 | |||
| 102 | /** |
||
| 103 | * Move the most recently added field before or after the given target field. Default is before. |
||
| 104 | * |
||
| 105 | * @param array $fields The form fields. |
||
| 106 | * @param string $targetFieldName The target field name. |
||
| 107 | * @param bool $before If true, the field will be moved before the target field, otherwise it will be moved after it. |
||
| 108 | * |
||
| 109 | * @return array |
||
| 110 | */ |
||
| 111 | 13 | private function moveField($fields, $targetFieldName, $before = true) |
|
| 131 | |||
| 132 | /** |
||
| 133 | * Remove a certain field from the create/update/both forms by its name. |
||
| 134 | * |
||
| 135 | * @param string $name Field name (as defined with the addField() procedure) |
||
| 136 | * @param string $form update/create/both |
||
| 137 | */ |
||
| 138 | 4 | public function removeField($name, $form = 'both') |
|
| 146 | |||
| 147 | /** |
||
| 148 | * Remove many fields from the create/update/both forms by their name. |
||
| 149 | * |
||
| 150 | * @param array $array_of_names A simple array of the names of the fields to be removed. |
||
| 151 | * @param string $form update/create/both |
||
| 152 | */ |
||
| 153 | 4 | public function removeFields($array_of_names, $form = 'both') |
|
| 161 | |||
| 162 | /** |
||
| 163 | * Remove all fields from the create/update/both forms. |
||
| 164 | * |
||
| 165 | * @param string $form update/create/both |
||
| 166 | */ |
||
| 167 | public function removeAllFields($form = 'both') |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Update value of a given key for a current field. |
||
| 179 | * |
||
| 180 | * @param string $field The field |
||
| 181 | * @param array $modifications An array of changes to be made. |
||
| 182 | * @param string $form update/create/both |
||
| 183 | */ |
||
| 184 | public function modifyField($field, $modifications, $form = 'both') |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Set label for a specific field. |
||
| 206 | * |
||
| 207 | * @param string $field |
||
| 208 | * @param string $label |
||
| 209 | */ |
||
| 210 | public function setFieldLabel($field, $label) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Check if field is the first of its type in the given fields array. |
||
| 222 | * 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). |
||
| 223 | * |
||
| 224 | * @param array $field The current field being tested if it's the first of its type. |
||
| 225 | * |
||
| 226 | * @return bool true/false |
||
| 227 | */ |
||
| 228 | 3 | public function checkIfFieldIsFirstOfItsType($field) |
|
| 229 | { |
||
| 230 | 3 | $fields_array = $this->getCurrentFields(); |
|
| 231 | 3 | $first_field = $this->getFirstOfItsTypeInArray($field['type'], $fields_array); |
|
| 232 | |||
| 233 | 2 | if ($field['name'] == $first_field['name']) { |
|
| 234 | 1 | return true; |
|
| 235 | } |
||
| 236 | |||
| 237 | 2 | return false; |
|
| 238 | } |
||
| 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) |
|
| 246 | { |
||
| 247 | // get the right fields according to the form type (create/update) |
||
| 248 | 6 | $fields = $this->getFields($form, $id); |
|
| 249 | 5 | $casted_attributes = $this->model->getCastedAttributes(); |
|
| 250 | |||
| 251 | 5 | foreach ($fields as $field) { |
|
| 252 | |||
| 253 | // Test the field is castable |
||
| 254 | 5 | if (isset($field['name']) && array_key_exists($field['name'], $casted_attributes)) { |
|
| 255 | |||
| 256 | // Handle JSON field types |
||
| 257 | 5 | $jsonCastables = ['array', 'object', 'json']; |
|
| 258 | 5 | $fieldCasting = $casted_attributes[$field['name']]; |
|
| 259 | |||
| 260 | 5 | if (in_array($fieldCasting, $jsonCastables) && isset($data[$field['name']]) && ! empty($data[$field['name']]) && ! is_array($data[$field['name']])) { |
|
| 261 | try { |
||
| 262 | $data[$field['name']] = json_decode($data[$field['name']]); |
||
| 263 | } catch (\Exception $e) { |
||
| 264 | $data[$field['name']] = []; |
||
| 265 | } |
||
| 266 | } |
||
| 267 | } |
||
| 268 | } |
||
| 269 | |||
| 270 | 5 | return $data; |
|
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @return array |
||
| 275 | */ |
||
| 276 | 12 | public function getCurrentFields() |
|
| 284 | |||
| 285 | /** |
||
| 286 | * Order the CRUD fields in the given form. If certain fields are missing from the given order array, they will be |
||
| 287 | * pushed to the new fields array in the original order. |
||
| 288 | * |
||
| 289 | * @param array $order An array of field names in the desired order. |
||
| 290 | * @param string $form The CRUD form. Can be 'create', 'update' or 'both'. |
||
| 291 | */ |
||
| 292 | 7 | public function orderFields($order, $form = 'both') |
|
| 298 | |||
| 299 | /** |
||
| 300 | * Apply the given order to the fields and return the new array. |
||
| 301 | * |
||
| 302 | * @param array $fields The fields array. |
||
| 303 | * @param array $order The desired field order array. |
||
| 304 | * |
||
| 305 | * @return array The ordered fields array. |
||
| 306 | */ |
||
| 307 | 7 | private function applyOrderToFields($fields, $order) |
|
| 324 | |||
| 325 | /** |
||
| 326 | * Set the order of the CRUD fields. |
||
| 327 | * |
||
| 328 | * @param array $fields Fields order. |
||
| 329 | * |
||
| 330 | * @deprecated This method was not and will not be implemented since its a duplicate of the orderFields method. |
||
| 331 | * @see Fields::orderFields() to order the CRUD fields. |
||
| 332 | */ |
||
| 333 | public function setFieldOrder($fields) |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Set the order of the CRUD fields. |
||
| 340 | * |
||
| 341 | * @param array $fields Fields order. |
||
| 342 | * |
||
| 343 | * @deprecated This method was not and will not be implemented since its a duplicate of the orderFields method. |
||
| 344 | * @see Fields::orderFields() to order the CRUD fields. |
||
| 345 | */ |
||
| 346 | public function setFieldsOrder($fields) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Apply the given callback to the form fields. |
||
| 353 | * |
||
| 354 | * @param string $form The CRUD form. Can be 'create', 'update' or 'both'. |
||
| 355 | * @param callable $callback The callback function to run for the given form fields. |
||
| 356 | */ |
||
| 357 | 67 | private function transformFields($form, callable $callback) |
|
| 374 | } |
||
| 375 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.