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:
| 1 | <?php |
||
| 9 | class Dates |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * @var DatesConfig $config |
||
| 13 | */ |
||
| 14 | private $config; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var DateTime $startDate |
||
| 18 | */ |
||
| 19 | private $startDate; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var DateTime $endDate |
||
| 23 | */ |
||
| 24 | private $endDate; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Dates constructor. |
||
| 28 | * |
||
| 29 | * @param DatesConfig|null $config |
||
| 30 | */ |
||
| 31 | public function __construct(DatesConfig $config = null) |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Returns an array of week intervals |
||
| 38 | * |
||
| 39 | * @param DateTime $startDate |
||
| 40 | * @param DateTime $endDate |
||
| 41 | * @param bool $full include non-business days if true |
||
| 42 | * @param bool $inclusive decide whether we should expand the days until the week start or not |
||
| 43 | * |
||
| 44 | * @return WeekInterval[] |
||
| 45 | * @throws Exception |
||
| 46 | */ |
||
| 47 | public function getWeeksBreak(DateTime $startDate, DateTime $endDate, $full = false, $inclusive = false) |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Returns a week interval, based on year week and year month |
||
| 66 | * Both parameters are necessary because a given week can have |
||
| 67 | * days from two different months, and in this case, we might |
||
| 68 | * want to pull days from one month, another month, or even |
||
| 69 | * the entire week, regardless the month |
||
| 70 | * |
||
| 71 | * @param string $yearWeek |
||
| 72 | * @param string $yearMonth |
||
| 73 | * @param bool $full |
||
| 74 | * @param bool $inclusive |
||
| 75 | * @return WeekInterval |
||
| 76 | * @throws Exception |
||
| 77 | */ |
||
| 78 | public function unwrapWeek( |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @throws Exception |
||
| 102 | */ |
||
| 103 | private function adjustDayLightSavingsTime() :void |
||
| 115 | |||
| 116 | private function addPaddingDays() |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @param bool $full |
||
| 137 | * @return array |
||
| 138 | * |
||
| 139 | * @throws Exception |
||
| 140 | */ |
||
| 141 | private function makeIntervals(bool $full): array |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @return array |
||
| 152 | * @throws Exception |
||
| 153 | */ |
||
| 154 | private function makeFull() :array |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @return array |
||
| 189 | * @throws Exception |
||
| 190 | */ |
||
| 191 | private function makeBusinessOnly() : array |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @param string $day |
||
| 228 | * @return bool |
||
| 229 | */ |
||
| 230 | private function isInRange($day) :bool |
||
| 240 | } |
||
| 241 |
This check looks for function calls that miss required arguments.