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 |
||
63 | abstract class Offer extends EventSourcedAggregateRoot implements LabelAwareAggregateRoot |
||
64 | { |
||
65 | const DUPLICATE_REASON = 'duplicate'; |
||
66 | const INAPPROPRIATE_REASON = 'inappropriate'; |
||
67 | |||
68 | /** |
||
69 | * @var LabelCollection |
||
70 | */ |
||
71 | protected $labels; |
||
72 | |||
73 | /** |
||
74 | * @var ImageCollection |
||
75 | */ |
||
76 | protected $images; |
||
77 | |||
78 | /** |
||
79 | * @var string |
||
80 | * |
||
81 | * Organizer ids can come from UDB2 which does not strictly use UUIDs. |
||
82 | */ |
||
83 | protected $organizerId; |
||
84 | |||
85 | /** |
||
86 | * @var WorkflowStatus |
||
87 | */ |
||
88 | protected $workflowStatus; |
||
89 | |||
90 | /** |
||
91 | * @var StringLiteral|null |
||
92 | */ |
||
93 | protected $rejectedReason; |
||
94 | |||
95 | /** |
||
96 | * @var PriceInfo |
||
97 | */ |
||
98 | protected $priceInfo; |
||
99 | |||
100 | /** |
||
101 | * @var StringLiteral[] |
||
102 | */ |
||
103 | protected $titles; |
||
104 | |||
105 | /** |
||
106 | * @var Description[] |
||
107 | */ |
||
108 | protected $descriptions; |
||
109 | |||
110 | /** |
||
111 | * @var Language |
||
112 | */ |
||
113 | protected $mainLanguage; |
||
114 | |||
115 | /** |
||
116 | * @var string; |
||
117 | */ |
||
118 | protected $typeId; |
||
119 | |||
120 | /** |
||
121 | * @var string; |
||
122 | */ |
||
123 | protected $themeId; |
||
124 | |||
125 | /** |
||
126 | * @var array |
||
127 | */ |
||
128 | protected $facilities; |
||
129 | |||
130 | /** |
||
131 | * @var ContactPoint |
||
132 | */ |
||
133 | protected $contactPoint; |
||
134 | |||
135 | /** |
||
136 | * @var Calendar |
||
137 | */ |
||
138 | protected $calendar; |
||
139 | |||
140 | /** |
||
141 | * @var AgeRange |
||
142 | */ |
||
143 | protected $typicalAgeRange; |
||
144 | |||
145 | /** |
||
146 | * @var BookingInfo |
||
147 | */ |
||
148 | protected $bookingInfo; |
||
149 | |||
150 | /** |
||
151 | * Offer constructor. |
||
152 | */ |
||
153 | public function __construct() |
||
165 | |||
166 | /** |
||
167 | * @param EventType $type |
||
168 | */ |
||
169 | public function updateType(EventType $type) |
||
175 | |||
176 | /** |
||
177 | * @param Theme $theme |
||
178 | */ |
||
179 | public function updateTheme(Theme $theme) |
||
185 | |||
186 | /** |
||
187 | * @param array $facilities |
||
188 | */ |
||
189 | public function updateFacilities(array $facilities) |
||
195 | |||
196 | /** |
||
197 | * @param AbstractFacilitiesUpdated $facilitiesUpdated |
||
198 | */ |
||
199 | protected function applyFacilitiesUpdated(AbstractFacilitiesUpdated $facilitiesUpdated) |
||
203 | |||
204 | /** |
||
205 | * @param array $facilities1 |
||
206 | * @param array $facilities2 |
||
207 | * @return bool |
||
208 | */ |
||
209 | private function sameFacilities($facilities1, $facilities2) |
||
225 | |||
226 | /** |
||
227 | * Get the id of the main image if one is selected for this offer. |
||
228 | * |
||
229 | * @return UUID|null |
||
230 | */ |
||
231 | protected function getMainImageId() |
||
236 | |||
237 | /** |
||
238 | * @inheritdoc |
||
239 | */ |
||
240 | public function addLabel(Label $label) |
||
248 | |||
249 | /** |
||
250 | * @inheritdoc |
||
251 | */ |
||
252 | public function removeLabel(Label $label) |
||
260 | |||
261 | /** |
||
262 | * @param Labels $labels |
||
263 | */ |
||
264 | public function importLabels(Labels $labels) |
||
306 | |||
307 | /** |
||
308 | * @param Language $language |
||
309 | * @param Title $title |
||
310 | */ |
||
311 | public function updateTitle(Language $language, Title $title) |
||
323 | |||
324 | /** |
||
325 | * @param Description $description |
||
326 | * @param Language $language |
||
327 | */ |
||
328 | public function updateDescription(Description $description, Language $language) |
||
340 | |||
341 | /** |
||
342 | * @param Calendar $calendar |
||
343 | */ |
||
344 | public function updateCalendar(Calendar $calendar) |
||
352 | |||
353 | /** |
||
354 | * @param AbstractCalendarUpdated $calendarUpdated |
||
355 | */ |
||
356 | protected function applyCalendarUpdated(AbstractCalendarUpdated $calendarUpdated) |
||
360 | |||
361 | /** |
||
362 | * @param string $typicalAgeRange |
||
363 | */ |
||
364 | public function updateTypicalAgeRange($typicalAgeRange) |
||
372 | |||
373 | /** |
||
374 | * @param AbstractTypicalAgeRangeUpdated $typicalAgeRangeUpdated |
||
375 | */ |
||
376 | protected function applyTypicalAgeRangeUpdated(AbstractTypicalAgeRangeUpdated $typicalAgeRangeUpdated) |
||
380 | |||
381 | public function deleteTypicalAgeRange() |
||
389 | |||
390 | /** |
||
391 | * @param AbstractTypicalAgeRangeDeleted $typicalAgeRangeDeleted |
||
392 | */ |
||
393 | public function applyTypicalAgeRangeDeleted(AbstractTypicalAgeRangeDeleted $typicalAgeRangeDeleted) |
||
397 | |||
398 | /** |
||
399 | * @param string $organizerId |
||
400 | */ |
||
401 | public function updateOrganizer($organizerId) |
||
409 | |||
410 | /** |
||
411 | * Delete the given organizer. |
||
412 | * |
||
413 | * @param string $organizerId |
||
414 | */ |
||
415 | public function deleteOrganizer($organizerId) |
||
423 | |||
424 | /** |
||
425 | * Delete the current organizer regardless of the id. |
||
426 | */ |
||
427 | public function deleteCurrentOrganizer() |
||
435 | |||
436 | /** |
||
437 | * Updated the contact info. |
||
438 | * @param ContactPoint $contactPoint |
||
439 | */ |
||
440 | public function updateContactPoint(ContactPoint $contactPoint) |
||
448 | |||
449 | /** |
||
450 | * @param AbstractContactPointUpdated $contactPointUpdated |
||
451 | */ |
||
452 | protected function applyContactPointUpdated(AbstractContactPointUpdated $contactPointUpdated) |
||
456 | |||
457 | /** |
||
458 | * @param Coordinates $coordinates |
||
459 | */ |
||
460 | public function updateGeoCoordinates(Coordinates $coordinates) |
||
470 | |||
471 | /** |
||
472 | * Updated the booking info. |
||
473 | * |
||
474 | * @param BookingInfo $bookingInfo |
||
475 | */ |
||
476 | public function updateBookingInfo(BookingInfo $bookingInfo) |
||
484 | |||
485 | /** |
||
486 | * @param AbstractBookingInfoUpdated $bookingInfoUpdated |
||
487 | */ |
||
488 | public function applyBookingInfoUpdated(AbstractBookingInfoUpdated $bookingInfoUpdated) |
||
492 | |||
493 | /** |
||
494 | * @param PriceInfo $priceInfo |
||
495 | */ |
||
496 | public function updatePriceInfo(PriceInfo $priceInfo) |
||
504 | |||
505 | /** |
||
506 | * @param AbstractPriceInfoUpdated $priceInfoUpdated |
||
507 | */ |
||
508 | protected function applyPriceInfoUpdated(AbstractPriceInfoUpdated $priceInfoUpdated) |
||
512 | |||
513 | /** |
||
514 | * @param AbstractLabelAdded $labelAdded |
||
515 | */ |
||
516 | protected function applyLabelAdded(AbstractLabelAdded $labelAdded) |
||
520 | |||
521 | /** |
||
522 | * @param AbstractLabelRemoved $labelRemoved |
||
523 | */ |
||
524 | protected function applyLabelRemoved(AbstractLabelRemoved $labelRemoved) |
||
528 | |||
529 | /** |
||
530 | * @param AbstractThemeUpdated $themeUpdated |
||
531 | */ |
||
532 | protected function applyThemeUpdated(AbstractThemeUpdated $themeUpdated) |
||
536 | |||
537 | /** |
||
538 | * @param AbstractTypeUpdated $themeUpdated |
||
539 | */ |
||
540 | protected function applyTypeUpdated(AbstractTypeUpdated $themeUpdated) |
||
544 | |||
545 | protected function applyDescriptionUpdated(AbstractDescriptionUpdated $descriptionUpdated) |
||
550 | |||
551 | protected function applyDescriptionTranslated(AbstractDescriptionTranslated $descriptionTranslated) |
||
556 | |||
557 | /** |
||
558 | * Add a new image. |
||
559 | * |
||
560 | * @param Image $image |
||
561 | */ |
||
562 | public function addImage(Image $image) |
||
570 | |||
571 | /** |
||
572 | * @param AbstractUpdateImage $updateImageCommand |
||
573 | */ |
||
574 | public function updateImage(AbstractUpdateImage $updateImageCommand) |
||
582 | |||
583 | /** |
||
584 | * @param AbstractUpdateImage $updateImageCommand |
||
585 | * @return bool |
||
586 | */ |
||
587 | private function updateImageAllowed(AbstractUpdateImage $updateImageCommand) |
||
600 | |||
601 | /** |
||
602 | * Remove an image. |
||
603 | * |
||
604 | * @param Image $image |
||
605 | */ |
||
606 | public function removeImage(Image $image) |
||
614 | |||
615 | /** |
||
616 | * Make an existing image of the item the main image. |
||
617 | * |
||
618 | * @param Image $image |
||
619 | */ |
||
620 | public function selectMainImage(Image $image) |
||
634 | |||
635 | /** |
||
636 | * Delete the offer. |
||
637 | */ |
||
638 | public function delete() |
||
644 | |||
645 | /** |
||
646 | * @param CultureFeed_Cdb_Item_Base $cdbItem |
||
647 | */ |
||
648 | protected function importWorkflowStatus(CultureFeed_Cdb_Item_Base $cdbItem) |
||
657 | |||
658 | /** |
||
659 | * Publish the offer when it has workflowstatus draft. |
||
660 | * @param \DateTimeInterface $publicationDate |
||
661 | */ |
||
662 | public function publish(\DateTimeInterface $publicationDate) |
||
668 | |||
669 | /** |
||
670 | * @return bool |
||
671 | * @throws Exception |
||
672 | */ |
||
673 | View Code Duplication | private function guardPublish() |
|
685 | |||
686 | /** |
||
687 | * Approve the offer when it's waiting for validation. |
||
688 | */ |
||
689 | public function approve() |
||
693 | |||
694 | /** |
||
695 | * @return bool |
||
696 | * @throws Exception |
||
697 | */ |
||
698 | View Code Duplication | private function guardApprove() |
|
710 | |||
711 | /** |
||
712 | * Reject an offer that is waiting for validation with a given reason. |
||
713 | * @param StringLiteral $reason |
||
714 | */ |
||
715 | public function reject(StringLiteral $reason) |
||
719 | |||
720 | public function flagAsDuplicate() |
||
725 | |||
726 | public function flagAsInappropriate() |
||
731 | |||
732 | /** |
||
733 | * @param StringLiteral $reason |
||
734 | * @return bool |
||
735 | * false when the offer can still be rejected, true when the offer is already rejected for the same reason |
||
736 | * @throws Exception |
||
737 | */ |
||
738 | private function guardRejection(StringLiteral $reason) |
||
754 | |||
755 | /** |
||
756 | * @param Title $title |
||
757 | * @param Language $language |
||
758 | * @return bool |
||
759 | */ |
||
760 | View Code Duplication | private function isTitleChanged(Title $title, Language $language) |
|
767 | |||
768 | /** |
||
769 | * @param Description $description |
||
770 | * @param Language $language |
||
771 | * @return bool |
||
772 | */ |
||
773 | View Code Duplication | private function isDescriptionChanged(Description $description, Language $language) |
|
780 | |||
781 | /** |
||
782 | * Overwrites or resets the main image and all media objects |
||
783 | * by importing a new collection of images from UDB2. |
||
784 | * |
||
785 | * @param ImageCollection $images |
||
786 | */ |
||
787 | public function importImagesFromUDB2(ImageCollection $images) |
||
791 | |||
792 | /** |
||
793 | * Overwrites or resets the main image and all media objects |
||
794 | * by updating with a new collection of images from UDB2. |
||
795 | * |
||
796 | * @param ImageCollection $images |
||
797 | */ |
||
798 | public function updateImagesFromUDB2(ImageCollection $images) |
||
802 | |||
803 | /** |
||
804 | * @param AbstractPublished $published |
||
805 | */ |
||
806 | protected function applyPublished(AbstractPublished $published) |
||
810 | |||
811 | /** |
||
812 | * @param AbstractApproved $approved |
||
813 | */ |
||
814 | protected function applyApproved(AbstractApproved $approved) |
||
818 | |||
819 | /** |
||
820 | * @param AbstractRejected $rejected |
||
821 | */ |
||
822 | protected function applyRejected(AbstractRejected $rejected) |
||
827 | |||
828 | /** |
||
829 | * @param AbstractFlaggedAsDuplicate $flaggedAsDuplicate |
||
830 | */ |
||
831 | protected function applyFlaggedAsDuplicate(AbstractFlaggedAsDuplicate $flaggedAsDuplicate) |
||
836 | |||
837 | /** |
||
838 | * @param AbstractFlaggedAsInappropriate $flaggedAsInappropriate |
||
839 | */ |
||
840 | protected function applyFlaggedAsInappropriate(AbstractFlaggedAsInappropriate $flaggedAsInappropriate) |
||
845 | |||
846 | protected function applyImageAdded(AbstractImageAdded $imageAdded) |
||
850 | |||
851 | protected function applyImageUpdated(AbstractImageUpdated $imageUpdated) |
||
868 | |||
869 | protected function applyImageRemoved(AbstractImageRemoved $imageRemoved) |
||
873 | |||
874 | protected function applyMainImageSelected(AbstractMainImageSelected $mainImageSelected) |
||
878 | |||
879 | protected function applyOrganizerUpdated(AbstractOrganizerUpdated $organizerUpdated) |
||
883 | |||
884 | protected function applyOrganizerDeleted(AbstractOrganizerDeleted $organizerDeleted) |
||
888 | |||
889 | /** |
||
890 | * @param AbstractImagesImportedFromUDB2 $imagesImportedFromUDB2 |
||
891 | */ |
||
892 | protected function applyImagesImportedFromUDB2(AbstractImagesImportedFromUDB2 $imagesImportedFromUDB2) |
||
896 | |||
897 | /** |
||
898 | * @param AbstractImagesUpdatedFromUDB2 $imagesUpdatedFromUDB2 |
||
899 | */ |
||
900 | protected function applyImagesUpdatedFromUDB2(AbstractImagesUpdatedFromUDB2 $imagesUpdatedFromUDB2) |
||
904 | |||
905 | /** |
||
906 | * This indirect apply method can be called internally to deal with images coming from UDB2. |
||
907 | * Imports from UDB2 only contain the native Dutch content. |
||
908 | * @see https://github.com/cultuurnet/udb3-udb2-bridge/blob/db0a7ab2444f55bb3faae3d59b82b39aaeba253b/test/Media/ImageCollectionFactoryTest.php#L79-L103 |
||
909 | * Because of this we have to make sure translated images are left in place. |
||
910 | * |
||
911 | * @param AbstractImagesEvent $imagesEvent |
||
912 | */ |
||
913 | protected function applyUdb2ImagesEvent(AbstractImagesEvent $imagesEvent) |
||
929 | |||
930 | /** |
||
931 | * @param Label $label |
||
932 | * @return AbstractLabelAdded |
||
933 | */ |
||
934 | abstract protected function createLabelAddedEvent(Label $label); |
||
935 | |||
936 | /** |
||
937 | * @param Label $label |
||
938 | * @return AbstractLabelRemoved |
||
939 | */ |
||
940 | abstract protected function createLabelRemovedEvent(Label $label); |
||
941 | |||
942 | /** |
||
943 | * @param Labels $labels |
||
944 | * @return AbstractLabelsImported |
||
945 | */ |
||
946 | abstract protected function createLabelsImportedEvent(Labels $labels); |
||
947 | |||
948 | /** |
||
949 | * @param Language $language |
||
950 | * @param StringLiteral $title |
||
951 | * @return AbstractTitleTranslated |
||
952 | */ |
||
953 | abstract protected function createTitleTranslatedEvent(Language $language, StringLiteral $title); |
||
954 | |||
955 | /** |
||
956 | * @param Language $language |
||
957 | * @param StringLiteral $description |
||
958 | * @return AbstractDescriptionTranslated |
||
959 | */ |
||
960 | abstract protected function createDescriptionTranslatedEvent(Language $language, StringLiteral $description); |
||
961 | |||
962 | /** |
||
963 | * @param Image $image |
||
964 | * @return AbstractImageAdded |
||
965 | */ |
||
966 | abstract protected function createImageAddedEvent(Image $image); |
||
967 | |||
968 | /** |
||
969 | * @param Image $image |
||
970 | * @return AbstractImageRemoved |
||
971 | */ |
||
972 | abstract protected function createImageRemovedEvent(Image $image); |
||
973 | |||
974 | /** |
||
975 | * @param AbstractUpdateImage $updateImageCommand |
||
976 | * @return AbstractImageUpdated |
||
977 | */ |
||
978 | abstract protected function createImageUpdatedEvent( |
||
981 | |||
982 | /** |
||
983 | * @param Image $image |
||
984 | * @return AbstractMainImageSelected |
||
985 | */ |
||
986 | abstract protected function createMainImageSelectedEvent(Image $image); |
||
987 | |||
988 | /** |
||
989 | * @return AbstractOfferDeleted |
||
990 | */ |
||
991 | abstract protected function createOfferDeletedEvent(); |
||
992 | |||
993 | /** |
||
994 | * @param Title $title |
||
995 | * @return AbstractTitleUpdated |
||
996 | */ |
||
997 | abstract protected function createTitleUpdatedEvent(Title $title); |
||
998 | |||
999 | /** |
||
1000 | * @param string $description |
||
1001 | * @return AbstractDescriptionUpdated |
||
1002 | */ |
||
1003 | abstract protected function createDescriptionUpdatedEvent($description); |
||
1004 | |||
1005 | /** |
||
1006 | * @param Calendar $calendar |
||
1007 | * @return AbstractCalendarUpdated |
||
1008 | */ |
||
1009 | abstract protected function createCalendarUpdatedEvent(Calendar $calendar); |
||
1010 | |||
1011 | /** |
||
1012 | * @param string $typicalAgeRange |
||
1013 | * @return AbstractTypicalAgeRangeUpdated |
||
1014 | */ |
||
1015 | abstract protected function createTypicalAgeRangeUpdatedEvent($typicalAgeRange); |
||
1016 | |||
1017 | /** |
||
1018 | * @return AbstractTypicalAgeRangeDeleted |
||
1019 | */ |
||
1020 | abstract protected function createTypicalAgeRangeDeletedEvent(); |
||
1021 | |||
1022 | /** |
||
1023 | * @param string $organizerId |
||
1024 | * @return AbstractOrganizerUpdated |
||
1025 | */ |
||
1026 | abstract protected function createOrganizerUpdatedEvent($organizerId); |
||
1027 | |||
1028 | /** |
||
1029 | * @param string $organizerId |
||
1030 | * @return AbstractOrganizerDeleted |
||
1031 | */ |
||
1032 | abstract protected function createOrganizerDeletedEvent($organizerId); |
||
1033 | |||
1034 | /** |
||
1035 | * @param ContactPoint $contactPoint |
||
1036 | * @return AbstractContactPointUpdated |
||
1037 | */ |
||
1038 | abstract protected function createContactPointUpdatedEvent(ContactPoint $contactPoint); |
||
1039 | |||
1040 | /** |
||
1041 | * @param Coordinates $coordinates |
||
1042 | * @return AbstractGeoCoordinatesUpdated |
||
1043 | */ |
||
1044 | abstract protected function createGeoCoordinatesUpdatedEvent(Coordinates $coordinates); |
||
1045 | |||
1046 | /** |
||
1047 | * @param BookingInfo $bookingInfo |
||
1048 | * @return AbstractBookingInfoUpdated |
||
1049 | */ |
||
1050 | abstract protected function createBookingInfoUpdatedEvent(BookingInfo $bookingInfo); |
||
1051 | |||
1052 | /** |
||
1053 | * @param PriceInfo $priceInfo |
||
1054 | * @return AbstractPriceInfoUpdated |
||
1055 | */ |
||
1056 | abstract protected function createPriceInfoUpdatedEvent(PriceInfo $priceInfo); |
||
1057 | |||
1058 | /** |
||
1059 | * @param \DateTimeInterface $publicationDate |
||
1060 | * @return AbstractPublished |
||
1061 | */ |
||
1062 | abstract protected function createPublishedEvent(\DateTimeInterface $publicationDate); |
||
1063 | |||
1064 | /** |
||
1065 | * @return AbstractApproved |
||
1066 | */ |
||
1067 | abstract protected function createApprovedEvent(); |
||
1068 | |||
1069 | /** |
||
1070 | * @param StringLiteral $reason |
||
1071 | * @return AbstractRejected |
||
1072 | */ |
||
1073 | abstract protected function createRejectedEvent(StringLiteral $reason); |
||
1074 | |||
1075 | /** |
||
1076 | * @return AbstractFlaggedAsDuplicate |
||
1077 | */ |
||
1078 | abstract protected function createFlaggedAsDuplicate(); |
||
1079 | |||
1080 | /** |
||
1081 | * @return AbstractFlaggedAsInappropriate |
||
1082 | */ |
||
1083 | abstract protected function createFlaggedAsInappropriate(); |
||
1084 | |||
1085 | /** |
||
1086 | * @param ImageCollection $images |
||
1087 | * @return AbstractImagesImportedFromUDB2 |
||
1088 | */ |
||
1089 | abstract protected function createImagesImportedFromUDB2(ImageCollection $images); |
||
1090 | |||
1091 | /** |
||
1092 | * @param ImageCollection $images |
||
1093 | * @return AbstractImagesUpdatedFromUDB2 |
||
1094 | */ |
||
1095 | abstract protected function createImagesUpdatedFromUDB2(ImageCollection $images); |
||
1096 | |||
1097 | /** |
||
1098 | * @param EventType $type |
||
1099 | * @return AbstractTypeUpdated |
||
1100 | */ |
||
1101 | abstract protected function createTypeUpdatedEvent(EventType $type); |
||
1102 | |||
1103 | /** |
||
1104 | * @param Theme $theme |
||
1105 | * @return AbstractThemeUpdated |
||
1106 | */ |
||
1107 | abstract protected function createThemeUpdatedEvent(Theme $theme); |
||
1108 | |||
1109 | /** |
||
1110 | * @param array $facilities |
||
1111 | * @return AbstractFacilitiesUpdated |
||
1112 | */ |
||
1113 | abstract protected function createFacilitiesUpdatedEvent(array $facilities); |
||
1114 | } |
||
1115 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.