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 Event 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 Event, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | class Event extends Component |
||
31 | { |
||
32 | const TIME_TRANSPARENCY_OPAQUE = 'OPAQUE'; |
||
33 | const TIME_TRANSPARENCY_TRANSPARENT = 'TRANSPARENT'; |
||
34 | |||
35 | const STATUS_TENTATIVE = 'TENTATIVE'; |
||
36 | const STATUS_CONFIRMED = 'CONFIRMED'; |
||
37 | const STATUS_CANCELLED = 'CANCELLED'; |
||
38 | |||
39 | const MS_BUSYSTATUS_FREE = 'FREE'; |
||
40 | const MS_BUSYSTATUS_TENTATIVE = 'TENTATIVE'; |
||
41 | const MS_BUSYSTATUS_BUSY = 'BUSY'; |
||
42 | const MS_BUSYSTATUS_OOF = 'OOF'; |
||
43 | |||
44 | /** |
||
45 | * @var string |
||
46 | */ |
||
47 | protected $uniqueId; |
||
48 | |||
49 | /** |
||
50 | * The property indicates the date/time that the instance of |
||
51 | * the iCalendar object was created. |
||
52 | * |
||
53 | * The value MUST be specified in the UTC time format. |
||
54 | * |
||
55 | * @var \DateTime |
||
56 | */ |
||
57 | protected $dtStamp; |
||
58 | |||
59 | /** |
||
60 | * @var \DateTime |
||
61 | */ |
||
62 | protected $dtStart; |
||
63 | |||
64 | /** |
||
65 | * Preferentially chosen over the duration if both are set. |
||
66 | * |
||
67 | * @var \DateTime |
||
68 | */ |
||
69 | protected $dtEnd; |
||
70 | |||
71 | /** |
||
72 | * @var \DateInterval |
||
73 | */ |
||
74 | protected $duration; |
||
75 | |||
76 | /** |
||
77 | * @var bool |
||
78 | */ |
||
79 | protected $noTime = false; |
||
80 | |||
81 | /** |
||
82 | * @var string |
||
83 | */ |
||
84 | protected $msBusyStatus = null; |
||
85 | |||
86 | /** |
||
87 | * @var string |
||
88 | */ |
||
89 | protected $url; |
||
90 | |||
91 | /** |
||
92 | * @var string |
||
93 | */ |
||
94 | protected $location; |
||
95 | |||
96 | /** |
||
97 | * @var string |
||
98 | */ |
||
99 | protected $locationTitle; |
||
100 | |||
101 | /** |
||
102 | * @var Geo |
||
103 | */ |
||
104 | protected $locationGeo; |
||
105 | |||
106 | /** |
||
107 | * @var string |
||
108 | */ |
||
109 | protected $summary; |
||
110 | |||
111 | /** |
||
112 | * @var Organizer |
||
113 | */ |
||
114 | protected $organizer; |
||
115 | |||
116 | /** |
||
117 | * @see https://tools.ietf.org/html/rfc5545#section-3.8.2.7 |
||
118 | * |
||
119 | * @var string |
||
120 | */ |
||
121 | protected $transparency = self::TIME_TRANSPARENCY_OPAQUE; |
||
122 | |||
123 | /** |
||
124 | * If set to true the timezone will be added to the event. |
||
125 | * |
||
126 | * @var bool |
||
127 | */ |
||
128 | protected $useTimezone = false; |
||
129 | |||
130 | /** |
||
131 | * If set will be used as the timezone identifier. |
||
132 | * |
||
133 | * @var string |
||
134 | */ |
||
135 | protected $timezoneString = ''; |
||
136 | |||
137 | /** |
||
138 | * @var int |
||
139 | */ |
||
140 | protected $sequence = 0; |
||
141 | |||
142 | /** |
||
143 | * @var Attendees |
||
144 | */ |
||
145 | protected $attendees; |
||
146 | |||
147 | /** |
||
148 | * @var string |
||
149 | */ |
||
150 | protected $description; |
||
151 | |||
152 | /** |
||
153 | * @var string |
||
154 | */ |
||
155 | protected $descriptionHTML; |
||
156 | |||
157 | /** |
||
158 | * @var string |
||
159 | */ |
||
160 | protected $status; |
||
161 | |||
162 | /** |
||
163 | * @var RecurrenceRule |
||
164 | */ |
||
165 | protected $recurrenceRule; |
||
166 | |||
167 | /** |
||
168 | * @var array |
||
169 | */ |
||
170 | protected $recurrenceRules = []; |
||
171 | |||
172 | /** |
||
173 | * This property specifies the date and time that the calendar |
||
174 | * information was created. |
||
175 | * |
||
176 | * The value MUST be specified in the UTC time format. |
||
177 | * |
||
178 | * @var \DateTime |
||
179 | */ |
||
180 | protected $created; |
||
181 | |||
182 | /** |
||
183 | * The property specifies the date and time that the information |
||
184 | * associated with the calendar component was last revised. |
||
185 | * |
||
186 | * The value MUST be specified in the UTC time format. |
||
187 | * |
||
188 | * @var \DateTime |
||
189 | */ |
||
190 | protected $modified; |
||
191 | |||
192 | /** |
||
193 | * Indicates if the UTC time should be used or not. |
||
194 | * |
||
195 | * @var bool |
||
196 | */ |
||
197 | protected $useUtc = true; |
||
198 | |||
199 | /** |
||
200 | * @var bool |
||
201 | */ |
||
202 | protected $cancelled; |
||
203 | |||
204 | /** |
||
205 | * This property is used to specify categories or subtypes |
||
206 | * of the calendar component. The categories are useful in searching |
||
207 | * for a calendar component of a particular type and category. |
||
208 | * |
||
209 | * @see https://tools.ietf.org/html/rfc5545#section-3.8.1.2 |
||
210 | * |
||
211 | * @var array |
||
212 | */ |
||
213 | protected $categories; |
||
214 | |||
215 | /** |
||
216 | * https://tools.ietf.org/html/rfc5545#section-3.8.1.3. |
||
217 | * |
||
218 | * @var bool |
||
219 | */ |
||
220 | protected $isPrivate = false; |
||
221 | |||
222 | /** |
||
223 | * Dates to be excluded from a series of events. |
||
224 | * |
||
225 | * @var \DateTimeInterface[] |
||
226 | */ |
||
227 | protected $exDates = []; |
||
228 | |||
229 | /** |
||
230 | * @var RecurrenceId |
||
231 | */ |
||
232 | protected $recurrenceId; |
||
233 | |||
234 | /** |
||
235 | * @var Attachment[] |
||
236 | */ |
||
237 | protected $attachments = []; |
||
238 | |||
239 | 28 | public function __construct(string $uniqueId = null) |
|
248 | |||
249 | /** |
||
250 | * {@inheritdoc} |
||
251 | */ |
||
252 | 7 | public function getType() |
|
256 | |||
257 | /** |
||
258 | * {@inheritdoc} |
||
259 | */ |
||
260 | 18 | public function buildPropertyBag() |
|
396 | |||
397 | /** |
||
398 | * @param $dtEnd |
||
399 | * |
||
400 | * @return $this |
||
401 | */ |
||
402 | 5 | public function setDtEnd($dtEnd) |
|
408 | |||
409 | 2 | public function getDtEnd() |
|
413 | |||
414 | 5 | public function setDtStart($dtStart) |
|
420 | |||
421 | 2 | public function getDtStart() |
|
425 | |||
426 | /** |
||
427 | * @param $dtStamp |
||
428 | * |
||
429 | * @return $this |
||
430 | */ |
||
431 | 1 | public function setDtStamp($dtStamp) |
|
437 | |||
438 | /** |
||
439 | * @param $duration |
||
440 | * |
||
441 | * @return $this |
||
442 | */ |
||
443 | 1 | public function setDuration($duration) |
|
449 | |||
450 | /** |
||
451 | * @param string $location |
||
452 | * @param string $title |
||
453 | * @param Geo|string $geo |
||
454 | * |
||
455 | * @return $this |
||
456 | */ |
||
457 | 3 | public function setLocation($location, $title = '', $geo = null) |
|
472 | |||
473 | /** |
||
474 | * @return $this |
||
475 | */ |
||
476 | 1 | public function setGeoLocation(Geo $geoProperty) |
|
482 | |||
483 | /** |
||
484 | * @param $noTime |
||
485 | * |
||
486 | * @return $this |
||
487 | */ |
||
488 | 1 | public function setNoTime($noTime) |
|
494 | |||
495 | /** |
||
496 | * @param $msBusyStatus |
||
497 | * |
||
498 | * @return $this |
||
499 | * |
||
500 | * @throws \InvalidArgumentException |
||
501 | */ |
||
502 | 2 | View Code Duplication | public function setMsBusyStatus($msBusyStatus) |
517 | |||
518 | /** |
||
519 | * @return string|null |
||
520 | */ |
||
521 | 1 | public function getMsBusyStatus() |
|
525 | |||
526 | /** |
||
527 | * @param int $sequence |
||
528 | * |
||
529 | * @return $this |
||
530 | */ |
||
531 | 2 | public function setSequence($sequence) |
|
537 | |||
538 | /** |
||
539 | * @return int |
||
540 | */ |
||
541 | 1 | public function getSequence() |
|
545 | |||
546 | /** |
||
547 | * @return $this |
||
548 | */ |
||
549 | 2 | public function setOrganizer(Organizer $organizer) |
|
555 | |||
556 | /** |
||
557 | * @param $summary |
||
558 | * |
||
559 | * @return $this |
||
560 | */ |
||
561 | 1 | public function setSummary($summary) |
|
567 | |||
568 | /** |
||
569 | * @param $uniqueId |
||
570 | * |
||
571 | * @return $this |
||
572 | */ |
||
573 | 1 | public function setUniqueId($uniqueId) |
|
579 | |||
580 | /** |
||
581 | * @return string |
||
582 | */ |
||
583 | 2 | public function getUniqueId() |
|
587 | |||
588 | /** |
||
589 | * @param $url |
||
590 | * |
||
591 | * @return $this |
||
592 | */ |
||
593 | 1 | public function setUrl($url) |
|
599 | |||
600 | /** |
||
601 | * @param $useTimezone |
||
602 | * |
||
603 | * @return $this |
||
604 | */ |
||
605 | public function setUseTimezone($useTimezone) |
||
611 | |||
612 | /** |
||
613 | * @return bool |
||
614 | */ |
||
615 | public function getUseTimezone() |
||
619 | |||
620 | /** |
||
621 | * @param $timezoneString |
||
622 | * |
||
623 | * @return $this |
||
624 | */ |
||
625 | 2 | public function setTimezoneString($timezoneString) |
|
631 | |||
632 | /** |
||
633 | * @return bool |
||
634 | */ |
||
635 | 1 | public function getTimezoneString() |
|
639 | |||
640 | /** |
||
641 | * @return $this |
||
642 | */ |
||
643 | public function setAttendees(Attendees $attendees) |
||
649 | |||
650 | /** |
||
651 | * @param string $attendee |
||
652 | * @param array $params |
||
653 | * |
||
654 | * @return $this |
||
655 | */ |
||
656 | public function addAttendee($attendee, $params = []) |
||
662 | |||
663 | public function getAttendees(): Attendees |
||
667 | |||
668 | /** |
||
669 | * @param $description |
||
670 | * |
||
671 | * @return $this |
||
672 | */ |
||
673 | 4 | public function setDescription($description) |
|
679 | |||
680 | /** |
||
681 | * @param $descriptionHTML |
||
682 | * |
||
683 | * @return $this |
||
684 | */ |
||
685 | public function setDescriptionHTML($descriptionHTML) |
||
691 | |||
692 | /** |
||
693 | * @param bool $useUtc |
||
694 | * |
||
695 | * @return $this |
||
696 | */ |
||
697 | public function setUseUtc($useUtc = true) |
||
703 | |||
704 | /** |
||
705 | * @return string |
||
706 | */ |
||
707 | public function getDescription() |
||
711 | |||
712 | /** |
||
713 | * @return string |
||
714 | */ |
||
715 | public function getDescriptionHTML() |
||
719 | |||
720 | /** |
||
721 | * @param $status |
||
722 | * |
||
723 | * @return $this |
||
724 | */ |
||
725 | public function setCancelled($status) |
||
731 | |||
732 | /** |
||
733 | * @param $transparency |
||
734 | * |
||
735 | * @return $this |
||
736 | * |
||
737 | * @throws \InvalidArgumentException |
||
738 | */ |
||
739 | View Code Duplication | public function setTimeTransparency($transparency) |
|
752 | |||
753 | /** |
||
754 | * @param $status |
||
755 | * |
||
756 | * @return $this |
||
757 | * |
||
758 | * @throws \InvalidArgumentException |
||
759 | */ |
||
760 | View Code Duplication | public function setStatus($status) |
|
774 | |||
775 | /** |
||
776 | * @deprecated Deprecated since version 0.11.0, to be removed in 1.0. Use addRecurrenceRule instead. |
||
777 | * |
||
778 | * @return $this |
||
779 | */ |
||
780 | public function setRecurrenceRule(RecurrenceRule $recurrenceRule) |
||
788 | |||
789 | /** |
||
790 | * @deprecated Deprecated since version 0.11.0, to be removed in 1.0. Use getRecurrenceRules instead. |
||
791 | * |
||
792 | * @return RecurrenceRule |
||
793 | */ |
||
794 | public function getRecurrenceRule() |
||
800 | |||
801 | /** |
||
802 | * @return $this |
||
803 | */ |
||
804 | public function addRecurrenceRule(RecurrenceRule $recurrenceRule) |
||
810 | |||
811 | /** |
||
812 | * @return array |
||
813 | */ |
||
814 | public function getRecurrenceRules() |
||
818 | |||
819 | /** |
||
820 | * @param $dtStamp |
||
821 | * |
||
822 | * @return $this |
||
823 | */ |
||
824 | public function setCreated($dtStamp) |
||
830 | |||
831 | /** |
||
832 | * @param $dtStamp |
||
833 | * |
||
834 | * @return $this |
||
835 | */ |
||
836 | public function setModified($dtStamp) |
||
842 | |||
843 | /** |
||
844 | * @param $categories |
||
845 | * |
||
846 | * @return $this |
||
847 | */ |
||
848 | public function setCategories($categories) |
||
854 | |||
855 | /** |
||
856 | * Sets the event privacy. |
||
857 | * |
||
858 | * @param bool $flag |
||
859 | * |
||
860 | * @return $this |
||
861 | */ |
||
862 | public function setIsPrivate($flag) |
||
868 | |||
869 | /** |
||
870 | * @return \Eluceo\iCal\Component\Event |
||
871 | */ |
||
872 | public function addExDate(\DateTimeInterface $dateTime) |
||
878 | |||
879 | /** |
||
880 | * @return \DateTimeInterface[] |
||
881 | */ |
||
882 | public function getExDates() |
||
886 | |||
887 | /** |
||
888 | * @param \DateTimeInterface[] |
||
889 | * |
||
890 | * @return \Eluceo\iCal\Component\Event |
||
891 | */ |
||
892 | public function setExDates(array $exDates) |
||
898 | |||
899 | /** |
||
900 | * @return \Eluceo\iCal\Property\Event\RecurrenceId |
||
901 | */ |
||
902 | public function getRecurrenceId() |
||
906 | |||
907 | /** |
||
908 | * @return \Eluceo\iCal\Component\Event |
||
909 | */ |
||
910 | public function setRecurrenceId(RecurrenceId $recurrenceId) |
||
916 | |||
917 | /** |
||
918 | * @param array $attachment |
||
919 | * |
||
920 | * @return $this |
||
921 | */ |
||
922 | 1 | public function addAttachment(Attachment $attachment) |
|
928 | |||
929 | /** |
||
930 | * @return array |
||
931 | */ |
||
932 | public function getAttachments() |
||
936 | |||
937 | public function addUrlAttachment(string $url) |
||
941 | } |
||
942 |