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 | use Options\Plugins\SearchPanes; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Set deferLoading option value. |
||
| 34 | * |
||
| 35 | * @param mixed $value |
||
| 36 | * @return $this |
||
| 37 | * @see https://datatables.net/reference/option/deferLoading |
||
| 38 | */ |
||
| 39 | public function deferLoading($value = null) |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Set destroy option value. |
||
| 48 | * |
||
| 49 | * @param bool $value |
||
| 50 | * @return $this |
||
| 51 | * @see https://datatables.net/reference/option/destroy |
||
| 52 | */ |
||
| 53 | public function destroy(bool $value = false) |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Set displayStart option value. |
||
| 62 | * |
||
| 63 | * @param int $value |
||
| 64 | * @return $this |
||
| 65 | * @see https://datatables.net/reference/option/displayStart |
||
| 66 | */ |
||
| 67 | public function displayStart(int $value = 0) |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Set dom option value. |
||
| 76 | * |
||
| 77 | * @param string $value |
||
| 78 | * @return $this |
||
| 79 | * @see https://datatables.net/reference/option/dom |
||
| 80 | */ |
||
| 81 | public function dom(string $value) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Set lengthMenu option value. |
||
| 90 | * |
||
| 91 | * @param array $value |
||
| 92 | * @return $this |
||
| 93 | * @see https://datatables.net/reference/option/lengthMenu |
||
| 94 | */ |
||
| 95 | public function lengthMenu(array $value = [10, 25, 50, 100]) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Set orders option value. |
||
| 104 | * |
||
| 105 | * @param array $value |
||
| 106 | * @return $this |
||
| 107 | * @see https://datatables.net/reference/option/order |
||
| 108 | */ |
||
| 109 | public function orders(array $value) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Set orderCellsTop option value. |
||
| 118 | * |
||
| 119 | * @param bool $value |
||
| 120 | * @return $this |
||
| 121 | * @see https://datatables.net/reference/option/orderCellsTop |
||
| 122 | */ |
||
| 123 | public function orderCellsTop(bool $value = false) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Set orderClasses option value. |
||
| 132 | * |
||
| 133 | * @param bool $value |
||
| 134 | * @return $this |
||
| 135 | * @see https://datatables.net/reference/option/orderClasses |
||
| 136 | */ |
||
| 137 | public function orderClasses(bool $value = true) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Order option builder. |
||
| 146 | * |
||
| 147 | * @param int|array $index |
||
| 148 | * @param string $direction |
||
| 149 | * @return $this |
||
| 150 | * @see https://datatables.net/reference/option/order |
||
| 151 | */ |
||
| 152 | View Code Duplication | public function orderBy($index, $direction = 'desc') |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Order Fixed option builder. |
||
| 169 | * |
||
| 170 | * @param int|array $index |
||
| 171 | * @param string $direction |
||
| 172 | * @return $this |
||
| 173 | * @see https://datatables.net/reference/option/orderFixed |
||
| 174 | */ |
||
| 175 | View Code Duplication | public function orderByFixed($index, $direction = 'desc') |
|
| 189 | |||
| 190 | /** |
||
| 191 | * Set orderMulti option value. |
||
| 192 | * |
||
| 193 | * @param bool $value |
||
| 194 | * @return $this |
||
| 195 | * @see https://datatables.net/reference/option/orderMulti |
||
| 196 | */ |
||
| 197 | public function orderMulti(bool $value = true) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Set pageLength option value. |
||
| 206 | * |
||
| 207 | * @param int $value |
||
| 208 | * @return $this |
||
| 209 | * @see https://datatables.net/reference/option/pageLength |
||
| 210 | */ |
||
| 211 | public function pageLength(int $value = 10) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Set pagingType option value. |
||
| 220 | * |
||
| 221 | * @param string $value |
||
| 222 | * @return $this |
||
| 223 | * @see https://datatables.net/reference/option/pagingType |
||
| 224 | */ |
||
| 225 | public function pagingType(string $value = 'simple_numbers') |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Set renderer option value. |
||
| 234 | * |
||
| 235 | * @param mixed $value |
||
| 236 | * @return $this |
||
| 237 | * @see https://datatables.net/reference/option/renderer |
||
| 238 | */ |
||
| 239 | public function renderer($value = 'bootstrap') |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Set retrieve option value. |
||
| 248 | * |
||
| 249 | * @param bool $value |
||
| 250 | * @return $this |
||
| 251 | * @see https://datatables.net/reference/option/retrieve |
||
| 252 | */ |
||
| 253 | public function retrieve(bool $value = false) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Set rowId option value. |
||
| 262 | * |
||
| 263 | * @param string $value |
||
| 264 | * @return $this |
||
| 265 | * @see https://datatables.net/reference/option/rowId |
||
| 266 | */ |
||
| 267 | public function rowId(string $value = 'DT_RowId') |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Set scrollCollapse option value. |
||
| 276 | * |
||
| 277 | * @param mixed $value |
||
| 278 | * @return $this |
||
| 279 | * @see https://datatables.net/reference/option/scrollCollapse |
||
| 280 | */ |
||
| 281 | public function scrollCollapse($value = false) |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Set search option value. |
||
| 290 | * |
||
| 291 | * @param array $value |
||
| 292 | * @return $this |
||
| 293 | * @see https://datatables.net/reference/option/search |
||
| 294 | */ |
||
| 295 | public function search(array $value) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Set searchCols option value. |
||
| 304 | * |
||
| 305 | * @param array $value |
||
| 306 | * @return $this |
||
| 307 | * @see https://datatables.net/reference/option/searchCols |
||
| 308 | */ |
||
| 309 | public function searchCols(array $value) |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Set searchDelay option value. |
||
| 318 | * |
||
| 319 | * @param int $value |
||
| 320 | * @return $this |
||
| 321 | * @see https://datatables.net/reference/option/searchDelay |
||
| 322 | */ |
||
| 323 | public function searchDelay(int $value) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Set stateDuration option value. |
||
| 332 | * |
||
| 333 | * @param int $value |
||
| 334 | * @return $this |
||
| 335 | * @see https://datatables.net/reference/option/stateDuration |
||
| 336 | */ |
||
| 337 | public function stateDuration(int $value) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Set stripeClasses option value. |
||
| 346 | * |
||
| 347 | * @param array $value |
||
| 348 | * @return $this |
||
| 349 | * @see https://datatables.net/reference/option/stripeClasses |
||
| 350 | */ |
||
| 351 | public function stripeClasses(array $value) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Set tabIndex option value. |
||
| 360 | * |
||
| 361 | * @param int $value |
||
| 362 | * @return $this |
||
| 363 | * @see https://datatables.net/reference/option/tabIndex |
||
| 364 | */ |
||
| 365 | public function tabIndex(int $value = 0) |
||
| 371 | } |
||
| 372 |
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: