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:
Complex classes like Form often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Form, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class Form extends Interactor |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @var array |
||
| 19 | */ |
||
| 20 | protected $fields = []; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | protected $modalId; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | protected $modalSize = ''; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | protected $confirm = ''; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @param string $label |
||
| 39 | * |
||
| 40 | * @return array |
||
| 41 | */ |
||
| 42 | protected function formatLabel($label) |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @param string $column |
||
| 49 | * @param string $label |
||
| 50 | * |
||
| 51 | * @return Field\Text |
||
| 52 | */ |
||
| 53 | public function text($column, $label = '') |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @param $column |
||
| 64 | * @param string $label |
||
| 65 | * @param \Closure $builder |
||
| 66 | * |
||
| 67 | * @return Field\Table |
||
| 68 | */ |
||
| 69 | public function table($column, $label = '', $builder = null) |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @param string $column |
||
| 80 | * @param string $label |
||
| 81 | * |
||
| 82 | * @return Field\Text |
||
| 83 | */ |
||
| 84 | public function email($column, $label = '') |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @param string $column |
||
| 95 | * @param string $label |
||
| 96 | * |
||
| 97 | * @return Field\Text |
||
| 98 | */ |
||
| 99 | public function integer($column, $label = '') |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @param string $column |
||
| 108 | * @param string $label |
||
| 109 | * |
||
| 110 | * @return Field\Text |
||
| 111 | */ |
||
| 112 | public function ip($column, $label = '') |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @param string $column |
||
| 121 | * @param string $label |
||
| 122 | * |
||
| 123 | * @return Field\Text |
||
| 124 | */ |
||
| 125 | public function url($column, $label = '') |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @param string $column |
||
| 134 | * @param string $label |
||
| 135 | * |
||
| 136 | * @return Field\Text |
||
| 137 | */ |
||
| 138 | public function password($column, $label = '') |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @param string $column |
||
| 146 | * @param string $label |
||
| 147 | * |
||
| 148 | * @return Field\Text |
||
| 149 | */ |
||
| 150 | public function mobile($column, $label = '') |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @param string $column |
||
| 159 | * @param string $label |
||
| 160 | * |
||
| 161 | * @return Field\Textarea |
||
| 162 | */ |
||
| 163 | public function textarea($column, $label = '') |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @param string $column |
||
| 174 | * @param string $label |
||
| 175 | * |
||
| 176 | * @return Field\Select |
||
| 177 | */ |
||
| 178 | public function select($column, $label = '') |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @param string $column |
||
| 189 | * @param string $label |
||
| 190 | * |
||
| 191 | * @return Field\MultipleSelect |
||
| 192 | */ |
||
| 193 | public function multipleSelect($column, $label = '') |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @param string $column |
||
| 204 | * @param string $label |
||
| 205 | * |
||
| 206 | * @return Field\Checkbox |
||
| 207 | */ |
||
| 208 | public function checkbox($column, $label = '') |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @param string $column |
||
| 219 | * @param string $label |
||
| 220 | * |
||
| 221 | * @return Field\Radio |
||
| 222 | */ |
||
| 223 | public function radio($column, $label = '') |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @param string $column |
||
| 234 | * @param string $label |
||
| 235 | * |
||
| 236 | * @return Field\File |
||
| 237 | */ |
||
| 238 | public function file($column, $label = '') |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @param string $column |
||
| 249 | * @param string $label |
||
| 250 | * |
||
| 251 | * @return Field\MultipleFile |
||
| 252 | */ |
||
| 253 | public function multipleFile($column, $label = '') |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @param string $column |
||
| 264 | * @param string $label |
||
| 265 | * |
||
| 266 | * @return Field\Image |
||
| 267 | */ |
||
| 268 | public function image($column, $label = '') |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @param string $column |
||
| 279 | * @param string $label |
||
| 280 | * |
||
| 281 | * @return Field\MultipleImage |
||
| 282 | */ |
||
| 283 | public function multipleImage($column, $label = '') |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @param string $column |
||
| 294 | * @param string $label |
||
| 295 | * |
||
| 296 | * @return Field\Date |
||
| 297 | */ |
||
| 298 | public function date($column, $label = '') |
||
| 306 | |||
| 307 | /** |
||
| 308 | * @param string $column |
||
| 309 | * @param string $label |
||
| 310 | * |
||
| 311 | * @return Field\Date |
||
| 312 | */ |
||
| 313 | public function datetime($column, $label = '') |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @param string $column |
||
| 320 | * @param string $label |
||
| 321 | * |
||
| 322 | * @return Field\Date |
||
| 323 | */ |
||
| 324 | public function time($column, $label = '') |
||
| 328 | |||
| 329 | /** |
||
| 330 | * @param string $column |
||
| 331 | * @param string $label |
||
| 332 | * |
||
| 333 | * @return Field\Hidden |
||
| 334 | */ |
||
| 335 | public function hidden($column, $label = '') |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @param $message |
||
| 346 | * @return $this |
||
| 347 | */ |
||
| 348 | public function confirm($message) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @return $this |
||
| 357 | */ |
||
| 358 | public function modalLarge() |
||
| 364 | |||
| 365 | /** |
||
| 366 | * @return $this |
||
| 367 | */ |
||
| 368 | public function modalSmall() |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @param string $content |
||
| 377 | * @param string $selector |
||
| 378 | * |
||
| 379 | * @return string |
||
| 380 | */ |
||
| 381 | public function addElementAttr($content, $selector) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * @param Field $field |
||
| 393 | * |
||
| 394 | * @return Field |
||
| 395 | */ |
||
| 396 | View Code Duplication | protected function addField(Field $field) |
|
| 408 | |||
| 409 | /** |
||
| 410 | * @param Request $request |
||
| 411 | * |
||
| 412 | * @throws ValidationException |
||
| 413 | * @throws \Exception |
||
| 414 | * |
||
| 415 | * @return void |
||
| 416 | */ |
||
| 417 | public function validate(Request $request) |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Merge validation messages from input validators. |
||
| 447 | * |
||
| 448 | * @param \Illuminate\Validation\Validator[] $validators |
||
| 449 | * |
||
| 450 | * @return MessageBag |
||
| 451 | */ |
||
| 452 | protected function mergeValidationMessages($validators) |
||
| 462 | |||
| 463 | /** |
||
| 464 | * @param string $class |
||
| 465 | * |
||
| 466 | * @return string |
||
| 467 | */ |
||
| 468 | View Code Duplication | protected function resolveView($class) |
|
| 476 | |||
| 477 | /** |
||
| 478 | * @return void |
||
| 479 | */ |
||
| 480 | public function addModalHtml() |
||
| 493 | |||
| 494 | /** |
||
| 495 | * @return string |
||
| 496 | */ |
||
| 497 | public function getModalId() |
||
| 509 | |||
| 510 | /** |
||
| 511 | * @return void |
||
| 512 | */ |
||
| 513 | public function addScript() |
||
| 542 | |||
| 543 | /** |
||
| 544 | * @return string |
||
| 545 | */ |
||
| 546 | protected function buildConfirmActionPromise() |
||
| 591 | |||
| 592 | protected function buildGeneralActionPromise() |
||
| 626 | |||
| 627 | /** |
||
| 628 | * @throws \Exception |
||
| 629 | * |
||
| 630 | * @return string |
||
| 631 | */ |
||
| 632 | protected function buildActionPromise() |
||
| 648 | } |
||
| 649 |
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.