Complex classes like DateRange 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 DateRange, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class DateRange |
||
| 18 | { |
||
| 19 | /** @var DateTime */ |
||
| 20 | private $startDate; |
||
| 21 | |||
| 22 | /** @var DateTime */ |
||
| 23 | private $endDate; |
||
| 24 | |||
| 25 | /** @var DateRangeFormatter */ |
||
| 26 | private $formatter; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @param DateTime|null $startDate |
||
| 30 | * @param DateTime|null $endDate |
||
| 31 | * @throws \InvalidArgumentException |
||
| 32 | */ |
||
| 33 | 18 | public function __construct(DateTime $startDate = null, DateTime $endDate = null) |
|
| 44 | |||
| 45 | /** |
||
| 46 | * @return DateTime|null |
||
| 47 | */ |
||
| 48 | 26 | public function getStartDate() |
|
| 52 | |||
| 53 | /** |
||
| 54 | * @return DateTime|null |
||
| 55 | */ |
||
| 56 | 22 | public function getEndDate() |
|
| 60 | |||
| 61 | /** |
||
| 62 | * @param DateTime $date |
||
| 63 | * @return bool |
||
| 64 | */ |
||
| 65 | 8 | public function containsDate(DateTime $date) : bool |
|
| 75 | |||
| 76 | /** |
||
| 77 | * @param DateRange $dateRange |
||
| 78 | * @return bool |
||
| 79 | */ |
||
| 80 | 10 | public function containsDateRange(DateRange $dateRange) : bool |
|
| 90 | |||
| 91 | /** |
||
| 92 | * @param DateRange $dateRange |
||
| 93 | * @return bool |
||
| 94 | */ |
||
| 95 | 6 | public function overlapsDateRange(DateRange $dateRange) : bool |
|
| 99 | |||
| 100 | /** |
||
| 101 | * @param DateTime $date |
||
| 102 | * @return bool |
||
| 103 | */ |
||
| 104 | 5 | public function isBefore(DateTime $date) : bool |
|
| 112 | |||
| 113 | /** |
||
| 114 | * @return bool |
||
| 115 | */ |
||
| 116 | 1 | public function isNow() : bool |
|
| 120 | |||
| 121 | /** |
||
| 122 | * @param DateTime $date |
||
| 123 | * @return bool |
||
| 124 | */ |
||
| 125 | 5 | public function isAfter(DateTime $date) : bool |
|
| 133 | |||
| 134 | /** |
||
| 135 | * @param DateRange $dateRange |
||
| 136 | * @return DateRange|null |
||
| 137 | */ |
||
| 138 | 9 | public function intersect(DateRange $dateRange) |
|
| 149 | |||
| 150 | /** |
||
| 151 | * @param string $nullPlaceholder |
||
| 152 | * @return string |
||
| 153 | */ |
||
| 154 | 10 | public function getShort(string $nullPlaceholder = 'Ongoing') : string |
|
| 158 | |||
| 159 | /** |
||
| 160 | * @param string $nullPlaceholder |
||
| 161 | * @return string |
||
| 162 | */ |
||
| 163 | 21 | public function getLong(string $nullPlaceholder = 'Ongoing') : string |
|
| 167 | |||
| 168 | /** |
||
| 169 | * @return DateTime[] |
||
| 170 | * @throws InvalidArgumentException |
||
| 171 | */ |
||
| 172 | 4 | public function getAllDates() : array |
|
| 191 | |||
| 192 | /** |
||
| 193 | * @return Time|null |
||
| 194 | */ |
||
| 195 | 4 | public function getDuration() |
|
| 203 | |||
| 204 | /** |
||
| 205 | * @return string |
||
| 206 | */ |
||
| 207 | 7 | public function __toString() : string |
|
| 211 | |||
| 212 | /** |
||
| 213 | * |
||
| 214 | */ |
||
| 215 | 1 | public function __clone() |
|
| 220 | } |
||
| 221 |