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 |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @param Period $period |
||
| 234 | * @return bool |
||
| 235 | * @throws CannotComparePeriods |
||
| 236 | */ |
||
| 237 | public function touchesWith(Period $period): bool |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @param DateTimeInterface $date |
||
| 254 | * @return bool |
||
| 255 | */ |
||
| 256 | public function startsBefore(DateTimeInterface $date): bool |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @param DateTimeInterface $date |
||
| 263 | * @return bool |
||
| 264 | */ |
||
| 265 | public function startsBeforeOrAt(DateTimeInterface $date): bool |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @param DateTimeInterface $date |
||
| 272 | * @return bool |
||
| 273 | */ |
||
| 274 | public function startsAfter(DateTimeInterface $date): bool |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @param DateTimeInterface $date |
||
| 281 | * @return bool |
||
| 282 | */ |
||
| 283 | public function startsAfterOrAt(DateTimeInterface $date): bool |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @param DateTimeInterface $date |
||
| 290 | * @return bool |
||
| 291 | */ |
||
| 292 | public function startsAt(DateTimeInterface $date): bool |
||
| 299 | |||
| 300 | /** |
||
| 301 | * @param DateTimeInterface $date |
||
| 302 | * @return bool |
||
| 303 | */ |
||
| 304 | public function endsBefore(DateTimeInterface $date): bool |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @param DateTimeInterface $date |
||
| 314 | * @return bool |
||
| 315 | */ |
||
| 316 | public function endsBeforeOrAt(DateTimeInterface $date): bool |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @param DateTimeInterface $date |
||
| 326 | * @return bool |
||
| 327 | */ |
||
| 328 | public function endsAfter(DateTimeInterface $date): bool |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @param DateTimeInterface $date |
||
| 338 | * @return bool |
||
| 339 | */ |
||
| 340 | public function endsAfterOrAt(DateTimeInterface $date): bool |
||
| 347 | |||
| 348 | /** |
||
| 349 | * @param DateTimeInterface $date |
||
| 350 | * @return bool |
||
| 351 | */ |
||
| 352 | public function endsAt(DateTimeInterface $date): bool |
||
| 359 | |||
| 360 | /** |
||
| 361 | * @param DateTimeInterface $date |
||
| 362 | * @return bool |
||
| 363 | */ |
||
| 364 | public function contains(DateTimeInterface $date): bool |
||
| 376 | |||
| 377 | /** |
||
| 378 | * @param Period $period |
||
| 379 | * @return bool |
||
| 380 | * @throws CannotComparePeriods |
||
| 381 | */ |
||
| 382 | public function equals(Period $period): bool |
||
| 396 | |||
| 397 | /** |
||
| 398 | * @param \Spatie\Period\Period $period |
||
| 399 | * |
||
| 400 | * @return static|null |
||
| 401 | * @throws \Exception |
||
| 402 | */ |
||
| 403 | public function gap(Period $period): ?Period |
||
| 429 | |||
| 430 | /** |
||
| 431 | * @param Period $period |
||
| 432 | * @return Period|null |
||
| 433 | * @throws CannotComparePeriods |
||
| 434 | */ |
||
| 435 | public function overlapSingle(Period $period): ?Period |
||
| 453 | |||
| 454 | /** |
||
| 455 | * @param Period ...$periods |
||
| 456 | * @return PeriodCollection |
||
| 457 | * @throws CannotComparePeriods |
||
| 458 | */ |
||
| 459 | public function overlap(Period ...$periods): PeriodCollection |
||
| 475 | |||
| 476 | /** |
||
| 477 | * @param Period ...$periods |
||
| 478 | * @return Period |
||
| 479 | * @throws CannotComparePeriods |
||
| 480 | */ |
||
| 481 | public function overlapAll(Period ...$periods): Period |
||
| 495 | |||
| 496 | /** |
||
| 497 | * @param Period $period |
||
| 498 | * @return PeriodCollection |
||
| 499 | * @throws CannotComparePeriods |
||
| 500 | */ |
||
| 501 | public function diffSingle(Period $period): PeriodCollection |
||
| 542 | |||
| 543 | /** |
||
| 544 | * @param Period ...$periods |
||
| 545 | * @return PeriodCollection |
||
| 546 | * @throws CannotComparePeriods |
||
| 547 | */ |
||
| 548 | public function diff(Period ...$periods): PeriodCollection |
||
| 572 | |||
| 573 | /** |
||
| 574 | * @return int |
||
| 575 | */ |
||
| 576 | public function getPrecisionMask(): int |
||
| 580 | |||
| 581 | /** |
||
| 582 | * @return DatePeriod|\Traversable |
||
| 583 | */ |
||
| 584 | public function getIterator() |
||
| 592 | |||
| 593 | /** |
||
| 594 | * @param $date |
||
| 595 | * @param string|null $format |
||
| 596 | * @return DateTimeImmutable |
||
| 597 | */ |
||
| 598 | protected static function resolveDate($date, ?string $format): DateTimeImmutable |
||
| 626 | |||
| 627 | /** |
||
| 628 | * @param $date |
||
| 629 | * @param string|null $format |
||
| 630 | * @return string |
||
| 631 | */ |
||
| 632 | protected static function resolveFormat($date, ?string $format): string |
||
| 644 | |||
| 645 | /** |
||
| 646 | * @param DateTimeInterface $date |
||
| 647 | * @param int $precision |
||
| 648 | * @return DateTimeImmutable |
||
| 649 | */ |
||
| 650 | protected function roundDate(DateTimeInterface $date, int $precision): DateTimeImmutable |
||
| 665 | |||
| 666 | /** |
||
| 667 | * @param int $precision |
||
| 668 | * @return DateInterval |
||
| 669 | * @throws \Exception |
||
| 670 | */ |
||
| 671 | protected function createDateInterval(int $precision): DateInterval |
||
| 684 | |||
| 685 | /** |
||
| 686 | * @param Period $period |
||
| 687 | * @throws CannotComparePeriods |
||
| 688 | */ |
||
| 689 | protected function ensurePrecisionMatches(Period $period): void |
||
| 697 | } |
||
| 698 |
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: