We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 | public function addField($field, $form = 'both') |
||
54 | |||
55 | public function addFields($fields, $form = 'both') |
||
63 | |||
64 | /** |
||
65 | * Moves the recently added field to 'after' the $target_field. |
||
66 | * |
||
67 | * @param $target_field |
||
68 | */ |
||
69 | public function afterField($target_field) |
||
84 | |||
85 | /** |
||
86 | * Moves the recently added field to 'before' the $target_field. |
||
87 | * |
||
88 | * @param $target_field |
||
89 | */ |
||
90 | public function beforeField($target_field) |
||
109 | |||
110 | /** |
||
111 | * Remove a certain field from the create/update/both forms by its name. |
||
112 | * |
||
113 | * @param string $name Field name (as defined with the addField() procedure) |
||
114 | * @param string $form update/create/both |
||
115 | */ |
||
116 | public function removeField($name, $form = 'both') |
||
133 | |||
134 | /** |
||
135 | * Remove many fields from the create/update/both forms by their name. |
||
136 | * |
||
137 | * @param array $array_of_names A simple array of the names of the fields to be removed. |
||
138 | * @param string $form update/create/both |
||
139 | */ |
||
140 | public function removeFields($array_of_names, $form = 'both') |
||
148 | |||
149 | /** |
||
150 | * Check if field is the first of its type in the given fields array. |
||
151 | * 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). |
||
152 | * |
||
153 | * @param array $field The current field being tested if it's the first of its type. |
||
154 | * @param array $fields_array All the fields in that particular form. |
||
155 | * |
||
156 | * @return bool true/false |
||
157 | */ |
||
158 | public function checkIfFieldIsFirstOfItsType($field, $fields_array) |
||
166 | |||
167 | /** |
||
168 | * Order the fields in a certain way. |
||
169 | * |
||
170 | * @param [string] Column name. |
||
171 | * @param [attributes and values array] |
||
172 | */ |
||
173 | public function setFieldOrder($fields) |
||
177 | |||
178 | // ALIAS of setFieldOrder($fields) |
||
179 | public function setFieldsOrder($fields) |
||
183 | |||
184 | /** |
||
185 | * Decode attributes that are casted as array/object/json in the model. |
||
186 | * So that they are not json_encoded twice before they are stored in the db |
||
187 | * (once by Backpack in front-end, once by Laravel Attribute Casting). |
||
188 | */ |
||
189 | public function decodeJsonCastedAttributes($data, $form, $id = false) |
||
215 | |||
216 | // ------------ |
||
217 | // TONE FUNCTIONS - UNDOCUMENTED, UNTESTED, SOME MAY BE USED |
||
218 | // ------------ |
||
219 | // TODO: check them |
||
220 | |||
221 | public function orderFields($order) |
||
225 | } |
||
226 |
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
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key 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.