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 | * Delete the current organizer regardless of the id. |
||
378 | */ |
||
379 | public function deleteCurrentOrganizer() |
||
387 | |||
388 | /** |
||
389 | * Updated the contact info. |
||
390 | * @param ContactPoint $contactPoint |
||
391 | */ |
||
392 | public function updateContactPoint(ContactPoint $contactPoint) |
||
400 | |||
401 | /** |
||
402 | * @param AbstractContactPointUpdated $contactPointUpdated |
||
403 | */ |
||
404 | protected function applyContactPointUpdated(AbstractContactPointUpdated $contactPointUpdated) |
||
408 | |||
409 | /** |
||
410 | * @param Coordinates $coordinates |
||
411 | */ |
||
412 | public function updateGeoCoordinates(Coordinates $coordinates) |
||
422 | |||
423 | /** |
||
424 | * Updated the booking info. |
||
425 | * |
||
426 | * @param BookingInfo $bookingInfo |
||
427 | */ |
||
428 | public function updateBookingInfo(BookingInfo $bookingInfo) |
||
436 | |||
437 | /** |
||
438 | * @param AbstractBookingInfoUpdated $bookingInfoUpdated |
||
439 | */ |
||
440 | public function applyBookingInfoUpdated(AbstractBookingInfoUpdated $bookingInfoUpdated) |
||
444 | |||
445 | /** |
||
446 | * @param PriceInfo $priceInfo |
||
447 | */ |
||
448 | public function updatePriceInfo(PriceInfo $priceInfo) |
||
456 | |||
457 | /** |
||
458 | * @param AbstractPriceInfoUpdated $priceInfoUpdated |
||
459 | */ |
||
460 | protected function applyPriceInfoUpdated(AbstractPriceInfoUpdated $priceInfoUpdated) |
||
464 | |||
465 | /** |
||
466 | * @param AbstractLabelAdded $labelAdded |
||
467 | */ |
||
468 | protected function applyLabelAdded(AbstractLabelAdded $labelAdded) |
||
472 | |||
473 | /** |
||
474 | * @param AbstractLabelRemoved $labelRemoved |
||
475 | */ |
||
476 | protected function applyLabelRemoved(AbstractLabelRemoved $labelRemoved) |
||
480 | |||
481 | /** |
||
482 | * @param AbstractThemeUpdated $themeUpdated |
||
483 | */ |
||
484 | protected function applyThemeUpdated(AbstractThemeUpdated $themeUpdated) |
||
488 | |||
489 | /** |
||
490 | * @param AbstractTypeUpdated $themeUpdated |
||
491 | */ |
||
492 | protected function applyTypeUpdated(AbstractTypeUpdated $themeUpdated) |
||
496 | |||
497 | protected function applyDescriptionUpdated(AbstractDescriptionUpdated $descriptionUpdated) |
||
502 | |||
503 | protected function applyDescriptionTranslated(AbstractDescriptionTranslated $descriptionTranslated) |
||
508 | |||
509 | /** |
||
510 | * Add a new image. |
||
511 | * |
||
512 | * @param Image $image |
||
513 | */ |
||
514 | public function addImage(Image $image) |
||
522 | |||
523 | /** |
||
524 | * @param AbstractUpdateImage $updateImageCommand |
||
525 | */ |
||
526 | public function updateImage(AbstractUpdateImage $updateImageCommand) |
||
534 | |||
535 | /** |
||
536 | * @param AbstractUpdateImage $updateImageCommand |
||
537 | * @return bool |
||
538 | */ |
||
539 | private function updateImageAllowed(AbstractUpdateImage $updateImageCommand) |
||
552 | |||
553 | /** |
||
554 | * Remove an image. |
||
555 | * |
||
556 | * @param Image $image |
||
557 | */ |
||
558 | public function removeImage(Image $image) |
||
566 | |||
567 | /** |
||
568 | * Make an existing image of the item the main image. |
||
569 | * |
||
570 | * @param Image $image |
||
571 | */ |
||
572 | public function selectMainImage(Image $image) |
||
586 | |||
587 | /** |
||
588 | * Delete the offer. |
||
589 | */ |
||
590 | public function delete() |
||
596 | |||
597 | /** |
||
598 | * @param CultureFeed_Cdb_Item_Base $cdbItem |
||
599 | */ |
||
600 | protected function importWorkflowStatus(CultureFeed_Cdb_Item_Base $cdbItem) |
||
609 | |||
610 | /** |
||
611 | * Publish the offer when it has workflowstatus draft. |
||
612 | * @param \DateTimeInterface $publicationDate |
||
613 | */ |
||
614 | public function publish(\DateTimeInterface $publicationDate) |
||
620 | |||
621 | /** |
||
622 | * @return bool |
||
623 | * @throws Exception |
||
624 | */ |
||
625 | View Code Duplication | private function guardPublish() |
|
637 | |||
638 | /** |
||
639 | * Approve the offer when it's waiting for validation. |
||
640 | */ |
||
641 | public function approve() |
||
645 | |||
646 | /** |
||
647 | * @return bool |
||
648 | * @throws Exception |
||
649 | */ |
||
650 | View Code Duplication | private function guardApprove() |
|
662 | |||
663 | /** |
||
664 | * Reject an offer that is waiting for validation with a given reason. |
||
665 | * @param StringLiteral $reason |
||
666 | */ |
||
667 | public function reject(StringLiteral $reason) |
||
671 | |||
672 | public function flagAsDuplicate() |
||
677 | |||
678 | public function flagAsInappropriate() |
||
683 | |||
684 | /** |
||
685 | * @param StringLiteral $reason |
||
686 | * @return bool |
||
687 | * false when the offer can still be rejected, true when the offer is already rejected for the same reason |
||
688 | * @throws Exception |
||
689 | */ |
||
690 | private function guardRejection(StringLiteral $reason) |
||
706 | |||
707 | /** |
||
708 | * @param Title $title |
||
709 | * @param Language $language |
||
710 | * @return bool |
||
711 | */ |
||
712 | View Code Duplication | private function isTitleChanged(Title $title, Language $language) |
|
719 | |||
720 | /** |
||
721 | * @param Description $description |
||
722 | * @param Language $language |
||
723 | * @return bool |
||
724 | */ |
||
725 | View Code Duplication | private function isDescriptionChanged(Description $description, Language $language) |
|
732 | |||
733 | /** |
||
734 | * Overwrites or resets the main image and all media objects |
||
735 | * by importing a new collection of images from UDB2. |
||
736 | * |
||
737 | * @param ImageCollection $images |
||
738 | */ |
||
739 | public function importImagesFromUDB2(ImageCollection $images) |
||
743 | |||
744 | /** |
||
745 | * Overwrites or resets the main image and all media objects |
||
746 | * by updating with a new collection of images from UDB2. |
||
747 | * |
||
748 | * @param ImageCollection $images |
||
749 | */ |
||
750 | public function updateImagesFromUDB2(ImageCollection $images) |
||
754 | |||
755 | /** |
||
756 | * @param AbstractPublished $published |
||
757 | */ |
||
758 | protected function applyPublished(AbstractPublished $published) |
||
762 | |||
763 | /** |
||
764 | * @param AbstractApproved $approved |
||
765 | */ |
||
766 | protected function applyApproved(AbstractApproved $approved) |
||
770 | |||
771 | /** |
||
772 | * @param AbstractRejected $rejected |
||
773 | */ |
||
774 | protected function applyRejected(AbstractRejected $rejected) |
||
779 | |||
780 | /** |
||
781 | * @param AbstractFlaggedAsDuplicate $flaggedAsDuplicate |
||
782 | */ |
||
783 | protected function applyFlaggedAsDuplicate(AbstractFlaggedAsDuplicate $flaggedAsDuplicate) |
||
788 | |||
789 | /** |
||
790 | * @param AbstractFlaggedAsInappropriate $flaggedAsInappropriate |
||
791 | */ |
||
792 | protected function applyFlaggedAsInappropriate(AbstractFlaggedAsInappropriate $flaggedAsInappropriate) |
||
797 | |||
798 | protected function applyImageAdded(AbstractImageAdded $imageAdded) |
||
802 | |||
803 | protected function applyImageUpdated(AbstractImageUpdated $imageUpdated) |
||
820 | |||
821 | protected function applyImageRemoved(AbstractImageRemoved $imageRemoved) |
||
825 | |||
826 | protected function applyMainImageSelected(AbstractMainImageSelected $mainImageSelected) |
||
830 | |||
831 | protected function applyOrganizerUpdated(AbstractOrganizerUpdated $organizerUpdated) |
||
835 | |||
836 | protected function applyOrganizerDeleted(AbstractOrganizerDeleted $organizerDeleted) |
||
840 | |||
841 | /** |
||
842 | * @param AbstractImagesImportedFromUDB2 $imagesImportedFromUDB2 |
||
843 | */ |
||
844 | protected function applyImagesImportedFromUDB2(AbstractImagesImportedFromUDB2 $imagesImportedFromUDB2) |
||
848 | |||
849 | /** |
||
850 | * @param AbstractImagesUpdatedFromUDB2 $imagesUpdatedFromUDB2 |
||
851 | */ |
||
852 | protected function applyImagesUpdatedFromUDB2(AbstractImagesUpdatedFromUDB2 $imagesUpdatedFromUDB2) |
||
856 | |||
857 | /** |
||
858 | * This indirect apply method can be called internally to deal with images coming from UDB2. |
||
859 | * Imports from UDB2 only contain the native Dutch content. |
||
860 | * @see https://github.com/cultuurnet/udb3-udb2-bridge/blob/db0a7ab2444f55bb3faae3d59b82b39aaeba253b/test/Media/ImageCollectionFactoryTest.php#L79-L103 |
||
861 | * Because of this we have to make sure translated images are left in place. |
||
862 | * |
||
863 | * @param AbstractImagesEvent $imagesEvent |
||
864 | */ |
||
865 | protected function applyUdb2ImagesEvent(AbstractImagesEvent $imagesEvent) |
||
881 | |||
882 | /** |
||
883 | * @param Label $label |
||
884 | * @return AbstractLabelAdded |
||
885 | */ |
||
886 | abstract protected function createLabelAddedEvent(Label $label); |
||
887 | |||
888 | /** |
||
889 | * @param Label $label |
||
890 | * @return AbstractLabelRemoved |
||
891 | */ |
||
892 | abstract protected function createLabelRemovedEvent(Label $label); |
||
893 | |||
894 | /** |
||
895 | * @param Language $language |
||
896 | * @param StringLiteral $title |
||
897 | * @return AbstractTitleTranslated |
||
898 | */ |
||
899 | abstract protected function createTitleTranslatedEvent(Language $language, StringLiteral $title); |
||
900 | |||
901 | /** |
||
902 | * @param Language $language |
||
903 | * @param StringLiteral $description |
||
904 | * @return AbstractDescriptionTranslated |
||
905 | */ |
||
906 | abstract protected function createDescriptionTranslatedEvent(Language $language, StringLiteral $description); |
||
907 | |||
908 | /** |
||
909 | * @param Image $image |
||
910 | * @return AbstractImageAdded |
||
911 | */ |
||
912 | abstract protected function createImageAddedEvent(Image $image); |
||
913 | |||
914 | /** |
||
915 | * @param Image $image |
||
916 | * @return AbstractImageRemoved |
||
917 | */ |
||
918 | abstract protected function createImageRemovedEvent(Image $image); |
||
919 | |||
920 | /** |
||
921 | * @param AbstractUpdateImage $updateImageCommand |
||
922 | * @return AbstractImageUpdated |
||
923 | */ |
||
924 | abstract protected function createImageUpdatedEvent( |
||
927 | |||
928 | /** |
||
929 | * @param Image $image |
||
930 | * @return AbstractMainImageSelected |
||
931 | */ |
||
932 | abstract protected function createMainImageSelectedEvent(Image $image); |
||
933 | |||
934 | /** |
||
935 | * @return AbstractOfferDeleted |
||
936 | */ |
||
937 | abstract protected function createOfferDeletedEvent(); |
||
938 | |||
939 | /** |
||
940 | * @param Title $title |
||
941 | * @return AbstractTitleUpdated |
||
942 | */ |
||
943 | abstract protected function createTitleUpdatedEvent(Title $title); |
||
944 | |||
945 | /** |
||
946 | * @param string $description |
||
947 | * @return AbstractDescriptionUpdated |
||
948 | */ |
||
949 | abstract protected function createDescriptionUpdatedEvent($description); |
||
950 | |||
951 | /** |
||
952 | * @param Calendar $calendar |
||
953 | * @return AbstractCalendarUpdated |
||
954 | */ |
||
955 | abstract protected function createCalendarUpdatedEvent(Calendar $calendar); |
||
956 | |||
957 | /** |
||
958 | * @param string $typicalAgeRange |
||
959 | * @return AbstractTypicalAgeRangeUpdated |
||
960 | */ |
||
961 | abstract protected function createTypicalAgeRangeUpdatedEvent($typicalAgeRange); |
||
962 | |||
963 | /** |
||
964 | * @return AbstractTypicalAgeRangeDeleted |
||
965 | */ |
||
966 | abstract protected function createTypicalAgeRangeDeletedEvent(); |
||
967 | |||
968 | /** |
||
969 | * @param string $organizerId |
||
970 | * @return AbstractOrganizerUpdated |
||
971 | */ |
||
972 | abstract protected function createOrganizerUpdatedEvent($organizerId); |
||
973 | |||
974 | /** |
||
975 | * @param string $organizerId |
||
976 | * @return AbstractOrganizerDeleted |
||
977 | */ |
||
978 | abstract protected function createOrganizerDeletedEvent($organizerId); |
||
979 | |||
980 | /** |
||
981 | * @param ContactPoint $contactPoint |
||
982 | * @return AbstractContactPointUpdated |
||
983 | */ |
||
984 | abstract protected function createContactPointUpdatedEvent(ContactPoint $contactPoint); |
||
985 | |||
986 | /** |
||
987 | * @param Coordinates $coordinates |
||
988 | * @return AbstractGeoCoordinatesUpdated |
||
989 | */ |
||
990 | abstract protected function createGeoCoordinatesUpdatedEvent(Coordinates $coordinates); |
||
991 | |||
992 | /** |
||
993 | * @param BookingInfo $bookingInfo |
||
994 | * @return AbstractBookingInfoUpdated |
||
995 | */ |
||
996 | abstract protected function createBookingInfoUpdatedEvent(BookingInfo $bookingInfo); |
||
997 | |||
998 | /** |
||
999 | * @param PriceInfo $priceInfo |
||
1000 | * @return AbstractPriceInfoUpdated |
||
1001 | */ |
||
1002 | abstract protected function createPriceInfoUpdatedEvent(PriceInfo $priceInfo); |
||
1003 | |||
1004 | /** |
||
1005 | * @param \DateTimeInterface $publicationDate |
||
1006 | * @return AbstractPublished |
||
1007 | */ |
||
1008 | abstract protected function createPublishedEvent(\DateTimeInterface $publicationDate); |
||
1009 | |||
1010 | /** |
||
1011 | * @return AbstractApproved |
||
1012 | */ |
||
1013 | abstract protected function createApprovedEvent(); |
||
1014 | |||
1015 | /** |
||
1016 | * @param StringLiteral $reason |
||
1017 | * @return AbstractRejected |
||
1018 | */ |
||
1019 | abstract protected function createRejectedEvent(StringLiteral $reason); |
||
1020 | |||
1021 | /** |
||
1022 | * @return AbstractFlaggedAsDuplicate |
||
1023 | */ |
||
1024 | abstract protected function createFlaggedAsDuplicate(); |
||
1025 | |||
1026 | /** |
||
1027 | * @return AbstractFlaggedAsInappropriate |
||
1028 | */ |
||
1029 | abstract protected function createFlaggedAsInappropriate(); |
||
1030 | |||
1031 | /** |
||
1032 | * @param ImageCollection $images |
||
1033 | * @return AbstractImagesImportedFromUDB2 |
||
1034 | */ |
||
1035 | abstract protected function createImagesImportedFromUDB2(ImageCollection $images); |
||
1036 | |||
1037 | /** |
||
1038 | * @param ImageCollection $images |
||
1039 | * @return AbstractImagesUpdatedFromUDB2 |
||
1040 | */ |
||
1041 | abstract protected function createImagesUpdatedFromUDB2(ImageCollection $images); |
||
1042 | |||
1043 | /** |
||
1044 | * @param EventType $type |
||
1045 | * @return AbstractTypeUpdated |
||
1046 | */ |
||
1047 | abstract protected function createTypeUpdatedEvent(EventType $type); |
||
1048 | |||
1049 | /** |
||
1050 | * @param Theme $theme |
||
1051 | * @return AbstractThemeUpdated |
||
1052 | */ |
||
1053 | abstract protected function createThemeUpdatedEvent(Theme $theme); |
||
1054 | |||
1055 | /** |
||
1056 | * @param array $facilities |
||
1057 | * @return AbstractFacilitiesUpdated |
||
1058 | */ |
||
1059 | abstract protected function createFacilitiesUpdatedEvent(array $facilities); |
||
1060 | } |
||
1061 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.