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 |
||
| 44 | abstract class OfferCommandHandler extends Udb3CommandHandler |
||
| 45 | { |
||
| 46 | /** |
||
| 47 | * @var RepositoryInterface |
||
| 48 | */ |
||
| 49 | protected $offerRepository; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var RepositoryInterface |
||
| 53 | */ |
||
| 54 | protected $organizerRepository; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var RepositoryInterface |
||
| 58 | */ |
||
| 59 | protected $labelRepository; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var MediaManagerInterface|MediaManager |
||
| 63 | */ |
||
| 64 | protected $mediaManager; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @param RepositoryInterface $offerRepository |
||
| 68 | * @param RepositoryInterface $organizerRepository |
||
| 69 | * @param ReadRepositoryInterface $labelRepository |
||
| 70 | * @param MediaManagerInterface $mediaManager |
||
| 71 | */ |
||
| 72 | public function __construct( |
||
| 73 | RepositoryInterface $offerRepository, |
||
| 74 | RepositoryInterface $organizerRepository, |
||
| 75 | ReadRepositoryInterface $labelRepository, |
||
| 76 | MediaManagerInterface $mediaManager |
||
| 77 | ) { |
||
| 78 | $this->offerRepository = $offerRepository; |
||
| 79 | $this->organizerRepository = $organizerRepository; |
||
| 80 | $this->labelRepository = $labelRepository; |
||
|
|
|||
| 81 | $this->mediaManager = $mediaManager; |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * {@inheritdoc} |
||
| 86 | */ |
||
| 87 | View Code Duplication | public function handle($command) |
|
| 88 | { |
||
| 89 | $commandName = get_class($command); |
||
| 90 | $commandHandlers = $this->getCommandHandlers(); |
||
| 91 | |||
| 92 | if (isset($commandHandlers[$commandName])) { |
||
| 93 | $handler = $commandHandlers[$commandName]; |
||
| 94 | call_user_func(array($this, $handler), $command); |
||
| 95 | } else { |
||
| 96 | parent::handle($command); |
||
| 97 | } |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @return string[] |
||
| 102 | * An associative array of commands and their handler methods. |
||
| 103 | */ |
||
| 104 | View Code Duplication | private function getCommandHandlers() |
|
| 105 | { |
||
| 106 | $commands = []; |
||
| 107 | |||
| 108 | foreach (get_class_methods($this) as $method) { |
||
| 109 | $matches = []; |
||
| 110 | if (preg_match('/^handle(.+)$/', $method, $matches)) { |
||
| 111 | $command = $matches[1]; |
||
| 112 | $classNameMethod = 'get' . $command . 'ClassName'; |
||
| 113 | |||
| 114 | if (method_exists($this, $classNameMethod)) { |
||
| 115 | $commandFullClassName = call_user_func(array($this, $classNameMethod)); |
||
| 116 | $commands[$commandFullClassName] = $method; |
||
| 117 | } |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | return $commands; |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @return string |
||
| 126 | */ |
||
| 127 | abstract protected function getAddLabelClassName(); |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @return string |
||
| 131 | */ |
||
| 132 | abstract protected function getRemoveLabelClassName(); |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @return string |
||
| 136 | */ |
||
| 137 | abstract protected function getImportLabelsClassName(); |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @return string |
||
| 141 | */ |
||
| 142 | abstract protected function getUpdateTitleClassName(); |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @return string |
||
| 146 | */ |
||
| 147 | abstract protected function getAddImageClassName(); |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @return string |
||
| 151 | */ |
||
| 152 | abstract protected function getUpdateImageClassName(); |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @return string |
||
| 156 | */ |
||
| 157 | abstract protected function getRemoveImageClassName(); |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @return string |
||
| 161 | */ |
||
| 162 | abstract protected function getSelectMainImageClassName(); |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @return string |
||
| 166 | */ |
||
| 167 | abstract protected function getImportImagesClassName(); |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @return string |
||
| 171 | */ |
||
| 172 | abstract protected function getUpdateDescriptionClassName(); |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @return string |
||
| 176 | */ |
||
| 177 | abstract protected function getUpdateCalendarClassName(); |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @return string |
||
| 181 | */ |
||
| 182 | abstract protected function getUpdateTypicalAgeRangeClassName(); |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @return string |
||
| 186 | */ |
||
| 187 | abstract protected function getDeleteTypicalAgeRangeClassName(); |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @return string |
||
| 191 | */ |
||
| 192 | abstract protected function getUpdateOrganizerClassName(); |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @return string |
||
| 196 | */ |
||
| 197 | abstract protected function getDeleteOrganizerClassName(); |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @return string |
||
| 201 | */ |
||
| 202 | abstract protected function getDeleteCurrentOrganizerClassName(); |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @return string |
||
| 206 | */ |
||
| 207 | abstract protected function getUpdateContactPointClassName(); |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @return string |
||
| 211 | */ |
||
| 212 | abstract protected function getUpdateBookingInfoClassName(); |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @return string |
||
| 216 | */ |
||
| 217 | abstract protected function getUpdatePriceInfoClassName(); |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @return string |
||
| 221 | */ |
||
| 222 | abstract protected function getDeleteOfferClassName(); |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @return string |
||
| 226 | */ |
||
| 227 | abstract protected function getPublishClassName(); |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @return string |
||
| 231 | */ |
||
| 232 | abstract protected function getApproveClassName(); |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @return string |
||
| 236 | */ |
||
| 237 | abstract protected function getRejectClassName(); |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @return string |
||
| 241 | */ |
||
| 242 | abstract protected function getFlagAsDuplicateClassName(); |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @return string |
||
| 246 | */ |
||
| 247 | abstract protected function getFlagAsInappropriateClassName(); |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @return string |
||
| 251 | */ |
||
| 252 | abstract protected function getUpdateTypeClassName(); |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @return string |
||
| 256 | */ |
||
| 257 | abstract protected function getUpdateThemeClassName(); |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @return string |
||
| 261 | */ |
||
| 262 | abstract protected function getUpdateFacilitiesClassName(); |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @param AbstractUpdateType $updateType |
||
| 266 | */ |
||
| 267 | public function handleUpdateType(AbstractUpdateType $updateType) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * @param AbstractUpdateTheme $updateTheme |
||
| 278 | */ |
||
| 279 | public function handleUpdateTheme(AbstractUpdateTheme $updateTheme) |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @param AbstractUpdateFacilities $updateFacilities |
||
| 290 | */ |
||
| 291 | public function handleUpdateFacilities(AbstractUpdateFacilities $updateFacilities) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * @param AbstractAddLabel $addLabel |
||
| 302 | */ |
||
| 303 | private function handleAddLabel(AbstractAddLabel $addLabel) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @param AbstractRemoveLabel $removeLabel |
||
| 325 | */ |
||
| 326 | private function handleRemoveLabel(AbstractRemoveLabel $removeLabel) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @param AbstractImportLabels $importLabels |
||
| 339 | */ |
||
| 340 | private function handleImportLabels(AbstractImportLabels $importLabels) |
||
| 352 | |||
| 353 | /** |
||
| 354 | * @param AbstractUpdateTitle $translateTitle |
||
| 355 | */ |
||
| 356 | private function handleUpdateTitle(AbstractUpdateTitle $translateTitle) |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Handle an add image command. |
||
| 365 | * @param AbstractAddImage $addImage |
||
| 366 | */ |
||
| 367 | public function handleAddImage(AbstractAddImage $addImage) |
||
| 376 | |||
| 377 | /** |
||
| 378 | * @param AbstractRemoveImage $removeImage |
||
| 379 | */ |
||
| 380 | public function handleRemoveImage(AbstractRemoveImage $removeImage) |
||
| 386 | |||
| 387 | /** |
||
| 388 | * @param AbstractUpdateImage $updateImage |
||
| 389 | */ |
||
| 390 | public function handleUpdateImage(AbstractUpdateImage $updateImage) |
||
| 400 | |||
| 401 | /** |
||
| 402 | * @param AbstractSelectMainImage $selectMainImage |
||
| 403 | */ |
||
| 404 | public function handleSelectMainImage(AbstractSelectMainImage $selectMainImage) |
||
| 410 | |||
| 411 | /** |
||
| 412 | * @param AbstractImportImages $importImages |
||
| 413 | */ |
||
| 414 | public function handleImportImages(AbstractImportImages $importImages) |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Handle the update of description on a place. |
||
| 423 | * @param AbstractUpdateDescription $updateDescription |
||
| 424 | */ |
||
| 425 | public function handleUpdateDescription(AbstractUpdateDescription $updateDescription) |
||
| 437 | |||
| 438 | /** |
||
| 439 | * @param AbstractUpdateCalendar $updateCalendar |
||
| 440 | */ |
||
| 441 | public function handleUpdateCalendar(AbstractUpdateCalendar $updateCalendar) |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Handle the update of typical age range on a place. |
||
| 452 | * @param AbstractUpdateTypicalAgeRange $updateTypicalAgeRange |
||
| 453 | */ |
||
| 454 | public function handleUpdateTypicalAgeRange(AbstractUpdateTypicalAgeRange $updateTypicalAgeRange) |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Handle the deletion of typical age range on a place. |
||
| 468 | * @param AbstractDeleteTypicalAgeRange $deleteTypicalAgeRange |
||
| 469 | */ |
||
| 470 | public function handleDeleteTypicalAgeRange(AbstractDeleteTypicalAgeRange $deleteTypicalAgeRange) |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Handle an update command to update organizer of a place. |
||
| 482 | * @param AbstractUpdateOrganizer $updateOrganizer |
||
| 483 | */ |
||
| 484 | public function handleUpdateOrganizer(AbstractUpdateOrganizer $updateOrganizer) |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Handle an update command to delete the organizer. |
||
| 498 | * @param AbstractDeleteOrganizer $deleteOrganizer |
||
| 499 | */ |
||
| 500 | public function handleDeleteOrganizer(AbstractDeleteOrganizer $deleteOrganizer) |
||
| 510 | |||
| 511 | /** |
||
| 512 | * @param AbstractDeleteCurrentOrganizer $deleteCurrentOrganizer |
||
| 513 | */ |
||
| 514 | public function handleDeleteCurrentOrganizer(AbstractDeleteCurrentOrganizer $deleteCurrentOrganizer) |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Handle an update command to updated the contact point. |
||
| 525 | * @param AbstractUpdateContactPoint $updateContactPoint |
||
| 526 | */ |
||
| 527 | public function handleUpdateContactPoint(AbstractUpdateContactPoint $updateContactPoint) |
||
| 538 | |||
| 539 | /** |
||
| 540 | * Handle an update command to updated the booking info. |
||
| 541 | * @param AbstractUpdateBookingInfo $updateBookingInfo |
||
| 542 | */ |
||
| 543 | public function handleUpdateBookingInfo(AbstractUpdateBookingInfo $updateBookingInfo) |
||
| 553 | |||
| 554 | /** |
||
| 555 | * @param AbstractUpdatePriceInfo $updatePriceInfo |
||
| 556 | */ |
||
| 557 | private function handleUpdatePriceInfo(AbstractUpdatePriceInfo $updatePriceInfo) |
||
| 567 | |||
| 568 | /** |
||
| 569 | * @param AbstractDeleteOffer $deleteOffer |
||
| 570 | */ |
||
| 571 | private function handleDeleteOffer(AbstractDeleteOffer $deleteOffer) |
||
| 577 | |||
| 578 | /** |
||
| 579 | * @param AbstractPublish $publish |
||
| 580 | */ |
||
| 581 | private function handlePublish(AbstractPublish $publish) |
||
| 587 | |||
| 588 | /** |
||
| 589 | * @param AbstractApprove $approve |
||
| 590 | */ |
||
| 591 | private function handleApprove(AbstractApprove $approve) |
||
| 597 | |||
| 598 | /** |
||
| 599 | * @param AbstractReject $reject |
||
| 600 | */ |
||
| 601 | private function handleReject(AbstractReject $reject) |
||
| 607 | |||
| 608 | /** |
||
| 609 | * @param AbstractFlagAsDuplicate $flagAsDuplicate |
||
| 610 | */ |
||
| 611 | private function handleFlagAsDuplicate(AbstractFlagAsDuplicate $flagAsDuplicate) |
||
| 617 | |||
| 618 | /** |
||
| 619 | * @param AbstractFlagAsInappropriate $flagAsInappropriate |
||
| 620 | */ |
||
| 621 | private function handleFlagAsInappropriate(AbstractFlagAsInappropriate $flagAsInappropriate) |
||
| 627 | |||
| 628 | /** |
||
| 629 | * Makes it easier to type-hint to Offer. |
||
| 630 | * |
||
| 631 | * @param string $id |
||
| 632 | * @return Offer |
||
| 633 | */ |
||
| 634 | private function load($id) |
||
| 638 | |||
| 639 | /** |
||
| 640 | * Makes it easier to type-hint to Organizer. |
||
| 641 | * |
||
| 642 | * @param string $id |
||
| 643 | * @return Organizer |
||
| 644 | */ |
||
| 645 | private function loadOrganizer($id) |
||
| 650 | } |
||
| 651 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..