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 |
||
| 17 | View Code Duplication | class WeekInterval implements IntervalInterface |
|
|
|
|||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | const REGEXP = '/^ |
||
| 23 | (?:\(|\[) # start type char |
||
| 24 | \s* |
||
| 25 | (?<start>\d{4}-\d{2}-\d{2}) # start point |
||
| 26 | \s*,\s* # separator |
||
| 27 | (?<end>\d{4}-\d{2}-\d{2}) # end point |
||
| 28 | \s* |
||
| 29 | (?:\)|\]) # end type char |
||
| 30 | $/x'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var IntervalType |
||
| 34 | */ |
||
| 35 | private $type; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var WeekIntervalComparator |
||
| 39 | */ |
||
| 40 | private $comparator; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var WeekIntervalPoint |
||
| 44 | */ |
||
| 45 | private $start; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var WeekIntervalPoint |
||
| 49 | */ |
||
| 50 | private $end; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @param WeekIntervalPoint $start |
||
| 54 | * @param WeekIntervalPoint $end |
||
| 55 | * @param IntervalType $type |
||
| 56 | */ |
||
| 57 | private function __construct(WeekIntervalPoint $start, WeekIntervalPoint $end, IntervalType $type) |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @param \DateTime $start |
||
| 71 | * @param \DateTime $end |
||
| 72 | * @param IntervalType $type |
||
| 73 | * |
||
| 74 | * @return self |
||
| 75 | */ |
||
| 76 | public static function create(\DateTime $start, \DateTime $end, IntervalType $type) |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @param \DateTime $start |
||
| 83 | * @param \DateTime $end |
||
| 84 | * |
||
| 85 | * @return self |
||
| 86 | */ |
||
| 87 | public static function closed(\DateTime $start, \DateTime $end) |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @param \DateTime $start |
||
| 94 | * @param \DateTime $end |
||
| 95 | * |
||
| 96 | * @return self |
||
| 97 | */ |
||
| 98 | public static function halfClosed(\DateTime $start, \DateTime $end) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @param \DateTime $start |
||
| 105 | * @param \DateTime $end |
||
| 106 | * |
||
| 107 | * @return self |
||
| 108 | */ |
||
| 109 | public static function halfOpen(\DateTime $start, \DateTime $end) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @param \DateTime $start |
||
| 116 | * @param \DateTime $end |
||
| 117 | * |
||
| 118 | * @return self |
||
| 119 | */ |
||
| 120 | public static function open(\DateTime $start, \DateTime $end) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Create interval from string. |
||
| 127 | * |
||
| 128 | * Example formats for all interval types: |
||
| 129 | * [2016-12-09, 2016-12-21] |
||
| 130 | * (2015-03-07, 2015-10-19] |
||
| 131 | * [2014-09-11, 2015-02-08) |
||
| 132 | * (2013-10-02, 2013-10-30) |
||
| 133 | * |
||
| 134 | * Spaces are ignored in format. |
||
| 135 | * |
||
| 136 | * @param string $string |
||
| 137 | * |
||
| 138 | * @return self |
||
| 139 | */ |
||
| 140 | public static function fromString($string) |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @param \DateTime $point |
||
| 155 | * |
||
| 156 | * @return bool |
||
| 157 | */ |
||
| 158 | public function contains(\DateTime $point) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @param WeekInterval $interval |
||
| 165 | * @param bool $check_interval_type |
||
| 166 | * |
||
| 167 | * @return bool |
||
| 168 | */ |
||
| 169 | public function intersect(WeekInterval $interval, $check_interval_type = true) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @param WeekInterval $interval |
||
| 176 | * |
||
| 177 | * @return WeekInterval|null |
||
| 178 | */ |
||
| 179 | public function intersectInterval(WeekInterval $interval) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * The point is before the interval |
||
| 186 | * |
||
| 187 | * @param \DateTime $point |
||
| 188 | * |
||
| 189 | * @return bool |
||
| 190 | */ |
||
| 191 | public function before(\DateTime $point) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * The point is after the interval |
||
| 198 | * |
||
| 199 | * @param \DateTime $point |
||
| 200 | * |
||
| 201 | * @return bool |
||
| 202 | */ |
||
| 203 | public function after(\DateTime $point) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @return IntervalType |
||
| 210 | */ |
||
| 211 | public function type() |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @return \DateTime |
||
| 218 | */ |
||
| 219 | public function start() |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @return \DateTime |
||
| 226 | */ |
||
| 227 | public function end() |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @return WeekIntervalPoint |
||
| 234 | */ |
||
| 235 | public function startPoint() |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @return WeekIntervalPoint |
||
| 242 | */ |
||
| 243 | public function endPoint() |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @return string |
||
| 250 | */ |
||
| 251 | public function __toString() |
||
| 255 | } |
||
| 256 |
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.