Complex classes like TimeSlot 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 TimeSlot, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class TimeSlot { |
||
| 25 | |||
| 26 | /** |
||
| 27 | * End date. |
||
| 28 | * |
||
| 29 | * @var DateTime |
||
| 30 | */ |
||
| 31 | private $endDate; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * StartDate. |
||
| 35 | * |
||
| 36 | * @var DateTime |
||
| 37 | */ |
||
| 38 | private $startDate; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Time slots. |
||
| 42 | * |
||
| 43 | * @var TimeSlot[] |
||
| 44 | */ |
||
| 45 | private $timeSlots; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Constructor. |
||
| 49 | * |
||
| 50 | * @param DateTime $startDate The start date. |
||
| 51 | * @param DateTime $endDate The end date. |
||
| 52 | * @throws IllegalArgumentException Throws an illegal argument exception. |
||
| 53 | */ |
||
| 54 | public function __construct(DateTime $startDate, DateTime $endDate) { |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Add a time slot. |
||
| 65 | * |
||
| 66 | * @param TimeSlot $timeSlot The time slot. |
||
| 67 | * @return TimeSlot Returns this time slot. |
||
| 68 | */ |
||
| 69 | public function addTimeSlot(TimeSlot $timeSlot) { |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Determines if a time slot A contains a time slot b. |
||
| 76 | * |
||
| 77 | * @param TimeSlot $a The time slot A. |
||
| 78 | * @param TimeSlot $b The time slot B. |
||
| 79 | * @return bool Returns true in case of success, false otherwise. |
||
| 80 | */ |
||
| 81 | public static function contains(TimeSlot $a, TimeSlot $b) { |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Determines if two time slots are equals. |
||
| 89 | * |
||
| 90 | * @param TimeSlot $a The time slot A. |
||
| 91 | * @param TimeSlot $b The time slot B. |
||
| 92 | * @return boolean Returns true in case of success, false otherwise. |
||
| 93 | */ |
||
| 94 | public static function equals(TimeSlot $a, TimeSlot $b) { |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Full join two time slots. |
||
| 124 | * |
||
| 125 | * @param TimeSlot $a The time slot A. |
||
| 126 | * @param TimeSlot $b The time slot B. |
||
| 127 | * @return TimeSlot|null Returns a time slot in case of success, null otherwise. |
||
| 128 | */ |
||
| 129 | public static function fullJoin(TimeSlot $a, TimeSlot $b) { |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Full join two time slots without time slots intersection. |
||
| 146 | * |
||
| 147 | * @param TimeSlot $a The time slot A. |
||
| 148 | * @param TimeSlot $b The time slot B. |
||
| 149 | * @return TimeSlot[] Returns the time slots in case of success, null otherwise. |
||
| 150 | */ |
||
| 151 | public static function fullJoinWithout(TimeSlot $a, TimeSlot $b) { |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Get the duration. |
||
| 174 | * |
||
| 175 | * @return int Returns the duration. |
||
| 176 | */ |
||
| 177 | public function getDuration() { |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Get the end date. |
||
| 183 | * |
||
| 184 | * @return DateTime Returns the end date. |
||
| 185 | */ |
||
| 186 | public function getEndDate() { |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Get the start date. |
||
| 192 | * |
||
| 193 | * @return DateTime Returns the start date. |
||
| 194 | */ |
||
| 195 | public function getStartDate() { |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Get the time slots. |
||
| 201 | * |
||
| 202 | * @return TimeSlot[] Returns the time slots. |
||
| 203 | */ |
||
| 204 | public function getTimeSlots() { |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Determines if a time slot A has full join with time slot B. |
||
| 210 | * |
||
| 211 | * @param TimeSlot $a The time slot A. |
||
| 212 | * @param TimeSlot $b The time slot B. |
||
| 213 | * @return bool Returns true in case of success, false otherwise. |
||
| 214 | */ |
||
| 215 | public static function hasFullJoin(TimeSlot $a, TimeSlot $b) { |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Determines if a time slot A has an inner join with time slot B. |
||
| 221 | * |
||
| 222 | * @param TimeSlot $a The time slot A. |
||
| 223 | * @param TimeSlot $b The time slot B. |
||
| 224 | * @return bool Returns true in case of success, false otherwise. |
||
| 225 | */ |
||
| 226 | public static function hasInnerJoin(TimeSlot $a, TimeSlot $b) { |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Inner join two time slots. |
||
| 236 | * |
||
| 237 | * @param TimeSlot $a The time slot A. |
||
| 238 | * @param TimeSlot $b The time slot B. |
||
| 239 | * @return TimeSlot|null Returns a time slot in case of success, null otherwise. |
||
| 240 | */ |
||
| 241 | public static function innerJoin(TimeSlot $a, TimeSlot $b) { |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Left join two time slots. |
||
| 258 | * |
||
| 259 | * @param TimeSlot $a The time slot A. |
||
| 260 | * @param TimeSlot $b The time slot B. |
||
| 261 | * @return TimeSlot Returns the time slot in case of success, null otherwise. |
||
| 262 | */ |
||
| 263 | public static function leftJoin(TimeSlot $a, TimeSlot $b) { |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Left join two time slots without time slot B intersection. |
||
| 276 | * |
||
| 277 | * @param TimeSlot $a The time slot A. |
||
| 278 | * @param TimeSlot $b The time slot B. |
||
| 279 | * @return TimeSlot[] Returns the time slots in case of success, null otherwise. |
||
| 280 | */ |
||
| 281 | public static function leftJoinWithout(TimeSlot $a, TimeSlot $b) { |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Remove a time slot. |
||
| 308 | * |
||
| 309 | * @param TimeSlot $timeSlot The time slot. |
||
| 310 | * @return TimeSlot Returns this time slot. |
||
| 311 | */ |
||
| 312 | public function removeTimeSlot(TimeSlot $timeSlot) { |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Right join two time slots. |
||
| 324 | * |
||
| 325 | * @param TimeSlot $a The time slot A. |
||
| 326 | * @param TimeSlot $b The time slot B. |
||
| 327 | * @return TimeSlot Returns the time slot in case of success, null otherwise. |
||
| 328 | */ |
||
| 329 | public static function rightJoin(TimeSlot $a, TimeSlot $b) { |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Right join two time slots without time slot B intersection. |
||
| 335 | * |
||
| 336 | * @param TimeSlot $a The time slot A. |
||
| 337 | * @param TimeSlot $b The time slot B. |
||
| 338 | * @return TimeSlot[] Returns the time slots in case of success, null otherwise. |
||
| 339 | */ |
||
| 340 | public static function rightJoinWithout(TimeSlot $a, TimeSlot $b) { |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Set the end date. |
||
| 346 | * |
||
| 347 | * @param DateTime $endDate The end date. |
||
| 348 | * @return TimeSlot Returns this time slot. |
||
| 349 | */ |
||
| 350 | protected function setEndDate(DateTime $endDate) { |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Set the start date. |
||
| 357 | * |
||
| 358 | * @param DateTime $startDate The start date. |
||
| 359 | * @return TimeSlot Returns this time slot. |
||
| 360 | */ |
||
| 361 | protected function setStartDate(DateTime $startDate) { |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Set the time slots. |
||
| 368 | * |
||
| 369 | * @param TimeSlot[] $timeSlots The time slots. |
||
| 370 | * @return TimeSlot Returns this time slot. |
||
| 371 | */ |
||
| 372 | protected function setTimeSlots($timeSlots) { |
||
| 376 | |||
| 377 | } |
||
| 378 |