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 IntervalComparator 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 IntervalComparator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class IntervalComparator |
||
17 | { |
||
18 | /** |
||
19 | * @var ComparableIntervalInterface |
||
20 | */ |
||
21 | private $interval; |
||
22 | |||
23 | /** |
||
24 | * @param ComparableIntervalInterface $interval |
||
25 | */ |
||
26 | 43 | public function __construct(ComparableIntervalInterface $interval) |
|
30 | |||
31 | /** |
||
32 | * Checks if this Interval is equal to the specified interval. |
||
33 | * |
||
34 | * @param ComparableIntervalInterface $interval |
||
35 | * |
||
36 | * @return bool |
||
37 | */ |
||
38 | public function equal(ComparableIntervalInterface $interval) |
||
46 | |||
47 | /** |
||
48 | * Does this interval contain the specified point. |
||
49 | * |
||
50 | * @param IntervalPointInterface $point |
||
51 | * |
||
52 | * @return bool |
||
53 | */ |
||
54 | 7 | public function contains(IntervalPointInterface $point) |
|
66 | |||
67 | /** |
||
68 | * Does this interval intersect the specified interval. |
||
69 | * |
||
70 | * @param ComparableIntervalInterface $interval |
||
71 | * |
||
72 | * @return bool |
||
73 | */ |
||
74 | 8 | public function intersects(ComparableIntervalInterface $interval) |
|
93 | |||
94 | /** |
||
95 | * Gets the intersection between this interval and another interval. |
||
96 | * |
||
97 | * @param ComparableIntervalInterface $interval |
||
98 | * |
||
99 | * @return ComparableIntervalInterface|null |
||
100 | */ |
||
101 | 12 | public function intersection(ComparableIntervalInterface $interval) |
|
144 | |||
145 | /** |
||
146 | * Gets the covered interval between this Interval and another interval. |
||
147 | * |
||
148 | * @param ComparableIntervalInterface $interval |
||
149 | * |
||
150 | * @return ComparableIntervalInterface |
||
151 | */ |
||
152 | public function cover(ComparableIntervalInterface $interval) |
||
184 | |||
185 | /** |
||
186 | * Gets the gap between this interval and another interval. |
||
187 | * |
||
188 | * @param ComparableIntervalInterface $interval |
||
189 | * |
||
190 | * @return ComparableIntervalInterface|null |
||
191 | */ |
||
192 | public function gap(ComparableIntervalInterface $interval) |
||
230 | |||
231 | /** |
||
232 | * Does this interval abut with the interval specified. |
||
233 | * |
||
234 | * @param ComparableIntervalInterface $interval |
||
235 | * |
||
236 | * @return bool |
||
237 | */ |
||
238 | public function abuts(ComparableIntervalInterface $interval) |
||
245 | |||
246 | /** |
||
247 | * Joins the interval between the adjacent. |
||
248 | * |
||
249 | * @param ComparableIntervalInterface $interval |
||
250 | * |
||
251 | * @return ComparableIntervalInterface|null |
||
252 | */ |
||
253 | public function join(ComparableIntervalInterface $interval) |
||
261 | |||
262 | /** |
||
263 | * Gets the union between this interval and another interval. |
||
264 | * |
||
265 | * @param ComparableIntervalInterface $interval |
||
266 | * |
||
267 | * @return ComparableIntervalInterface|null |
||
268 | */ |
||
269 | public function union(ComparableIntervalInterface $interval) |
||
277 | |||
278 | /** |
||
279 | * The point is before the interval. |
||
280 | * |
||
281 | * @param IntervalPointInterface $point |
||
282 | * |
||
283 | * @return bool |
||
284 | */ |
||
285 | public function before(IntervalPointInterface $point) |
||
293 | |||
294 | /** |
||
295 | * The point is after the interval. |
||
296 | * |
||
297 | * @param IntervalPointInterface $point |
||
298 | * |
||
299 | * @return bool |
||
300 | */ |
||
301 | public function after(IntervalPointInterface $point) |
||
309 | } |
||
310 |
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.