Complex classes like Calendar 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 Calendar, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | final class Calendar implements CalendarInterface, JsonLdSerializableInterface, SerializableInterface |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * @var CalendarType |
||
| 23 | */ |
||
| 24 | protected $type; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var DateTimeInterface |
||
| 28 | */ |
||
| 29 | protected $startDate; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var DateTimeInterface |
||
| 33 | */ |
||
| 34 | protected $endDate; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var Timestamp[] |
||
| 38 | */ |
||
| 39 | protected $timestamps = []; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var OpeningHour[] |
||
| 43 | */ |
||
| 44 | protected $openingHours = []; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @param CalendarType $type |
||
| 48 | * @param DateTimeInterface|null $startDate |
||
| 49 | * @param DateTimeInterface|null $endDate |
||
| 50 | * @param Timestamp[] $timestamps |
||
| 51 | * @param OpeningHour[] $openingHours |
||
| 52 | */ |
||
| 53 | public function __construct( |
||
| 54 | CalendarType $type, |
||
| 55 | ?DateTimeInterface $startDate = null, |
||
| 56 | ?DateTimeInterface $endDate = null, |
||
| 57 | array $timestamps = [], |
||
| 58 | array $openingHours = [] |
||
| 59 | ) { |
||
| 60 | if (empty($timestamps) && ($type->is(CalendarType::SINGLE()) || $type->is(CalendarType::MULTIPLE()))) { |
||
| 61 | throw new \UnexpectedValueException('A single or multiple calendar should have timestamps.'); |
||
| 62 | } |
||
| 63 | |||
| 64 | if (($startDate === null || $endDate === null) && $type->is(CalendarType::PERIODIC())) { |
||
| 65 | throw new \UnexpectedValueException('A period should have a start- and end-date.'); |
||
| 66 | } |
||
| 67 | |||
| 68 | foreach ($timestamps as $timestamp) { |
||
| 69 | if (!is_a($timestamp, Timestamp::class)) { |
||
| 70 | throw new \InvalidArgumentException('Timestamps should have type TimeStamp.'); |
||
| 71 | } |
||
| 72 | } |
||
| 73 | |||
| 74 | foreach ($openingHours as $openingHour) { |
||
| 75 | if (!is_a($openingHour, OpeningHour::class)) { |
||
| 76 | throw new \InvalidArgumentException('OpeningHours should have type OpeningHour.'); |
||
| 77 | } |
||
| 78 | } |
||
| 79 | |||
| 80 | $this->type = $type->toNative(); |
||
| 81 | $this->startDate = $startDate; |
||
| 82 | $this->endDate = $endDate; |
||
| 83 | $this->openingHours = $openingHours; |
||
| 84 | |||
| 85 | usort($timestamps, function (Timestamp $timestamp, Timestamp $otherTimestamp) { |
||
| 86 | return $timestamp->getStartDate() <=> $otherTimestamp->getStartDate(); |
||
| 87 | }); |
||
| 88 | |||
| 89 | $this->timestamps = $timestamps; |
||
| 90 | |||
| 91 | } |
||
| 92 | |||
| 93 | public function getType(): CalendarType |
||
| 94 | { |
||
| 95 | return CalendarType::fromNative($this->type); |
||
| 96 | } |
||
| 97 | |||
| 98 | public function serialize(): array |
||
| 99 | { |
||
| 100 | $serializedTimestamps = array_map( |
||
| 101 | function (Timestamp $timestamp) { |
||
| 102 | return $timestamp->serialize(); |
||
| 103 | }, |
||
| 104 | $this->timestamps |
||
| 105 | ); |
||
| 106 | |||
| 107 | $serializedOpeningHours = array_map( |
||
| 108 | function (OpeningHour $openingHour) { |
||
| 109 | return $openingHour->serialize(); |
||
| 110 | }, |
||
| 111 | $this->openingHours |
||
| 112 | ); |
||
| 113 | |||
| 114 | $calendar = [ |
||
| 115 | 'type' => $this->type, |
||
| 116 | ]; |
||
| 117 | |||
| 118 | empty($this->startDate) ?: $calendar['startDate'] = $this->startDate->format(DateTime::ATOM); |
||
| 119 | empty($this->endDate) ?: $calendar['endDate'] = $this->endDate->format(DateTime::ATOM); |
||
| 120 | empty($serializedTimestamps) ?: $calendar['timestamps'] = $serializedTimestamps; |
||
| 121 | empty($serializedOpeningHours) ?: $calendar['openingHours'] = $serializedOpeningHours; |
||
| 122 | |||
| 123 | return $calendar; |
||
| 124 | } |
||
| 125 | |||
| 126 | public static function deserialize(array $data): Calendar |
||
| 127 | { |
||
| 128 | $calendarType = CalendarType::fromNative($data['type']); |
||
| 129 | |||
| 130 | // Backwards compatibility for serialized single or multiple calendar types that are missing timestamps but do |
||
| 131 | // have a start and end date. |
||
| 132 | $defaultTimeStamps = []; |
||
| 133 | if ($calendarType->sameValueAs(CalendarType::SINGLE()) || $calendarType->sameValueAs(CalendarType::MULTIPLE())) { |
||
| 134 | $defaultTimeStampStartDate = !empty($data['startDate']) ? self::deserializeDateTime($data['startDate']) : null; |
||
| 135 | $defaultTimeStampEndDate = !empty($data['endDate']) ? self::deserializeDateTime($data['endDate']) : $defaultTimeStampStartDate; |
||
| 136 | $defaultTimeStamp = $defaultTimeStampStartDate && $defaultTimeStampEndDate ? new Timestamp($defaultTimeStampStartDate, $defaultTimeStampEndDate) : null; |
||
| 137 | $defaultTimeStamps = $defaultTimeStamp ? [$defaultTimeStamp] : []; |
||
| 138 | } |
||
| 139 | |||
| 140 | return new self( |
||
| 141 | $calendarType, |
||
| 142 | !empty($data['startDate']) ? self::deserializeDateTime($data['startDate']) : null, |
||
| 143 | !empty($data['endDate']) ? self::deserializeDateTime($data['endDate']) : null, |
||
| 144 | !empty($data['timestamps']) ? array_map( |
||
| 145 | function ($timestamp) { |
||
| 146 | return Timestamp::deserialize($timestamp); |
||
| 147 | }, |
||
| 148 | $data['timestamps'] |
||
| 149 | ) : $defaultTimeStamps, |
||
| 150 | !empty($data['openingHours']) ? array_map( |
||
| 151 | function ($openingHour) { |
||
| 152 | return OpeningHour::deserialize($openingHour); |
||
| 153 | }, |
||
| 154 | $data['openingHours'] |
||
| 155 | ) : [] |
||
| 156 | ); |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * This deserialization function takes into account old data that might be missing a timezone. |
||
| 161 | * It will fall back to creating a DateTime object and assume Brussels. |
||
| 162 | * If this still fails an error will be thrown. |
||
| 163 | */ |
||
| 164 | private static function deserializeDateTime(string $dateTimeData): DateTime |
||
| 178 | |||
| 179 | public function getStartDate(): ?DateTimeInterface |
||
| 180 | { |
||
| 181 | $timestamps = $this->getTimestamps(); |
||
| 182 | |||
| 196 | |||
| 197 | public function getEndDate(): ?DateTimeInterface |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @return array|OpeningHour[] |
||
| 217 | */ |
||
| 218 | public function getOpeningHours(): array |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @return array|Timestamp[] |
||
| 225 | */ |
||
| 226 | public function getTimestamps(): array |
||
| 230 | |||
| 231 | public function getEventStatusType(): EventStatusType |
||
| 257 | |||
| 258 | public function toJsonLd(): array |
||
| 293 | |||
| 294 | public function sameAs(Calendar $otherCalendar): bool |
||
| 298 | |||
| 299 | public static function fromUdb3ModelCalendar(Udb3ModelCalendar $calendar): Calendar |
||
| 333 | } |
||
| 334 |