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:
| 1 | <?php |
||
| 15 | class VisitorForm extends BaseForm |
||
| 16 | { |
||
| 17 | |||
| 18 | const TEMPLATE_NAME = 'VisitorForm'; |
||
| 19 | |||
| 20 | const MESSAGE_REQUIRED = 'Hodnota musí být vyplněna!'; |
||
| 21 | const MESSAGE_MAX_LENGTH = '%label nesmí mít více jak %d znaků!'; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var Closure |
||
| 25 | */ |
||
| 26 | public $onVisitorSave; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var Closure |
||
| 30 | */ |
||
| 31 | public $onVisitorReset; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var ProvinceModel |
||
| 35 | */ |
||
| 36 | protected $provinceModel; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var ProgramRepository |
||
| 40 | */ |
||
| 41 | protected $programRepository; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var BlockModel |
||
| 45 | */ |
||
| 46 | protected $blockModel; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var MeetingModel |
||
| 50 | */ |
||
| 51 | protected $meetingModel; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var array |
||
| 55 | */ |
||
| 56 | protected $mealFields = []; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var array |
||
| 60 | */ |
||
| 61 | protected $programFields = []; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * VisitorForm constructor. |
||
| 65 | * @param ProvinceModel $province |
||
| 66 | * @param ProgramRepository $program |
||
| 67 | * @param BlockModel $block |
||
| 68 | * @param MeetingModel $meeting |
||
| 69 | */ |
||
| 70 | View Code Duplication | public function __construct( |
|
| 81 | |||
| 82 | /** |
||
| 83 | * @return void |
||
| 84 | */ |
||
| 85 | public function render() |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @param array|ArrayHash $defaults |
||
| 99 | * @return self |
||
| 100 | */ |
||
| 101 | public function setDefaults($defaults): BaseForm |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @return Form |
||
| 110 | */ |
||
| 111 | public function createComponentVisitorForm(): Form |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @param SubmitButton $button |
||
| 210 | * @return void |
||
| 211 | */ |
||
| 212 | public function processSave(SubmitButton $button) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @param SubmitButton $button |
||
| 221 | * @return void |
||
| 222 | */ |
||
| 223 | public function processReset(SubmitButton $button) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @param Form $form |
||
| 232 | * @return Form |
||
| 233 | */ |
||
| 234 | protected function buildProgramSwitcher(Form $form): Form |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @param Form $form |
||
| 263 | * @return Form |
||
| 264 | */ |
||
| 265 | protected function buildMealSwitcher(Form $form): Form |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @return ProvinceModel |
||
| 282 | */ |
||
| 283 | protected function getProvinceModel(): ProvinceModel |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @param ProvinceModel $model |
||
| 290 | * @return self |
||
| 291 | */ |
||
| 292 | protected function setProvinceModel(ProvinceModel $model): self |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @return ProgramRepository |
||
| 301 | */ |
||
| 302 | protected function getProgramRepository(): ProgramRepository |
||
| 306 | |||
| 307 | /** |
||
| 308 | * @param ProgramRepository $repository |
||
| 309 | * @return self |
||
| 310 | */ |
||
| 311 | protected function setProgramRepository(ProgramRepository $repository): self |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @return BlockModel |
||
| 320 | */ |
||
| 321 | protected function getBlockModel(): BlockModel |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @param BlockModel $model |
||
| 328 | * @return self |
||
| 329 | */ |
||
| 330 | protected function setBlockModel(BlockModel $model): self |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @return MeetingModel |
||
| 339 | */ |
||
| 340 | protected function getMeetingModel(): MeetingModel |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @param MeetingModel $model |
||
| 347 | * @return VisitorForm |
||
| 348 | */ |
||
| 349 | protected function setMeetingModel(MeetingModel $model): VisitorForm |
||
| 355 | |||
| 356 | /** |
||
| 357 | * @return array |
||
| 358 | */ |
||
| 359 | protected function getMealFields(): array |
||
| 363 | |||
| 364 | /** |
||
| 365 | * @param string $meal |
||
| 366 | * @return self |
||
| 367 | */ |
||
| 368 | protected function setMealField(string $meal): self |
||
| 376 | |||
| 377 | /** |
||
| 378 | * @return array |
||
| 379 | */ |
||
| 380 | protected function getProgramFields(): array |
||
| 384 | |||
| 385 | /** |
||
| 386 | * @param string $program |
||
| 387 | * @return self |
||
| 388 | */ |
||
| 389 | protected function setProgramField(string $program): self |
||
| 395 | |||
| 396 | /** |
||
| 397 | * @return self |
||
| 398 | */ |
||
| 399 | protected function setProgramFields(): self |
||
| 410 | |||
| 411 | /** |
||
| 412 | * @return self |
||
| 413 | */ |
||
| 414 | protected function setMealFields(): self |
||
| 424 | |||
| 425 | /** |
||
| 426 | * @return Row |
||
| 427 | */ |
||
| 428 | protected function fetchProgramBlocks() |
||
| 432 | |||
| 433 | /** |
||
| 434 | * @return array |
||
| 435 | */ |
||
| 436 | protected function fetchMeals() |
||
| 440 | |||
| 441 | /** |
||
| 442 | * @return UserService |
||
| 443 | */ |
||
| 444 | protected function getUserService() |
||
| 448 | |||
| 449 | /** |
||
| 450 | * @param UserService $service |
||
| 451 | * @return $this |
||
| 452 | */ |
||
| 453 | protected function setUserService(UserService $service) |
||
| 459 | |||
| 460 | /** |
||
| 461 | * @param array $programs |
||
| 462 | * @return array |
||
| 463 | */ |
||
| 464 | protected function filterFilledCapacity(array $programs = []): array |
||
| 477 | |||
| 478 | } |
||
| 479 |
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.