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 |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @param Form $form |
||
| 279 | * @return Form |
||
| 280 | */ |
||
| 281 | protected function buildMealSwitcher(Form $form): Form |
||
| 295 | |||
| 296 | /** |
||
| 297 | * @return ProvinceModel |
||
| 298 | */ |
||
| 299 | protected function getProvinceModel(): ProvinceModel |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @param ProvinceModel $model |
||
| 306 | * @return RegistrationFormFactory |
||
| 307 | */ |
||
| 308 | protected function setProvinceModel(ProvinceModel $model): RegistrationForm |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @return ProgramModel |
||
| 317 | */ |
||
| 318 | protected function getProgramModel(): ProgramModel |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @param ProgramModel $model |
||
| 325 | * @return RegistrationFormFactory |
||
| 326 | */ |
||
| 327 | protected function setProgramModel(ProgramModel $model): RegistrationForm |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @return BlockModel |
||
| 336 | */ |
||
| 337 | protected function getBlockModel(): BlockModel |
||
| 341 | |||
| 342 | /** |
||
| 343 | * @param BlockModel $model |
||
| 344 | * @return RegistrationForm |
||
| 345 | */ |
||
| 346 | protected function setBlockModel(BlockModel $model): RegistrationForm |
||
| 352 | |||
| 353 | /** |
||
| 354 | * @return MeetingModel |
||
| 355 | */ |
||
| 356 | protected function getMeetingModel(): MeetingModel |
||
| 360 | |||
| 361 | /** |
||
| 362 | * @param MeetingModel $model |
||
| 363 | * @return RegistrationForm |
||
| 364 | */ |
||
| 365 | protected function setMeetingModel(MeetingModel $model): RegistrationForm |
||
| 371 | |||
| 372 | /** |
||
| 373 | * @return array |
||
| 374 | */ |
||
| 375 | protected function getMealFields(): array |
||
| 379 | |||
| 380 | /** |
||
| 381 | * @param string $meal |
||
| 382 | * @return RegistrationForm |
||
| 383 | */ |
||
| 384 | protected function setMealField(string $meal): RegistrationForm |
||
| 392 | |||
| 393 | /** |
||
| 394 | * @return array |
||
| 395 | */ |
||
| 396 | protected function getProgramFields(): array |
||
| 400 | |||
| 401 | /** |
||
| 402 | * @param string $program |
||
| 403 | * @return RegistrationForm |
||
| 404 | */ |
||
| 405 | protected function setProgramField(string $program): RegistrationForm |
||
| 411 | |||
| 412 | /** |
||
| 413 | * @return RegistrationForm |
||
| 414 | */ |
||
| 415 | protected function setProgramFields(): RegistrationForm |
||
| 426 | |||
| 427 | /** |
||
| 428 | * @return RegistrationForm |
||
| 429 | */ |
||
| 430 | protected function setMealFields(): RegistrationForm |
||
| 440 | |||
| 441 | protected function fetchProgramBlocks() |
||
| 445 | |||
| 446 | protected function fetchMeals() |
||
| 450 | |||
| 451 | /** |
||
| 452 | * @return UserService |
||
| 453 | */ |
||
| 454 | protected function getUserService() |
||
| 458 | |||
| 459 | /** |
||
| 460 | * @param UserService $service |
||
| 461 | * @return $this |
||
| 462 | */ |
||
| 463 | protected function setUserService(UserService $service) |
||
| 469 | |||
| 470 | } |
||
| 471 |
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.