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 |
||
| 13 | trait Select |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * Set select option value. |
||
| 17 | * |
||
| 18 | * @param bool|array $value |
||
| 19 | * @return $this |
||
| 20 | * @see https://datatables.net/reference/option/select |
||
| 21 | */ |
||
| 22 | public function select($value = true) |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Set select blurable option value. |
||
| 31 | * |
||
| 32 | * @param bool $value |
||
| 33 | * @return $this |
||
| 34 | * @see https://datatables.net/reference/option/select.blurable |
||
| 35 | */ |
||
| 36 | public function selectBlurable(bool $value = true) |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Set select className option value. |
||
| 45 | * |
||
| 46 | * @param string $value |
||
| 47 | * @return $this |
||
| 48 | * @see https://datatables.net/reference/option/select.className |
||
| 49 | */ |
||
| 50 | public function selectClassName($value = 'selected') |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Append a class name to className option value. |
||
| 59 | * |
||
| 60 | * @param string $class |
||
| 61 | * @return $this |
||
| 62 | */ |
||
| 63 | View Code Duplication | public function addClass($class) |
|
| 72 | |||
| 73 | |||
| 74 | /** |
||
| 75 | * Set select info option value. |
||
| 76 | * |
||
| 77 | * @param bool $value |
||
| 78 | * @return $this |
||
| 79 | * @see https://datatables.net/reference/option/select.info |
||
| 80 | */ |
||
| 81 | public function selectInfo(bool $value = true) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Set select items option value. |
||
| 90 | * |
||
| 91 | * @param string $value |
||
| 92 | * @return $this |
||
| 93 | * @see https://datatables.net/reference/option/select.items |
||
| 94 | */ |
||
| 95 | public function selectItems($value = 'row') |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Set select items option value to row. |
||
| 104 | * |
||
| 105 | * @return $this |
||
| 106 | * @see https://datatables.net/reference/option/select.items |
||
| 107 | */ |
||
| 108 | public function selectItemsRow() |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Set select items option value to column. |
||
| 117 | * |
||
| 118 | * @return $this |
||
| 119 | * @see https://datatables.net/reference/option/select.items |
||
| 120 | */ |
||
| 121 | public function selectItemsColumn() |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Set select items option value to cell. |
||
| 130 | * |
||
| 131 | * @return $this |
||
| 132 | * @see https://datatables.net/reference/option/select.items |
||
| 133 | */ |
||
| 134 | public function selectItemsCell() |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Set select selector option value. |
||
| 143 | * |
||
| 144 | * @param string $value |
||
| 145 | * @return $this |
||
| 146 | * @see https://datatables.net/reference/option/select.selector |
||
| 147 | */ |
||
| 148 | public function selectSelector($value = 'td') |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Set select style option value. |
||
| 157 | * |
||
| 158 | * @param string $value |
||
| 159 | * @return $this |
||
| 160 | * @see https://datatables.net/reference/option/select.style |
||
| 161 | */ |
||
| 162 | public function selectStyle($value = 'os') |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Set select style option value to api. |
||
| 171 | * |
||
| 172 | * @return $this |
||
| 173 | * @see https://datatables.net/reference/option/select.style |
||
| 174 | */ |
||
| 175 | public function selectStyleApi() |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Set select style option value to single. |
||
| 184 | * |
||
| 185 | * @return $this |
||
| 186 | * @see https://datatables.net/reference/option/select.style |
||
| 187 | */ |
||
| 188 | public function selectStyleSingle() |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Set select style option value to multi. |
||
| 197 | * |
||
| 198 | * @return $this |
||
| 199 | * @see https://datatables.net/reference/option/select.style |
||
| 200 | */ |
||
| 201 | public function selectStyleMulti() |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Set select style option value to os. |
||
| 210 | * |
||
| 211 | * @return $this |
||
| 212 | * @see https://datatables.net/reference/option/select.style |
||
| 213 | */ |
||
| 214 | public function selectStyleOS() |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Set select style option value to multi+shift. |
||
| 223 | * |
||
| 224 | * @return $this |
||
| 225 | * @see https://datatables.net/reference/option/select.style |
||
| 226 | */ |
||
| 227 | public function selectStyleMultiShift() |
||
| 233 | } |
||
| 234 |
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: