Complex classes like Period 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 Period, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class Period implements IteratorAggregate |
||
16 | { |
||
17 | /** @var \DateTimeImmutable */ |
||
18 | protected $start; |
||
19 | |||
20 | /** @var \DateTimeImmutable */ |
||
21 | protected $end; |
||
22 | |||
23 | /** @var \DateInterval */ |
||
24 | protected $interval; |
||
25 | |||
26 | /** @var \DateTimeImmutable */ |
||
27 | private $includedStart; |
||
28 | |||
29 | /** @var \DateTimeImmutable */ |
||
30 | private $includedEnd; |
||
31 | |||
32 | /** @var int */ |
||
33 | private $boundaryExclusionMask; |
||
34 | |||
35 | /** @var int */ |
||
36 | private $precisionMask; |
||
37 | |||
38 | /** |
||
39 | * Period constructor. |
||
40 | * @param DateTimeImmutable $start |
||
41 | * @param DateTimeImmutable $end |
||
42 | * @param int|null $precisionMask |
||
43 | * @param int|null $boundaryExclusionMask |
||
44 | * @throws \Exception |
||
45 | */ |
||
46 | public function __construct( |
||
47 | DateTimeImmutable $start, |
||
48 | ?DateTimeImmutable $end, |
||
49 | ?int $precisionMask = null, |
||
50 | ?int $boundaryExclusionMask = null |
||
51 | ) { |
||
52 | if ($start > $end && null != $end) { |
||
53 | throw InvalidPeriod::endBeforeStart($start, $end); |
||
54 | } |
||
55 | |||
56 | $this->boundaryExclusionMask = $boundaryExclusionMask ?? Boundaries::EXCLUDE_NONE; |
||
57 | $this->precisionMask = $precisionMask ?? Precision::DAY; |
||
58 | |||
59 | $this->start = $this->roundDate($start, $this->precisionMask); |
||
60 | if (null != $end) { |
||
61 | $this->end = $this->roundDate($end, $this->precisionMask); |
||
62 | } else { |
||
63 | $this->end = null; |
||
64 | } |
||
65 | |||
66 | $this->interval = $this->createDateInterval($this->precisionMask); |
||
67 | |||
68 | $this->includedStart = $this->startIncluded() |
||
69 | ? $this->start |
||
70 | : $this->start->add($this->interval); |
||
71 | |||
72 | $this->includedEnd = $this->endIncluded() |
||
73 | ? $this->end |
||
74 | : $this->end->sub($this->interval); |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * @param string|DateTimeInterface $start |
||
79 | * @param string|DateTimeInterface $end |
||
80 | * @param int|null $precisionMask |
||
81 | * @param int|null $boundaryExclusionMask |
||
82 | * @param string|null $format |
||
83 | * |
||
84 | * @return static |
||
85 | */ |
||
86 | public static function make( |
||
87 | $start, |
||
88 | $end, |
||
89 | ?int $precisionMask = null, |
||
90 | ?int $boundaryExclusionMask = null, |
||
91 | ?string $format = null |
||
92 | ): Period { |
||
93 | if ($start === null) { |
||
94 | throw InvalidDate::cannotBeNull('Start date'); |
||
95 | } |
||
96 | |||
97 | // if ($end === null) { |
||
98 | // throw InvalidDate::cannotBeNull('End date'); |
||
99 | // } |
||
100 | |||
101 | return new static( |
||
102 | static::resolveDate($start, $format), |
||
103 | $end ? static::resolveDate($end, $format) : null, |
||
|
|||
104 | $precisionMask, |
||
105 | $boundaryExclusionMask |
||
106 | ); |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * @return bool |
||
111 | */ |
||
112 | public function startIncluded(): bool |
||
113 | { |
||
114 | return ! $this->startExcluded(); |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * @return bool |
||
119 | */ |
||
120 | public function startExcluded(): bool |
||
121 | { |
||
122 | return Boundaries::EXCLUDE_START & $this->boundaryExclusionMask; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * @return bool |
||
127 | */ |
||
128 | public function endIncluded(): bool |
||
129 | { |
||
130 | return ! $this->endExcluded(); |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * @return bool |
||
135 | */ |
||
136 | public function endExcluded(): bool |
||
137 | { |
||
138 | return Boundaries::EXCLUDE_END & $this->boundaryExclusionMask; |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * @return DateTimeImmutable |
||
143 | */ |
||
144 | public function getStart(): DateTimeImmutable |
||
145 | { |
||
146 | return $this->start; |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * @return DateTimeImmutable |
||
151 | */ |
||
152 | public function getIncludedStart(): DateTimeImmutable |
||
153 | { |
||
154 | return $this->includedStart; |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * @return DateTimeImmutable |
||
159 | */ |
||
160 | public function getEnd(): DateTimeImmutable |
||
161 | { |
||
162 | return $this->end; |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * @return DateTimeImmutable |
||
167 | */ |
||
168 | public function getIncludedEnd(): DateTimeImmutable |
||
172 | |||
173 | /** |
||
174 | * @return int |
||
175 | */ |
||
176 | public function length(): int |
||
177 | { |
||
178 | $length = $this->getIncludedStart()->diff($this->getIncludedEnd())->days + 1; |
||
179 | |||
180 | return $length; |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * @param Period $period |
||
185 | * @return bool |
||
186 | * @throws CannotComparePeriods |
||
187 | */ |
||
188 | public function overlapsWith(Period $period): bool |
||
232 | |||
233 | /** |
||
234 | * @param Period $period |
||
235 | * @return bool |
||
236 | * @throws CannotComparePeriods |
||
237 | */ |
||
238 | public function touchesWith(Period $period): bool |
||
252 | |||
253 | /** |
||
254 | * @param DateTimeInterface $date |
||
255 | * @return bool |
||
256 | */ |
||
257 | public function startsBefore(DateTimeInterface $date): bool |
||
261 | |||
262 | /** |
||
263 | * @param DateTimeInterface $date |
||
264 | * @return bool |
||
265 | */ |
||
266 | public function startsBeforeOrAt(DateTimeInterface $date): bool |
||
270 | |||
271 | /** |
||
272 | * @param DateTimeInterface $date |
||
273 | * @return bool |
||
274 | */ |
||
275 | public function startsAfter(DateTimeInterface $date): bool |
||
279 | |||
280 | /** |
||
281 | * @param DateTimeInterface $date |
||
282 | * @return bool |
||
283 | */ |
||
284 | public function startsAfterOrAt(DateTimeInterface $date): bool |
||
288 | |||
289 | /** |
||
290 | * @param DateTimeInterface $date |
||
291 | * @return bool |
||
292 | */ |
||
293 | public function startsAt(DateTimeInterface $date): bool |
||
300 | |||
301 | /** |
||
302 | * @param DateTimeInterface $date |
||
303 | * @return bool |
||
304 | */ |
||
305 | public function endsBefore(DateTimeInterface $date): bool |
||
312 | |||
313 | /** |
||
314 | * @param DateTimeInterface $date |
||
315 | * @return bool |
||
316 | */ |
||
317 | public function endsBeforeOrAt(DateTimeInterface $date): bool |
||
324 | |||
325 | /** |
||
326 | * @param DateTimeInterface $date |
||
327 | * @return bool |
||
328 | */ |
||
329 | public function endsAfter(DateTimeInterface $date): bool |
||
336 | |||
337 | /** |
||
338 | * @param DateTimeInterface $date |
||
339 | * @return bool |
||
340 | */ |
||
341 | public function endsAfterOrAt(DateTimeInterface $date): bool |
||
348 | |||
349 | /** |
||
350 | * @param DateTimeInterface $date |
||
351 | * @return bool |
||
352 | */ |
||
353 | public function endsAt(DateTimeInterface $date): bool |
||
360 | |||
361 | /** |
||
362 | * @param DateTimeInterface $date |
||
363 | * @return bool |
||
364 | */ |
||
365 | public function contains(DateTimeInterface $date): bool |
||
377 | |||
378 | /** |
||
379 | * @param Period $period |
||
380 | * @return bool |
||
381 | * @throws CannotComparePeriods |
||
382 | */ |
||
383 | public function equals(Period $period): bool |
||
397 | |||
398 | /** |
||
399 | * @param \Spatie\Period\Period $period |
||
400 | * |
||
401 | * @return static|null |
||
402 | * @throws \Exception |
||
403 | */ |
||
404 | public function gap(Period $period): ?Period |
||
430 | |||
431 | /** |
||
432 | * @param Period $period |
||
433 | * @return Period|null |
||
434 | * @throws CannotComparePeriods |
||
435 | */ |
||
436 | public function overlapSingle(Period $period): ?Period |
||
454 | |||
455 | /** |
||
456 | * @param Period ...$periods |
||
457 | * @return PeriodCollection |
||
458 | * @throws CannotComparePeriods |
||
459 | */ |
||
460 | public function overlap(Period ...$periods): PeriodCollection |
||
476 | |||
477 | /** |
||
478 | * @param Period ...$periods |
||
479 | * @return Period |
||
480 | * @throws CannotComparePeriods |
||
481 | */ |
||
482 | public function overlapAll(Period ...$periods): Period |
||
496 | |||
497 | /** |
||
498 | * @param Period $period |
||
499 | * @return PeriodCollection |
||
500 | * @throws CannotComparePeriods |
||
501 | */ |
||
502 | public function diffSingle(Period $period): PeriodCollection |
||
543 | |||
544 | /** |
||
545 | * @param Period ...$periods |
||
546 | * @return PeriodCollection |
||
547 | * @throws CannotComparePeriods |
||
548 | */ |
||
549 | public function diff(Period ...$periods): PeriodCollection |
||
573 | |||
574 | /** |
||
575 | * @return int |
||
576 | */ |
||
577 | public function getPrecisionMask(): int |
||
581 | |||
582 | /** |
||
583 | * @return DatePeriod|\Traversable |
||
584 | */ |
||
585 | public function getIterator() |
||
593 | |||
594 | /** |
||
595 | * @param $date |
||
596 | * @param string|null $format |
||
597 | * @return DateTimeImmutable |
||
598 | */ |
||
599 | protected static function resolveDate($date, ?string $format): DateTimeImmutable |
||
627 | |||
628 | /** |
||
629 | * @param $date |
||
630 | * @param string|null $format |
||
631 | * @return string |
||
632 | */ |
||
633 | protected static function resolveFormat($date, ?string $format): string |
||
645 | |||
646 | /** |
||
647 | * @param DateTimeInterface $date |
||
648 | * @param int $precision |
||
649 | * @return DateTimeImmutable |
||
650 | */ |
||
651 | protected function roundDate(DateTimeInterface $date, int $precision): DateTimeImmutable |
||
666 | |||
667 | /** |
||
668 | * @param int $precision |
||
669 | * @return DateInterval |
||
670 | * @throws \Exception |
||
671 | */ |
||
672 | protected function createDateInterval(int $precision): DateInterval |
||
685 | |||
686 | /** |
||
687 | * @param Period $period |
||
688 | * @throws CannotComparePeriods |
||
689 | */ |
||
690 | protected function ensurePrecisionMatches(Period $period): void |
||
698 | } |
||
699 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: