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 |
||
| 38 | abstract class OfferCommandHandler extends Udb3CommandHandler |
||
| 39 | { |
||
| 40 | /** |
||
| 41 | * @var RepositoryInterface |
||
| 42 | */ |
||
| 43 | protected $offerRepository; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var RepositoryInterface |
||
| 47 | */ |
||
| 48 | protected $organizerRepository; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var RepositoryInterface |
||
| 52 | */ |
||
| 53 | protected $labelRepository; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @param RepositoryInterface $offerRepository |
||
| 57 | * @param RepositoryInterface $organizerRepository |
||
| 58 | * @param ReadRepositoryInterface $labelRepository |
||
| 59 | */ |
||
| 60 | public function __construct( |
||
| 61 | RepositoryInterface $offerRepository, |
||
| 62 | RepositoryInterface $organizerRepository, |
||
| 63 | ReadRepositoryInterface $labelRepository |
||
| 64 | ) { |
||
| 65 | $this->offerRepository = $offerRepository; |
||
| 66 | $this->organizerRepository = $organizerRepository; |
||
| 67 | $this->labelRepository = $labelRepository; |
||
|
|
|||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * {@inheritdoc} |
||
| 72 | */ |
||
| 73 | public function handle($command) |
||
| 74 | { |
||
| 75 | $commandName = get_class($command); |
||
| 76 | $commandHandlers = $this->getCommandHandlers(); |
||
| 77 | |||
| 78 | if (isset($commandHandlers[$commandName])) { |
||
| 79 | $handler = $commandHandlers[$commandName]; |
||
| 80 | call_user_func(array($this, $handler), $command); |
||
| 81 | } else { |
||
| 82 | parent::handle($command); |
||
| 83 | } |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @return string[] |
||
| 88 | * An associative array of commands and their handler methods. |
||
| 89 | */ |
||
| 90 | View Code Duplication | private function getCommandHandlers() |
|
| 91 | { |
||
| 92 | $commands = []; |
||
| 93 | |||
| 94 | foreach (get_class_methods($this) as $method) { |
||
| 95 | $matches = []; |
||
| 96 | if (preg_match('/^handle(.+)$/', $method, $matches)) { |
||
| 97 | $command = $matches[1]; |
||
| 98 | $classNameMethod = 'get' . $command . 'ClassName'; |
||
| 99 | |||
| 100 | if (method_exists($this, $classNameMethod)) { |
||
| 101 | $commandFullClassName = call_user_func(array($this, $classNameMethod)); |
||
| 102 | $commands[$commandFullClassName] = $method; |
||
| 103 | } |
||
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | return $commands; |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @return string |
||
| 112 | */ |
||
| 113 | abstract protected function getAddLabelClassName(); |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @return string |
||
| 117 | */ |
||
| 118 | abstract protected function getRemoveLabelClassName(); |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @return string |
||
| 122 | */ |
||
| 123 | abstract protected function getUpdateTitleClassName(); |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @return string |
||
| 127 | */ |
||
| 128 | abstract protected function getAddImageClassName(); |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @return string |
||
| 132 | */ |
||
| 133 | abstract protected function getUpdateImageClassName(); |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @return string |
||
| 137 | */ |
||
| 138 | abstract protected function getRemoveImageClassName(); |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @return string |
||
| 142 | */ |
||
| 143 | abstract protected function getSelectMainImageClassName(); |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @return string |
||
| 147 | */ |
||
| 148 | abstract protected function getUpdateDescriptionClassName(); |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @return string |
||
| 152 | */ |
||
| 153 | abstract protected function getUpdateCalendarClassName(); |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @return string |
||
| 157 | */ |
||
| 158 | abstract protected function getUpdateTypicalAgeRangeClassName(); |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @return string |
||
| 162 | */ |
||
| 163 | abstract protected function getDeleteTypicalAgeRangeClassName(); |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @return string |
||
| 167 | */ |
||
| 168 | abstract protected function getUpdateOrganizerClassName(); |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @return string |
||
| 172 | */ |
||
| 173 | abstract protected function getDeleteOrganizerClassName(); |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @return string |
||
| 177 | */ |
||
| 178 | abstract protected function getUpdateContactPointClassName(); |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @return string |
||
| 182 | */ |
||
| 183 | abstract protected function getUpdateBookingInfoClassName(); |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @return string |
||
| 187 | */ |
||
| 188 | abstract protected function getUpdatePriceInfoClassName(); |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @return string |
||
| 192 | */ |
||
| 193 | abstract protected function getDeleteOfferClassName(); |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @return string |
||
| 197 | */ |
||
| 198 | abstract protected function getPublishClassName(); |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @return string |
||
| 202 | */ |
||
| 203 | abstract protected function getApproveClassName(); |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @return string |
||
| 207 | */ |
||
| 208 | abstract protected function getRejectClassName(); |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @return string |
||
| 212 | */ |
||
| 213 | abstract protected function getFlagAsDuplicateClassName(); |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @return string |
||
| 217 | */ |
||
| 218 | abstract protected function getFlagAsInappropriateClassName(); |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @return string |
||
| 222 | */ |
||
| 223 | abstract protected function getUpdateTypeClassName(); |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @return string |
||
| 227 | */ |
||
| 228 | abstract protected function getUpdateThemeClassName(); |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @param AbstractUpdateType $updateType |
||
| 232 | */ |
||
| 233 | public function handleUpdateType(AbstractUpdateType $updateType) |
||
| 241 | |||
| 242 | /** |
||
| 243 | * @param AbstractUpdateTheme $updateTheme |
||
| 244 | */ |
||
| 245 | public function handleUpdateTheme(AbstractUpdateTheme $updateTheme) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @param AbstractAddLabel $addLabel |
||
| 256 | */ |
||
| 257 | private function handleAddLabel(AbstractAddLabel $addLabel) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @param AbstractRemoveLabel $removeLabel |
||
| 268 | */ |
||
| 269 | private function handleRemoveLabel(AbstractRemoveLabel $removeLabel) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @param AbstractLabelCommand $labelCommand |
||
| 280 | * @return Label |
||
| 281 | */ |
||
| 282 | View Code Duplication | private function createLabel(AbstractLabelCommand $labelCommand) |
|
| 292 | |||
| 293 | /** |
||
| 294 | * @param AbstractUpdateTitle $translateTitle |
||
| 295 | */ |
||
| 296 | private function handleUpdateTitle(AbstractUpdateTitle $translateTitle) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Handle an add image command. |
||
| 305 | * @param AbstractAddImage $addImage |
||
| 306 | */ |
||
| 307 | public function handleAddImage(AbstractAddImage $addImage) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @param AbstractRemoveImage $removeImage |
||
| 316 | */ |
||
| 317 | public function handleRemoveImage(AbstractRemoveImage $removeImage) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @param AbstractUpdateImage $updateImage |
||
| 326 | */ |
||
| 327 | public function handleUpdateImage(AbstractUpdateImage $updateImage) |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @param AbstractSelectMainImage $selectMainImage |
||
| 336 | */ |
||
| 337 | public function handleSelectMainImage(AbstractSelectMainImage $selectMainImage) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Handle the update of description on a place. |
||
| 346 | * @param AbstractUpdateDescription $updateDescription |
||
| 347 | */ |
||
| 348 | public function handleUpdateDescription(AbstractUpdateDescription $updateDescription) |
||
| 360 | |||
| 361 | /** |
||
| 362 | * @param AbstractUpdateCalendar $updateCalendar |
||
| 363 | */ |
||
| 364 | public function handleUpdateCalendar(AbstractUpdateCalendar $updateCalendar) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Handle the update of typical age range on a place. |
||
| 375 | * @param AbstractUpdateTypicalAgeRange $updateTypicalAgeRange |
||
| 376 | */ |
||
| 377 | public function handleUpdateTypicalAgeRange(AbstractUpdateTypicalAgeRange $updateTypicalAgeRange) |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Handle the deletion of typical age range on a place. |
||
| 391 | * @param AbstractDeleteTypicalAgeRange $deleteTypicalAgeRange |
||
| 392 | */ |
||
| 393 | public function handleDeleteTypicalAgeRange(AbstractDeleteTypicalAgeRange $deleteTypicalAgeRange) |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Handle an update command to update organizer of a place. |
||
| 405 | * @param AbstractUpdateOrganizer $updateOrganizer |
||
| 406 | */ |
||
| 407 | public function handleUpdateOrganizer(AbstractUpdateOrganizer $updateOrganizer) |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Handle an update command to delete the organizer. |
||
| 421 | * @param AbstractDeleteOrganizer $deleteOrganizer |
||
| 422 | */ |
||
| 423 | public function handleDeleteOrganizer(AbstractDeleteOrganizer $deleteOrganizer) |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Handle an update command to updated the contact point. |
||
| 436 | * @param AbstractUpdateContactPoint $updateContactPoint |
||
| 437 | */ |
||
| 438 | public function handleUpdateContactPoint(AbstractUpdateContactPoint $updateContactPoint) |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Handle an update command to updated the booking info. |
||
| 452 | * @param AbstractUpdateBookingInfo $updateBookingInfo |
||
| 453 | */ |
||
| 454 | public function handleUpdateBookingInfo(AbstractUpdateBookingInfo $updateBookingInfo) |
||
| 464 | |||
| 465 | /** |
||
| 466 | * @param AbstractUpdatePriceInfo $updatePriceInfo |
||
| 467 | */ |
||
| 468 | private function handleUpdatePriceInfo(AbstractUpdatePriceInfo $updatePriceInfo) |
||
| 478 | |||
| 479 | /** |
||
| 480 | * @param AbstractDeleteOffer $deleteOffer |
||
| 481 | */ |
||
| 482 | private function handleDeleteOffer(AbstractDeleteOffer $deleteOffer) |
||
| 488 | |||
| 489 | /** |
||
| 490 | * @param AbstractPublish $publish |
||
| 491 | */ |
||
| 492 | private function handlePublish(AbstractPublish $publish) |
||
| 498 | |||
| 499 | /** |
||
| 500 | * @param AbstractApprove $approve |
||
| 501 | */ |
||
| 502 | private function handleApprove(AbstractApprove $approve) |
||
| 508 | |||
| 509 | /** |
||
| 510 | * @param AbstractReject $reject |
||
| 511 | */ |
||
| 512 | private function handleReject(AbstractReject $reject) |
||
| 518 | |||
| 519 | /** |
||
| 520 | * @param AbstractFlagAsDuplicate $flagAsDuplicate |
||
| 521 | */ |
||
| 522 | private function handleFlagAsDuplicate(AbstractFlagAsDuplicate $flagAsDuplicate) |
||
| 528 | |||
| 529 | /** |
||
| 530 | * @param AbstractFlagAsInappropriate $flagAsInappropriate |
||
| 531 | */ |
||
| 532 | private function handleFlagAsInappropriate(AbstractFlagAsInappropriate $flagAsInappropriate) |
||
| 538 | |||
| 539 | /** |
||
| 540 | * Makes it easier to type-hint to Offer. |
||
| 541 | * |
||
| 542 | * @param string $id |
||
| 543 | * @return Offer |
||
| 544 | */ |
||
| 545 | private function load($id) |
||
| 549 | |||
| 550 | /** |
||
| 551 | * Makes it easier to type-hint to Organizer. |
||
| 552 | * |
||
| 553 | * @param string $id |
||
| 554 | * @return Organizer |
||
| 555 | */ |
||
| 556 | private function loadOrganizer($id) |
||
| 561 | } |
||
| 562 |
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..