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:
Complex classes like PlaceLDProjector 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 PlaceLDProjector, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 60 | class PlaceLDProjector extends OfferLDProjector implements EventListenerInterface |
||
| 61 | { |
||
| 62 | /** |
||
| 63 | * @var CdbXMLImporter |
||
| 64 | */ |
||
| 65 | protected $cdbXMLImporter; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @param DocumentRepositoryInterface $repository |
||
| 69 | * @param IriGeneratorInterface $iriGenerator |
||
| 70 | * @param EntityServiceInterface $organizerService |
||
| 71 | * @param SerializerInterface $mediaObjectSerializer |
||
| 72 | * @param CdbXMLImporter $cdbXMLImporter |
||
| 73 | */ |
||
| 74 | public function __construct( |
||
| 75 | DocumentRepositoryInterface $repository, |
||
| 76 | IriGeneratorInterface $iriGenerator, |
||
| 77 | EntityServiceInterface $organizerService, |
||
| 78 | SerializerInterface $mediaObjectSerializer, |
||
| 79 | CdbXMLImporter $cdbXMLImporter |
||
| 80 | ) { |
||
| 81 | parent::__construct( |
||
| 82 | $repository, |
||
| 83 | $iriGenerator, |
||
| 84 | $organizerService, |
||
| 85 | $mediaObjectSerializer |
||
| 86 | ); |
||
| 87 | |||
| 88 | $this->slugger = new CulturefeedSlugger(); |
||
| 89 | $this->cdbXMLImporter = $cdbXMLImporter; |
||
| 90 | } |
||
| 91 | |||
| 92 | protected function getImagesImportedFromUdb2ClassName() |
||
| 93 | { |
||
| 94 | return ImagesImportedFromUDB2::class; |
||
| 95 | } |
||
| 96 | |||
| 97 | protected function getImagesUpdatedFromUdb2ClassName() |
||
| 98 | { |
||
| 99 | return ImagesUpdatedFromUDB2::class; |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @param PlaceImportedFromUDB2 $placeImportedFromUDB2 |
||
| 104 | */ |
||
| 105 | protected function applyPlaceImportedFromUDB2( |
||
| 106 | PlaceImportedFromUDB2 $placeImportedFromUDB2 |
||
| 107 | ) { |
||
| 108 | $this->projectActorImportedFromUDB2($placeImportedFromUDB2); |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @param PlaceUpdatedFromUDB2 $placeUpdatedFromUDB2 |
||
| 113 | */ |
||
| 114 | protected function applyPlaceUpdatedFromUDB2( |
||
| 115 | PlaceUpdatedFromUDB2 $placeUpdatedFromUDB2 |
||
| 116 | ) { |
||
| 117 | $this->projectActorImportedFromUDB2($placeUpdatedFromUDB2); |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @param ActorImportedFromUDB2 $actorImportedFromUDB2 |
||
| 122 | */ |
||
| 123 | protected function projectActorImportedFromUDB2( |
||
| 124 | ActorImportedFromUDB2 $actorImportedFromUDB2 |
||
| 125 | ) { |
||
| 126 | $actorId = $actorImportedFromUDB2->getActorId(); |
||
| 127 | |||
| 128 | $udb2Actor = ActorItemFactory::createActorFromCdbXml( |
||
| 129 | $actorImportedFromUDB2->getCdbXmlNamespaceUri(), |
||
| 130 | $actorImportedFromUDB2->getCdbXml() |
||
| 131 | ); |
||
| 132 | |||
| 133 | try { |
||
| 134 | $document = $this->loadPlaceDocumentFromRepositoryById($actorId); |
||
| 135 | } catch (DocumentGoneException $e) { |
||
| 136 | $document = $this->newDocument($actorId); |
||
| 137 | } |
||
| 138 | |||
| 139 | $actorLd = $document->getBody(); |
||
| 140 | |||
| 141 | $actorLd = $this->cdbXMLImporter->documentWithCdbXML( |
||
| 142 | $actorLd, |
||
| 143 | $udb2Actor |
||
| 144 | ); |
||
| 145 | |||
| 146 | // Remove geocoordinates, because the address might have been |
||
| 147 | // updated and we might get inconsistent data if it takes a while |
||
| 148 | // before the new geocoordinates are added. |
||
| 149 | // In case geocoding fails, it's also easier to look for places that |
||
| 150 | // have no geocoordinates instead of places that have incorrect |
||
| 151 | // geocoordinates. |
||
| 152 | unset($actorLd->geo); |
||
| 153 | |||
| 154 | $this->repository->save($document->withBody($actorLd)); |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @param string $id |
||
| 159 | * @return JsonDocument |
||
| 160 | */ |
||
| 161 | View Code Duplication | protected function newDocument($id) |
|
| 171 | |||
| 172 | /** |
||
| 173 | * @param PlaceCreated $placeCreated |
||
| 174 | * @param DomainMessage $domainMessage |
||
| 175 | */ |
||
| 176 | protected function applyPlaceCreated(PlaceCreated $placeCreated, DomainMessage $domainMessage) |
||
| 177 | { |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @param PlaceDeleted $placeDeleted |
||
| 232 | */ |
||
| 233 | protected function applyPlaceDeleted(PlaceDeleted $placeDeleted) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Apply the major info updated command to the projector. |
||
| 240 | * @param MajorInfoUpdated $majorInfoUpdated |
||
| 241 | */ |
||
| 242 | protected function applyMajorInfoUpdated(MajorInfoUpdated $majorInfoUpdated) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Apply the facilitiesupdated event to the place repository. |
||
| 285 | * @param FacilitiesUpdated $facilitiesUpdated |
||
| 286 | */ |
||
| 287 | protected function applyFacilitiesUpdated(FacilitiesUpdated $facilitiesUpdated) |
||
| 312 | |||
| 313 | /** |
||
| 314 | * @param GeoCoordinatesUpdated $geoCoordinatesUpdated |
||
| 315 | */ |
||
| 316 | protected function applyGeoCoordinatesUpdated(GeoCoordinatesUpdated $geoCoordinatesUpdated) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * @param PlaceEvent $place |
||
| 332 | * @return JsonDocument |
||
| 333 | */ |
||
| 334 | protected function loadPlaceDocumentFromRepository(PlaceEvent $place) |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @param string $itemId |
||
| 347 | * @return JsonDocument |
||
| 348 | */ |
||
| 349 | protected function loadPlaceDocumentFromRepositoryById($itemId) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * @return string |
||
| 362 | */ |
||
| 363 | protected function getLabelAddedClassName() |
||
| 367 | |||
| 368 | /** |
||
| 369 | * @return string |
||
| 370 | */ |
||
| 371 | protected function getLabelRemovedClassName() |
||
| 375 | |||
| 376 | /** |
||
| 377 | * @return string |
||
| 378 | */ |
||
| 379 | protected function getImageAddedClassName() |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @return string |
||
| 386 | */ |
||
| 387 | protected function getImageRemovedClassName() |
||
| 391 | |||
| 392 | /** |
||
| 393 | * @return string |
||
| 394 | */ |
||
| 395 | protected function getImageUpdatedClassName() |
||
| 399 | |||
| 400 | protected function getMainImageSelectedClassName() |
||
| 404 | |||
| 405 | /** |
||
| 406 | * @return string |
||
| 407 | */ |
||
| 408 | protected function getTitleTranslatedClassName() |
||
| 412 | |||
| 413 | /** |
||
| 414 | * @return string |
||
| 415 | */ |
||
| 416 | protected function getDescriptionTranslatedClassName() |
||
| 420 | |||
| 421 | /** |
||
| 422 | * @return string |
||
| 423 | */ |
||
| 424 | protected function getOrganizerUpdatedClassName() |
||
| 428 | |||
| 429 | /** |
||
| 430 | * @return string |
||
| 431 | */ |
||
| 432 | protected function getOrganizerDeletedClassName() |
||
| 436 | |||
| 437 | protected function getBookingInfoUpdatedClassName() |
||
| 441 | |||
| 442 | /** |
||
| 443 | * @return string |
||
| 444 | */ |
||
| 445 | protected function getPriceInfoUpdatedClassName() |
||
| 449 | |||
| 450 | protected function getContactPointUpdatedClassName() |
||
| 454 | |||
| 455 | protected function getDescriptionUpdatedClassName() |
||
| 459 | |||
| 460 | protected function getTypicalAgeRangeUpdatedClassName() |
||
| 464 | |||
| 465 | protected function getTypicalAgeRangeDeletedClassName() |
||
| 469 | |||
| 470 | protected function getPublishedClassName() |
||
| 474 | |||
| 475 | protected function getApprovedClassName() |
||
| 479 | |||
| 480 | protected function getRejectedClassName() |
||
| 484 | |||
| 485 | protected function getFlaggedAsDuplicateClassName() |
||
| 489 | |||
| 490 | protected function getFlaggedAsInappropriateClassName() |
||
| 494 | } |
||
| 495 |
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.