Complex classes like DateTimeRange 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 DateTimeRange, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | final class DateTimeRange implements \IteratorAggregate |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * DateTime object representing the start date of the iterator |
||
| 21 | * |
||
| 22 | * @var DateTime |
||
| 23 | * @since 2.0.0 |
||
| 24 | */ |
||
| 25 | protected $start; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * DateTime object representing the end date of the iterator |
||
| 29 | * |
||
| 30 | * @var DateTime |
||
| 31 | * @since 2.0.0 |
||
| 32 | */ |
||
| 33 | protected $end; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Interval between dates |
||
| 37 | * |
||
| 38 | * @var DateInterval |
||
| 39 | * @since 2.0.0 |
||
| 40 | */ |
||
| 41 | protected $interval; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Constructor. |
||
| 45 | * |
||
| 46 | * @param DateTime $start The start date. |
||
| 47 | * @param DateTime $end The end date. |
||
| 48 | * @param DateInterval $interval The interval between adjacent dates. |
||
| 49 | * |
||
| 50 | * @since 2.0.0 |
||
| 51 | * @throws \InvalidArgumentException |
||
| 52 | */ |
||
| 53 | 32 | public function __construct(DateTime $start, DateTime $end, DateInterval $interval) |
|
| 64 | |||
| 65 | /** |
||
| 66 | * Creates a DateTimeRange object from the start date for the given amount od dates. |
||
| 67 | * |
||
| 68 | * @param DateTime $start The start date. |
||
| 69 | * @param integer $amount The amount of dates included in a range. |
||
| 70 | * @param DateInterval $interval The interval between adjacent dates. |
||
| 71 | * |
||
| 72 | * @return DateTimeRange |
||
| 73 | * |
||
| 74 | * @since 2.0.0 |
||
| 75 | */ |
||
| 76 | 3 | public static function from(DateTime $start, $amount, DateInterval $interval) |
|
| 82 | |||
| 83 | /** |
||
| 84 | * Creates a DateTimeRange object to the end date for the given amount od dates. |
||
| 85 | * |
||
| 86 | * @param DateTime $end The end date. |
||
| 87 | * @param integer $amount The amount of dates included in a range. |
||
| 88 | * @param DateInterval $interval The interval between adjacent dates. |
||
| 89 | * |
||
| 90 | * @return DateTimeRange |
||
| 91 | * |
||
| 92 | * @since 2.0.0 |
||
| 93 | */ |
||
| 94 | 19 | public static function to(DateTime $end, $amount, DateInterval $interval) |
|
| 100 | |||
| 101 | /** |
||
| 102 | * Returns an empty range. |
||
| 103 | * |
||
| 104 | * @return DateTimeRange |
||
| 105 | * |
||
| 106 | * @since 2.0.0 |
||
| 107 | */ |
||
| 108 | 4 | public static function emptyRange() |
|
| 112 | |||
| 113 | /** |
||
| 114 | * Returns the start date. |
||
| 115 | * |
||
| 116 | * @return DateTime |
||
| 117 | * |
||
| 118 | * @since 2.0.0 |
||
| 119 | */ |
||
| 120 | 20 | public function start() |
|
| 124 | |||
| 125 | /** |
||
| 126 | * Returns the end date. |
||
| 127 | * |
||
| 128 | * @return DateTime |
||
| 129 | * |
||
| 130 | * @since 2.0.0 |
||
| 131 | */ |
||
| 132 | 11 | public function end() |
|
| 136 | |||
| 137 | /** |
||
| 138 | * Checks if a range is empty. |
||
| 139 | * |
||
| 140 | * @return boolean |
||
| 141 | * |
||
| 142 | * @since 2.0.0 |
||
| 143 | */ |
||
| 144 | 15 | public function isEmpty() |
|
| 148 | |||
| 149 | /** |
||
| 150 | * Checks if the given date is included in the range. |
||
| 151 | * |
||
| 152 | * @param DateTime $datetime The date to compare to. |
||
| 153 | * |
||
| 154 | * @return boolean |
||
| 155 | * |
||
| 156 | * @since 2.0.0 |
||
| 157 | */ |
||
| 158 | 80 | public function includes(DateTime $datetime) |
|
| 162 | |||
| 163 | /** |
||
| 164 | * Checks if ranges are equal. |
||
| 165 | * |
||
| 166 | * @param DateTimeRange $range The range to compare to. |
||
| 167 | * |
||
| 168 | * @return boolean |
||
| 169 | * |
||
| 170 | * @since 2.0.0 |
||
| 171 | */ |
||
| 172 | 16 | public function equals(DateTimeRange $range) |
|
| 176 | |||
| 177 | /** |
||
| 178 | * Checks if ranges overlap with each other. |
||
| 179 | * |
||
| 180 | * @param DateTimeRange $range The range to compare to. |
||
| 181 | * |
||
| 182 | * @return boolean |
||
| 183 | * |
||
| 184 | * @since 2.0.0 |
||
| 185 | */ |
||
| 186 | 50 | public function overlaps(DateTimeRange $range) |
|
| 190 | |||
| 191 | /** |
||
| 192 | * Checks if the given range is included in the current one. |
||
| 193 | * |
||
| 194 | * @param DateTimeRange $range The range to compare to. |
||
| 195 | * |
||
| 196 | * @return boolean |
||
| 197 | * |
||
| 198 | * @since 2.0.0 |
||
| 199 | */ |
||
| 200 | 42 | public function includesRange(DateTimeRange $range) |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Returns a gap range between two ranges. |
||
| 207 | * |
||
| 208 | * @param DateTimeRange $range The range to compare to. |
||
| 209 | * |
||
| 210 | * @return DateTimeRange |
||
| 211 | * |
||
| 212 | * @since 2.0.0 |
||
| 213 | */ |
||
| 214 | 18 | public function gap(DateTimeRange $range) |
|
| 234 | |||
| 235 | /** |
||
| 236 | * Checks if ranges abuts with each other. |
||
| 237 | * |
||
| 238 | * @param DateTimeRange $range The range to compare to. |
||
| 239 | * |
||
| 240 | * @return boolean |
||
| 241 | * |
||
| 242 | * @since 2.0.0 |
||
| 243 | */ |
||
| 244 | 24 | public function abuts(DateTimeRange $range) |
|
| 248 | |||
| 249 | /** |
||
| 250 | * Returns an array of dates which are included in the current range. |
||
| 251 | * |
||
| 252 | * @return DateTime[] |
||
| 253 | * |
||
| 254 | * @since 2.0.0 |
||
| 255 | */ |
||
| 256 | 7 | public function toArray() |
|
| 267 | |||
| 268 | /** |
||
| 269 | * Returns an external iterator. |
||
| 270 | * |
||
| 271 | * @return DateTimeIterator |
||
| 272 | * |
||
| 273 | * @since 2.0.0 |
||
| 274 | */ |
||
| 275 | 7 | public function getIterator() |
|
| 279 | |||
| 280 | /** |
||
| 281 | * Returns string representation of the range. |
||
| 282 | * |
||
| 283 | * @return string |
||
| 284 | * |
||
| 285 | * @since 2.0.0 |
||
| 286 | */ |
||
| 287 | 1 | public function __toString() |
|
| 291 | |||
| 292 | /** |
||
| 293 | * Returns a range which is created by combination of given ranges. There can't |
||
| 294 | * be a gap between given ranges. |
||
| 295 | * |
||
| 296 | * @param DateTimeRange[] $ranges An array of ranges to combine. |
||
| 297 | * |
||
| 298 | * @return DateTimeRange |
||
| 299 | * |
||
| 300 | * @since 2.0.0 |
||
| 301 | * @throws \InvalidArgumentException |
||
| 302 | */ |
||
| 303 | 10 | public static function combination(array $ranges) |
|
| 312 | |||
| 313 | /** |
||
| 314 | * Checks if ranges are contiguous. |
||
| 315 | * |
||
| 316 | * @param DateTimeRange[] $ranges An array of ranges to combine. |
||
| 317 | * |
||
| 318 | * @return boolean |
||
| 319 | * |
||
| 320 | * @since 2.0.0 |
||
| 321 | */ |
||
| 322 | 15 | public static function isContiguous(array $ranges) |
|
| 336 | |||
| 337 | /** |
||
| 338 | * Sorts an array of ranges. |
||
| 339 | * |
||
| 340 | * @param DateTimeRange[] $ranges An array of ranges to sort. |
||
| 341 | * |
||
| 342 | * @return DateTimeRange[] |
||
| 343 | * |
||
| 344 | * @since 2.0.0 |
||
| 345 | */ |
||
| 346 | 15 | protected static function sortArrayOfRanges(array $ranges) |
|
| 352 | |||
| 353 | /** |
||
| 354 | * Compares two objects. |
||
| 355 | * |
||
| 356 | * @param DateTimeRange $a Base object. |
||
| 357 | * @param DateTimeRange $b Object to compare to. |
||
| 358 | * |
||
| 359 | * @return integer |
||
| 360 | * |
||
| 361 | * @since 2.0.0 |
||
| 362 | * @throws \InvalidArgumentException |
||
| 363 | */ |
||
| 364 | 15 | private static function compare(DateTimeRange $a, DateTimeRange $b) |
|
| 388 | |||
| 389 | /** |
||
| 390 | * Builds the date. |
||
| 391 | * |
||
| 392 | * @param DateTime $base The base date. |
||
| 393 | * @param integer $amount The amount of dates included in a range. |
||
| 394 | * @param DateInterval $interval The interval between adjacent dates. |
||
| 395 | * @param boolean $byAddition Should build the final date using addition or subtraction? |
||
| 396 | * |
||
| 397 | * @return DateTime |
||
| 398 | * |
||
| 399 | * @since 2.0.0 |
||
| 400 | * @throws \InvalidArgumentException |
||
| 401 | */ |
||
| 402 | 5 | private static function buildDatetime(DateTime $base, $amount, DateInterval $interval, $byAddition = true) |
|
| 417 | } |
||
| 418 |