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 |
||
60 | abstract class Offer extends EventSourcedAggregateRoot implements LabelAwareAggregateRoot |
||
61 | { |
||
62 | const DUPLICATE_REASON = 'duplicate'; |
||
63 | const INAPPROPRIATE_REASON = 'inappropriate'; |
||
64 | |||
65 | /** |
||
66 | * @var LabelCollection |
||
67 | */ |
||
68 | protected $labels; |
||
69 | |||
70 | /** |
||
71 | * @var ImageCollection |
||
72 | */ |
||
73 | protected $images; |
||
74 | |||
75 | /** |
||
76 | * @var string |
||
77 | * |
||
78 | * Organizer ids can come from UDB2 which does not strictly use UUIDs. |
||
79 | */ |
||
80 | protected $organizerId; |
||
81 | |||
82 | /** |
||
83 | * @var WorkflowStatus |
||
84 | */ |
||
85 | protected $workflowStatus; |
||
86 | |||
87 | /** |
||
88 | * @var StringLiteral|null |
||
89 | */ |
||
90 | protected $rejectedReason; |
||
91 | |||
92 | /** |
||
93 | * @var PriceInfo |
||
94 | */ |
||
95 | protected $priceInfo; |
||
96 | |||
97 | /** |
||
98 | * @var StringLiteral[] |
||
99 | */ |
||
100 | protected $titles; |
||
101 | |||
102 | /** |
||
103 | * @var Description[] |
||
104 | */ |
||
105 | protected $descriptions; |
||
106 | |||
107 | /** |
||
108 | * @var Language |
||
109 | */ |
||
110 | protected $mainLanguage; |
||
111 | |||
112 | /** |
||
113 | * @var string; |
||
114 | */ |
||
115 | protected $typeId; |
||
116 | |||
117 | /** |
||
118 | * @var string; |
||
119 | */ |
||
120 | protected $themeId; |
||
121 | |||
122 | /** |
||
123 | * @var array |
||
124 | */ |
||
125 | protected $facilities; |
||
126 | |||
127 | /** |
||
128 | * @var ContactPoint |
||
129 | */ |
||
130 | protected $contactPoint; |
||
131 | |||
132 | /** |
||
133 | * @var Calendar |
||
134 | */ |
||
135 | protected $calendar; |
||
136 | |||
137 | /** |
||
138 | * @var AgeRange |
||
139 | */ |
||
140 | protected $typicalAgeRange; |
||
141 | |||
142 | /** |
||
143 | * @var BookingInfo |
||
144 | */ |
||
145 | protected $bookingInfo; |
||
146 | |||
147 | /** |
||
148 | * Offer constructor. |
||
149 | */ |
||
150 | public function __construct() |
||
162 | |||
163 | /** |
||
164 | * @param EventType $type |
||
165 | */ |
||
166 | public function updateType(EventType $type) |
||
172 | |||
173 | /** |
||
174 | * @param Theme $theme |
||
175 | */ |
||
176 | public function updateTheme(Theme $theme) |
||
182 | |||
183 | /** |
||
184 | * @param array $facilities |
||
185 | */ |
||
186 | public function updateFacilities(array $facilities) |
||
192 | |||
193 | /** |
||
194 | * @param AbstractFacilitiesUpdated $facilitiesUpdated |
||
195 | */ |
||
196 | protected function applyFacilitiesUpdated(AbstractFacilitiesUpdated $facilitiesUpdated) |
||
200 | |||
201 | /** |
||
202 | * @param array $facilities1 |
||
203 | * @param array $facilities2 |
||
204 | * @return bool |
||
205 | */ |
||
206 | private function sameFacilities($facilities1, $facilities2) |
||
222 | |||
223 | /** |
||
224 | * Get the id of the main image if one is selected for this offer. |
||
225 | * |
||
226 | * @return UUID|null |
||
227 | */ |
||
228 | protected function getMainImageId() |
||
233 | |||
234 | /** |
||
235 | * @inheritdoc |
||
236 | */ |
||
237 | public function addLabel(Label $label) |
||
245 | |||
246 | /** |
||
247 | * @inheritdoc |
||
248 | */ |
||
249 | public function removeLabel(Label $label) |
||
257 | |||
258 | /** |
||
259 | * @param Language $language |
||
260 | * @param Title $title |
||
261 | */ |
||
262 | public function updateTitle(Language $language, Title $title) |
||
274 | |||
275 | /** |
||
276 | * @param Description $description |
||
277 | * @param Language $language |
||
278 | */ |
||
279 | public function updateDescription(Description $description, Language $language) |
||
291 | |||
292 | /** |
||
293 | * @param Calendar $calendar |
||
294 | */ |
||
295 | public function updateCalendar(Calendar $calendar) |
||
303 | |||
304 | /** |
||
305 | * @param AbstractCalendarUpdated $calendarUpdated |
||
306 | */ |
||
307 | protected function applyCalendarUpdated(AbstractCalendarUpdated $calendarUpdated) |
||
311 | |||
312 | /** |
||
313 | * @param string $typicalAgeRange |
||
314 | */ |
||
315 | public function updateTypicalAgeRange($typicalAgeRange) |
||
323 | |||
324 | /** |
||
325 | * @param AbstractTypicalAgeRangeUpdated $typicalAgeRangeUpdated |
||
326 | */ |
||
327 | protected function applyTypicalAgeRangeUpdated(AbstractTypicalAgeRangeUpdated $typicalAgeRangeUpdated) |
||
331 | |||
332 | public function deleteTypicalAgeRange() |
||
338 | |||
339 | /** |
||
340 | * @param AbstractTypicalAgeRangeDeleted $typicalAgeRangeDeleted |
||
341 | */ |
||
342 | protected function applyTypicalAgeRangeDeleted(AbstractTypicalAgeRangeDeleted $typicalAgeRangeDeleted) |
||
346 | |||
347 | /** |
||
348 | * @param string $organizerId |
||
349 | */ |
||
350 | public function updateOrganizer($organizerId) |
||
358 | |||
359 | /** |
||
360 | * Delete the given organizer. |
||
361 | * |
||
362 | * @param string $organizerId |
||
363 | */ |
||
364 | public function deleteOrganizer($organizerId) |
||
372 | |||
373 | /** |
||
374 | * Updated the contact info. |
||
375 | * @param ContactPoint $contactPoint |
||
376 | */ |
||
377 | public function updateContactPoint(ContactPoint $contactPoint) |
||
385 | |||
386 | /** |
||
387 | * @param AbstractContactPointUpdated $contactPointUpdated |
||
388 | */ |
||
389 | protected function applyContactPointUpdated(AbstractContactPointUpdated $contactPointUpdated) |
||
393 | |||
394 | /** |
||
395 | * @param Coordinates $coordinates |
||
396 | */ |
||
397 | public function updateGeoCoordinates(Coordinates $coordinates) |
||
407 | |||
408 | /** |
||
409 | * Updated the booking info. |
||
410 | * |
||
411 | * @param BookingInfo $bookingInfo |
||
412 | */ |
||
413 | public function updateBookingInfo(BookingInfo $bookingInfo) |
||
421 | |||
422 | /** |
||
423 | * @param AbstractBookingInfoUpdated $bookingInfoUpdated |
||
424 | */ |
||
425 | public function applyBookingInfoUpdated(AbstractBookingInfoUpdated $bookingInfoUpdated) |
||
429 | |||
430 | /** |
||
431 | * @param PriceInfo $priceInfo |
||
432 | */ |
||
433 | public function updatePriceInfo(PriceInfo $priceInfo) |
||
441 | |||
442 | /** |
||
443 | * @param AbstractPriceInfoUpdated $priceInfoUpdated |
||
444 | */ |
||
445 | protected function applyPriceInfoUpdated(AbstractPriceInfoUpdated $priceInfoUpdated) |
||
449 | |||
450 | /** |
||
451 | * @param AbstractLabelAdded $labelAdded |
||
452 | */ |
||
453 | protected function applyLabelAdded(AbstractLabelAdded $labelAdded) |
||
457 | |||
458 | /** |
||
459 | * @param AbstractLabelRemoved $labelRemoved |
||
460 | */ |
||
461 | protected function applyLabelRemoved(AbstractLabelRemoved $labelRemoved) |
||
465 | |||
466 | /** |
||
467 | * @param AbstractThemeUpdated $themeUpdated |
||
468 | */ |
||
469 | protected function applyThemeUpdated(AbstractThemeUpdated $themeUpdated) |
||
473 | |||
474 | /** |
||
475 | * @param AbstractTypeUpdated $themeUpdated |
||
476 | */ |
||
477 | protected function applyTypeUpdated(AbstractTypeUpdated $themeUpdated) |
||
481 | |||
482 | protected function applyDescriptionUpdated(AbstractDescriptionUpdated $descriptionUpdated) |
||
487 | |||
488 | protected function applyDescriptionTranslated(AbstractDescriptionTranslated $descriptionTranslated) |
||
493 | |||
494 | /** |
||
495 | * Add a new image. |
||
496 | * |
||
497 | * @param Image $image |
||
498 | */ |
||
499 | public function addImage(Image $image) |
||
507 | |||
508 | /** |
||
509 | * @param AbstractUpdateImage $updateImageCommand |
||
510 | */ |
||
511 | public function updateImage(AbstractUpdateImage $updateImageCommand) |
||
519 | |||
520 | /** |
||
521 | * Remove an image. |
||
522 | * |
||
523 | * @param Image $image |
||
524 | */ |
||
525 | public function removeImage(Image $image) |
||
533 | |||
534 | /** |
||
535 | * Make an existing image of the item the main image. |
||
536 | * |
||
537 | * @param Image $image |
||
538 | */ |
||
539 | public function selectMainImage(Image $image) |
||
553 | |||
554 | /** |
||
555 | * Delete the offer. |
||
556 | */ |
||
557 | public function delete() |
||
563 | |||
564 | /** |
||
565 | * @param CultureFeed_Cdb_Item_Base $cdbItem |
||
566 | */ |
||
567 | protected function importWorkflowStatus(CultureFeed_Cdb_Item_Base $cdbItem) |
||
576 | |||
577 | /** |
||
578 | * Publish the offer when it has workflowstatus draft. |
||
579 | * @param \DateTimeInterface $publicationDate |
||
580 | */ |
||
581 | public function publish(\DateTimeInterface $publicationDate) |
||
587 | |||
588 | /** |
||
589 | * @return bool |
||
590 | * @throws Exception |
||
591 | */ |
||
592 | View Code Duplication | private function guardPublish() |
|
604 | |||
605 | /** |
||
606 | * Approve the offer when it's waiting for validation. |
||
607 | */ |
||
608 | public function approve() |
||
612 | |||
613 | /** |
||
614 | * @return bool |
||
615 | * @throws Exception |
||
616 | */ |
||
617 | View Code Duplication | private function guardApprove() |
|
629 | |||
630 | /** |
||
631 | * Reject an offer that is waiting for validation with a given reason. |
||
632 | * @param StringLiteral $reason |
||
633 | */ |
||
634 | public function reject(StringLiteral $reason) |
||
638 | |||
639 | public function flagAsDuplicate() |
||
644 | |||
645 | public function flagAsInappropriate() |
||
650 | |||
651 | /** |
||
652 | * @param StringLiteral $reason |
||
653 | * @return bool |
||
654 | * false when the offer can still be rejected, true when the offer is already rejected for the same reason |
||
655 | * @throws Exception |
||
656 | */ |
||
657 | private function guardRejection(StringLiteral $reason) |
||
673 | |||
674 | /** |
||
675 | * @param Title $title |
||
676 | * @param Language $language |
||
677 | * @return bool |
||
678 | */ |
||
679 | View Code Duplication | private function isTitleChanged(Title $title, Language $language) |
|
686 | |||
687 | /** |
||
688 | * @param Description $description |
||
689 | * @param Language $language |
||
690 | * @return bool |
||
691 | */ |
||
692 | View Code Duplication | private function isDescriptionChanged(Description $description, Language $language) |
|
699 | |||
700 | /** |
||
701 | * Overwrites or resets the main image and all media objects |
||
702 | * by importing a new collection of images from UDB2. |
||
703 | * |
||
704 | * @param ImageCollection $images |
||
705 | */ |
||
706 | public function importImagesFromUDB2(ImageCollection $images) |
||
710 | |||
711 | /** |
||
712 | * Overwrites or resets the main image and all media objects |
||
713 | * by updating with a new collection of images from UDB2. |
||
714 | * |
||
715 | * @param ImageCollection $images |
||
716 | */ |
||
717 | public function updateImagesFromUDB2(ImageCollection $images) |
||
721 | |||
722 | /** |
||
723 | * @param AbstractPublished $published |
||
724 | */ |
||
725 | protected function applyPublished(AbstractPublished $published) |
||
729 | |||
730 | /** |
||
731 | * @param AbstractApproved $approved |
||
732 | */ |
||
733 | protected function applyApproved(AbstractApproved $approved) |
||
737 | |||
738 | /** |
||
739 | * @param AbstractRejected $rejected |
||
740 | */ |
||
741 | protected function applyRejected(AbstractRejected $rejected) |
||
746 | |||
747 | /** |
||
748 | * @param AbstractFlaggedAsDuplicate $flaggedAsDuplicate |
||
749 | */ |
||
750 | protected function applyFlaggedAsDuplicate(AbstractFlaggedAsDuplicate $flaggedAsDuplicate) |
||
755 | |||
756 | /** |
||
757 | * @param AbstractFlaggedAsInappropriate $flaggedAsInappropriate |
||
758 | */ |
||
759 | protected function applyFlaggedAsInappropriate(AbstractFlaggedAsInappropriate $flaggedAsInappropriate) |
||
764 | |||
765 | protected function applyImageAdded(AbstractImageAdded $imageAdded) |
||
769 | |||
770 | protected function applyImageRemoved(AbstractImageRemoved $imageRemoved) |
||
774 | |||
775 | protected function applyMainImageSelected(AbstractMainImageSelected $mainImageSelected) |
||
779 | |||
780 | protected function applyOrganizerUpdated(AbstractOrganizerUpdated $organizerUpdated) |
||
784 | |||
785 | protected function applyOrganizerDeleted(AbstractOrganizerDeleted $organizerDeleted) |
||
789 | |||
790 | /** |
||
791 | * @param AbstractImagesImportedFromUDB2 $imagesImportedFromUDB2 |
||
792 | */ |
||
793 | protected function applyImagesImportedFromUDB2(AbstractImagesImportedFromUDB2 $imagesImportedFromUDB2) |
||
797 | |||
798 | /** |
||
799 | * @param AbstractImagesUpdatedFromUDB2 $imagesUpdatedFromUDB2 |
||
800 | */ |
||
801 | protected function applyImagesUpdatedFromUDB2(AbstractImagesUpdatedFromUDB2 $imagesUpdatedFromUDB2) |
||
805 | |||
806 | /** |
||
807 | * This indirect apply method can be called internally to deal with images coming from UDB2. |
||
808 | * Imports from UDB2 only contain the native Dutch content. |
||
809 | * @see https://github.com/cultuurnet/udb3-udb2-bridge/blob/db0a7ab2444f55bb3faae3d59b82b39aaeba253b/test/Media/ImageCollectionFactoryTest.php#L79-L103 |
||
810 | * Because of this we have to make sure translated images are left in place. |
||
811 | * |
||
812 | * @param AbstractImagesEvent $imagesEvent |
||
813 | */ |
||
814 | protected function applyUdb2ImagesEvent(AbstractImagesEvent $imagesEvent) |
||
830 | |||
831 | /** |
||
832 | * @param Label $label |
||
833 | * @return AbstractLabelAdded |
||
834 | */ |
||
835 | abstract protected function createLabelAddedEvent(Label $label); |
||
836 | |||
837 | /** |
||
838 | * @param Label $label |
||
839 | * @return AbstractLabelRemoved |
||
840 | */ |
||
841 | abstract protected function createLabelRemovedEvent(Label $label); |
||
842 | |||
843 | /** |
||
844 | * @param Language $language |
||
845 | * @param StringLiteral $title |
||
846 | * @return AbstractTitleTranslated |
||
847 | */ |
||
848 | abstract protected function createTitleTranslatedEvent(Language $language, StringLiteral $title); |
||
849 | |||
850 | /** |
||
851 | * @param Language $language |
||
852 | * @param StringLiteral $description |
||
853 | * @return AbstractDescriptionTranslated |
||
854 | */ |
||
855 | abstract protected function createDescriptionTranslatedEvent(Language $language, StringLiteral $description); |
||
856 | |||
857 | /** |
||
858 | * @param Image $image |
||
859 | * @return AbstractImageAdded |
||
860 | */ |
||
861 | abstract protected function createImageAddedEvent(Image $image); |
||
862 | |||
863 | /** |
||
864 | * @param Image $image |
||
865 | * @return AbstractImageRemoved |
||
866 | */ |
||
867 | abstract protected function createImageRemovedEvent(Image $image); |
||
868 | |||
869 | /** |
||
870 | * @param AbstractUpdateImage $updateImageCommand |
||
871 | * @return AbstractImageUpdated |
||
872 | */ |
||
873 | abstract protected function createImageUpdatedEvent( |
||
876 | |||
877 | /** |
||
878 | * @param Image $image |
||
879 | * @return AbstractMainImageSelected |
||
880 | */ |
||
881 | abstract protected function createMainImageSelectedEvent(Image $image); |
||
882 | |||
883 | /** |
||
884 | * @return AbstractOfferDeleted |
||
885 | */ |
||
886 | abstract protected function createOfferDeletedEvent(); |
||
887 | |||
888 | /** |
||
889 | * @param Title $title |
||
890 | * @return AbstractTitleUpdated |
||
891 | */ |
||
892 | abstract protected function createTitleUpdatedEvent(Title $title); |
||
893 | |||
894 | /** |
||
895 | * @param string $description |
||
896 | * @return AbstractDescriptionUpdated |
||
897 | */ |
||
898 | abstract protected function createDescriptionUpdatedEvent($description); |
||
899 | |||
900 | /** |
||
901 | * @param Calendar $calendar |
||
902 | * @return AbstractCalendarUpdated |
||
903 | */ |
||
904 | abstract protected function createCalendarUpdatedEvent(Calendar $calendar); |
||
905 | |||
906 | /** |
||
907 | * @param string $typicalAgeRange |
||
908 | * @return AbstractTypicalAgeRangeUpdated |
||
909 | */ |
||
910 | abstract protected function createTypicalAgeRangeUpdatedEvent($typicalAgeRange); |
||
911 | |||
912 | /** |
||
913 | * @return AbstractTypicalAgeRangeDeleted |
||
914 | */ |
||
915 | abstract protected function createTypicalAgeRangeDeletedEvent(); |
||
916 | |||
917 | /** |
||
918 | * @param string $organizerId |
||
919 | * @return AbstractOrganizerUpdated |
||
920 | */ |
||
921 | abstract protected function createOrganizerUpdatedEvent($organizerId); |
||
922 | |||
923 | /** |
||
924 | * @param string $organizerId |
||
925 | * @return AbstractOrganizerDeleted |
||
926 | */ |
||
927 | abstract protected function createOrganizerDeletedEvent($organizerId); |
||
928 | |||
929 | /** |
||
930 | * @param ContactPoint $contactPoint |
||
931 | * @return AbstractContactPointUpdated |
||
932 | */ |
||
933 | abstract protected function createContactPointUpdatedEvent(ContactPoint $contactPoint); |
||
934 | |||
935 | /** |
||
936 | * @param Coordinates $coordinates |
||
937 | * @return AbstractGeoCoordinatesUpdated |
||
938 | */ |
||
939 | abstract protected function createGeoCoordinatesUpdatedEvent(Coordinates $coordinates); |
||
940 | |||
941 | /** |
||
942 | * @param BookingInfo $bookingInfo |
||
943 | * @return AbstractBookingInfoUpdated |
||
944 | */ |
||
945 | abstract protected function createBookingInfoUpdatedEvent(BookingInfo $bookingInfo); |
||
946 | |||
947 | /** |
||
948 | * @param PriceInfo $priceInfo |
||
949 | * @return AbstractPriceInfoUpdated |
||
950 | */ |
||
951 | abstract protected function createPriceInfoUpdatedEvent(PriceInfo $priceInfo); |
||
952 | |||
953 | /** |
||
954 | * @param \DateTimeInterface $publicationDate |
||
955 | * @return AbstractPublished |
||
956 | */ |
||
957 | abstract protected function createPublishedEvent(\DateTimeInterface $publicationDate); |
||
958 | |||
959 | /** |
||
960 | * @return AbstractApproved |
||
961 | */ |
||
962 | abstract protected function createApprovedEvent(); |
||
963 | |||
964 | /** |
||
965 | * @param StringLiteral $reason |
||
966 | * @return AbstractRejected |
||
967 | */ |
||
968 | abstract protected function createRejectedEvent(StringLiteral $reason); |
||
969 | |||
970 | /** |
||
971 | * @return AbstractFlaggedAsDuplicate |
||
972 | */ |
||
973 | abstract protected function createFlaggedAsDuplicate(); |
||
974 | |||
975 | /** |
||
976 | * @return AbstractFlaggedAsInappropriate |
||
977 | */ |
||
978 | abstract protected function createFlaggedAsInappropriate(); |
||
979 | |||
980 | /** |
||
981 | * @param ImageCollection $images |
||
982 | * @return AbstractImagesImportedFromUDB2 |
||
983 | */ |
||
984 | abstract protected function createImagesImportedFromUDB2(ImageCollection $images); |
||
985 | |||
986 | /** |
||
987 | * @param ImageCollection $images |
||
988 | * @return AbstractImagesUpdatedFromUDB2 |
||
989 | */ |
||
990 | abstract protected function createImagesUpdatedFromUDB2(ImageCollection $images); |
||
991 | |||
992 | /** |
||
993 | * @param EventType $type |
||
994 | * @return AbstractTypeUpdated |
||
995 | */ |
||
996 | abstract protected function createTypeUpdatedEvent(EventType $type); |
||
997 | |||
998 | /** |
||
999 | * @param Theme $theme |
||
1000 | * @return AbstractThemeUpdated |
||
1001 | */ |
||
1002 | abstract protected function createThemeUpdatedEvent(Theme $theme); |
||
1003 | |||
1004 | /** |
||
1005 | * @param array $facilities |
||
1006 | * @return AbstractFacilitiesUpdated |
||
1007 | */ |
||
1008 | abstract protected function createFacilitiesUpdatedEvent(array $facilities); |
||
1009 | } |
||
1010 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.