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) { |
||
83 | |||
84 | /** |
||
85 | * Moves the recently added field to 'before' the $target_field |
||
86 | * |
||
87 | * @param $target_field |
||
88 | */ |
||
89 | public function beforeField($target_field) { |
||
107 | |||
108 | /** |
||
109 | * Remove a certain field from the create/update/both forms by its name. |
||
110 | * |
||
111 | * @param string $name Field name (as defined with the addField() procedure) |
||
112 | * @param string $form update/create/both |
||
113 | */ |
||
114 | public function removeField($name, $form = 'both') |
||
131 | |||
132 | /** |
||
133 | * Remove many fields from the create/update/both forms by their name. |
||
134 | * |
||
135 | * @param array $array_of_names A simple array of the names of the fields to be removed. |
||
136 | * @param string $form update/create/both |
||
137 | */ |
||
138 | public function removeFields($array_of_names, $form = 'both') |
||
146 | |||
147 | /** |
||
148 | * Check if field is the first of its type in the given fields array. |
||
149 | * 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). |
||
150 | * |
||
151 | * @param array $field The current field being tested if it's the first of its type. |
||
152 | * @param array $fields_array All the fields in that particular form. |
||
153 | * |
||
154 | * @return bool true/false |
||
155 | */ |
||
156 | public function checkIfFieldIsFirstOfItsType($field, $fields_array) |
||
164 | |||
165 | /** |
||
166 | * Order the fields in a certain way. |
||
167 | * |
||
168 | * @param [string] Column name. |
||
169 | * @param [attributes and values array] |
||
170 | */ |
||
171 | public function setFieldOrder($fields) |
||
175 | |||
176 | // ALIAS of setFieldOrder($fields) |
||
177 | public function setFieldsOrder($fields) |
||
181 | |||
182 | // ------------ |
||
183 | // TONE FUNCTIONS - UNDOCUMENTED, UNTESTED, SOME MAY BE USED |
||
184 | // ------------ |
||
185 | // TODO: check them |
||
186 | |||
187 | public function orderFields($order) |
||
191 | } |
||
192 |
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.