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 | class NumberInterval implements ComparableIntervalInterface |
||
21 | { |
||
22 | /** |
||
23 | * @var string |
||
24 | */ |
||
25 | const REGEXP = '/^ |
||
26 | (?:\(|\[) # start type char |
||
27 | \s* |
||
28 | (?<start>\-?\d+) # start point |
||
29 | \s*,\s* # separator |
||
30 | (?<end>\-?\d+) # 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 NumberIntervalPoint |
||
47 | */ |
||
48 | private $start; |
||
49 | |||
50 | /** |
||
51 | * @var NumberIntervalPoint |
||
52 | */ |
||
53 | private $end; |
||
54 | |||
55 | /** |
||
56 | * @param NumberIntervalPoint $start |
||
57 | * @param NumberIntervalPoint $end |
||
58 | * @param IntervalType $type |
||
59 | */ |
||
60 | 31 | private function __construct(NumberIntervalPoint $start, NumberIntervalPoint $end, IntervalType $type) |
|
71 | |||
72 | /** |
||
73 | * @param int|float $start |
||
74 | * @param int|float $end |
||
75 | * @param IntervalType $type |
||
76 | * |
||
77 | * @return self |
||
78 | */ |
||
79 | 31 | public static function create($start, $end, IntervalType $type) |
|
83 | |||
84 | /** |
||
85 | * @param int|float $start |
||
86 | * @param int|float $end |
||
87 | * |
||
88 | * @return self |
||
89 | */ |
||
90 | 1 | public static function closed($start, $end) |
|
94 | |||
95 | /** |
||
96 | * @param int|float $start |
||
97 | * @param int|float $end |
||
98 | * |
||
99 | * @return self |
||
100 | */ |
||
101 | public static function halfClosed($start, $end) |
||
105 | |||
106 | /** |
||
107 | * @param int|float $start |
||
108 | * @param int|float $end |
||
109 | * |
||
110 | * @return self |
||
111 | */ |
||
112 | public static function halfOpen($start, $end) |
||
116 | |||
117 | /** |
||
118 | * @param int|float $start |
||
119 | * @param int|float $end |
||
120 | * |
||
121 | * @return self |
||
122 | */ |
||
123 | 1 | public static function open($start, $end) |
|
127 | |||
128 | /** |
||
129 | * Create interval from string. |
||
130 | * |
||
131 | * Example formats: |
||
132 | * [0, 5] |
||
133 | * (-3, 2] |
||
134 | * [-3, -1) |
||
135 | * (3, 9) |
||
136 | * |
||
137 | * Spaces are ignored in format. |
||
138 | * |
||
139 | * @param string $string |
||
140 | * |
||
141 | * @return self |
||
142 | */ |
||
143 | 29 | public static function fromString($string) |
|
151 | |||
152 | /** |
||
153 | * Checks if this interval is equal to the specified interval. |
||
154 | * |
||
155 | * @param NumberInterval $interval |
||
156 | * |
||
157 | * @return bool |
||
158 | */ |
||
159 | public function equal(self $interval) |
||
163 | |||
164 | /** |
||
165 | * Does this interval contain the specified point. |
||
166 | * |
||
167 | * @param int|float $point |
||
168 | * |
||
169 | * @return bool |
||
170 | */ |
||
171 | 7 | public function contains($point) |
|
175 | |||
176 | /** |
||
177 | * Does this interval intersect the specified interval. |
||
178 | * |
||
179 | * @param NumberInterval $interval |
||
180 | * |
||
181 | * @return bool |
||
182 | */ |
||
183 | 8 | public function intersects(self $interval) |
|
187 | |||
188 | /** |
||
189 | * Gets the intersection between this interval and another interval. |
||
190 | * |
||
191 | * @param NumberInterval $interval |
||
192 | * |
||
193 | * @return self|null |
||
194 | */ |
||
195 | 12 | public function intersection(self $interval) |
|
199 | |||
200 | /** |
||
201 | * Gets the covered interval between this Interval and another interval. |
||
202 | * |
||
203 | * @param NumberInterval $interval |
||
204 | * |
||
205 | * @return self |
||
206 | */ |
||
207 | public function cover(self $interval) |
||
211 | |||
212 | /** |
||
213 | * Gets the gap between this interval and another interval. |
||
214 | * |
||
215 | * @param NumberInterval $interval |
||
216 | * |
||
217 | * @return self|null |
||
218 | */ |
||
219 | public function gap(self $interval) |
||
223 | |||
224 | /** |
||
225 | * Does this interval abuts with the interval specified. |
||
226 | * |
||
227 | * @param NumberInterval $interval |
||
228 | * |
||
229 | * @return bool |
||
230 | */ |
||
231 | public function abuts(self $interval) |
||
235 | |||
236 | /** |
||
237 | * Joins the interval between the adjacent. |
||
238 | * |
||
239 | * @param NumberInterval $interval |
||
240 | * |
||
241 | * @return self|null |
||
242 | */ |
||
243 | public function join(self $interval) |
||
247 | |||
248 | /** |
||
249 | * Gets the union between this interval and another interval. |
||
250 | * |
||
251 | * @param NumberInterval $interval |
||
252 | * |
||
253 | * @return self|null |
||
254 | */ |
||
255 | public function union(self $interval) |
||
259 | |||
260 | /** |
||
261 | * The point is before the interval. |
||
262 | * |
||
263 | * @param int|float $point |
||
264 | * |
||
265 | * @return bool |
||
266 | */ |
||
267 | public function before($point) |
||
268 | { |
||
269 | return $this->comparator->before(new NumberIntervalPoint($point)); |
||
270 | } |
||
271 | |||
272 | /** |
||
273 | * The point is after the interval. |
||
274 | * |
||
275 | * @param int|float $point |
||
276 | * |
||
277 | * @return bool |
||
278 | */ |
||
279 | public function after($point) |
||
283 | |||
284 | /** |
||
285 | * @param int $step |
||
286 | * |
||
287 | * @return \Generator |
||
288 | */ |
||
289 | 2 | View Code Duplication | public function iterate($step = 1) |
|
|||
290 | { |
||
291 | 2 | $end = $this->end(); |
|
292 | 2 | $number = $this->start(); |
|
293 | |||
294 | 2 | if ($this->type->startExcluded()) { |
|
295 | 1 | $number += $step; |
|
296 | } |
||
297 | |||
298 | 2 | while ($number < $end || (!$this->type->endExcluded() && $number == $end)) { |
|
299 | 2 | yield $number; |
|
300 | 2 | $number += $step; |
|
301 | } |
||
302 | 2 | } |
|
303 | |||
304 | /** |
||
305 | * @return IntervalType |
||
306 | */ |
||
307 | 18 | public function type() |
|
311 | |||
312 | /** |
||
313 | * @return int|float |
||
314 | */ |
||
315 | 2 | public function start() |
|
319 | |||
320 | /** |
||
321 | * @return int|float |
||
322 | */ |
||
323 | 2 | public function end() |
|
327 | |||
328 | /** |
||
329 | * @return NumberIntervalPoint |
||
330 | */ |
||
331 | 28 | public function startPoint() |
|
335 | |||
336 | /** |
||
337 | * @return NumberIntervalPoint |
||
338 | */ |
||
339 | 26 | public function endPoint() |
|
343 | |||
344 | /** |
||
345 | * Returns a copy of this Interval with the start point altered. |
||
346 | * |
||
347 | * @param IntervalPointInterface|NumberIntervalPoint $start |
||
348 | * |
||
349 | * @return self |
||
350 | */ |
||
351 | 8 | public function withStart(IntervalPointInterface $start) |
|
355 | |||
356 | /** |
||
357 | * Returns a copy of this Interval with the end point altered. |
||
358 | * |
||
359 | * @param IntervalPointInterface|NumberIntervalPoint $end |
||
360 | * |
||
361 | * @return self |
||
362 | */ |
||
363 | 8 | public function withEnd(IntervalPointInterface $end) |
|
367 | |||
368 | /** |
||
369 | * Returns a copy of this Interval with the interval type altered. |
||
370 | * |
||
371 | * @param IntervalType $type |
||
372 | * |
||
373 | * @return self |
||
374 | */ |
||
375 | 8 | public function withType(IntervalType $type) |
|
379 | |||
380 | /** |
||
381 | * @return string |
||
382 | */ |
||
383 | 1 | public function __toString() |
|
387 | } |
||
388 |
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.