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 |
||
11 | class Editor extends Fluent |
||
12 | { |
||
13 | use HasEvents; |
||
14 | const DISPLAY_LIGHTBOX = 'lightbox'; |
||
15 | const DISPLAY_ENVELOPE = 'envelope'; |
||
16 | const DISPLAY_BOOTSTRAP = 'bootstrap'; |
||
17 | const DISPLAY_FOUNDATION = 'foundation'; |
||
18 | const DISPLAY_JQUERYUI = 'jqueryui'; |
||
19 | |||
20 | /** |
||
21 | * Editor constructor. |
||
22 | * |
||
23 | * @param string $instance |
||
24 | */ |
||
25 | public function __construct($instance = 'editor') |
||
31 | |||
32 | /** |
||
33 | * Make new Editor instance. |
||
34 | * |
||
35 | * @param string $instance |
||
36 | * @return Editor |
||
37 | */ |
||
38 | public static function make($instance = 'editor') |
||
42 | |||
43 | /** |
||
44 | * Append raw scripts. |
||
45 | * |
||
46 | * @param string $scripts |
||
47 | * @return Editor |
||
48 | */ |
||
49 | public function scripts($scripts) |
||
55 | |||
56 | /** |
||
57 | * Set Editor's variable name / instance. |
||
58 | * |
||
59 | * @param $instance |
||
60 | * @return $this |
||
61 | */ |
||
62 | public function instance($instance) |
||
68 | |||
69 | /** |
||
70 | * Set Editor's ajax parameter. |
||
71 | * |
||
72 | * @param string|array $ajax |
||
73 | * @return $this |
||
74 | * @see https://editor.datatables.net/reference/option/ajax |
||
75 | */ |
||
76 | public function ajax($ajax) |
||
82 | |||
83 | /** |
||
84 | * Set Editor's table source. |
||
85 | * |
||
86 | * @param string $table |
||
87 | * @return $this |
||
88 | * @see https://editor.datatables.net/reference/option/table |
||
89 | */ |
||
90 | public function table($table) |
||
96 | |||
97 | /** |
||
98 | * Set Editor's idSrc option. |
||
99 | * |
||
100 | * @param string $idSrc |
||
101 | * @return $this |
||
102 | * @see https://editor.datatables.net/reference/option/idSrc |
||
103 | */ |
||
104 | public function idSrc($idSrc = 'DT_RowId') |
||
110 | |||
111 | /** |
||
112 | * Set Editor's display option. |
||
113 | * |
||
114 | * @param string $display |
||
115 | * @return $this |
||
116 | * @see https://editor.datatables.net/reference/option/display |
||
117 | */ |
||
118 | public function display($display) |
||
124 | |||
125 | /** |
||
126 | * Set Editor's fields. |
||
127 | * |
||
128 | * @param array $fields |
||
129 | * @return $this |
||
130 | * @see https://editor.datatables.net/reference/option/fields |
||
131 | */ |
||
132 | public function fields(array $fields) |
||
138 | |||
139 | /** |
||
140 | * Set Editor's formOptions. |
||
141 | * |
||
142 | * @param mixed $formOptions |
||
143 | * @return $this |
||
144 | * @see https://editor.datatables.net/reference/option/formOptions |
||
145 | * @see https://editor.datatables.net/reference/type/form-options |
||
146 | */ |
||
147 | public function formOptions(array $formOptions) |
||
153 | |||
154 | /** |
||
155 | * Set Editor's bubble formOptions. |
||
156 | * |
||
157 | * @param mixed $formOptions |
||
158 | * @return $this |
||
159 | * @see https://editor.datatables.net/reference/option/formOptions.bubble |
||
160 | */ |
||
161 | public function formOptionsBubble(array $formOptions) |
||
167 | |||
168 | /** |
||
169 | * Set Editor's inline formOptions. |
||
170 | * |
||
171 | * @param mixed $formOptions |
||
172 | * @return $this |
||
173 | * @see https://editor.datatables.net/reference/option/formOptions.inline |
||
174 | */ |
||
175 | public function formOptionsInline($formOptions) |
||
181 | |||
182 | /** |
||
183 | * Set Editor's main formOptions. |
||
184 | * |
||
185 | * @param mixed $formOptions |
||
186 | * @return $this |
||
187 | * @see https://editor.datatables.net/reference/option/formOptions.main |
||
188 | */ |
||
189 | public function formOptionsMain($formOptions) |
||
195 | |||
196 | /** |
||
197 | * Set Editor's language. |
||
198 | * |
||
199 | * @param array $language |
||
200 | * @return $this |
||
201 | * @see https://editor.datatables.net/reference/option/i18n |
||
202 | */ |
||
203 | public function language(array $language) |
||
209 | |||
210 | /** |
||
211 | * Set Editor's template. |
||
212 | * |
||
213 | * @param string $template |
||
214 | * @return $this |
||
215 | * @see https://editor.datatables.net/reference/option/template |
||
216 | */ |
||
217 | public function template($template) |
||
223 | |||
224 | /** |
||
225 | * Convert the fluent instance to an array. |
||
226 | * |
||
227 | * @return array |
||
228 | */ |
||
229 | public function toArray() |
||
243 | |||
244 | /** |
||
245 | * Convert the fluent instance to JSON. |
||
246 | * |
||
247 | * @param int $options |
||
248 | * @return string |
||
249 | */ |
||
250 | public function toJson($options = 0) |
||
282 | |||
283 | /** |
||
284 | * Check if given key & value is a valid callback js function. |
||
285 | * |
||
286 | * @param string $value |
||
287 | * @param string $key |
||
288 | * @return bool |
||
289 | */ |
||
290 | protected function isCallbackFunction($value, $key) |
||
300 | } |
||
301 |
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.