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 |
||
| 14 | class Form extends Interactor |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * @var array |
||
| 18 | */ |
||
| 19 | protected $fields = []; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | protected $modalId; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @param string $label |
||
| 28 | * |
||
| 29 | * @return array |
||
| 30 | */ |
||
| 31 | protected function formatLabel($label) |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @param string $column |
||
| 38 | * @param string $label |
||
| 39 | * |
||
| 40 | * @return Field\Text |
||
| 41 | */ |
||
| 42 | public function text($column, $label = '') |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @param string $column |
||
| 53 | * @param string $label |
||
| 54 | * |
||
| 55 | * @return Field\Text |
||
| 56 | */ |
||
| 57 | public function email($column, $label = '') |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @param string $column |
||
| 68 | * @param string $label |
||
| 69 | * |
||
| 70 | * @return Field\Text |
||
| 71 | */ |
||
| 72 | public function integer($column, $label = '') |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @param string $column |
||
| 81 | * @param string $label |
||
| 82 | * |
||
| 83 | * @return Field\Text |
||
| 84 | */ |
||
| 85 | public function ip($column, $label = '') |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @param string $column |
||
| 94 | * @param string $label |
||
| 95 | * |
||
| 96 | * @return Field\Text |
||
| 97 | */ |
||
| 98 | public function url($column, $label = '') |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @param string $column |
||
| 107 | * @param string $label |
||
| 108 | * |
||
| 109 | * @return Field\Text |
||
| 110 | */ |
||
| 111 | public function password($column, $label = '') |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @param string $column |
||
| 119 | * @param string $label |
||
| 120 | * |
||
| 121 | * @return Field\Text |
||
| 122 | */ |
||
| 123 | public function mobile($column, $label = '') |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @param string $column |
||
| 132 | * @param string $label |
||
| 133 | * |
||
| 134 | * @return Field\Textarea |
||
| 135 | */ |
||
| 136 | public function textarea($column, $label = '') |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @param string $column |
||
| 147 | * @param string $label |
||
| 148 | * |
||
| 149 | * @return Field\Select |
||
| 150 | */ |
||
| 151 | public function select($column, $label = '') |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @param string $column |
||
| 162 | * @param string $label |
||
| 163 | * |
||
| 164 | * @return Field\MultipleSelect |
||
| 165 | */ |
||
| 166 | public function multipleSelect($column, $label = '') |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @param string $column |
||
| 177 | * @param string $label |
||
| 178 | * |
||
| 179 | * @return Field\Checkbox |
||
| 180 | */ |
||
| 181 | public function checkbox($column, $label = '') |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @param string $column |
||
| 192 | * @param string $label |
||
| 193 | * |
||
| 194 | * @return Field\Radio |
||
| 195 | */ |
||
| 196 | public function radio($column, $label = '') |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @param string $column |
||
| 207 | * @param string $label |
||
| 208 | * |
||
| 209 | * @return Field\File |
||
| 210 | */ |
||
| 211 | public function file($column, $label = '') |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @param string $column |
||
| 222 | * @param string $label |
||
| 223 | * |
||
| 224 | * @return Field\Image |
||
| 225 | */ |
||
| 226 | public function image($column, $label = '') |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @param string $column |
||
| 237 | * @param string $label |
||
| 238 | * |
||
| 239 | * @return Field\Date |
||
| 240 | */ |
||
| 241 | public function date($column, $label = '') |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @param string $column |
||
| 252 | * @param string $label |
||
| 253 | * |
||
| 254 | * @return Field\Date |
||
| 255 | */ |
||
| 256 | public function datetime($column, $label = '') |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @param string $column |
||
| 263 | * @param string $label |
||
| 264 | * |
||
| 265 | * @return Field\Date |
||
| 266 | */ |
||
| 267 | public function time($column, $label = '') |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @param string $content |
||
| 274 | * @param string $selector |
||
| 275 | * |
||
| 276 | * @return string |
||
| 277 | */ |
||
| 278 | public function addElementAttr($content, $selector) |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @param Field $field |
||
| 290 | * |
||
| 291 | * @return Field |
||
| 292 | */ |
||
| 293 | protected function addField(Field $field) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @param Request $request |
||
| 304 | * |
||
| 305 | * @return void |
||
| 306 | * |
||
| 307 | * @throws ValidationException |
||
| 308 | * @throws \Exception |
||
| 309 | */ |
||
| 310 | public function validate(Request $request) |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Merge validation messages from input validators. |
||
| 340 | * |
||
| 341 | * @param \Illuminate\Validation\Validator[] $validators |
||
| 342 | * |
||
| 343 | * @return MessageBag |
||
| 344 | */ |
||
| 345 | protected function mergeValidationMessages($validators) |
||
| 355 | |||
| 356 | /** |
||
| 357 | * @param string $class |
||
| 358 | * |
||
| 359 | * @return string |
||
| 360 | */ |
||
| 361 | View Code Duplication | protected function resolveView($class) |
|
| 369 | |||
| 370 | /** |
||
| 371 | * @param string $modalID |
||
| 372 | */ |
||
| 373 | public function addModalHtml($modalID) |
||
| 385 | |||
| 386 | /** |
||
| 387 | * @return void |
||
| 388 | */ |
||
| 389 | public function addScript() |
||
| 419 | |||
| 420 | /** |
||
| 421 | * @return string |
||
| 422 | * @throws \Exception |
||
| 423 | */ |
||
| 424 | protected function buildActionPromise() |
||
| 468 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.