Complex classes like RegistrationForm 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 RegistrationForm, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class RegistrationForm extends BaseForm |
||
| 17 | { |
||
| 18 | |||
| 19 | const TEMPLATE_NAME = 'RegistrationForm'; |
||
| 20 | |||
| 21 | const MESSAGE_REQUIRED = 'Hodnota musí být vyplněna!'; |
||
| 22 | const MESSAGE_MAX_LENGTH = '%label nesmí mít více jak %d znaků!'; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var Closure |
||
| 26 | */ |
||
| 27 | public $onRegistrationSave; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var ProvinceModel |
||
| 31 | */ |
||
| 32 | protected $provinceModel; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var ProgramModel |
||
| 36 | */ |
||
| 37 | protected $programModel; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var BlockModel |
||
| 41 | */ |
||
| 42 | protected $blockModel; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var MeetingModel |
||
| 46 | */ |
||
| 47 | protected $meetingModel; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var array |
||
| 51 | */ |
||
| 52 | protected $mealFields = []; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | protected $programFields = []; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var UserService |
||
| 61 | */ |
||
| 62 | protected $userService; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @param ProvinceModel $model |
||
|
|
|||
| 66 | */ |
||
| 67 | public function __construct( |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @return void |
||
| 83 | */ |
||
| 84 | public function render() |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @param array $defaults |
||
| 98 | * @return RegistrationForm |
||
| 99 | */ |
||
| 100 | public function setDefaults(array $defaults = []): RegistrationForm |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @return Form |
||
| 109 | */ |
||
| 110 | public function createComponentRegistrationForm(): Form |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @param Form $form |
||
| 199 | * @return void |
||
| 200 | */ |
||
| 201 | public function processForm(Form $form) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @param Form $form |
||
| 211 | * @return Form |
||
| 212 | */ |
||
| 213 | protected function setupRendering(Form $form): Form |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @param Form $form |
||
| 252 | * @return Form |
||
| 253 | */ |
||
| 254 | protected function buildProgramSwitcher(Form $form): Form |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @param Form $form |
||
| 282 | * @return Form |
||
| 283 | */ |
||
| 284 | protected function buildMealSwitcher(Form $form): Form |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @return ProvinceModel |
||
| 301 | */ |
||
| 302 | protected function getProvinceModel(): ProvinceModel |
||
| 306 | |||
| 307 | /** |
||
| 308 | * @param ProvinceModel $model |
||
| 309 | * @return RegistrationFormFactory |
||
| 310 | */ |
||
| 311 | protected function setProvinceModel(ProvinceModel $model): RegistrationForm |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @return ProgramModel |
||
| 320 | */ |
||
| 321 | protected function getProgramModel(): ProgramModel |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @param ProgramModel $model |
||
| 328 | * @return RegistrationFormFactory |
||
| 329 | */ |
||
| 330 | protected function setProgramModel(ProgramModel $model): RegistrationForm |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @return BlockModel |
||
| 339 | */ |
||
| 340 | protected function getBlockModel(): BlockModel |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @param BlockModel $model |
||
| 347 | * @return RegistrationForm |
||
| 348 | */ |
||
| 349 | protected function setBlockModel(BlockModel $model): RegistrationForm |
||
| 355 | |||
| 356 | /** |
||
| 357 | * @return MeetingModel |
||
| 358 | */ |
||
| 359 | protected function getMeetingModel(): MeetingModel |
||
| 363 | |||
| 364 | /** |
||
| 365 | * @param MeetingModel $model |
||
| 366 | * @return RegistrationForm |
||
| 367 | */ |
||
| 368 | protected function setMeetingModel(MeetingModel $model): RegistrationForm |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @return array |
||
| 377 | */ |
||
| 378 | protected function getMealFields(): array |
||
| 382 | |||
| 383 | /** |
||
| 384 | * @param string $meal |
||
| 385 | * @return RegistrationForm |
||
| 386 | */ |
||
| 387 | protected function setMealField(string $meal): RegistrationForm |
||
| 395 | |||
| 396 | /** |
||
| 397 | * @return array |
||
| 398 | */ |
||
| 399 | protected function getProgramFields(): array |
||
| 403 | |||
| 404 | /** |
||
| 405 | * @param string $program |
||
| 406 | * @return RegistrationForm |
||
| 407 | */ |
||
| 408 | protected function setProgramField(string $program): RegistrationForm |
||
| 414 | |||
| 415 | /** |
||
| 416 | * @return RegistrationForm |
||
| 417 | */ |
||
| 418 | protected function setProgramFields(): RegistrationForm |
||
| 429 | |||
| 430 | /** |
||
| 431 | * @return RegistrationForm |
||
| 432 | */ |
||
| 433 | protected function setMealFields(): RegistrationForm |
||
| 443 | |||
| 444 | /** |
||
| 445 | * @return Row |
||
| 446 | */ |
||
| 447 | protected function fetchProgramBlocks() |
||
| 451 | |||
| 452 | /** |
||
| 453 | * @return array |
||
| 454 | */ |
||
| 455 | protected function fetchMeals() |
||
| 459 | |||
| 460 | /** |
||
| 461 | * @return UserService |
||
| 462 | */ |
||
| 463 | protected function getUserService() |
||
| 467 | |||
| 468 | /** |
||
| 469 | * @param UserService $service |
||
| 470 | * @return $this |
||
| 471 | */ |
||
| 472 | protected function setUserService(UserService $service) |
||
| 478 | |||
| 479 | /** |
||
| 480 | * @param array $programs |
||
| 481 | * @return array |
||
| 482 | */ |
||
| 483 | protected function filterFilledCapacity(array $programs = []): array |
||
| 496 | |||
| 497 | } |
||
| 498 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.