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 |
||
59 | abstract class Offer extends EventSourcedAggregateRoot implements LabelAwareAggregateRoot |
||
60 | { |
||
61 | const DUPLICATE_REASON = 'duplicate'; |
||
62 | const INAPPROPRIATE_REASON = 'inappropriate'; |
||
63 | |||
64 | /** |
||
65 | * @var LabelCollection |
||
66 | */ |
||
67 | protected $labels; |
||
68 | |||
69 | /** |
||
70 | * @var ImageCollection |
||
71 | */ |
||
72 | protected $images; |
||
73 | |||
74 | /** |
||
75 | * @var string |
||
76 | * |
||
77 | * Organizer ids can come from UDB2 which does not strictly use UUIDs. |
||
78 | */ |
||
79 | protected $organizerId; |
||
80 | |||
81 | /** |
||
82 | * @var WorkflowStatus |
||
83 | */ |
||
84 | protected $workflowStatus; |
||
85 | |||
86 | /** |
||
87 | * @var StringLiteral|null |
||
88 | */ |
||
89 | protected $rejectedReason; |
||
90 | |||
91 | /** |
||
92 | * @var PriceInfo |
||
93 | */ |
||
94 | protected $priceInfo; |
||
95 | |||
96 | /** |
||
97 | * @var StringLiteral[] |
||
98 | */ |
||
99 | protected $titles; |
||
100 | |||
101 | /** |
||
102 | * @var Description[] |
||
103 | */ |
||
104 | protected $descriptions; |
||
105 | |||
106 | /** |
||
107 | * @var Language |
||
108 | */ |
||
109 | protected $mainLanguage; |
||
110 | |||
111 | /** |
||
112 | * @var string; |
||
113 | */ |
||
114 | protected $typeId; |
||
115 | |||
116 | /** |
||
117 | * @var string; |
||
118 | */ |
||
119 | protected $themeId; |
||
120 | |||
121 | /** |
||
122 | * @var array |
||
123 | */ |
||
124 | protected $facilities; |
||
125 | |||
126 | /** |
||
127 | * @var ContactPoint |
||
128 | */ |
||
129 | protected $contactPoint; |
||
130 | |||
131 | /** |
||
132 | * @var Calendar |
||
133 | */ |
||
134 | protected $calendar; |
||
135 | |||
136 | /** |
||
137 | * @var AgeRange |
||
138 | */ |
||
139 | protected $typicalAgeRange; |
||
140 | |||
141 | /** |
||
142 | * Offer constructor. |
||
143 | */ |
||
144 | public function __construct() |
||
155 | |||
156 | /** |
||
157 | * @param EventType $type |
||
158 | */ |
||
159 | public function updateType(EventType $type) |
||
165 | |||
166 | /** |
||
167 | * @param Theme $theme |
||
168 | */ |
||
169 | public function updateTheme(Theme $theme) |
||
175 | |||
176 | /** |
||
177 | * @param array $facilities |
||
178 | */ |
||
179 | public function updateFacilities(array $facilities) |
||
185 | |||
186 | /** |
||
187 | * @param AbstractFacilitiesUpdated $facilitiesUpdated |
||
188 | */ |
||
189 | protected function applyFacilitiesUpdated(AbstractFacilitiesUpdated $facilitiesUpdated) |
||
193 | |||
194 | /** |
||
195 | * @param array $facilities1 |
||
196 | * @param array $facilities2 |
||
197 | * @return bool |
||
198 | */ |
||
199 | private function sameFacilities($facilities1, $facilities2) |
||
215 | |||
216 | /** |
||
217 | * Get the id of the main image if one is selected for this offer. |
||
218 | * |
||
219 | * @return UUID|null |
||
220 | */ |
||
221 | protected function getMainImageId() |
||
226 | |||
227 | /** |
||
228 | * @inheritdoc |
||
229 | */ |
||
230 | public function addLabel(Label $label) |
||
238 | |||
239 | /** |
||
240 | * @inheritdoc |
||
241 | */ |
||
242 | public function removeLabel(Label $label) |
||
250 | |||
251 | /** |
||
252 | * @param Language $language |
||
253 | * @param Title $title |
||
254 | */ |
||
255 | public function updateTitle(Language $language, Title $title) |
||
267 | |||
268 | /** |
||
269 | * @param Description $description |
||
270 | * @param Language $language |
||
271 | */ |
||
272 | public function updateDescription(Description $description, Language $language) |
||
284 | |||
285 | /** |
||
286 | * @param Calendar $calendar |
||
287 | */ |
||
288 | public function updateCalendar(Calendar $calendar) |
||
296 | |||
297 | /** |
||
298 | * @param AbstractCalendarUpdated $calendarUpdated |
||
299 | */ |
||
300 | protected function applyCalendarUpdated(AbstractCalendarUpdated $calendarUpdated) |
||
304 | |||
305 | /** |
||
306 | * @param string $typicalAgeRange |
||
307 | */ |
||
308 | public function updateTypicalAgeRange($typicalAgeRange) |
||
316 | |||
317 | /** |
||
318 | * @param AbstractTypicalAgeRangeUpdated $typicalAgeRangeUpdated |
||
319 | */ |
||
320 | protected function applyTypicalAgeRangeUpdated(AbstractTypicalAgeRangeUpdated $typicalAgeRangeUpdated) |
||
324 | |||
325 | public function deleteTypicalAgeRange() |
||
331 | |||
332 | /** |
||
333 | * @param AbstractTypicalAgeRangeDeleted $typicalAgeRangeDeleted |
||
334 | */ |
||
335 | protected function applyTypicalAgeRangeDeleted(AbstractTypicalAgeRangeDeleted $typicalAgeRangeDeleted) |
||
339 | |||
340 | /** |
||
341 | * @param string $organizerId |
||
342 | */ |
||
343 | public function updateOrganizer($organizerId) |
||
351 | |||
352 | /** |
||
353 | * Delete the given organizer. |
||
354 | * |
||
355 | * @param string $organizerId |
||
356 | */ |
||
357 | public function deleteOrganizer($organizerId) |
||
365 | |||
366 | /** |
||
367 | * Updated the contact info. |
||
368 | * @param ContactPoint $contactPoint |
||
369 | */ |
||
370 | public function updateContactPoint(ContactPoint $contactPoint) |
||
378 | |||
379 | /** |
||
380 | * @param AbstractContactPointUpdated $contactPointUpdated |
||
381 | */ |
||
382 | protected function applyContactPointUpdated(AbstractContactPointUpdated $contactPointUpdated) |
||
386 | |||
387 | /** |
||
388 | * @param Coordinates $coordinates |
||
389 | */ |
||
390 | public function updateGeoCoordinates(Coordinates $coordinates) |
||
400 | |||
401 | /** |
||
402 | * Updated the booking info. |
||
403 | * |
||
404 | * @param BookingInfo $bookingInfo |
||
405 | */ |
||
406 | public function updateBookingInfo(BookingInfo $bookingInfo) |
||
412 | |||
413 | /** |
||
414 | * @param PriceInfo $priceInfo |
||
415 | */ |
||
416 | public function updatePriceInfo(PriceInfo $priceInfo) |
||
424 | |||
425 | /** |
||
426 | * @param AbstractPriceInfoUpdated $priceInfoUpdated |
||
427 | */ |
||
428 | protected function applyPriceInfoUpdated(AbstractPriceInfoUpdated $priceInfoUpdated) |
||
432 | |||
433 | /** |
||
434 | * @param AbstractLabelAdded $labelAdded |
||
435 | */ |
||
436 | protected function applyLabelAdded(AbstractLabelAdded $labelAdded) |
||
440 | |||
441 | /** |
||
442 | * @param AbstractLabelRemoved $labelRemoved |
||
443 | */ |
||
444 | protected function applyLabelRemoved(AbstractLabelRemoved $labelRemoved) |
||
448 | |||
449 | /** |
||
450 | * @param AbstractThemeUpdated $themeUpdated |
||
451 | */ |
||
452 | protected function applyThemeUpdated(AbstractThemeUpdated $themeUpdated) |
||
456 | |||
457 | /** |
||
458 | * @param AbstractTypeUpdated $themeUpdated |
||
459 | */ |
||
460 | protected function applyTypeUpdated(AbstractTypeUpdated $themeUpdated) |
||
464 | |||
465 | protected function applyDescriptionUpdated(AbstractDescriptionUpdated $descriptionUpdated) |
||
470 | |||
471 | protected function applyDescriptionTranslated(AbstractDescriptionTranslated $descriptionTranslated) |
||
476 | |||
477 | /** |
||
478 | * Add a new image. |
||
479 | * |
||
480 | * @param Image $image |
||
481 | */ |
||
482 | public function addImage(Image $image) |
||
490 | |||
491 | /** |
||
492 | * @param AbstractUpdateImage $updateImageCommand |
||
493 | */ |
||
494 | public function updateImage(AbstractUpdateImage $updateImageCommand) |
||
502 | |||
503 | /** |
||
504 | * Remove an image. |
||
505 | * |
||
506 | * @param Image $image |
||
507 | */ |
||
508 | public function removeImage(Image $image) |
||
516 | |||
517 | /** |
||
518 | * Make an existing image of the item the main image. |
||
519 | * |
||
520 | * @param Image $image |
||
521 | */ |
||
522 | public function selectMainImage(Image $image) |
||
536 | |||
537 | /** |
||
538 | * Delete the offer. |
||
539 | */ |
||
540 | public function delete() |
||
546 | |||
547 | /** |
||
548 | * @param CultureFeed_Cdb_Item_Base $cdbItem |
||
549 | */ |
||
550 | protected function importWorkflowStatus(CultureFeed_Cdb_Item_Base $cdbItem) |
||
559 | |||
560 | /** |
||
561 | * Publish the offer when it has workflowstatus draft. |
||
562 | * @param \DateTimeInterface $publicationDate |
||
563 | */ |
||
564 | public function publish(\DateTimeInterface $publicationDate) |
||
570 | |||
571 | /** |
||
572 | * @return bool |
||
573 | * @throws Exception |
||
574 | */ |
||
575 | View Code Duplication | private function guardPublish() |
|
587 | |||
588 | /** |
||
589 | * Approve the offer when it's waiting for validation. |
||
590 | */ |
||
591 | public function approve() |
||
595 | |||
596 | /** |
||
597 | * @return bool |
||
598 | * @throws Exception |
||
599 | */ |
||
600 | View Code Duplication | private function guardApprove() |
|
612 | |||
613 | /** |
||
614 | * Reject an offer that is waiting for validation with a given reason. |
||
615 | * @param StringLiteral $reason |
||
616 | */ |
||
617 | public function reject(StringLiteral $reason) |
||
621 | |||
622 | public function flagAsDuplicate() |
||
627 | |||
628 | public function flagAsInappropriate() |
||
633 | |||
634 | /** |
||
635 | * @param StringLiteral $reason |
||
636 | * @return bool |
||
637 | * false when the offer can still be rejected, true when the offer is already rejected for the same reason |
||
638 | * @throws Exception |
||
639 | */ |
||
640 | private function guardRejection(StringLiteral $reason) |
||
656 | |||
657 | /** |
||
658 | * @param Title $title |
||
659 | * @param Language $language |
||
660 | * @return bool |
||
661 | */ |
||
662 | View Code Duplication | private function isTitleChanged(Title $title, Language $language) |
|
669 | |||
670 | /** |
||
671 | * @param Description $description |
||
672 | * @param Language $language |
||
673 | * @return bool |
||
674 | */ |
||
675 | View Code Duplication | private function isDescriptionChanged(Description $description, Language $language) |
|
682 | |||
683 | /** |
||
684 | * Overwrites or resets the main image and all media objects |
||
685 | * by importing a new collection of images from UDB2. |
||
686 | * |
||
687 | * @param ImageCollection $images |
||
688 | */ |
||
689 | public function importImagesFromUDB2(ImageCollection $images) |
||
693 | |||
694 | /** |
||
695 | * Overwrites or resets the main image and all media objects |
||
696 | * by updating with a new collection of images from UDB2. |
||
697 | * |
||
698 | * @param ImageCollection $images |
||
699 | */ |
||
700 | public function updateImagesFromUDB2(ImageCollection $images) |
||
704 | |||
705 | /** |
||
706 | * @param AbstractPublished $published |
||
707 | */ |
||
708 | protected function applyPublished(AbstractPublished $published) |
||
712 | |||
713 | /** |
||
714 | * @param AbstractApproved $approved |
||
715 | */ |
||
716 | protected function applyApproved(AbstractApproved $approved) |
||
720 | |||
721 | /** |
||
722 | * @param AbstractRejected $rejected |
||
723 | */ |
||
724 | protected function applyRejected(AbstractRejected $rejected) |
||
729 | |||
730 | /** |
||
731 | * @param AbstractFlaggedAsDuplicate $flaggedAsDuplicate |
||
732 | */ |
||
733 | protected function applyFlaggedAsDuplicate(AbstractFlaggedAsDuplicate $flaggedAsDuplicate) |
||
738 | |||
739 | /** |
||
740 | * @param AbstractFlaggedAsInappropriate $flaggedAsInappropriate |
||
741 | */ |
||
742 | protected function applyFlaggedAsInappropriate(AbstractFlaggedAsInappropriate $flaggedAsInappropriate) |
||
747 | |||
748 | protected function applyImageAdded(AbstractImageAdded $imageAdded) |
||
752 | |||
753 | protected function applyImageRemoved(AbstractImageRemoved $imageRemoved) |
||
757 | |||
758 | protected function applyMainImageSelected(AbstractMainImageSelected $mainImageSelected) |
||
762 | |||
763 | protected function applyOrganizerUpdated(AbstractOrganizerUpdated $organizerUpdated) |
||
767 | |||
768 | protected function applyOrganizerDeleted(AbstractOrganizerDeleted $organizerDeleted) |
||
772 | |||
773 | /** |
||
774 | * @param AbstractImagesImportedFromUDB2 $imagesImportedFromUDB2 |
||
775 | */ |
||
776 | protected function applyImagesImportedFromUDB2(AbstractImagesImportedFromUDB2 $imagesImportedFromUDB2) |
||
780 | |||
781 | /** |
||
782 | * @param AbstractImagesUpdatedFromUDB2 $imagesUpdatedFromUDB2 |
||
783 | */ |
||
784 | protected function applyImagesUpdatedFromUDB2(AbstractImagesUpdatedFromUDB2 $imagesUpdatedFromUDB2) |
||
788 | |||
789 | /** |
||
790 | * This indirect apply method can be called internally to deal with images coming from UDB2. |
||
791 | * Imports from UDB2 only contain the native Dutch content. |
||
792 | * @see https://github.com/cultuurnet/udb3-udb2-bridge/blob/db0a7ab2444f55bb3faae3d59b82b39aaeba253b/test/Media/ImageCollectionFactoryTest.php#L79-L103 |
||
793 | * Because of this we have to make sure translated images are left in place. |
||
794 | * |
||
795 | * @param AbstractImagesEvent $imagesEvent |
||
796 | */ |
||
797 | protected function applyUdb2ImagesEvent(AbstractImagesEvent $imagesEvent) |
||
813 | |||
814 | /** |
||
815 | * @param Label $label |
||
816 | * @return AbstractLabelAdded |
||
817 | */ |
||
818 | abstract protected function createLabelAddedEvent(Label $label); |
||
819 | |||
820 | /** |
||
821 | * @param Label $label |
||
822 | * @return AbstractLabelRemoved |
||
823 | */ |
||
824 | abstract protected function createLabelRemovedEvent(Label $label); |
||
825 | |||
826 | /** |
||
827 | * @param Language $language |
||
828 | * @param StringLiteral $title |
||
829 | * @return AbstractTitleTranslated |
||
830 | */ |
||
831 | abstract protected function createTitleTranslatedEvent(Language $language, StringLiteral $title); |
||
832 | |||
833 | /** |
||
834 | * @param Language $language |
||
835 | * @param StringLiteral $description |
||
836 | * @return AbstractDescriptionTranslated |
||
837 | */ |
||
838 | abstract protected function createDescriptionTranslatedEvent(Language $language, StringLiteral $description); |
||
839 | |||
840 | /** |
||
841 | * @param Image $image |
||
842 | * @return AbstractImageAdded |
||
843 | */ |
||
844 | abstract protected function createImageAddedEvent(Image $image); |
||
845 | |||
846 | /** |
||
847 | * @param Image $image |
||
848 | * @return AbstractImageRemoved |
||
849 | */ |
||
850 | abstract protected function createImageRemovedEvent(Image $image); |
||
851 | |||
852 | /** |
||
853 | * @param AbstractUpdateImage $updateImageCommand |
||
854 | * @return AbstractImageUpdated |
||
855 | */ |
||
856 | abstract protected function createImageUpdatedEvent( |
||
859 | |||
860 | /** |
||
861 | * @param Image $image |
||
862 | * @return AbstractMainImageSelected |
||
863 | */ |
||
864 | abstract protected function createMainImageSelectedEvent(Image $image); |
||
865 | |||
866 | /** |
||
867 | * @return AbstractOfferDeleted |
||
868 | */ |
||
869 | abstract protected function createOfferDeletedEvent(); |
||
870 | |||
871 | /** |
||
872 | * @param Title $title |
||
873 | * @return AbstractTitleUpdated |
||
874 | */ |
||
875 | abstract protected function createTitleUpdatedEvent(Title $title); |
||
876 | |||
877 | /** |
||
878 | * @param string $description |
||
879 | * @return AbstractDescriptionUpdated |
||
880 | */ |
||
881 | abstract protected function createDescriptionUpdatedEvent($description); |
||
882 | |||
883 | /** |
||
884 | * @param Calendar $calendar |
||
885 | * @return AbstractCalendarUpdated |
||
886 | */ |
||
887 | abstract protected function createCalendarUpdatedEvent(Calendar $calendar); |
||
888 | |||
889 | /** |
||
890 | * @param string $typicalAgeRange |
||
891 | * @return AbstractTypicalAgeRangeUpdated |
||
892 | */ |
||
893 | abstract protected function createTypicalAgeRangeUpdatedEvent($typicalAgeRange); |
||
894 | |||
895 | /** |
||
896 | * @return AbstractTypicalAgeRangeDeleted |
||
897 | */ |
||
898 | abstract protected function createTypicalAgeRangeDeletedEvent(); |
||
899 | |||
900 | /** |
||
901 | * @param string $organizerId |
||
902 | * @return AbstractOrganizerUpdated |
||
903 | */ |
||
904 | abstract protected function createOrganizerUpdatedEvent($organizerId); |
||
905 | |||
906 | /** |
||
907 | * @param string $organizerId |
||
908 | * @return AbstractOrganizerDeleted |
||
909 | */ |
||
910 | abstract protected function createOrganizerDeletedEvent($organizerId); |
||
911 | |||
912 | /** |
||
913 | * @param ContactPoint $contactPoint |
||
914 | * @return AbstractContactPointUpdated |
||
915 | */ |
||
916 | abstract protected function createContactPointUpdatedEvent(ContactPoint $contactPoint); |
||
917 | |||
918 | /** |
||
919 | * @param Coordinates $coordinates |
||
920 | * @return AbstractGeoCoordinatesUpdated |
||
921 | */ |
||
922 | abstract protected function createGeoCoordinatesUpdatedEvent(Coordinates $coordinates); |
||
923 | |||
924 | /** |
||
925 | * @param BookingInfo $bookingInfo |
||
926 | * @return AbstractBookingInfoUpdated |
||
927 | */ |
||
928 | abstract protected function createBookingInfoUpdatedEvent(BookingInfo $bookingInfo); |
||
929 | |||
930 | /** |
||
931 | * @param PriceInfo $priceInfo |
||
932 | * @return AbstractPriceInfoUpdated |
||
933 | */ |
||
934 | abstract protected function createPriceInfoUpdatedEvent(PriceInfo $priceInfo); |
||
935 | |||
936 | /** |
||
937 | * @param \DateTimeInterface $publicationDate |
||
938 | * @return AbstractPublished |
||
939 | */ |
||
940 | abstract protected function createPublishedEvent(\DateTimeInterface $publicationDate); |
||
941 | |||
942 | /** |
||
943 | * @return AbstractApproved |
||
944 | */ |
||
945 | abstract protected function createApprovedEvent(); |
||
946 | |||
947 | /** |
||
948 | * @param StringLiteral $reason |
||
949 | * @return AbstractRejected |
||
950 | */ |
||
951 | abstract protected function createRejectedEvent(StringLiteral $reason); |
||
952 | |||
953 | /** |
||
954 | * @return AbstractFlaggedAsDuplicate |
||
955 | */ |
||
956 | abstract protected function createFlaggedAsDuplicate(); |
||
957 | |||
958 | /** |
||
959 | * @return AbstractFlaggedAsInappropriate |
||
960 | */ |
||
961 | abstract protected function createFlaggedAsInappropriate(); |
||
962 | |||
963 | /** |
||
964 | * @param ImageCollection $images |
||
965 | * @return AbstractImagesImportedFromUDB2 |
||
966 | */ |
||
967 | abstract protected function createImagesImportedFromUDB2(ImageCollection $images); |
||
968 | |||
969 | /** |
||
970 | * @param ImageCollection $images |
||
971 | * @return AbstractImagesUpdatedFromUDB2 |
||
972 | */ |
||
973 | abstract protected function createImagesUpdatedFromUDB2(ImageCollection $images); |
||
974 | |||
975 | /** |
||
976 | * @param EventType $type |
||
977 | * @return AbstractTypeUpdated |
||
978 | */ |
||
979 | abstract protected function createTypeUpdatedEvent(EventType $type); |
||
980 | |||
981 | /** |
||
982 | * @param Theme $theme |
||
983 | * @return AbstractThemeUpdated |
||
984 | */ |
||
985 | abstract protected function createThemeUpdatedEvent(Theme $theme); |
||
986 | |||
987 | /** |
||
988 | * @param array $facilities |
||
989 | * @return AbstractFacilitiesUpdated |
||
990 | */ |
||
991 | abstract protected function createFacilitiesUpdatedEvent(array $facilities); |
||
992 | } |
||
993 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.