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 ModelConfiguration 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 ModelConfiguration, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class ModelConfiguration extends ModelConfigurationManager |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @var string |
||
| 16 | */ |
||
| 17 | protected $createTitle; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | protected $editTitle; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var Closure|null |
||
| 26 | */ |
||
| 27 | protected $display; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var Closure|null |
||
| 31 | */ |
||
| 32 | protected $create; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var bool |
||
| 36 | */ |
||
| 37 | protected $displayable = true; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var bool |
||
| 41 | */ |
||
| 42 | protected $creatable = true; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var bool |
||
| 46 | */ |
||
| 47 | protected $editable = true; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var bool |
||
| 51 | */ |
||
| 52 | protected $restorable = true; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var bool |
||
| 56 | */ |
||
| 57 | protected $deletable = true; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var bool |
||
| 61 | */ |
||
| 62 | protected $destroyable = true; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var Closure|null |
||
| 66 | */ |
||
| 67 | protected $edit; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var Closure|null |
||
| 71 | */ |
||
| 72 | protected $delete = true; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var Closure|null |
||
| 76 | */ |
||
| 77 | protected $destroy = true; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var Closure|null |
||
| 81 | */ |
||
| 82 | protected $restore = true; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var string |
||
| 86 | */ |
||
| 87 | protected $messageOnCreate; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var string |
||
| 91 | */ |
||
| 92 | protected $messageOnUpdate; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var string |
||
| 96 | */ |
||
| 97 | protected $messageOnDelete; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var string |
||
| 101 | */ |
||
| 102 | protected $messageOnDestroy; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @var string |
||
| 106 | */ |
||
| 107 | protected $messageOnRestore; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @param string $alias |
||
| 111 | * |
||
| 112 | * @return $this |
||
| 113 | */ |
||
| 114 | 1 | public function setAlias($alias) |
|
| 120 | |||
| 121 | /** |
||
| 122 | * @param string $title |
||
| 123 | * |
||
| 124 | * @return $this |
||
| 125 | */ |
||
| 126 | 1 | public function setTitle($title) |
|
| 132 | |||
| 133 | /** |
||
| 134 | * @return string|\Symfony\Component\Translation\TranslatorInterface |
||
| 135 | */ |
||
| 136 | 1 | public function getCreateTitle() |
|
| 144 | |||
| 145 | /** |
||
| 146 | * @param string $title |
||
| 147 | * |
||
| 148 | * @return $this |
||
| 149 | */ |
||
| 150 | 1 | public function setCreateTitle($title) |
|
| 156 | |||
| 157 | /** |
||
| 158 | * @return string|\Symfony\Component\Translation\TranslatorInterface |
||
| 159 | */ |
||
| 160 | 1 | public function getEditTitle() |
|
| 168 | |||
| 169 | /** |
||
| 170 | * @param string $title |
||
| 171 | * |
||
| 172 | * @return $this |
||
| 173 | */ |
||
| 174 | 1 | public function setEditTitle($title) |
|
| 180 | |||
| 181 | /** |
||
| 182 | * @return bool |
||
| 183 | */ |
||
| 184 | 1 | public function isDisplayable() |
|
| 188 | |||
| 189 | /** |
||
| 190 | * @return $this |
||
| 191 | */ |
||
| 192 | 1 | public function disableDisplay() |
|
| 198 | |||
| 199 | /** |
||
| 200 | * @param string $action |
||
| 201 | * @param \Illuminate\Database\Eloquent\Model $model |
||
| 202 | * |
||
| 203 | * @return bool |
||
| 204 | */ |
||
| 205 | 6 | public function can($action, Model $model) |
|
| 213 | |||
| 214 | /** |
||
| 215 | * @return bool |
||
| 216 | */ |
||
| 217 | 1 | public function isCreatable() |
|
| 225 | |||
| 226 | /** |
||
| 227 | * @return $this |
||
| 228 | */ |
||
| 229 | 1 | public function disableCreating() |
|
| 235 | |||
| 236 | /** |
||
| 237 | * @param Model $model |
||
| 238 | * |
||
| 239 | * @return bool |
||
| 240 | */ |
||
| 241 | 1 | public function isEditable(Model $model) |
|
| 249 | |||
| 250 | /** |
||
| 251 | * @return $this |
||
| 252 | */ |
||
| 253 | 1 | public function disableEditing() |
|
| 259 | |||
| 260 | /** |
||
| 261 | * @param bool $deletable |
||
| 262 | * |
||
| 263 | * @return $this |
||
| 264 | */ |
||
| 265 | 1 | public function setDeletable($deletable) |
|
| 271 | |||
| 272 | /** |
||
| 273 | * @param Model $model |
||
| 274 | * |
||
| 275 | * @return bool |
||
| 276 | */ |
||
| 277 | 1 | public function isDeletable(Model $model) |
|
| 281 | |||
| 282 | /** |
||
| 283 | * @return $this |
||
| 284 | */ |
||
| 285 | 1 | public function disableDeleting() |
|
| 291 | |||
| 292 | /** |
||
| 293 | * @param Model $model |
||
| 294 | * |
||
| 295 | * @return bool |
||
| 296 | */ |
||
| 297 | 3 | public function isDestroyable(Model $model) |
|
| 301 | |||
| 302 | /** |
||
| 303 | * @return $this |
||
| 304 | */ |
||
| 305 | 1 | public function disableDestroying() |
|
| 311 | |||
| 312 | /** |
||
| 313 | * @param Model $model |
||
| 314 | * |
||
| 315 | * @return bool |
||
| 316 | */ |
||
| 317 | 1 | public function isRestorable(Model $model) |
|
| 321 | |||
| 322 | /** |
||
| 323 | * @return bool |
||
| 324 | */ |
||
| 325 | 6 | public function isRestorableModel() |
|
| 329 | |||
| 330 | /** |
||
| 331 | * @return $this |
||
| 332 | */ |
||
| 333 | 1 | public function disableRestoring() |
|
| 339 | |||
| 340 | /** |
||
| 341 | * @return Closure|null |
||
| 342 | */ |
||
| 343 | 1 | public function getDisplay() |
|
| 347 | |||
| 348 | /** |
||
| 349 | * @param Closure $callback |
||
| 350 | * |
||
| 351 | * @return $this |
||
| 352 | */ |
||
| 353 | 1 | public function onDisplay(Closure $callback) |
|
| 359 | |||
| 360 | /** |
||
| 361 | * @param array|null $payload |
||
| 362 | * @return DisplayInterface|mixed |
||
| 363 | */ |
||
| 364 | 1 | View Code Duplication | public function fireDisplay(array $payload = []) |
| 386 | |||
| 387 | /** |
||
| 388 | * @return Closure|null |
||
| 389 | */ |
||
| 390 | 3 | public function getCreate() |
|
| 394 | |||
| 395 | /** |
||
| 396 | * @param Closure|null $callback |
||
| 397 | * |
||
| 398 | * @return $this |
||
| 399 | */ |
||
| 400 | 3 | public function onCreate(Closure $callback = null) |
|
| 406 | |||
| 407 | /** |
||
| 408 | * @return mixed|void |
||
| 409 | */ |
||
| 410 | 1 | View Code Duplication | public function fireCreate() |
| 431 | |||
| 432 | /** |
||
| 433 | * @return Closure|null |
||
| 434 | */ |
||
| 435 | 3 | public function getEdit() |
|
| 439 | |||
| 440 | /** |
||
| 441 | * @param Closure|null $callback |
||
| 442 | * |
||
| 443 | * @return $this |
||
| 444 | */ |
||
| 445 | 3 | public function onEdit(Closure $callback = null) |
|
| 451 | |||
| 452 | /** |
||
| 453 | * @param int|string $id |
||
| 454 | * |
||
| 455 | * @return mixed|void |
||
| 456 | */ |
||
| 457 | 1 | public function fireEdit($id) |
|
| 482 | |||
| 483 | /** |
||
| 484 | * @param Closure|null $callback |
||
| 485 | * |
||
| 486 | * @return $this |
||
| 487 | */ |
||
| 488 | 1 | public function onCreateAndEdit(Closure $callback = null) |
|
| 495 | |||
| 496 | /** |
||
| 497 | * @return Closure|null |
||
| 498 | */ |
||
| 499 | 1 | public function getDelete() |
|
| 503 | |||
| 504 | /** |
||
| 505 | * @param Closure|null $callback |
||
| 506 | * |
||
| 507 | * @return $this |
||
| 508 | */ |
||
| 509 | 1 | public function onDelete(Closure $callback = null) |
|
| 515 | |||
| 516 | /** |
||
| 517 | * @param int|string $id |
||
| 518 | * |
||
| 519 | * @return mixed |
||
| 520 | */ |
||
| 521 | 1 | public function fireDelete($id) |
|
| 527 | |||
| 528 | /** |
||
| 529 | * @return Closure|null |
||
| 530 | */ |
||
| 531 | 1 | public function getDestroy() |
|
| 535 | |||
| 536 | /** |
||
| 537 | * @param Closure|null $callback |
||
| 538 | * |
||
| 539 | * @return $this |
||
| 540 | */ |
||
| 541 | 1 | public function onDestroy(Closure $callback = null) |
|
| 547 | |||
| 548 | /** |
||
| 549 | * @param int|string $id |
||
| 550 | * |
||
| 551 | * @return mixed |
||
| 552 | */ |
||
| 553 | 1 | public function fireDestroy($id) |
|
| 559 | |||
| 560 | /** |
||
| 561 | * @return Closure|null |
||
| 562 | */ |
||
| 563 | 1 | public function getRestore() |
|
| 567 | |||
| 568 | /** |
||
| 569 | * @param Closure|null $callback |
||
| 570 | * |
||
| 571 | * @return $this |
||
| 572 | */ |
||
| 573 | 1 | public function onRestore(Closure $callback = null) |
|
| 579 | |||
| 580 | /** |
||
| 581 | * @param int|string $id |
||
| 582 | * |
||
| 583 | * @return bool|mixed |
||
| 584 | */ |
||
| 585 | 1 | public function fireRestore($id) |
|
| 591 | |||
| 592 | /** |
||
| 593 | * @return string |
||
| 594 | */ |
||
| 595 | 1 | public function getMessageOnCreate() |
|
| 603 | |||
| 604 | /** |
||
| 605 | * @param string $messageOnCreate |
||
| 606 | * |
||
| 607 | * @return $this |
||
| 608 | */ |
||
| 609 | 1 | public function setMessageOnCreate($messageOnCreate) |
|
| 615 | |||
| 616 | /** |
||
| 617 | * @return string |
||
| 618 | */ |
||
| 619 | 1 | public function getMessageOnUpdate() |
|
| 627 | |||
| 628 | /** |
||
| 629 | * @param string $messageOnUpdate |
||
| 630 | * |
||
| 631 | * @return $this |
||
| 632 | */ |
||
| 633 | 1 | public function setMessageOnUpdate($messageOnUpdate) |
|
| 639 | |||
| 640 | /** |
||
| 641 | * @return string |
||
| 642 | */ |
||
| 643 | 1 | public function getMessageOnDelete() |
|
| 651 | |||
| 652 | /** |
||
| 653 | * @param string $messageOnDelete |
||
| 654 | * |
||
| 655 | * @return $this |
||
| 656 | */ |
||
| 657 | 1 | public function setMessageOnDelete($messageOnDelete) |
|
| 663 | |||
| 664 | /** |
||
| 665 | * @return string |
||
| 666 | */ |
||
| 667 | 1 | public function getMessageOnDestroy() |
|
| 675 | |||
| 676 | /** |
||
| 677 | * @param string $messageOnDestroy |
||
| 678 | * |
||
| 679 | * @return $this |
||
| 680 | */ |
||
| 681 | 1 | public function setMessageOnDestroy($messageOnDestroy) |
|
| 687 | |||
| 688 | /** |
||
| 689 | * @return string |
||
| 690 | */ |
||
| 691 | 1 | public function getMessageOnRestore() |
|
| 699 | |||
| 700 | /** |
||
| 701 | * @param string $messageOnRestore |
||
| 702 | * |
||
| 703 | * @return $this |
||
| 704 | */ |
||
| 705 | 1 | public function setMessageOnRestore($messageOnRestore) |
|
| 711 | } |
||
| 712 |