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 |
||
| 29 | class Event extends Component |
||
| 30 | { |
||
| 31 | const TIME_TRANSPARENCY_OPAQUE = 'OPAQUE'; |
||
| 32 | const TIME_TRANSPARENCY_TRANSPARENT = 'TRANSPARENT'; |
||
| 33 | |||
| 34 | const STATUS_TENTATIVE = 'TENTATIVE'; |
||
| 35 | const STATUS_CONFIRMED = 'CONFIRMED'; |
||
| 36 | const STATUS_CANCELLED = 'CANCELLED'; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | protected $uniqueId; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * The property indicates the date/time that the instance of |
||
| 45 | * the iCalendar object was created. |
||
| 46 | * |
||
| 47 | * The value MUST be specified in the UTC time format. |
||
| 48 | * |
||
| 49 | * @var \DateTime |
||
| 50 | */ |
||
| 51 | protected $dtStamp; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var \DateTime |
||
| 55 | */ |
||
| 56 | protected $dtStart; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Preferentially chosen over the duration if both are set. |
||
| 60 | * |
||
| 61 | * @var \DateTime |
||
| 62 | */ |
||
| 63 | protected $dtEnd; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var \DateInterval |
||
| 67 | */ |
||
| 68 | protected $duration; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var bool |
||
| 72 | */ |
||
| 73 | protected $noTime = false; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var string |
||
| 77 | */ |
||
| 78 | protected $url; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var string |
||
| 82 | */ |
||
| 83 | protected $location; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var string |
||
| 87 | */ |
||
| 88 | protected $locationTitle; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var Geo |
||
| 92 | */ |
||
| 93 | protected $locationGeo; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var string |
||
| 97 | */ |
||
| 98 | protected $summary; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @var Organizer |
||
| 102 | */ |
||
| 103 | protected $organizer; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @see https://tools.ietf.org/html/rfc5545#section-3.8.2.7 |
||
| 107 | * |
||
| 108 | * @var string |
||
| 109 | */ |
||
| 110 | protected $transparency = self::TIME_TRANSPARENCY_OPAQUE; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * If set to true the timezone will be added to the event. |
||
| 114 | * |
||
| 115 | * @var bool |
||
| 116 | */ |
||
| 117 | protected $useTimezone = false; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * If set will we used as the timezone identifier. |
||
| 121 | * |
||
| 122 | * @var string |
||
| 123 | */ |
||
| 124 | protected $timezoneString = ''; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @var int |
||
| 128 | */ |
||
| 129 | protected $sequence = 0; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @var Attendees |
||
| 133 | */ |
||
| 134 | protected $attendees; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @var string |
||
| 138 | */ |
||
| 139 | protected $description; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @var string |
||
| 143 | */ |
||
| 144 | protected $descriptionHTML; |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @var string |
||
| 148 | */ |
||
| 149 | protected $status; |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @var RecurrenceRule |
||
| 153 | */ |
||
| 154 | protected $recurrenceRule; |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @var array |
||
| 158 | */ |
||
| 159 | protected $recurrenceRules = []; |
||
| 160 | |||
| 161 | /** |
||
| 162 | * This property specifies the date and time that the calendar |
||
| 163 | * information was created. |
||
| 164 | * |
||
| 165 | * The value MUST be specified in the UTC time format. |
||
| 166 | * |
||
| 167 | * @var \DateTime |
||
| 168 | */ |
||
| 169 | protected $created; |
||
| 170 | |||
| 171 | /** |
||
| 172 | * The property specifies the date and time that the information |
||
| 173 | * associated with the calendar component was last revised. |
||
| 174 | * |
||
| 175 | * The value MUST be specified in the UTC time format. |
||
| 176 | * |
||
| 177 | * @var \DateTime |
||
| 178 | */ |
||
| 179 | protected $modified; |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Indicates if the UTC time should be used or not. |
||
| 183 | * |
||
| 184 | * @var bool |
||
| 185 | */ |
||
| 186 | protected $useUtc = true; |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @var bool |
||
| 190 | */ |
||
| 191 | protected $cancelled; |
||
| 192 | |||
| 193 | /** |
||
| 194 | * This property is used to specify categories or subtypes |
||
| 195 | * of the calendar component. The categories are useful in searching |
||
| 196 | * for a calendar component of a particular type and category. |
||
| 197 | * |
||
| 198 | * @see https://tools.ietf.org/html/rfc5545#section-3.8.1.2 |
||
| 199 | * |
||
| 200 | * @var array |
||
| 201 | */ |
||
| 202 | protected $categories; |
||
| 203 | |||
| 204 | /** |
||
| 205 | * https://tools.ietf.org/html/rfc5545#section-3.8.1.3. |
||
| 206 | * |
||
| 207 | * @var bool |
||
| 208 | */ |
||
| 209 | protected $isPrivate = false; |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Dates to be excluded from a series of events. |
||
| 213 | * |
||
| 214 | * @var \DateTimeInterface[] |
||
| 215 | 4 | */ |
|
| 216 | protected $exDates = []; |
||
| 217 | 4 | ||
| 218 | 2 | /** |
|
| 219 | 2 | * @var RecurrenceId |
|
| 220 | */ |
||
| 221 | 4 | protected $recurrenceId; |
|
| 222 | 4 | ||
| 223 | public function __construct(string $uniqueId = null) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * {@inheritdoc} |
||
| 235 | 4 | */ |
|
| 236 | public function getType() |
||
| 240 | 4 | ||
| 241 | /** |
||
| 242 | 4 | * {@inheritdoc} |
|
| 243 | 4 | */ |
|
| 244 | 4 | public function buildPropertyBag() |
|
| 370 | |||
| 371 | /** |
||
| 372 | * @param $dtEnd |
||
| 373 | * |
||
| 374 | * @return $this |
||
| 375 | */ |
||
| 376 | 2 | public function setDtEnd($dtEnd) |
|
| 382 | |||
| 383 | public function getDtEnd() |
||
| 387 | |||
| 388 | public function setDtStart($dtStart) |
||
| 394 | |||
| 395 | public function getDtStart() |
||
| 399 | |||
| 400 | /** |
||
| 401 | * @param $dtStamp |
||
| 402 | * |
||
| 403 | * @return $this |
||
| 404 | */ |
||
| 405 | public function setDtStamp($dtStamp) |
||
| 411 | |||
| 412 | /** |
||
| 413 | * @param $duration |
||
| 414 | * |
||
| 415 | * @return $this |
||
| 416 | */ |
||
| 417 | public function setDuration($duration) |
||
| 423 | |||
| 424 | /** |
||
| 425 | * @param string $location |
||
| 426 | * @param string $title |
||
| 427 | * @param Geo|string $geo |
||
| 428 | * |
||
| 429 | * @return $this |
||
| 430 | */ |
||
| 431 | public function setLocation($location, $title = '', $geo = null) |
||
| 432 | { |
||
| 433 | if (is_scalar($geo)) { |
||
| 434 | $geo = Geo::fromString($geo); |
||
| 435 | } elseif (!is_null($geo) && !$geo instanceof Geo) { |
||
| 436 | $className = get_class($geo); |
||
| 437 | throw new \InvalidArgumentException( |
||
| 438 | "The parameter 'geo' must be a string or an instance of " . Geo::class |
||
| 439 | . " but an instance of {$className} was given." |
||
| 440 | ); |
||
| 441 | } |
||
| 442 | |||
| 443 | $this->location = $location; |
||
| 444 | $this->locationTitle = $title; |
||
| 445 | $this->locationGeo = $geo; |
||
| 446 | |||
| 447 | return $this; |
||
| 448 | } |
||
| 449 | |||
| 450 | /** |
||
| 451 | * @param Geo $geoProperty |
||
| 452 | * |
||
| 453 | * @return $this |
||
| 454 | */ |
||
| 455 | public function setGeoLocation(Geo $geoProperty) |
||
| 461 | |||
| 462 | /** |
||
| 463 | * @param $noTime |
||
| 464 | * |
||
| 465 | 2 | * @return $this |
|
| 466 | */ |
||
| 467 | 2 | public function setNoTime($noTime) |
|
| 473 | |||
| 474 | /** |
||
| 475 | * @param int $sequence |
||
| 476 | * |
||
| 477 | * @return $this |
||
| 478 | */ |
||
| 479 | public function setSequence($sequence) |
||
| 485 | |||
| 486 | /** |
||
| 487 | * @return int |
||
| 488 | */ |
||
| 489 | public function getSequence() |
||
| 493 | |||
| 494 | /** |
||
| 495 | * @param Organizer $organizer |
||
| 496 | * |
||
| 497 | * @return $this |
||
| 498 | */ |
||
| 499 | public function setOrganizer(Organizer $organizer) |
||
| 505 | |||
| 506 | /** |
||
| 507 | * @param $summary |
||
| 508 | * |
||
| 509 | * @return $this |
||
| 510 | */ |
||
| 511 | public function setSummary($summary) |
||
| 517 | |||
| 518 | /** |
||
| 519 | * @param $uniqueId |
||
| 520 | * |
||
| 521 | * @return $this |
||
| 522 | */ |
||
| 523 | public function setUniqueId($uniqueId) |
||
| 529 | |||
| 530 | /** |
||
| 531 | * @return string |
||
| 532 | */ |
||
| 533 | public function getUniqueId() |
||
| 537 | |||
| 538 | /** |
||
| 539 | * @param $url |
||
| 540 | * |
||
| 541 | * @return $this |
||
| 542 | */ |
||
| 543 | public function setUrl($url) |
||
| 549 | |||
| 550 | /** |
||
| 551 | * @param $useTimezone |
||
| 552 | * |
||
| 553 | * @return $this |
||
| 554 | */ |
||
| 555 | public function setUseTimezone($useTimezone) |
||
| 561 | |||
| 562 | /** |
||
| 563 | * @return bool |
||
| 564 | */ |
||
| 565 | public function getUseTimezone() |
||
| 569 | |||
| 570 | /** |
||
| 571 | * @param $timezoneString |
||
| 572 | * |
||
| 573 | * @return $this |
||
| 574 | */ |
||
| 575 | public function setTimezoneString($timezoneString) |
||
| 581 | 2 | ||
| 582 | /** |
||
| 583 | * @return bool |
||
| 584 | */ |
||
| 585 | public function getTimezoneString() |
||
| 589 | |||
| 590 | /** |
||
| 591 | * @param Attendees $attendees |
||
| 592 | * |
||
| 593 | * @return $this |
||
| 594 | */ |
||
| 595 | public function setAttendees(Attendees $attendees) |
||
| 601 | |||
| 602 | /** |
||
| 603 | * @param string $attendee |
||
| 604 | * @param array $params |
||
| 605 | * |
||
| 606 | * @return $this |
||
| 607 | */ |
||
| 608 | public function addAttendee($attendee, $params = []) |
||
| 614 | |||
| 615 | /** |
||
| 616 | * @return Attendees |
||
| 617 | */ |
||
| 618 | public function getAttendees(): Attendees |
||
| 622 | |||
| 623 | /** |
||
| 624 | * @param $description |
||
| 625 | * |
||
| 626 | * @return $this |
||
| 627 | */ |
||
| 628 | public function setDescription($description) |
||
| 634 | |||
| 635 | /** |
||
| 636 | * @param $descriptionHTML |
||
| 637 | * |
||
| 638 | * @return $this |
||
| 639 | */ |
||
| 640 | public function setDescriptionHTML($descriptionHTML) |
||
| 646 | |||
| 647 | /** |
||
| 648 | * @param bool $useUtc |
||
| 649 | * |
||
| 650 | * @return $this |
||
| 651 | */ |
||
| 652 | public function setUseUtc($useUtc = true) |
||
| 658 | |||
| 659 | /** |
||
| 660 | * @return string |
||
| 661 | */ |
||
| 662 | public function getDescription() |
||
| 666 | |||
| 667 | /** |
||
| 668 | * @return string |
||
| 669 | */ |
||
| 670 | public function getDescriptionHTML() |
||
| 674 | |||
| 675 | /** |
||
| 676 | * @param $status |
||
| 677 | * |
||
| 678 | * @return $this |
||
| 679 | */ |
||
| 680 | public function setCancelled($status) |
||
| 686 | |||
| 687 | /** |
||
| 688 | * @param $transparency |
||
| 689 | * |
||
| 690 | * @return $this |
||
| 691 | * |
||
| 692 | * @throws \InvalidArgumentException |
||
| 693 | */ |
||
| 694 | View Code Duplication | public function setTimeTransparency($transparency) |
|
| 707 | |||
| 708 | /** |
||
| 709 | * @param $status |
||
| 710 | * |
||
| 711 | * @return $this |
||
| 712 | * |
||
| 713 | * @throws \InvalidArgumentException |
||
| 714 | */ |
||
| 715 | public function setStatus($status) |
||
| 729 | |||
| 730 | /** |
||
| 731 | * @deprecated Deprecated since version 0.11.0, to be removed in 1.0. Use addRecurrenceRule instead. |
||
| 732 | * |
||
| 733 | * @param RecurrenceRule $recurrenceRule |
||
| 734 | * |
||
| 735 | * @return $this |
||
| 736 | */ |
||
| 737 | public function setRecurrenceRule(RecurrenceRule $recurrenceRule) |
||
| 745 | |||
| 746 | /** |
||
| 747 | * @deprecated Deprecated since version 0.11.0, to be removed in 1.0. Use getRecurrenceRules instead. |
||
| 748 | * |
||
| 749 | * @return RecurrenceRule |
||
| 750 | */ |
||
| 751 | public function getRecurrenceRule() |
||
| 757 | |||
| 758 | /** |
||
| 759 | * @param RecurrenceRule $recurrenceRule |
||
| 760 | * |
||
| 761 | * @return $this |
||
| 762 | */ |
||
| 763 | public function addRecurrenceRule(RecurrenceRule $recurrenceRule) |
||
| 769 | |||
| 770 | /** |
||
| 771 | * @return array |
||
| 772 | */ |
||
| 773 | public function getRecurrenceRules() |
||
| 777 | |||
| 778 | /** |
||
| 779 | * @param $dtStamp |
||
| 780 | * |
||
| 781 | * @return $this |
||
| 782 | */ |
||
| 783 | public function setCreated($dtStamp) |
||
| 789 | |||
| 790 | /** |
||
| 791 | * @param $dtStamp |
||
| 792 | * |
||
| 793 | * @return $this |
||
| 794 | */ |
||
| 795 | public function setModified($dtStamp) |
||
| 801 | |||
| 802 | /** |
||
| 803 | * @param $categories |
||
| 804 | * |
||
| 805 | * @return $this |
||
| 806 | */ |
||
| 807 | public function setCategories($categories) |
||
| 813 | |||
| 814 | /** |
||
| 815 | * Sets the event privacy. |
||
| 816 | * |
||
| 817 | * @param bool $flag |
||
| 818 | * |
||
| 819 | * @return $this |
||
| 820 | */ |
||
| 821 | public function setIsPrivate($flag) |
||
| 827 | |||
| 828 | /** |
||
| 829 | * @param \DateTimeInterface $dateTime |
||
| 830 | * |
||
| 831 | * @return \Eluceo\iCal\Component\Event |
||
| 832 | */ |
||
| 833 | public function addExDate(\DateTimeInterface $dateTime) |
||
| 839 | |||
| 840 | /** |
||
| 841 | * @return \DateTimeInterface[] |
||
| 842 | */ |
||
| 843 | public function getExDates() |
||
| 847 | |||
| 848 | /** |
||
| 849 | * @param \DateTimeInterface[] |
||
| 850 | * |
||
| 851 | * @return \Eluceo\iCal\Component\Event |
||
| 852 | */ |
||
| 853 | public function setExDates(array $exDates) |
||
| 859 | |||
| 860 | /** |
||
| 861 | * @return \Eluceo\iCal\Property\Event\RecurrenceId |
||
| 862 | */ |
||
| 863 | public function getRecurrenceId() |
||
| 867 | |||
| 868 | /** |
||
| 869 | * @param RecurrenceId $recurrenceId |
||
| 870 | * |
||
| 871 | * @return \Eluceo\iCal\Component\Event |
||
| 872 | */ |
||
| 873 | public function setRecurrenceId(RecurrenceId $recurrenceId) |
||
| 879 | } |
||
| 880 |