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 RegistrationPresenter 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 RegistrationPresenter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class RegistrationPresenter extends VisitorPresenter |
||
| 32 | { |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var VisitorModel |
||
| 36 | */ |
||
| 37 | private $visitorModel; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var ProgramModel |
||
| 41 | */ |
||
| 42 | private $programModel; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var UserService |
||
| 46 | */ |
||
| 47 | private $userService; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var ProgramService |
||
| 51 | */ |
||
| 52 | private $programService; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var SettingsModel |
||
| 56 | */ |
||
| 57 | protected $settingsModel; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var boolean |
||
| 61 | */ |
||
| 62 | private $disabled = false; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var IRegistrationFormFactory |
||
| 66 | */ |
||
| 67 | private $registrationFormFactory; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var EventService |
||
| 71 | */ |
||
| 72 | protected $skautisEventService; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @param MeetingModel $meetingModel |
||
| 76 | * @param UserService $userService |
||
| 77 | * @param VisitorModel $visitorModel |
||
| 78 | * @param MealModel $mealModel |
||
| 79 | * @param ProgramModel $programModel |
||
| 80 | * @param VisitorService $visitorService |
||
| 81 | * @param SettingsModel $settingsModel |
||
| 82 | */ |
||
| 83 | public function __construct( |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @return IRegistrationFormFactory |
||
| 109 | */ |
||
| 110 | public function getRegistrationFormFactory(): IRegistrationFormFactory |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Injector |
||
| 117 | * |
||
| 118 | * @param IRegistrationFormFactory $factory |
||
| 119 | */ |
||
| 120 | |||
| 121 | protected $error = FALSE; |
||
| 122 | |||
| 123 | protected $hash = NULL; |
||
| 124 | private $item; |
||
| 125 | private $mealData; |
||
| 126 | private $user; |
||
|
|
|||
| 127 | private $event; |
||
| 128 | |||
| 129 | public function injectRegistrationFormFactory(IRegistrationFormFactory $factory) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @return void |
||
| 136 | */ |
||
| 137 | public function startup() |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Process data from form |
||
| 162 | * |
||
| 163 | * @return void |
||
| 164 | */ |
||
| 165 | View Code Duplication | public function actionCreate() |
|
| 189 | |||
| 190 | /** |
||
| 191 | * @param string $guid |
||
| 192 | * @return void |
||
| 193 | */ |
||
| 194 | View Code Duplication | public function actionUpdate($guid) |
|
| 217 | |||
| 218 | /** |
||
| 219 | * Renders default template |
||
| 220 | */ |
||
| 221 | public function renderDefault() |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Renders new template |
||
| 235 | */ |
||
| 236 | public function renderNew() |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @param string $guid |
||
| 246 | * @return void |
||
| 247 | */ |
||
| 248 | public function renderCheck($guid) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @param string $guid |
||
| 265 | * @return void |
||
| 266 | */ |
||
| 267 | public function renderEdit($guid) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @return RegistrationFormControl |
||
| 285 | */ |
||
| 286 | protected function createComponentRegistrationForm(): RegistrationForm |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @return VisitorEntity |
||
| 338 | */ |
||
| 339 | protected function useLoggedVisitor(): VisitorEntity |
||
| 368 | |||
| 369 | /** |
||
| 370 | * @return MealModel |
||
| 371 | */ |
||
| 372 | protected function getMealModel() |
||
| 376 | |||
| 377 | /** |
||
| 378 | * @param MealModel $model |
||
| 379 | * @return $this |
||
| 380 | */ |
||
| 381 | protected function setMealModel(MealModel $model) |
||
| 387 | |||
| 388 | /** |
||
| 389 | * @return MeetingModel |
||
| 390 | */ |
||
| 391 | protected function getMeetingModel() |
||
| 395 | |||
| 396 | /** |
||
| 397 | * @param MeetingModel $model |
||
| 398 | * @return $this |
||
| 399 | */ |
||
| 400 | protected function setMeetingModel(MeetingModel $model) |
||
| 406 | |||
| 407 | /** |
||
| 408 | * @return VisitorModel |
||
| 409 | */ |
||
| 410 | protected function getVisitorModel() |
||
| 414 | |||
| 415 | /** |
||
| 416 | * @param VisitorModel $model |
||
| 417 | * @return $this |
||
| 418 | */ |
||
| 419 | protected function setVisitorModel(VisitorModel $model) |
||
| 425 | |||
| 426 | /** |
||
| 427 | * @return ProgramModel |
||
| 428 | */ |
||
| 429 | protected function getProgramModel() |
||
| 433 | |||
| 434 | /** |
||
| 435 | * @param ProgramModel $model |
||
| 436 | * @return $this |
||
| 437 | */ |
||
| 438 | protected function setProgramModel(ProgramModel $model) |
||
| 444 | |||
| 445 | /** |
||
| 446 | * @return UserService |
||
| 447 | */ |
||
| 448 | protected function getUserService() |
||
| 452 | |||
| 453 | /** |
||
| 454 | * @param UserService $service |
||
| 455 | * @return $this |
||
| 456 | */ |
||
| 457 | protected function setUserService(UserService $service) |
||
| 463 | |||
| 464 | /** |
||
| 465 | * @return ProgramService |
||
| 466 | */ |
||
| 467 | protected function getProgramService() |
||
| 471 | |||
| 472 | /** |
||
| 473 | * @param ProgramService $service |
||
| 474 | * @return $this |
||
| 475 | */ |
||
| 476 | protected function setProgramService(ProgramService $service) |
||
| 482 | |||
| 483 | /** |
||
| 484 | * @return EventService |
||
| 485 | */ |
||
| 486 | protected function getEventService(): EventService |
||
| 490 | |||
| 491 | /** |
||
| 492 | * @param EventService $skautisEvent |
||
| 493 | * |
||
| 494 | * @return self |
||
| 495 | */ |
||
| 496 | protected function setEventService(EventService $service): self |
||
| 502 | |||
| 503 | |||
| 504 | /** |
||
| 505 | * @return SettingsModel |
||
| 506 | */ |
||
| 507 | public function getSettingsModel() |
||
| 511 | |||
| 512 | /** |
||
| 513 | * @param SettingsModel $model |
||
| 514 | * |
||
| 515 | * @return self |
||
| 516 | */ |
||
| 517 | public function setSettingsModel(SettingsModel $model): self |
||
| 523 | |||
| 524 | } |
||
| 525 |