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 |
||
12 | trait HasOptions |
||
13 | { |
||
14 | use Options\HasFeatures; |
||
15 | use Options\HasData; |
||
16 | use Options\HasCallbacks; |
||
17 | use Options\HasColumns; |
||
18 | use Options\HasInternationalisation; |
||
19 | use Options\Plugins\AutoFill; |
||
20 | use Options\Plugins\Buttons; |
||
21 | use Options\Plugins\ColReorder; |
||
22 | use Options\Plugins\FixedColumns; |
||
23 | use Options\Plugins\FixedHeader; |
||
24 | use Options\Plugins\KeyTable; |
||
25 | use Options\Plugins\Responsive; |
||
26 | use Options\Plugins\RowGroup; |
||
27 | use Options\Plugins\RowReorder; |
||
28 | use Options\Plugins\Scroller; |
||
29 | use Options\Plugins\Select; |
||
30 | |||
31 | /** |
||
32 | * Set deferLoading option value. |
||
33 | * |
||
34 | * @param mixed $value |
||
35 | * @return $this |
||
36 | * @see https://datatables.net/reference/option/deferLoading |
||
37 | */ |
||
38 | public function deferLoading($value = null) |
||
44 | |||
45 | /** |
||
46 | * Set destroy option value. |
||
47 | * |
||
48 | * @param bool $value |
||
49 | * @return $this |
||
50 | * @see https://datatables.net/reference/option/destroy |
||
51 | */ |
||
52 | public function destroy(bool $value = false) |
||
58 | |||
59 | /** |
||
60 | * Set displayStart option value. |
||
61 | * |
||
62 | * @param int $value |
||
63 | * @return $this |
||
64 | * @see https://datatables.net/reference/option/displayStart |
||
65 | */ |
||
66 | public function displayStart(int $value = 0) |
||
72 | |||
73 | /** |
||
74 | * Set dom option value. |
||
75 | * |
||
76 | * @param string $value |
||
77 | * @return $this |
||
78 | * @see https://datatables.net/reference/option/dom |
||
79 | */ |
||
80 | public function dom(string $value) |
||
86 | |||
87 | /** |
||
88 | * Set lengthMenu option value. |
||
89 | * |
||
90 | * @param array $value |
||
91 | * @return $this |
||
92 | * @see https://datatables.net/reference/option/lengthMenu |
||
93 | */ |
||
94 | public function lengthMenu(array $value = [10, 25, 50, 100]) |
||
100 | |||
101 | /** |
||
102 | * Set orders option value. |
||
103 | * |
||
104 | * @param array $value |
||
105 | * @return $this |
||
106 | * @see https://datatables.net/reference/option/order |
||
107 | */ |
||
108 | public function orders(array $value) |
||
114 | |||
115 | /** |
||
116 | * Set orderCellsTop option value. |
||
117 | * |
||
118 | * @param bool $value |
||
119 | * @return $this |
||
120 | * @see https://datatables.net/reference/option/orderCellsTop |
||
121 | */ |
||
122 | public function orderCellsTop(bool $value = false) |
||
128 | |||
129 | /** |
||
130 | * Set orderClasses option value. |
||
131 | * |
||
132 | * @param bool $value |
||
133 | * @return $this |
||
134 | * @see https://datatables.net/reference/option/orderClasses |
||
135 | */ |
||
136 | public function orderClasses(bool $value = true) |
||
142 | |||
143 | /** |
||
144 | * Order option builder. |
||
145 | * |
||
146 | * @param int|array $index |
||
147 | * @param string $direction |
||
148 | * @return $this |
||
149 | * @see https://datatables.net/reference/option/order |
||
150 | */ |
||
151 | View Code Duplication | public function orderBy($index, $direction = 'desc') |
|
165 | |||
166 | /** |
||
167 | * Order Fixed option builder. |
||
168 | * |
||
169 | * @param int|array $index |
||
170 | * @param string $direction |
||
171 | * @return $this |
||
172 | * @see https://datatables.net/reference/option/orderFixed |
||
173 | */ |
||
174 | View Code Duplication | public function orderByFixed($index, $direction = 'desc') |
|
188 | |||
189 | /** |
||
190 | * Set orderMulti option value. |
||
191 | * |
||
192 | * @param bool $value |
||
193 | * @return $this |
||
194 | * @see https://datatables.net/reference/option/orderMulti |
||
195 | */ |
||
196 | public function orderMulti(bool $value = true) |
||
202 | |||
203 | /** |
||
204 | * Set pageLength option value. |
||
205 | * |
||
206 | * @param int $value |
||
207 | * @return $this |
||
208 | * @see https://datatables.net/reference/option/pageLength |
||
209 | */ |
||
210 | public function pageLength(int $value = 10) |
||
216 | |||
217 | /** |
||
218 | * Set pagingType option value. |
||
219 | * |
||
220 | * @param string $value |
||
221 | * @return $this |
||
222 | * @see https://datatables.net/reference/option/pagingType |
||
223 | */ |
||
224 | public function pagingType(string $value = 'simple_numbers') |
||
230 | |||
231 | /** |
||
232 | * Set renderer option value. |
||
233 | * |
||
234 | * @param mixed $value |
||
235 | * @return $this |
||
236 | * @see https://datatables.net/reference/option/renderer |
||
237 | */ |
||
238 | public function renderer($value = 'bootstrap') |
||
244 | |||
245 | /** |
||
246 | * Set retrieve option value. |
||
247 | * |
||
248 | * @param bool $value |
||
249 | * @return $this |
||
250 | * @see https://datatables.net/reference/option/retrieve |
||
251 | */ |
||
252 | public function retrieve(bool $value = false) |
||
258 | |||
259 | /** |
||
260 | * Set rowId option value. |
||
261 | * |
||
262 | * @param string $value |
||
263 | * @return $this |
||
264 | * @see https://datatables.net/reference/option/rowId |
||
265 | */ |
||
266 | public function rowId(string $value = 'DT_RowId') |
||
272 | |||
273 | /** |
||
274 | * Set scrollCollapse option value. |
||
275 | * |
||
276 | * @param mixed $value |
||
277 | * @return $this |
||
278 | * @see https://datatables.net/reference/option/scrollCollapse |
||
279 | */ |
||
280 | public function scrollCollapse($value = false) |
||
286 | |||
287 | /** |
||
288 | * Set search option value. |
||
289 | * |
||
290 | * @param array $value |
||
291 | * @return $this |
||
292 | * @see https://datatables.net/reference/option/search |
||
293 | */ |
||
294 | public function search(array $value) |
||
300 | |||
301 | /** |
||
302 | * Set searchCols option value. |
||
303 | * |
||
304 | * @param array $value |
||
305 | * @return $this |
||
306 | * @see https://datatables.net/reference/option/searchCols |
||
307 | */ |
||
308 | public function searchCols(array $value) |
||
314 | |||
315 | /** |
||
316 | * Set searchDelay option value. |
||
317 | * |
||
318 | * @param int $value |
||
319 | * @return $this |
||
320 | * @see https://datatables.net/reference/option/searchDelay |
||
321 | */ |
||
322 | public function searchDelay(int $value) |
||
328 | |||
329 | /** |
||
330 | * Set stateDuration option value. |
||
331 | * |
||
332 | * @param int $value |
||
333 | * @return $this |
||
334 | * @see https://datatables.net/reference/option/stateDuration |
||
335 | */ |
||
336 | public function stateDuration(int $value) |
||
342 | |||
343 | /** |
||
344 | * Set stripeClasses option value. |
||
345 | * |
||
346 | * @param array $value |
||
347 | * @return $this |
||
348 | * @see https://datatables.net/reference/option/stripeClasses |
||
349 | */ |
||
350 | public function stripeClasses(array $value) |
||
356 | |||
357 | /** |
||
358 | * Set tabIndex option value. |
||
359 | * |
||
360 | * @param int $value |
||
361 | * @return $this |
||
362 | * @see https://datatables.net/reference/option/tabIndex |
||
363 | */ |
||
364 | public function tabIndex(int $value = 0) |
||
370 | } |
||
371 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: