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) |
|
| 475 | |||
| 476 | /** |
||
| 477 | * @param Geo $geoProperty |
||
| 478 | * |
||
| 479 | * @return $this |
||
| 480 | */ |
||
| 481 | 1 | public function setGeoLocation(Geo $geoProperty) |
|
| 487 | |||
| 488 | /** |
||
| 489 | * @param $noTime |
||
| 490 | * |
||
| 491 | * @return $this |
||
| 492 | */ |
||
| 493 | 1 | public function setNoTime($noTime) |
|
| 499 | |||
| 500 | /** |
||
| 501 | * @param $msBusyStatus |
||
| 502 | * |
||
| 503 | * @return $this |
||
| 504 | * |
||
| 505 | * @throws \InvalidArgumentException |
||
| 506 | */ |
||
| 507 | 2 | View Code Duplication | public function setMsBusyStatus($msBusyStatus) |
| 522 | |||
| 523 | /** |
||
| 524 | * @return string|null |
||
| 525 | */ |
||
| 526 | 1 | public function getMsBusyStatus() |
|
| 530 | |||
| 531 | /** |
||
| 532 | * @param int $sequence |
||
| 533 | * |
||
| 534 | * @return $this |
||
| 535 | */ |
||
| 536 | 2 | public function setSequence($sequence) |
|
| 542 | |||
| 543 | /** |
||
| 544 | * @return int |
||
| 545 | */ |
||
| 546 | 1 | public function getSequence() |
|
| 550 | |||
| 551 | /** |
||
| 552 | * @param Organizer $organizer |
||
| 553 | * |
||
| 554 | * @return $this |
||
| 555 | */ |
||
| 556 | 2 | public function setOrganizer(Organizer $organizer) |
|
| 562 | |||
| 563 | /** |
||
| 564 | * @param $summary |
||
| 565 | * |
||
| 566 | * @return $this |
||
| 567 | */ |
||
| 568 | 1 | public function setSummary($summary) |
|
| 574 | |||
| 575 | /** |
||
| 576 | * @param $uniqueId |
||
| 577 | * |
||
| 578 | * @return $this |
||
| 579 | */ |
||
| 580 | 1 | public function setUniqueId($uniqueId) |
|
| 586 | |||
| 587 | /** |
||
| 588 | * @return string |
||
| 589 | */ |
||
| 590 | 2 | public function getUniqueId() |
|
| 594 | |||
| 595 | /** |
||
| 596 | * @param $url |
||
| 597 | * |
||
| 598 | * @return $this |
||
| 599 | */ |
||
| 600 | 1 | public function setUrl($url) |
|
| 606 | |||
| 607 | /** |
||
| 608 | * @param $useTimezone |
||
| 609 | * |
||
| 610 | * @return $this |
||
| 611 | */ |
||
| 612 | public function setUseTimezone($useTimezone) |
||
| 618 | |||
| 619 | /** |
||
| 620 | * @return bool |
||
| 621 | */ |
||
| 622 | public function getUseTimezone() |
||
| 626 | |||
| 627 | /** |
||
| 628 | * @param $timezoneString |
||
| 629 | * |
||
| 630 | * @return $this |
||
| 631 | */ |
||
| 632 | 2 | public function setTimezoneString($timezoneString) |
|
| 638 | |||
| 639 | /** |
||
| 640 | * @return bool |
||
| 641 | */ |
||
| 642 | 1 | public function getTimezoneString() |
|
| 646 | |||
| 647 | /** |
||
| 648 | * @param Attendees $attendees |
||
| 649 | * |
||
| 650 | * @return $this |
||
| 651 | */ |
||
| 652 | public function setAttendees(Attendees $attendees) |
||
| 658 | |||
| 659 | /** |
||
| 660 | * @param string $attendee |
||
| 661 | * @param array $params |
||
| 662 | * |
||
| 663 | * @return $this |
||
| 664 | */ |
||
| 665 | public function addAttendee($attendee, $params = []) |
||
| 671 | |||
| 672 | /** |
||
| 673 | * @return Attendees |
||
| 674 | */ |
||
| 675 | public function getAttendees(): Attendees |
||
| 679 | |||
| 680 | /** |
||
| 681 | * @param $description |
||
| 682 | * |
||
| 683 | * @return $this |
||
| 684 | */ |
||
| 685 | 4 | public function setDescription($description) |
|
| 691 | |||
| 692 | /** |
||
| 693 | * @param $descriptionHTML |
||
| 694 | * |
||
| 695 | * @return $this |
||
| 696 | */ |
||
| 697 | public function setDescriptionHTML($descriptionHTML) |
||
| 703 | |||
| 704 | /** |
||
| 705 | * @param bool $useUtc |
||
| 706 | * |
||
| 707 | * @return $this |
||
| 708 | */ |
||
| 709 | public function setUseUtc($useUtc = true) |
||
| 715 | |||
| 716 | /** |
||
| 717 | * @return string |
||
| 718 | */ |
||
| 719 | public function getDescription() |
||
| 723 | |||
| 724 | /** |
||
| 725 | * @return string |
||
| 726 | */ |
||
| 727 | public function getDescriptionHTML() |
||
| 731 | |||
| 732 | /** |
||
| 733 | * @param $status |
||
| 734 | * |
||
| 735 | * @return $this |
||
| 736 | */ |
||
| 737 | public function setCancelled($status) |
||
| 743 | |||
| 744 | /** |
||
| 745 | * @param $transparency |
||
| 746 | * |
||
| 747 | * @return $this |
||
| 748 | * |
||
| 749 | * @throws \InvalidArgumentException |
||
| 750 | */ |
||
| 751 | View Code Duplication | public function setTimeTransparency($transparency) |
|
| 764 | |||
| 765 | /** |
||
| 766 | * @param $status |
||
| 767 | * |
||
| 768 | * @return $this |
||
| 769 | * |
||
| 770 | * @throws \InvalidArgumentException |
||
| 771 | */ |
||
| 772 | View Code Duplication | public function setStatus($status) |
|
| 786 | |||
| 787 | /** |
||
| 788 | * @deprecated Deprecated since version 0.11.0, to be removed in 1.0. Use addRecurrenceRule instead. |
||
| 789 | * |
||
| 790 | * @param RecurrenceRule $recurrenceRule |
||
| 791 | * |
||
| 792 | * @return $this |
||
| 793 | */ |
||
| 794 | public function setRecurrenceRule(RecurrenceRule $recurrenceRule) |
||
| 802 | |||
| 803 | /** |
||
| 804 | * @deprecated Deprecated since version 0.11.0, to be removed in 1.0. Use getRecurrenceRules instead. |
||
| 805 | * |
||
| 806 | * @return RecurrenceRule |
||
| 807 | */ |
||
| 808 | public function getRecurrenceRule() |
||
| 814 | |||
| 815 | /** |
||
| 816 | * @param RecurrenceRule $recurrenceRule |
||
| 817 | * |
||
| 818 | * @return $this |
||
| 819 | */ |
||
| 820 | public function addRecurrenceRule(RecurrenceRule $recurrenceRule) |
||
| 826 | |||
| 827 | /** |
||
| 828 | * @return array |
||
| 829 | */ |
||
| 830 | public function getRecurrenceRules() |
||
| 834 | |||
| 835 | /** |
||
| 836 | * @param $dtStamp |
||
| 837 | * |
||
| 838 | * @return $this |
||
| 839 | */ |
||
| 840 | public function setCreated($dtStamp) |
||
| 846 | |||
| 847 | /** |
||
| 848 | * @param $dtStamp |
||
| 849 | * |
||
| 850 | * @return $this |
||
| 851 | */ |
||
| 852 | public function setModified($dtStamp) |
||
| 858 | |||
| 859 | /** |
||
| 860 | * @param $categories |
||
| 861 | * |
||
| 862 | * @return $this |
||
| 863 | */ |
||
| 864 | public function setCategories($categories) |
||
| 870 | |||
| 871 | /** |
||
| 872 | * Sets the event privacy. |
||
| 873 | * |
||
| 874 | * @param bool $flag |
||
| 875 | * |
||
| 876 | * @return $this |
||
| 877 | */ |
||
| 878 | public function setIsPrivate($flag) |
||
| 884 | |||
| 885 | /** |
||
| 886 | * @param \DateTimeInterface $dateTime |
||
| 887 | * |
||
| 888 | * @return \Eluceo\iCal\Component\Event |
||
| 889 | */ |
||
| 890 | public function addExDate(\DateTimeInterface $dateTime) |
||
| 896 | |||
| 897 | /** |
||
| 898 | * @return \DateTimeInterface[] |
||
| 899 | */ |
||
| 900 | public function getExDates() |
||
| 904 | |||
| 905 | /** |
||
| 906 | * @param \DateTimeInterface[] |
||
| 907 | * |
||
| 908 | * @return \Eluceo\iCal\Component\Event |
||
| 909 | */ |
||
| 910 | public function setExDates(array $exDates) |
||
| 916 | |||
| 917 | /** |
||
| 918 | * @return \Eluceo\iCal\Property\Event\RecurrenceId |
||
| 919 | */ |
||
| 920 | public function getRecurrenceId() |
||
| 924 | |||
| 925 | /** |
||
| 926 | * @param RecurrenceId $recurrenceId |
||
| 927 | * |
||
| 928 | * @return \Eluceo\iCal\Component\Event |
||
| 929 | */ |
||
| 930 | public function setRecurrenceId(RecurrenceId $recurrenceId) |
||
| 936 | |||
| 937 | /** |
||
| 938 | * @param array $attachment |
||
| 939 | * |
||
| 940 | * @return $this |
||
| 941 | */ |
||
| 942 | 1 | public function addAttachment(Attachment $attachment) |
|
| 948 | |||
| 949 | /** |
||
| 950 | * @return array |
||
| 951 | */ |
||
| 952 | public function getAttachments() |
||
| 956 | |||
| 957 | /** |
||
| 958 | * @param string $url |
||
| 959 | */ |
||
| 960 | public function addUrlAttachment(string $url) |
||
| 964 | } |
||
| 965 |