Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like BusinessHours 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 BusinessHours, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class BusinessHours implements BusinessHoursInterface |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * The days. |
||
| 15 | * |
||
| 16 | * @var DayInterface[] |
||
| 17 | */ |
||
| 18 | protected $days; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * The time zone. |
||
| 22 | * |
||
| 23 | * @var \DateTimeZone |
||
| 24 | */ |
||
| 25 | protected $timezone; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Constructor. |
||
| 29 | * |
||
| 30 | * @param DayInterface[] $days |
||
| 31 | * @param \DateTimeZone|null $timezone |
||
| 32 | */ |
||
| 33 | public function __construct(array $days, \DateTimeZone $timezone = null) |
||
| 38 | |||
| 39 | /** |
||
| 40 | * {@inheritdoc} |
||
| 41 | */ |
||
| 42 | public function getTimezone() |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Get the days. |
||
| 49 | * |
||
| 50 | * @return DayInterface[] |
||
| 51 | */ |
||
| 52 | public function getDays() |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Add a set of days. |
||
| 59 | * |
||
| 60 | * @param DayInterface[] $days The days. |
||
| 61 | * @throws \InvalidArgumentException If no days are passed. |
||
| 62 | */ |
||
| 63 | public function setDays(array $days) |
||
| 75 | |||
| 76 | /** |
||
| 77 | * {@inheritdoc} |
||
| 78 | */ |
||
| 79 | public function within(\DateTime $date) |
||
| 90 | |||
| 91 | /** |
||
| 92 | * {@inheritdoc} |
||
| 93 | */ |
||
| 94 | public function getNextChangeDateTime(\DateTime $date = null) |
||
| 108 | |||
| 109 | /** |
||
| 110 | * {@inheritdoc} |
||
| 111 | */ |
||
| 112 | public function closestDateInterval(\DateTime $date) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * {@inheritdoc} |
||
| 122 | */ |
||
| 123 | public function jsonSerialize() |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Get the closest business hours date interval after the given date. |
||
| 133 | * |
||
| 134 | * @param \DateTime $date |
||
| 135 | * @return DateTimeInterval |
||
| 136 | */ |
||
| 137 | private function getClosestDateIntervalAfter(\DateTime $date) |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Get the business hours date after the given date (excluding holidays). |
||
| 189 | * |
||
| 190 | * @param \DateTime $date |
||
| 191 | * @return \DateTime |
||
| 192 | */ |
||
| 193 | private function getDateAfter(\DateTime $date) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Get the closest interval endpoint after the given date. |
||
| 210 | * |
||
| 211 | * @param \DateTime $date |
||
| 212 | * @return DateTimeInterval |
||
| 213 | */ |
||
| 214 | private function getClosestInterval(\DateTime $date) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Get the closest business hours day before a given day number (including it). |
||
| 245 | * |
||
| 246 | * @param integer $dayNumber |
||
| 247 | * @return DayInterface|null |
||
| 248 | */ |
||
| 249 | private function getClosestDayBefore($dayNumber) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Get the closest business hours day after a given day number (including it). |
||
| 260 | * |
||
| 261 | * @param integer $dayNumber |
||
| 262 | * @return DayInterface|null |
||
| 263 | */ |
||
| 264 | private function getClosestDayAfter($dayNumber) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Get the business hours day before the day number. |
||
| 275 | * |
||
| 276 | * @param integer $dayNumber |
||
| 277 | * @return DayInterface|null |
||
| 278 | */ |
||
| 279 | View Code Duplication | private function getDayBefore($dayNumber) |
|
| 293 | |||
| 294 | /** |
||
| 295 | * Get the business hours day after the day number. |
||
| 296 | * |
||
| 297 | * @param integer $dayNumber |
||
| 298 | * @return DayInterface|null |
||
| 299 | */ |
||
| 300 | View Code Duplication | private function getDayAfter($dayNumber) |
|
| 314 | |||
| 315 | /** |
||
| 316 | * Get the day corresponding to the day number. |
||
| 317 | * |
||
| 318 | * @param integer $dayNumber |
||
| 319 | * @return DayInterface|null |
||
| 320 | */ |
||
| 321 | private function getDay($dayNumber) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Add a day. |
||
| 328 | * |
||
| 329 | * @param DayInterface $day The day. |
||
| 330 | */ |
||
| 331 | private function addDay(DayInterface $day) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Clone. |
||
| 338 | */ |
||
| 339 | public function __clone() |
||
| 348 | } |
||
| 349 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.