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 Offer 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 Offer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
61 | abstract class Offer extends EventSourcedAggregateRoot implements LabelAwareAggregateRoot |
||
62 | { |
||
63 | const DUPLICATE_REASON = 'duplicate'; |
||
64 | const INAPPROPRIATE_REASON = 'inappropriate'; |
||
65 | |||
66 | /** |
||
67 | * @var LabelCollection |
||
68 | */ |
||
69 | protected $labels; |
||
70 | |||
71 | /** |
||
72 | * @var ImageCollection |
||
73 | */ |
||
74 | protected $images; |
||
75 | |||
76 | /** |
||
77 | * @var string |
||
78 | * |
||
79 | * Organizer ids can come from UDB2 which does not strictly use UUIDs. |
||
80 | */ |
||
81 | protected $organizerId; |
||
82 | |||
83 | /** |
||
84 | * @var WorkflowStatus |
||
85 | */ |
||
86 | protected $workflowStatus; |
||
87 | |||
88 | /** |
||
89 | * @var StringLiteral|null |
||
90 | */ |
||
91 | protected $rejectedReason; |
||
92 | |||
93 | /** |
||
94 | * @var PriceInfo |
||
95 | */ |
||
96 | protected $priceInfo; |
||
97 | |||
98 | /** |
||
99 | * @var StringLiteral[] |
||
100 | */ |
||
101 | protected $titles; |
||
102 | |||
103 | /** |
||
104 | * @var Description[] |
||
105 | */ |
||
106 | protected $descriptions; |
||
107 | |||
108 | /** |
||
109 | * @var Language |
||
110 | */ |
||
111 | protected $mainLanguage; |
||
112 | |||
113 | /** |
||
114 | * @var string; |
||
115 | */ |
||
116 | protected $typeId; |
||
117 | |||
118 | /** |
||
119 | * @var string; |
||
120 | */ |
||
121 | protected $themeId; |
||
122 | |||
123 | /** |
||
124 | * @var array |
||
125 | */ |
||
126 | protected $facilities; |
||
127 | |||
128 | /** |
||
129 | * @var ContactPoint |
||
130 | */ |
||
131 | protected $contactPoint; |
||
132 | |||
133 | /** |
||
134 | * @var Calendar |
||
135 | */ |
||
136 | protected $calendar; |
||
137 | |||
138 | /** |
||
139 | * @var AgeRange |
||
140 | */ |
||
141 | protected $typicalAgeRange; |
||
142 | |||
143 | /** |
||
144 | * @var BookingInfo |
||
145 | */ |
||
146 | protected $bookingInfo; |
||
147 | |||
148 | /** |
||
149 | * Offer constructor. |
||
150 | */ |
||
151 | public function __construct() |
||
163 | |||
164 | /** |
||
165 | * @param EventType $type |
||
166 | */ |
||
167 | public function updateType(EventType $type) |
||
173 | |||
174 | /** |
||
175 | * @param Theme $theme |
||
176 | */ |
||
177 | public function updateTheme(Theme $theme) |
||
183 | |||
184 | /** |
||
185 | * @param array $facilities |
||
186 | */ |
||
187 | public function updateFacilities(array $facilities) |
||
193 | |||
194 | /** |
||
195 | * @param AbstractFacilitiesUpdated $facilitiesUpdated |
||
196 | */ |
||
197 | protected function applyFacilitiesUpdated(AbstractFacilitiesUpdated $facilitiesUpdated) |
||
201 | |||
202 | /** |
||
203 | * @param array $facilities1 |
||
204 | * @param array $facilities2 |
||
205 | * @return bool |
||
206 | */ |
||
207 | private function sameFacilities($facilities1, $facilities2) |
||
223 | |||
224 | /** |
||
225 | * Get the id of the main image if one is selected for this offer. |
||
226 | * |
||
227 | * @return UUID|null |
||
228 | */ |
||
229 | protected function getMainImageId() |
||
234 | |||
235 | /** |
||
236 | * @inheritdoc |
||
237 | */ |
||
238 | public function addLabel(Label $label) |
||
246 | |||
247 | /** |
||
248 | * @inheritdoc |
||
249 | */ |
||
250 | public function removeLabel(Label $label) |
||
258 | |||
259 | /** |
||
260 | * @param Language $language |
||
261 | * @param Title $title |
||
262 | */ |
||
263 | public function updateTitle(Language $language, Title $title) |
||
275 | |||
276 | /** |
||
277 | * @param Description $description |
||
278 | * @param Language $language |
||
279 | */ |
||
280 | public function updateDescription(Description $description, Language $language) |
||
292 | |||
293 | /** |
||
294 | * @param Calendar $calendar |
||
295 | */ |
||
296 | public function updateCalendar(Calendar $calendar) |
||
304 | |||
305 | /** |
||
306 | * @param AbstractCalendarUpdated $calendarUpdated |
||
307 | */ |
||
308 | protected function applyCalendarUpdated(AbstractCalendarUpdated $calendarUpdated) |
||
312 | |||
313 | /** |
||
314 | * @param string $typicalAgeRange |
||
315 | */ |
||
316 | public function updateTypicalAgeRange($typicalAgeRange) |
||
324 | |||
325 | /** |
||
326 | * @param AbstractTypicalAgeRangeUpdated $typicalAgeRangeUpdated |
||
327 | */ |
||
328 | protected function applyTypicalAgeRangeUpdated(AbstractTypicalAgeRangeUpdated $typicalAgeRangeUpdated) |
||
332 | |||
333 | public function deleteTypicalAgeRange() |
||
341 | |||
342 | /** |
||
343 | * @param AbstractTypicalAgeRangeDeleted $typicalAgeRangeDeleted |
||
344 | */ |
||
345 | public function applyTypicalAgeRangeDeleted(AbstractTypicalAgeRangeDeleted $typicalAgeRangeDeleted) |
||
349 | |||
350 | /** |
||
351 | * @param string $organizerId |
||
352 | */ |
||
353 | public function updateOrganizer($organizerId) |
||
361 | |||
362 | /** |
||
363 | * Delete the given organizer. |
||
364 | * |
||
365 | * @param string $organizerId |
||
366 | */ |
||
367 | public function deleteOrganizer($organizerId) |
||
375 | |||
376 | /** |
||
377 | * Updated the contact info. |
||
378 | * @param ContactPoint $contactPoint |
||
379 | */ |
||
380 | public function updateContactPoint(ContactPoint $contactPoint) |
||
388 | |||
389 | /** |
||
390 | * @param AbstractContactPointUpdated $contactPointUpdated |
||
391 | */ |
||
392 | protected function applyContactPointUpdated(AbstractContactPointUpdated $contactPointUpdated) |
||
396 | |||
397 | /** |
||
398 | * @param Coordinates $coordinates |
||
399 | */ |
||
400 | public function updateGeoCoordinates(Coordinates $coordinates) |
||
410 | |||
411 | /** |
||
412 | * Updated the booking info. |
||
413 | * |
||
414 | * @param BookingInfo $bookingInfo |
||
415 | */ |
||
416 | public function updateBookingInfo(BookingInfo $bookingInfo) |
||
424 | |||
425 | /** |
||
426 | * @param AbstractBookingInfoUpdated $bookingInfoUpdated |
||
427 | */ |
||
428 | public function applyBookingInfoUpdated(AbstractBookingInfoUpdated $bookingInfoUpdated) |
||
432 | |||
433 | /** |
||
434 | * @param PriceInfo $priceInfo |
||
435 | */ |
||
436 | public function updatePriceInfo(PriceInfo $priceInfo) |
||
444 | |||
445 | /** |
||
446 | * @param AbstractPriceInfoUpdated $priceInfoUpdated |
||
447 | */ |
||
448 | protected function applyPriceInfoUpdated(AbstractPriceInfoUpdated $priceInfoUpdated) |
||
452 | |||
453 | /** |
||
454 | * @param AbstractLabelAdded $labelAdded |
||
455 | */ |
||
456 | protected function applyLabelAdded(AbstractLabelAdded $labelAdded) |
||
460 | |||
461 | /** |
||
462 | * @param AbstractLabelRemoved $labelRemoved |
||
463 | */ |
||
464 | protected function applyLabelRemoved(AbstractLabelRemoved $labelRemoved) |
||
468 | |||
469 | /** |
||
470 | * @param AbstractThemeUpdated $themeUpdated |
||
471 | */ |
||
472 | protected function applyThemeUpdated(AbstractThemeUpdated $themeUpdated) |
||
476 | |||
477 | /** |
||
478 | * @param AbstractTypeUpdated $themeUpdated |
||
479 | */ |
||
480 | protected function applyTypeUpdated(AbstractTypeUpdated $themeUpdated) |
||
484 | |||
485 | protected function applyDescriptionUpdated(AbstractDescriptionUpdated $descriptionUpdated) |
||
490 | |||
491 | protected function applyDescriptionTranslated(AbstractDescriptionTranslated $descriptionTranslated) |
||
496 | |||
497 | /** |
||
498 | * Add a new image. |
||
499 | * |
||
500 | * @param Image $image |
||
501 | */ |
||
502 | public function addImage(Image $image) |
||
510 | |||
511 | /** |
||
512 | * @param AbstractUpdateImage $updateImageCommand |
||
513 | */ |
||
514 | public function updateImage(AbstractUpdateImage $updateImageCommand) |
||
522 | |||
523 | /** |
||
524 | * @param AbstractUpdateImage $updateImageCommand |
||
525 | * @return bool |
||
526 | */ |
||
527 | private function updateImageAllowed(AbstractUpdateImage $updateImageCommand) |
||
540 | |||
541 | /** |
||
542 | * Remove an image. |
||
543 | * |
||
544 | * @param Image $image |
||
545 | */ |
||
546 | public function removeImage(Image $image) |
||
554 | |||
555 | /** |
||
556 | * Make an existing image of the item the main image. |
||
557 | * |
||
558 | * @param Image $image |
||
559 | */ |
||
560 | public function selectMainImage(Image $image) |
||
574 | |||
575 | /** |
||
576 | * Delete the offer. |
||
577 | */ |
||
578 | public function delete() |
||
584 | |||
585 | /** |
||
586 | * @param CultureFeed_Cdb_Item_Base $cdbItem |
||
587 | */ |
||
588 | protected function importWorkflowStatus(CultureFeed_Cdb_Item_Base $cdbItem) |
||
597 | |||
598 | /** |
||
599 | * Publish the offer when it has workflowstatus draft. |
||
600 | * @param \DateTimeInterface $publicationDate |
||
601 | */ |
||
602 | public function publish(\DateTimeInterface $publicationDate) |
||
608 | |||
609 | /** |
||
610 | * @return bool |
||
611 | * @throws Exception |
||
612 | */ |
||
613 | View Code Duplication | private function guardPublish() |
|
625 | |||
626 | /** |
||
627 | * Approve the offer when it's waiting for validation. |
||
628 | */ |
||
629 | public function approve() |
||
633 | |||
634 | /** |
||
635 | * @return bool |
||
636 | * @throws Exception |
||
637 | */ |
||
638 | View Code Duplication | private function guardApprove() |
|
650 | |||
651 | /** |
||
652 | * Reject an offer that is waiting for validation with a given reason. |
||
653 | * @param StringLiteral $reason |
||
654 | */ |
||
655 | public function reject(StringLiteral $reason) |
||
659 | |||
660 | public function flagAsDuplicate() |
||
665 | |||
666 | public function flagAsInappropriate() |
||
671 | |||
672 | /** |
||
673 | * @param StringLiteral $reason |
||
674 | * @return bool |
||
675 | * false when the offer can still be rejected, true when the offer is already rejected for the same reason |
||
676 | * @throws Exception |
||
677 | */ |
||
678 | private function guardRejection(StringLiteral $reason) |
||
694 | |||
695 | /** |
||
696 | * @param Title $title |
||
697 | * @param Language $language |
||
698 | * @return bool |
||
699 | */ |
||
700 | View Code Duplication | private function isTitleChanged(Title $title, Language $language) |
|
707 | |||
708 | /** |
||
709 | * @param Description $description |
||
710 | * @param Language $language |
||
711 | * @return bool |
||
712 | */ |
||
713 | View Code Duplication | private function isDescriptionChanged(Description $description, Language $language) |
|
720 | |||
721 | /** |
||
722 | * Overwrites or resets the main image and all media objects |
||
723 | * by importing a new collection of images from UDB2. |
||
724 | * |
||
725 | * @param ImageCollection $images |
||
726 | */ |
||
727 | public function importImagesFromUDB2(ImageCollection $images) |
||
731 | |||
732 | /** |
||
733 | * Overwrites or resets the main image and all media objects |
||
734 | * by updating with a new collection of images from UDB2. |
||
735 | * |
||
736 | * @param ImageCollection $images |
||
737 | */ |
||
738 | public function updateImagesFromUDB2(ImageCollection $images) |
||
742 | |||
743 | /** |
||
744 | * @param AbstractPublished $published |
||
745 | */ |
||
746 | protected function applyPublished(AbstractPublished $published) |
||
750 | |||
751 | /** |
||
752 | * @param AbstractApproved $approved |
||
753 | */ |
||
754 | protected function applyApproved(AbstractApproved $approved) |
||
758 | |||
759 | /** |
||
760 | * @param AbstractRejected $rejected |
||
761 | */ |
||
762 | protected function applyRejected(AbstractRejected $rejected) |
||
767 | |||
768 | /** |
||
769 | * @param AbstractFlaggedAsDuplicate $flaggedAsDuplicate |
||
770 | */ |
||
771 | protected function applyFlaggedAsDuplicate(AbstractFlaggedAsDuplicate $flaggedAsDuplicate) |
||
776 | |||
777 | /** |
||
778 | * @param AbstractFlaggedAsInappropriate $flaggedAsInappropriate |
||
779 | */ |
||
780 | protected function applyFlaggedAsInappropriate(AbstractFlaggedAsInappropriate $flaggedAsInappropriate) |
||
785 | |||
786 | protected function applyImageAdded(AbstractImageAdded $imageAdded) |
||
790 | |||
791 | protected function applyImageUpdated(AbstractImageUpdated $imageUpdated) |
||
808 | |||
809 | protected function applyImageRemoved(AbstractImageRemoved $imageRemoved) |
||
813 | |||
814 | protected function applyMainImageSelected(AbstractMainImageSelected $mainImageSelected) |
||
818 | |||
819 | protected function applyOrganizerUpdated(AbstractOrganizerUpdated $organizerUpdated) |
||
823 | |||
824 | protected function applyOrganizerDeleted(AbstractOrganizerDeleted $organizerDeleted) |
||
828 | |||
829 | /** |
||
830 | * @param AbstractImagesImportedFromUDB2 $imagesImportedFromUDB2 |
||
831 | */ |
||
832 | protected function applyImagesImportedFromUDB2(AbstractImagesImportedFromUDB2 $imagesImportedFromUDB2) |
||
836 | |||
837 | /** |
||
838 | * @param AbstractImagesUpdatedFromUDB2 $imagesUpdatedFromUDB2 |
||
839 | */ |
||
840 | protected function applyImagesUpdatedFromUDB2(AbstractImagesUpdatedFromUDB2 $imagesUpdatedFromUDB2) |
||
844 | |||
845 | /** |
||
846 | * This indirect apply method can be called internally to deal with images coming from UDB2. |
||
847 | * Imports from UDB2 only contain the native Dutch content. |
||
848 | * @see https://github.com/cultuurnet/udb3-udb2-bridge/blob/db0a7ab2444f55bb3faae3d59b82b39aaeba253b/test/Media/ImageCollectionFactoryTest.php#L79-L103 |
||
849 | * Because of this we have to make sure translated images are left in place. |
||
850 | * |
||
851 | * @param AbstractImagesEvent $imagesEvent |
||
852 | */ |
||
853 | protected function applyUdb2ImagesEvent(AbstractImagesEvent $imagesEvent) |
||
869 | |||
870 | /** |
||
871 | * @param Label $label |
||
872 | * @return AbstractLabelAdded |
||
873 | */ |
||
874 | abstract protected function createLabelAddedEvent(Label $label); |
||
875 | |||
876 | /** |
||
877 | * @param Label $label |
||
878 | * @return AbstractLabelRemoved |
||
879 | */ |
||
880 | abstract protected function createLabelRemovedEvent(Label $label); |
||
881 | |||
882 | /** |
||
883 | * @param Language $language |
||
884 | * @param StringLiteral $title |
||
885 | * @return AbstractTitleTranslated |
||
886 | */ |
||
887 | abstract protected function createTitleTranslatedEvent(Language $language, StringLiteral $title); |
||
888 | |||
889 | /** |
||
890 | * @param Language $language |
||
891 | * @param StringLiteral $description |
||
892 | * @return AbstractDescriptionTranslated |
||
893 | */ |
||
894 | abstract protected function createDescriptionTranslatedEvent(Language $language, StringLiteral $description); |
||
895 | |||
896 | /** |
||
897 | * @param Image $image |
||
898 | * @return AbstractImageAdded |
||
899 | */ |
||
900 | abstract protected function createImageAddedEvent(Image $image); |
||
901 | |||
902 | /** |
||
903 | * @param Image $image |
||
904 | * @return AbstractImageRemoved |
||
905 | */ |
||
906 | abstract protected function createImageRemovedEvent(Image $image); |
||
907 | |||
908 | /** |
||
909 | * @param AbstractUpdateImage $updateImageCommand |
||
910 | * @return AbstractImageUpdated |
||
911 | */ |
||
912 | abstract protected function createImageUpdatedEvent( |
||
915 | |||
916 | /** |
||
917 | * @param Image $image |
||
918 | * @return AbstractMainImageSelected |
||
919 | */ |
||
920 | abstract protected function createMainImageSelectedEvent(Image $image); |
||
921 | |||
922 | /** |
||
923 | * @return AbstractOfferDeleted |
||
924 | */ |
||
925 | abstract protected function createOfferDeletedEvent(); |
||
926 | |||
927 | /** |
||
928 | * @param Title $title |
||
929 | * @return AbstractTitleUpdated |
||
930 | */ |
||
931 | abstract protected function createTitleUpdatedEvent(Title $title); |
||
932 | |||
933 | /** |
||
934 | * @param string $description |
||
935 | * @return AbstractDescriptionUpdated |
||
936 | */ |
||
937 | abstract protected function createDescriptionUpdatedEvent($description); |
||
938 | |||
939 | /** |
||
940 | * @param Calendar $calendar |
||
941 | * @return AbstractCalendarUpdated |
||
942 | */ |
||
943 | abstract protected function createCalendarUpdatedEvent(Calendar $calendar); |
||
944 | |||
945 | /** |
||
946 | * @param string $typicalAgeRange |
||
947 | * @return AbstractTypicalAgeRangeUpdated |
||
948 | */ |
||
949 | abstract protected function createTypicalAgeRangeUpdatedEvent($typicalAgeRange); |
||
950 | |||
951 | /** |
||
952 | * @return AbstractTypicalAgeRangeDeleted |
||
953 | */ |
||
954 | abstract protected function createTypicalAgeRangeDeletedEvent(); |
||
955 | |||
956 | /** |
||
957 | * @param string $organizerId |
||
958 | * @return AbstractOrganizerUpdated |
||
959 | */ |
||
960 | abstract protected function createOrganizerUpdatedEvent($organizerId); |
||
961 | |||
962 | /** |
||
963 | * @param string $organizerId |
||
964 | * @return AbstractOrganizerDeleted |
||
965 | */ |
||
966 | abstract protected function createOrganizerDeletedEvent($organizerId); |
||
967 | |||
968 | /** |
||
969 | * @param ContactPoint $contactPoint |
||
970 | * @return AbstractContactPointUpdated |
||
971 | */ |
||
972 | abstract protected function createContactPointUpdatedEvent(ContactPoint $contactPoint); |
||
973 | |||
974 | /** |
||
975 | * @param Coordinates $coordinates |
||
976 | * @return AbstractGeoCoordinatesUpdated |
||
977 | */ |
||
978 | abstract protected function createGeoCoordinatesUpdatedEvent(Coordinates $coordinates); |
||
979 | |||
980 | /** |
||
981 | * @param BookingInfo $bookingInfo |
||
982 | * @return AbstractBookingInfoUpdated |
||
983 | */ |
||
984 | abstract protected function createBookingInfoUpdatedEvent(BookingInfo $bookingInfo); |
||
985 | |||
986 | /** |
||
987 | * @param PriceInfo $priceInfo |
||
988 | * @return AbstractPriceInfoUpdated |
||
989 | */ |
||
990 | abstract protected function createPriceInfoUpdatedEvent(PriceInfo $priceInfo); |
||
991 | |||
992 | /** |
||
993 | * @param \DateTimeInterface $publicationDate |
||
994 | * @return AbstractPublished |
||
995 | */ |
||
996 | abstract protected function createPublishedEvent(\DateTimeInterface $publicationDate); |
||
997 | |||
998 | /** |
||
999 | * @return AbstractApproved |
||
1000 | */ |
||
1001 | abstract protected function createApprovedEvent(); |
||
1002 | |||
1003 | /** |
||
1004 | * @param StringLiteral $reason |
||
1005 | * @return AbstractRejected |
||
1006 | */ |
||
1007 | abstract protected function createRejectedEvent(StringLiteral $reason); |
||
1008 | |||
1009 | /** |
||
1010 | * @return AbstractFlaggedAsDuplicate |
||
1011 | */ |
||
1012 | abstract protected function createFlaggedAsDuplicate(); |
||
1013 | |||
1014 | /** |
||
1015 | * @return AbstractFlaggedAsInappropriate |
||
1016 | */ |
||
1017 | abstract protected function createFlaggedAsInappropriate(); |
||
1018 | |||
1019 | /** |
||
1020 | * @param ImageCollection $images |
||
1021 | * @return AbstractImagesImportedFromUDB2 |
||
1022 | */ |
||
1023 | abstract protected function createImagesImportedFromUDB2(ImageCollection $images); |
||
1024 | |||
1025 | /** |
||
1026 | * @param ImageCollection $images |
||
1027 | * @return AbstractImagesUpdatedFromUDB2 |
||
1028 | */ |
||
1029 | abstract protected function createImagesUpdatedFromUDB2(ImageCollection $images); |
||
1030 | |||
1031 | /** |
||
1032 | * @param EventType $type |
||
1033 | * @return AbstractTypeUpdated |
||
1034 | */ |
||
1035 | abstract protected function createTypeUpdatedEvent(EventType $type); |
||
1036 | |||
1037 | /** |
||
1038 | * @param Theme $theme |
||
1039 | * @return AbstractThemeUpdated |
||
1040 | */ |
||
1041 | abstract protected function createThemeUpdatedEvent(Theme $theme); |
||
1042 | |||
1043 | /** |
||
1044 | * @param array $facilities |
||
1045 | * @return AbstractFacilitiesUpdated |
||
1046 | */ |
||
1047 | abstract protected function createFacilitiesUpdatedEvent(array $facilities); |
||
1048 | } |
||
1049 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.