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 |
||
| 51 | class PlaceLDProjector implements EventListenerInterface |
||
| 52 | { |
||
| 53 | use DelegateEventHandlingToSpecificMethodTrait; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var DocumentRepositoryInterface |
||
| 57 | */ |
||
| 58 | protected $repository; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var IriGeneratorInterface |
||
| 62 | */ |
||
| 63 | protected $iriGenerator; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var EntityServiceInterface |
||
| 67 | */ |
||
| 68 | protected $organizerService; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var SluggerInterface |
||
| 72 | */ |
||
| 73 | protected $slugger; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var CdbXMLImporter |
||
| 77 | */ |
||
| 78 | protected $cdbXMLImporter; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var SerializerInterface |
||
| 82 | */ |
||
| 83 | protected $mediaObjectSerializer; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @param DocumentRepositoryInterface $repository |
||
| 87 | * @param IriGeneratorInterface $iriGenerator |
||
| 88 | * @param EntityServiceInterface $organizerService |
||
| 89 | * @param SerializerInterface $mediaObjectSerializer |
||
| 90 | */ |
||
| 91 | public function __construct( |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @param PlaceImportedFromUDB2 $actorImportedFromUDB2 |
||
| 109 | */ |
||
| 110 | protected function applyPlaceImportedFromUDB2( |
||
| 128 | |||
| 129 | |||
| 130 | /** |
||
| 131 | * @param PlaceImportedFromUDB2Event $eventImportedFromUDB2 |
||
| 132 | */ |
||
| 133 | protected function applyPlaceImportedFromUDB2Event( |
||
| 134 | PlaceImportedFromUDB2Event $eventImportedFromUDB2 |
||
| 135 | ) { |
||
| 136 | $udb2Event = EventItemFactory::createEventFromCdbXml( |
||
| 137 | $eventImportedFromUDB2->getCdbXmlNamespaceUri(), |
||
| 138 | $eventImportedFromUDB2->getCdbXml() |
||
| 139 | ); |
||
| 140 | |||
| 141 | $document = $this->newDocument($eventImportedFromUDB2->getActorId()); |
||
| 142 | $placeLd = $document->getBody(); |
||
| 143 | |||
| 144 | $placeLd = $this->cdbXMLImporter->eventDocumentWithCdbXML( |
||
| 145 | $placeLd, |
||
| 146 | $udb2Event |
||
| 147 | ); |
||
| 148 | |||
| 149 | $this->repository->save($document->withBody($placeLd)); |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @param string $id |
||
| 154 | * @return JsonDocument |
||
| 155 | */ |
||
| 156 | View Code Duplication | protected function newDocument($id) |
|
| 168 | |||
| 169 | /** |
||
| 170 | * @param PlaceCreated $placeCreated |
||
| 171 | * @param DomainMessage $domainMessage |
||
| 172 | */ |
||
| 173 | protected function applyPlaceCreated(PlaceCreated $placeCreated, DomainMessage $domainMessage) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @param PlaceDeleted $placeDeleted |
||
| 219 | */ |
||
| 220 | protected function applyPlaceDeleted(PlaceDeleted $placeDeleted) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Apply the major info updated command to the projector. |
||
| 227 | * @param MajorInfoUpdated $majorInfoUpdated |
||
| 228 | */ |
||
| 229 | protected function applyMajorInfoUpdated(MajorInfoUpdated $majorInfoUpdated) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Apply the description updated event to the place repository. |
||
| 262 | * @param DescriptionUpdated $descriptionUpdated |
||
| 263 | */ |
||
| 264 | View Code Duplication | protected function applyDescriptionUpdated( |
|
| 278 | |||
| 279 | /** |
||
| 280 | * Apply the booking info updated event to the place repository. |
||
| 281 | * @param BookingInfoUpdated $bookingInfoUpdated |
||
| 282 | */ |
||
| 283 | View Code Duplication | protected function applyBookingInfoUpdated(BookingInfoUpdated $bookingInfoUpdated) |
|
| 294 | |||
| 295 | /** |
||
| 296 | * Apply the typical age range updated event to the place repository. |
||
| 297 | * @param TypicalAgeRangeUpdated $typicalAgeRangeUpdated |
||
| 298 | */ |
||
| 299 | View Code Duplication | protected function applyTypicalAgeRangeUpdated( |
|
| 300 | TypicalAgeRangeUpdated $typicalAgeRangeUpdated |
||
| 301 | ) { |
||
| 302 | $document = $this->loadPlaceDocumentFromRepository($typicalAgeRangeUpdated); |
||
| 303 | |||
| 304 | $placeLd = $document->getBody(); |
||
| 305 | $placeLd->typicalAgeRange = $typicalAgeRangeUpdated->getTypicalAgeRange(); |
||
| 306 | |||
| 307 | $this->repository->save($document->withBody($placeLd)); |
||
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Apply the typical age range deleted event to the place repository. |
||
| 312 | * @param TypicalAgeRangeDeleted $typicalAgeRangeDeleted |
||
| 313 | */ |
||
| 314 | protected function applyTypicalAgeRangeDeleted( |
||
| 315 | TypicalAgeRangeDeleted $typicalAgeRangeDeleted |
||
| 316 | ) { |
||
| 317 | $document = $this->loadPlaceDocumentFromRepository($typicalAgeRangeDeleted); |
||
| 318 | |||
| 319 | $placeLd = $document->getBody(); |
||
| 320 | |||
| 321 | unset($placeLd->typicalAgeRange); |
||
| 322 | |||
| 323 | $this->repository->save($document->withBody($placeLd)); |
||
| 324 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Apply the organizer updated event to the place repository. |
||
| 328 | * @param OrganizerUpdated $organizerUpdated |
||
| 329 | */ |
||
| 330 | View Code Duplication | protected function applyOrganizerUpdated(OrganizerUpdated $organizerUpdated) |
|
| 342 | |||
| 343 | /** |
||
| 344 | * Apply the organizer delete event to the place repository. |
||
| 345 | * @param OrganizerDeleted $organizerDeleted |
||
| 346 | */ |
||
| 347 | protected function applyOrganizerDeleted(OrganizerDeleted $organizerDeleted) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Apply the contact point updated event to the place repository. |
||
| 360 | * @param ContactPointUpdated $contactPointUpdated |
||
| 361 | */ |
||
| 362 | View Code Duplication | protected function applyContactPointUpdated(ContactPointUpdated $contactPointUpdated) |
|
| 372 | |||
| 373 | /** |
||
| 374 | * Apply the facilitiesupdated event to the place repository. |
||
| 375 | * @param FacilitiesUpdated $facilitiesUpdated |
||
| 376 | */ |
||
| 377 | protected function applyFacilitiesUpdated(FacilitiesUpdated $facilitiesUpdated) |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Apply the imageAdded event to the place repository. |
||
| 407 | * |
||
| 408 | * @param ImageAdded $imageAdded |
||
| 409 | */ |
||
| 410 | View Code Duplication | protected function applyImageAdded(ImageAdded $imageAdded) |
|
| 427 | |||
| 428 | /** |
||
| 429 | * Apply the ImageUpdated event to the place repository. |
||
| 430 | * |
||
| 431 | * @param ImageUpdated $imageUpdated |
||
| 432 | */ |
||
| 433 | protected function applyImageUpdated(ImageUpdated $imageUpdated) |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Apply the imageDeleted event to the place repository. |
||
| 474 | * |
||
| 475 | * @param ImageDeleted $imageDeleted |
||
| 476 | */ |
||
| 477 | View Code Duplication | protected function applyImageDeleted(ImageDeleted $imageDeleted) |
|
| 491 | |||
| 492 | /** |
||
| 493 | * @param PlaceEvent $place |
||
| 494 | * @return JsonDocument |
||
| 495 | */ |
||
| 496 | protected function loadPlaceDocumentFromRepository(PlaceEvent $place) |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Get the organizer jsonLD. |
||
| 509 | * @param string $organizerId |
||
| 510 | * @return array |
||
| 511 | */ |
||
| 512 | View Code Duplication | protected function organizerJSONLD($organizerId) |
|
| 527 | } |
||
| 528 |
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.