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 |
||
20 | View Code Duplication | class MonthInterval implements ComparableIntervalInterface |
|
|
|||
21 | { |
||
22 | /** |
||
23 | * @var string |
||
24 | */ |
||
25 | const REGEXP = '/^ |
||
26 | (?:\(|\[) # start type char |
||
27 | \s* |
||
28 | (?<start>\d{4}\/\d{2}) # start point |
||
29 | \s*,\s* # separator |
||
30 | (?<end>\d{4}\/\d{2}) # end point |
||
31 | \s* |
||
32 | (?:\)|\]) # end type char |
||
33 | $/x'; |
||
34 | |||
35 | /** |
||
36 | * @var IntervalType |
||
37 | */ |
||
38 | private $type; |
||
39 | |||
40 | /** |
||
41 | * @var IntervalComparator |
||
42 | */ |
||
43 | private $comparator; |
||
44 | |||
45 | /** |
||
46 | * @var MonthIntervalPoint |
||
47 | */ |
||
48 | private $start; |
||
49 | |||
50 | /** |
||
51 | * @var MonthIntervalPoint |
||
52 | */ |
||
53 | private $end; |
||
54 | |||
55 | /** |
||
56 | * @param MonthIntervalPoint $start |
||
57 | * @param MonthIntervalPoint $end |
||
58 | * @param IntervalType $type |
||
59 | */ |
||
60 | 4 | private function __construct(MonthIntervalPoint $start, MonthIntervalPoint $end, IntervalType $type) |
|
71 | |||
72 | /** |
||
73 | * @param \DateTime $start |
||
74 | * @param \DateTime $end |
||
75 | * @param IntervalType $type |
||
76 | * |
||
77 | * @return self |
||
78 | */ |
||
79 | 4 | public static function create(\DateTime $start, \DateTime $end, IntervalType $type) |
|
83 | |||
84 | /** |
||
85 | * @param \DateTime $start |
||
86 | * @param \DateTime $end |
||
87 | * |
||
88 | * @return self |
||
89 | */ |
||
90 | 1 | public static function closed(\DateTime $start, \DateTime $end) |
|
94 | |||
95 | /** |
||
96 | * @param \DateTime $start |
||
97 | * @param \DateTime $end |
||
98 | * |
||
99 | * @return self |
||
100 | */ |
||
101 | public static function halfClosed(\DateTime $start, \DateTime $end) |
||
105 | |||
106 | /** |
||
107 | * @param \DateTime $start |
||
108 | * @param \DateTime $end |
||
109 | * |
||
110 | * @return self |
||
111 | */ |
||
112 | public static function halfOpen(\DateTime $start, \DateTime $end) |
||
116 | |||
117 | /** |
||
118 | * @param \DateTime $start |
||
119 | * @param \DateTime $end |
||
120 | * |
||
121 | * @return self |
||
122 | */ |
||
123 | 1 | public static function open(\DateTime $start, \DateTime $end) |
|
127 | |||
128 | /** |
||
129 | * Create interval from string. |
||
130 | * |
||
131 | * Example formats for all interval types: |
||
132 | * [2016/12, 2016/12] |
||
133 | * (2015/03, 2015/10] |
||
134 | * [2014/09, 2015/02) |
||
135 | * (2013/10, 2013/10) |
||
136 | * |
||
137 | * Spaces are ignored in format. |
||
138 | * |
||
139 | * @param string $string |
||
140 | * |
||
141 | * @return self |
||
142 | */ |
||
143 | 2 | public static function fromString($string) |
|
144 | { |
||
145 | 2 | if (!preg_match(self::REGEXP, $string, $match)) { |
|
146 | throw InvalidIntervalFormatException::create('[YYYY/MM, YYYY/MM]', $string); |
||
147 | } |
||
148 | |||
149 | 2 | return self::create( |
|
150 | 2 | new \DateTime($match['start'].'/01'), |
|
151 | 2 | new \DateTime($match['end'].'/01'), |
|
152 | 2 | IntervalType::fromString($string) |
|
153 | ); |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * Checks if this interval is equal to the specified interval. |
||
158 | * |
||
159 | * @param MonthInterval $interval |
||
160 | * |
||
161 | * @return bool |
||
162 | */ |
||
163 | public function equal(self $interval) |
||
167 | |||
168 | /** |
||
169 | * Does this interval contain the specified point. |
||
170 | * |
||
171 | * @param \DateTime $point |
||
172 | * |
||
173 | * @return bool |
||
174 | */ |
||
175 | public function contains(\DateTime $point) |
||
179 | |||
180 | /** |
||
181 | * Does this interval intersect the specified interval. |
||
182 | * |
||
183 | * @param MonthInterval $interval |
||
184 | * |
||
185 | * @return bool |
||
186 | */ |
||
187 | public function intersects(self $interval) |
||
191 | |||
192 | /** |
||
193 | * Gets the intersection between this interval and another interval. |
||
194 | * |
||
195 | * @param MonthInterval $interval |
||
196 | * |
||
197 | * @return self|null |
||
198 | */ |
||
199 | public function intersection(self $interval) |
||
203 | |||
204 | /** |
||
205 | * Gets the covered interval between this Interval and another interval. |
||
206 | * |
||
207 | * @param MonthInterval $interval |
||
208 | * |
||
209 | * @return self |
||
210 | */ |
||
211 | public function cover(self $interval) |
||
215 | |||
216 | /** |
||
217 | * Gets the gap between this interval and another interval. |
||
218 | * |
||
219 | * @param MonthInterval $interval |
||
220 | * |
||
221 | * @return self|null |
||
222 | */ |
||
223 | public function gap(self $interval) |
||
227 | |||
228 | /** |
||
229 | * Does this interval abuts with the interval specified. |
||
230 | * |
||
231 | * @param MonthInterval $interval |
||
232 | * |
||
233 | * @return bool |
||
234 | */ |
||
235 | public function abuts(self $interval) |
||
239 | |||
240 | /** |
||
241 | * Joins the interval between the adjacent. |
||
242 | * |
||
243 | * @param MonthInterval $interval |
||
244 | * |
||
245 | * @return self|null |
||
246 | */ |
||
247 | public function join(self $interval) |
||
251 | |||
252 | /** |
||
253 | * Gets the union between this interval and another interval. |
||
254 | * |
||
255 | * @param MonthInterval $interval |
||
256 | * |
||
257 | * @return self|null |
||
258 | */ |
||
259 | public function union(self $interval) |
||
263 | |||
264 | /** |
||
265 | * The point is before the interval. |
||
266 | * |
||
267 | * @param \DateTime $point |
||
268 | * |
||
269 | * @return bool |
||
270 | */ |
||
271 | public function before(\DateTime $point) |
||
275 | |||
276 | /** |
||
277 | * The point is after the interval. |
||
278 | * |
||
279 | * @param \DateTime $point |
||
280 | * |
||
281 | * @return bool |
||
282 | */ |
||
283 | public function after(\DateTime $point) |
||
287 | |||
288 | /** |
||
289 | * @param \DateInterval|null $step |
||
290 | * |
||
291 | * @return \Generator |
||
292 | */ |
||
293 | 2 | public function iterate(\DateInterval $step = null) |
|
294 | { |
||
295 | 2 | $step = $step ?: new \DateInterval('P1M'); |
|
296 | |||
297 | 2 | $date = $this->start(); |
|
298 | 2 | $end = $this->end(); |
|
299 | |||
300 | 2 | if ($this->type->startExcluded()) { |
|
301 | 1 | $date->add($step); |
|
302 | } |
||
303 | |||
304 | 2 | while ($date < $end || (!$this->type->endExcluded() && $date == $end)) { |
|
305 | 2 | yield $date; |
|
306 | 2 | $date->add($step); |
|
307 | } |
||
308 | 2 | } |
|
309 | |||
310 | /** |
||
311 | * @param \DateInterval|null $step |
||
312 | * |
||
313 | * @return \DatePeriod |
||
314 | */ |
||
315 | public function period(\DateInterval $step = null) |
||
321 | |||
322 | /** |
||
323 | * @return IntervalType |
||
324 | */ |
||
325 | public function type() |
||
329 | |||
330 | /** |
||
331 | * @return \DateTime |
||
332 | */ |
||
333 | 2 | public function start() |
|
337 | |||
338 | /** |
||
339 | * @return \DateTime |
||
340 | */ |
||
341 | 2 | public function end() |
|
345 | |||
346 | /** |
||
347 | * @return MonthIntervalPoint |
||
348 | */ |
||
349 | 1 | public function startPoint() |
|
353 | |||
354 | /** |
||
355 | * @return MonthIntervalPoint |
||
356 | */ |
||
357 | 1 | public function endPoint() |
|
361 | |||
362 | /** |
||
363 | * Returns a copy of this Interval with the start point altered. |
||
364 | * |
||
365 | * @param IntervalPointInterface|MonthIntervalPoint $start |
||
366 | * |
||
367 | * @return self |
||
368 | */ |
||
369 | public function withStart(IntervalPointInterface $start) |
||
373 | |||
374 | /** |
||
375 | * Returns a copy of this Interval with the end point altered. |
||
376 | * |
||
377 | * @param IntervalPointInterface|MonthIntervalPoint $end |
||
378 | * |
||
379 | * @return self |
||
380 | */ |
||
381 | public function withEnd(IntervalPointInterface $end) |
||
385 | |||
386 | /** |
||
387 | * Returns a copy of this Interval with the interval type altered. |
||
388 | * |
||
389 | * @param IntervalType $type |
||
390 | * |
||
391 | * @return self |
||
392 | */ |
||
393 | public function withType(IntervalType $type) |
||
397 | |||
398 | /** |
||
399 | * @return string |
||
400 | */ |
||
401 | 1 | public function __toString() |
|
405 | } |
||
406 |
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.