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 |
||
65 | class PlaceLDProjector extends OfferLDProjector implements EventListenerInterface |
||
66 | { |
||
67 | /** |
||
68 | * @var CdbXMLImporter |
||
69 | */ |
||
70 | protected $cdbXMLImporter; |
||
71 | |||
72 | /** |
||
73 | * @param DocumentRepositoryInterface $repository |
||
74 | * @param IriGeneratorInterface $iriGenerator |
||
75 | * @param EntityServiceInterface $organizerService |
||
76 | * @param SerializerInterface $mediaObjectSerializer |
||
77 | * @param CdbXMLImporter $cdbXMLImporter |
||
78 | * @param JsonDocumentMetaDataEnricherInterface $jsonDocumentMetaDataEnricher |
||
79 | */ |
||
80 | public function __construct( |
||
81 | DocumentRepositoryInterface $repository, |
||
82 | IriGeneratorInterface $iriGenerator, |
||
83 | EntityServiceInterface $organizerService, |
||
84 | SerializerInterface $mediaObjectSerializer, |
||
85 | CdbXMLImporter $cdbXMLImporter, |
||
86 | JsonDocumentMetaDataEnricherInterface $jsonDocumentMetaDataEnricher |
||
87 | ) { |
||
88 | parent::__construct( |
||
89 | $repository, |
||
90 | $iriGenerator, |
||
91 | $organizerService, |
||
92 | $mediaObjectSerializer, |
||
93 | $jsonDocumentMetaDataEnricher |
||
94 | ); |
||
95 | |||
96 | $this->cdbXMLImporter = $cdbXMLImporter; |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * @param PlaceImportedFromUDB2 $placeImportedFromUDB2 |
||
101 | * @return JsonDocument |
||
102 | */ |
||
103 | protected function applyPlaceImportedFromUDB2( |
||
104 | PlaceImportedFromUDB2 $placeImportedFromUDB2 |
||
105 | ) { |
||
106 | return $this->projectActorImportedFromUDB2($placeImportedFromUDB2); |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * @param PlaceUpdatedFromUDB2 $placeUpdatedFromUDB2 |
||
111 | * @return JsonDocument |
||
112 | */ |
||
113 | protected function applyPlaceUpdatedFromUDB2( |
||
118 | |||
119 | /** |
||
120 | * @param ActorImportedFromUDB2 $actorImportedFromUDB2 |
||
121 | * @return JsonDocument |
||
122 | */ |
||
123 | protected function projectActorImportedFromUDB2( |
||
158 | |||
159 | /** |
||
160 | * @param string $id |
||
161 | * @return JsonDocument |
||
162 | */ |
||
163 | View Code Duplication | protected function newDocument($id) |
|
173 | |||
174 | /** |
||
175 | * @param PlaceCreated $placeCreated |
||
176 | * @param DomainMessage $domainMessage |
||
177 | * @return JsonDocument |
||
178 | */ |
||
179 | protected function applyPlaceCreated(PlaceCreated $placeCreated, DomainMessage $domainMessage) |
||
238 | |||
239 | /** |
||
240 | * @param PlaceDeleted $placeDeleted |
||
241 | * @return null |
||
242 | */ |
||
243 | protected function applyPlaceDeleted(PlaceDeleted $placeDeleted) |
||
248 | |||
249 | /** |
||
250 | * Apply the major info updated command to the projector. |
||
251 | * @param MajorInfoUpdated $majorInfoUpdated |
||
252 | * @return JsonDocument |
||
253 | */ |
||
254 | protected function applyMajorInfoUpdated(MajorInfoUpdated $majorInfoUpdated) |
||
298 | |||
299 | /** |
||
300 | * @param AddressUpdated $addressUpdated |
||
301 | * @return JsonDocument |
||
302 | */ |
||
303 | View Code Duplication | protected function applyAddressUpdated(AddressUpdated $addressUpdated) |
|
310 | |||
311 | /** |
||
312 | * @param AddressTranslated $addressTranslated |
||
313 | * @return JsonDocument |
||
314 | */ |
||
315 | View Code Duplication | protected function applyAddressTranslated(AddressTranslated $addressTranslated) |
|
322 | |||
323 | /** |
||
324 | * @param \stdClass $jsonLd |
||
325 | * @param Address $address |
||
326 | * @param Language $language |
||
327 | */ |
||
328 | protected function setAddress(\stdClass $jsonLd, Address $address, Language $language) |
||
348 | |||
349 | /** |
||
350 | * Apply the facilitiesupdated event to the place repository. |
||
351 | * @param FacilitiesUpdated $facilitiesUpdated |
||
352 | * @return JsonDocument |
||
353 | */ |
||
354 | protected function applyFacilitiesUpdated(FacilitiesUpdated $facilitiesUpdated) |
||
379 | |||
380 | /** |
||
381 | * @param GeoCoordinatesUpdated $geoCoordinatesUpdated |
||
382 | * @return JsonDocument |
||
383 | */ |
||
384 | protected function applyGeoCoordinatesUpdated(GeoCoordinatesUpdated $geoCoordinatesUpdated) |
||
397 | |||
398 | /** |
||
399 | * @param PlaceEvent $place |
||
400 | * @return JsonDocument |
||
401 | */ |
||
402 | protected function loadPlaceDocumentFromRepository(PlaceEvent $place) |
||
412 | |||
413 | /** |
||
414 | * @param string $itemId |
||
415 | * @return JsonDocument |
||
416 | */ |
||
417 | protected function loadPlaceDocumentFromRepositoryById($itemId) |
||
427 | |||
428 | /** |
||
429 | * @return string |
||
430 | */ |
||
431 | protected function getLabelAddedClassName() |
||
435 | |||
436 | /** |
||
437 | * @return string |
||
438 | */ |
||
439 | protected function getLabelRemovedClassName() |
||
443 | |||
444 | /** |
||
445 | * @return string |
||
446 | */ |
||
447 | protected function getImageAddedClassName() |
||
451 | |||
452 | /** |
||
453 | * @return string |
||
454 | */ |
||
455 | protected function getImageRemovedClassName() |
||
459 | |||
460 | /** |
||
461 | * @return string |
||
462 | */ |
||
463 | protected function getImageUpdatedClassName() |
||
467 | |||
468 | protected function getMainImageSelectedClassName() |
||
472 | |||
473 | /** |
||
474 | * @return string |
||
475 | */ |
||
476 | protected function getTitleTranslatedClassName() |
||
480 | |||
481 | /** |
||
482 | * @return string |
||
483 | */ |
||
484 | protected function getDescriptionTranslatedClassName() |
||
488 | |||
489 | /** |
||
490 | * @return string |
||
491 | */ |
||
492 | protected function getOrganizerUpdatedClassName() |
||
496 | |||
497 | /** |
||
498 | * @return string |
||
499 | */ |
||
500 | protected function getOrganizerDeletedClassName() |
||
504 | |||
505 | protected function getBookingInfoUpdatedClassName() |
||
509 | |||
510 | /** |
||
511 | * @return string |
||
512 | */ |
||
513 | protected function getPriceInfoUpdatedClassName() |
||
517 | |||
518 | protected function getContactPointUpdatedClassName() |
||
522 | |||
523 | protected function getDescriptionUpdatedClassName() |
||
527 | |||
528 | protected function getCalendarUpdatedClassName() |
||
532 | |||
533 | protected function getTypicalAgeRangeUpdatedClassName() |
||
537 | |||
538 | protected function getTypicalAgeRangeDeletedClassName() |
||
542 | |||
543 | protected function getPublishedClassName() |
||
547 | |||
548 | protected function getApprovedClassName() |
||
552 | |||
553 | protected function getRejectedClassName() |
||
557 | |||
558 | protected function getFlaggedAsDuplicateClassName() |
||
562 | |||
563 | protected function getFlaggedAsInappropriateClassName() |
||
567 | |||
568 | protected function getImagesImportedFromUdb2ClassName() |
||
572 | |||
573 | protected function getImagesUpdatedFromUdb2ClassName() |
||
577 | } |
||
578 |
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.