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 |
||
| 35 | final class Period implements JsonSerializable |
||
| 36 | { |
||
| 37 | private const ISO8601_FORMAT = 'Y-m-d\TH:i:s.u\Z'; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Period starting included datepoint. |
||
| 41 | * |
||
| 42 | * @var DateTimeImmutable |
||
| 43 | */ |
||
| 44 | private $startDate; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Period ending excluded datepoint. |
||
| 48 | * |
||
| 49 | * @var DateTimeImmutable |
||
| 50 | */ |
||
| 51 | 3 | private $endDate; |
|
| 52 | |||
| 53 | 3 | /** |
|
| 54 | * @inheritdoc |
||
| 55 | */ |
||
| 56 | public static function __set_state(array $interval) |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Creates a new instance. |
||
| 63 | * |
||
| 64 | 255 | * @param mixed $startDate the interval start datepoint |
|
| 65 | * @param mixed $endDate the interval end datepoint |
||
| 66 | 255 | * |
|
| 67 | 255 | * @throws Exception If $startDate is greater than $endDate |
|
| 68 | 255 | */ |
|
| 69 | 39 | public function __construct($startDate, $endDate) |
|
| 79 | |||
| 80 | /** |
||
| 81 | * Returns the Interval starting datepoint. |
||
| 82 | 282 | * |
|
| 83 | * The starting datepoint is included in the specified period. |
||
| 84 | 282 | * The starting datepoint is always less than or equal to the ending datepoint. |
|
| 85 | 246 | */ |
|
| 86 | public function getStartDate(): DateTimeImmutable |
||
| 90 | |||
| 91 | /** |
||
| 92 | 153 | * Returns the Interval ending datepoint. |
|
| 93 | * |
||
| 94 | * The ending datepoint is excluded from the specified period. |
||
| 95 | * The ending datepoint is always greater than or equal to the starting datepoint. |
||
| 96 | */ |
||
| 97 | public function getEndDate(): DateTimeImmutable |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Returns the Interval duration as expressed in seconds. |
||
| 104 | 6 | */ |
|
| 105 | public function getTimestampInterval(): float |
||
| 109 | |||
| 110 | 3 | /** |
|
| 111 | * Returns the Interval duration as a DateInterval object. |
||
| 112 | */ |
||
| 113 | public function getDateInterval(): DateInterval |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Allows iteration over a set of dates and times, |
||
| 120 | * recurring at regular intervals, over the instance. |
||
| 121 | |||
| 122 | * |
||
| 123 | * @see http://php.net/manual/en/dateperiod.construct.php |
||
| 124 | */ |
||
| 125 | public function getDatePeriod($duration, int $option = 0): DatePeriod |
||
| 129 | |||
| 130 | 138 | /** |
|
| 131 | * Returns the string representation as a ISO8601 interval format. |
||
| 132 | 138 | * |
|
| 133 | * @see https://en.wikipedia.org/wiki/ISO_8601#Time_intervals |
||
| 134 | * |
||
| 135 | * @return string |
||
| 136 | */ |
||
| 137 | public function __toString() |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Returns the Json representation of an instance using |
||
| 146 | * the JSON representation of dates as returned by Javascript Date.toJSON() method. |
||
| 147 | * |
||
| 148 | * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toJSON |
||
| 149 | 177 | * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString |
|
| 150 | * |
||
| 151 | 177 | * @return string[] |
|
| 152 | 27 | */ |
|
| 153 | public function jsonSerialize() |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Returns the mathematical represeation of an instance as a right open interval. |
||
| 166 | * |
||
| 167 | * @see https://en.wikipedia.org/wiki/Interval_(methematics)#Notations_for_intervals |
||
| 168 | * @see https://php.net/manual/en/function.date.php for supported format string |
||
| 169 | * @see https://www.postgresql.org/docs/9.3/static/rangetypes.html |
||
| 170 | * |
||
| 171 | * @param string $format the format of the outputted date string |
||
| 172 | */ |
||
| 173 | public function format(string $format): string |
||
| 177 | 24 | ||
| 178 | /** |
||
| 179 | 24 | * Compares two instances according to their duration. |
|
| 180 | * |
||
| 181 | 24 | * Returns: |
|
| 182 | * <ul> |
||
| 183 | * <li> -1 if the current Interval is lesser than the submitted Interval object</li> |
||
| 184 | * <li> 1 if the current Interval is greater than the submitted Interval object</li> |
||
| 185 | * <li> 0 if both Interval objects have the same duration</li> |
||
| 186 | * </ul> |
||
| 187 | */ |
||
| 188 | public function durationCompare(self $interval): int |
||
| 192 | |||
| 193 | 18 | /** |
|
| 194 | 15 | * Tells whether the current instance duration is equal to the submitted one. |
|
| 195 | */ |
||
| 196 | 15 | public function durationEquals(self $interval): bool |
|
| 200 | |||
| 201 | 6 | /** |
|
| 202 | * Tells whether the current instance duration is greater than the submitted one. |
||
| 203 | */ |
||
| 204 | public function durationGreaterThan(self $interval): bool |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Tells whether the current instance duration is less than the submitted one. |
||
| 211 | */ |
||
| 212 | public function durationLessThan(self $interval): bool |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Tells whether two intervals share the same datepoints. |
||
| 219 | * |
||
| 220 | * [--------------------) |
||
| 221 | * [--------------------) |
||
| 222 | */ |
||
| 223 | public function equals(self $interval): bool |
||
| 228 | 12 | ||
| 229 | 3 | /** |
|
| 230 | 3 | * Tells whether two intervals abuts. |
|
| 231 | 3 | * |
|
| 232 | * [--------------------) |
||
| 233 | 3 | * [--------------------) |
|
| 234 | * or |
||
| 235 | * [--------------------) |
||
| 236 | 12 | * [--------------------) |
|
| 237 | 6 | */ |
|
| 238 | public function abuts(self $interval): bool |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Tells whether two intervals overlaps. |
||
| 246 | * |
||
| 247 | * [--------------------) |
||
| 248 | * [--------------------) |
||
| 249 | */ |
||
| 250 | public function overlaps(self $interval): bool |
||
| 255 | 75 | ||
| 256 | 75 | /** |
|
| 257 | 51 | * Tells whether an interval is entirely after the specified index. |
|
| 258 | * The index can be a DateTimeInterface object or another Period object. |
||
| 259 | * |
||
| 260 | 24 | * [--------------------) |
|
| 261 | * [--------------------) |
||
| 262 | */ |
||
| 263 | public function isAfter($index): bool |
||
| 271 | 12 | ||
| 272 | /** |
||
| 273 | 12 | * Tells whether a Interval is entirely before the specified index. |
|
| 274 | 3 | * The index can be a DateTimeInterface object or another Period object. |
|
| 275 | 3 | * |
|
| 276 | 3 | * [--------------------) |
|
| 277 | * [--------------------) |
||
| 278 | 3 | */ |
|
| 279 | public function isBefore($index): bool |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Tells whether the specified index is fully contained within |
||
| 290 | * the current Period object. |
||
| 291 | */ |
||
| 292 | public function contains($index): bool |
||
| 300 | 6 | ||
| 301 | /** |
||
| 302 | * Tells whether the an interval is fully contained within the current instance. |
||
| 303 | 36 | * |
|
| 304 | * [--------------------) |
||
| 305 | 30 | * [----------) |
|
| 306 | */ |
||
| 307 | private function containsPeriod(self $interval): bool |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Tells whether a datepoint is fully contained within the current instance. |
||
| 315 | * |
||
| 316 | 24 | * [------|------------) |
|
| 317 | */ |
||
| 318 | 24 | private function containsDatePoint(DateTimeInterface $datepoint): bool |
|
| 322 | 3 | ||
| 323 | /** |
||
| 324 | * Allows splitting an instance in smaller Period objects according to a given interval. |
||
| 325 | 3 | * |
|
| 326 | * The returned iterable Interval set is ordered so that: |
||
| 327 | * <ul> |
||
| 328 | 24 | * <li>The first returned object MUST share the starting datepoint of the parent object.</li> |
|
| 329 | 24 | * <li>The last returned object MUST share the ending datepoint of the parent object.</li> |
|
| 330 | 18 | * <li>The last returned object MUST have a duration equal or lesser than the submitted interval.</li> |
|
| 331 | * <li>All returned objects except for the first one MUST start immediately after the previously returned object</li> |
||
| 332 | * </ul> |
||
| 333 | 18 | * |
|
| 334 | * @param DateInterval|Period|string|int $duration |
||
| 335 | * |
||
| 336 | * @return Period[] |
||
| 337 | */ |
||
| 338 | public function split($duration): iterable |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Allows splitting an instance in smaller Period objects according to a given interval. |
||
| 355 | * |
||
| 356 | * The returned iterable Period set is ordered so that: |
||
| 357 | * <ul> |
||
| 358 | * <li>The first returned object MUST share the ending datepoint of the parent object.</li> |
||
| 359 | * <li>The last returned object MUST share the starting datepoint of the parent object.</li> |
||
| 360 | * <li>The last returned object MUST have a duration equal or lesser than the submitted interval.</li> |
||
| 361 | * <li>All returned objects except for the first one MUST end immediately before the previously returned object</li> |
||
| 362 | * </ul> |
||
| 363 | 3 | * |
|
| 364 | * @param DateInterval|Period|string|int $duration |
||
| 365 | 3 | * |
|
| 366 | * @return Period[] |
||
| 367 | 3 | */ |
|
| 368 | public function splitBackwards($duration): iterable |
||
| 382 | 3 | ||
| 383 | /** |
||
| 384 | 3 | * Computes the intersection between two instances. |
|
| 385 | * |
||
| 386 | * [--------------------) |
||
| 387 | * ∩ |
||
| 388 | * [----------) |
||
| 389 | * = |
||
| 390 | * [----) |
||
| 391 | * |
||
| 392 | * @throws Exception If both objects do not overlaps |
||
| 393 | */ |
||
| 394 | public function intersect(self $interval): self |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Computes the difference between two overlapsing instances. |
||
| 408 | * |
||
| 409 | * Returns an array containing the difference expressed as Period objects |
||
| 410 | * The array will always contains 2 elements: |
||
| 411 | * |
||
| 412 | 177 | * <ul> |
|
| 413 | * <li>an NULL filled array if both objects have the same datepoints</li> |
||
| 414 | 177 | * <li>one Period object and NULL if both objects share one datepoint</li> |
|
| 415 | * <li>two Period objects if both objects share no datepoint</li> |
||
| 416 | * </ul> |
||
| 417 | * |
||
| 418 | * [--------------------) |
||
| 419 | * - |
||
| 420 | * [-----------) |
||
| 421 | * = |
||
| 422 | * [--------------) + [-----) |
||
| 423 | * |
||
| 424 | * @throws Exception if both objects do not overlaps |
||
| 425 | 162 | */ |
|
| 426 | public function diff(self $interval): array |
||
| 447 | 33 | ||
| 448 | /** |
||
| 449 | * Computes the gap between two instances. |
||
| 450 | * |
||
| 451 | * [--------------------) |
||
| 452 | * + |
||
| 453 | * [----------) |
||
| 454 | * = |
||
| 455 | * [---) |
||
| 456 | * |
||
| 457 | * @throws Exception If both objects overlaps |
||
| 458 | */ |
||
| 459 | public function gap(self $interval): self |
||
| 471 | 24 | ||
| 472 | /** |
||
| 473 | * Returns an instance with the specified starting datepoint. |
||
| 474 | * |
||
| 475 | * This method MUST retain the state of the current instance, and return |
||
| 476 | * an instance that contains the specified starting datepoint. |
||
| 477 | * |
||
| 478 | * @param DateTimeInterface|int|string $datepoint |
||
| 479 | */ |
||
| 480 | public function startingOn($datepoint): self |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Returns an instance with the specified ending datepoint. |
||
| 492 | * |
||
| 493 | * This method MUST retain the state of the current instance, and return |
||
| 494 | * an instance that contains the specified ending datepoint. |
||
| 495 | * |
||
| 496 | * @param DateTimeInterface|int|string $datepoint |
||
| 497 | 12 | */ |
|
| 498 | public function endingOn($datepoint): self |
||
| 507 | |||
| 508 | 9 | /** |
|
| 509 | 9 | * Returns a new instance with a new ending datepoint. |
|
| 510 | 9 | * |
|
| 511 | * @param DateInterval|Period|int|string $duration |
||
| 512 | */ |
||
| 513 | public function withDurationAfterStart($duration): self |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Returns a new instance with a new starting datepoint. |
||
| 520 | * |
||
| 521 | * @param DateInterval|Period|int|string $duration |
||
| 522 | */ |
||
| 523 | public function withDurationBeforeEnd($duration): self |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Returns a new instance with a new starting datepoint |
||
| 530 | * moved forward or backward by the given interval. |
||
| 531 | * |
||
| 532 | * @param DateInterval|Period|int|string $duration |
||
| 533 | */ |
||
| 534 | public function moveStartDate($duration): self |
||
| 538 | 6 | ||
| 539 | /** |
||
| 540 | 6 | * Returns a new instance with a new ending datepoint |
|
| 541 | 6 | * moved forward or backward by the given interval. |
|
| 542 | 3 | * |
|
| 543 | * @param DateInterval|Period|int|string $duration |
||
| 544 | 6 | */ |
|
| 545 | public function moveEndDate($duration): self |
||
| 549 | |||
| 550 | /** |
||
| 551 | * Returns a new instance where the datepoints |
||
| 552 | * are moved forwards or backward simultaneously by the given DateInterval. |
||
| 553 | * |
||
| 554 | * This method MUST retain the state of the current instance, and return |
||
| 555 | * an instance that contains the specified new datepoints. |
||
| 556 | * |
||
| 557 | * @param DateInterval|Period|int|string $duration |
||
| 558 | 3 | */ |
|
| 559 | public function move($duration): self |
||
| 569 | |||
| 570 | /** |
||
| 571 | * Returns an instance where the given DateInterval is simultaneously |
||
| 572 | * substracted from the starting datepoint and added to the ending datepoint. |
||
| 573 | 6 | * |
|
| 574 | * This method MUST retain the state of the current instance, and return |
||
| 575 | 6 | * an instance that contains the specified new datepoints. |
|
| 576 | 6 | * |
|
| 577 | 6 | * Depending on the duration value, the resulting instance duration will be expanded or shrinked. |
|
| 578 | * |
||
| 579 | * @param DateInterval|Period|int|string $duration |
||
| 580 | 6 | */ |
|
| 581 | 6 | public function expand($duration): self |
|
| 591 | |||
| 592 | /** |
||
| 593 | * Returns the difference between two instances expressed in seconds. |
||
| 594 | */ |
||
| 595 | public function timestampIntervalDiff(self $interval): float |
||
| 599 | 21 | ||
| 600 | /** |
||
| 601 | 21 | * Returns the difference between two instances expressed in DateInterval. |
|
| 602 | */ |
||
| 603 | public function dateIntervalDiff(self $interval): DateInterval |
||
| 607 | |||
| 608 | /** |
||
| 609 | * Merges one or more instances to return a new instance. |
||
| 610 | * The resulting instance represents the largest duration possible. |
||
| 611 | * |
||
| 612 | 9 | * @param Period ...$intervals |
|
| 613 | */ |
||
| 614 | 9 | public function merge(self $interval, self ...$intervals): self |
|
| 630 | } |
||
| 631 |