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 | abstract class AbstractLoom implements GettersInterface, ComparisonsInterface, ArithmeticInterface |
||
18 | { |
||
19 | /** |
||
20 | * @var int |
||
21 | */ |
||
22 | protected $ms = 0; |
||
23 | |||
24 | |||
25 | /* ----------------------------------------------------------------------------------------------------------------- |
||
26 | * Getters |
||
27 | * ----------------------------------------------------------------------------------------------------------------- |
||
28 | * Transform the Loom unit to different unit |
||
29 | */ |
||
30 | |||
31 | |||
32 | /** |
||
33 | * Get microseconds |
||
34 | * |
||
35 | * @return int |
||
36 | */ |
||
37 | public function getMicroseconds() |
||
41 | |||
42 | |||
43 | /** |
||
44 | * Get milliseconds |
||
45 | * |
||
46 | * @return int |
||
47 | */ |
||
48 | public function getMilliseconds() |
||
52 | |||
53 | |||
54 | /** |
||
55 | * Get seconds |
||
56 | * |
||
57 | * @return float |
||
58 | */ |
||
59 | public function getSeconds() |
||
63 | |||
64 | |||
65 | /** |
||
66 | * Get minutes |
||
67 | * |
||
68 | * @return float |
||
69 | */ |
||
70 | public function getMinutes() |
||
74 | |||
75 | |||
76 | /** |
||
77 | * Get hours |
||
78 | * |
||
79 | * @return float |
||
80 | */ |
||
81 | public function getHours() |
||
85 | |||
86 | |||
87 | /** |
||
88 | * Get days |
||
89 | * |
||
90 | * @return float |
||
91 | */ |
||
92 | public function getDays() |
||
96 | |||
97 | |||
98 | /** |
||
99 | * Get weeks |
||
100 | * |
||
101 | * @return float |
||
102 | */ |
||
103 | public function getWeeks() |
||
107 | |||
108 | |||
109 | /** |
||
110 | * Get months |
||
111 | * |
||
112 | * @param null|int $daysPerMonth |
||
113 | * |
||
114 | * @return float |
||
115 | */ |
||
116 | public function getMonths($daysPerMonth = null) |
||
120 | |||
121 | |||
122 | /** |
||
123 | * Get years |
||
124 | * |
||
125 | * @param bool $solar |
||
126 | * |
||
127 | * @return float |
||
128 | */ |
||
129 | public function getYears($solar = false) |
||
134 | |||
135 | |||
136 | |||
137 | |||
138 | /** |
||
139 | * Get a new DateTime instance |
||
140 | * |
||
141 | * @return \DateTime |
||
142 | */ |
||
143 | public function getDateTime() |
||
147 | |||
148 | |||
149 | /* ----------------------------------------------------------------------------------------------------------------- |
||
150 | * Comparisons |
||
151 | * ----------------------------------------------------------------------------------------------------------------- |
||
152 | * Equality comparisons and validators. |
||
153 | */ |
||
154 | |||
155 | |||
156 | /** |
||
157 | * Test equality |
||
158 | * |
||
159 | * @param Loom $loom |
||
160 | * |
||
161 | * @return bool |
||
162 | */ |
||
163 | public function eq(Loom $loom) |
||
170 | |||
171 | |||
172 | /** |
||
173 | * Test not equal to |
||
174 | * |
||
175 | * @param Loom $loom |
||
176 | * |
||
177 | * @return bool |
||
178 | */ |
||
179 | public function ne(Loom $loom) |
||
183 | |||
184 | |||
185 | /** |
||
186 | * Test less than |
||
187 | * |
||
188 | * @param Loom $loom |
||
189 | * |
||
190 | * @return bool |
||
191 | */ |
||
192 | public function lt(Loom $loom) |
||
196 | |||
197 | |||
198 | /** |
||
199 | * Test less than or equal to |
||
200 | * |
||
201 | * @param Loom $loom |
||
202 | * |
||
203 | * @return bool |
||
204 | */ |
||
205 | public function lte(Loom $loom) |
||
209 | |||
210 | |||
211 | /** |
||
212 | * Test greater than |
||
213 | * |
||
214 | * @param Loom $loom |
||
215 | * |
||
216 | * @return bool |
||
217 | */ |
||
218 | public function gt(Loom $loom) |
||
222 | |||
223 | |||
224 | /** |
||
225 | * Test greater than or equal to |
||
226 | * |
||
227 | * @param Loom $loom |
||
228 | * |
||
229 | * @return bool |
||
230 | */ |
||
231 | public function gte(Loom $loom) |
||
235 | |||
236 | |||
237 | /** |
||
238 | * Get the difference between |
||
239 | * |
||
240 | * @param Loom $loom |
||
241 | * |
||
242 | * @return Loom |
||
243 | */ |
||
244 | public function diff(Loom $loom) |
||
253 | |||
254 | |||
255 | /** |
||
256 | * Is between two units |
||
257 | * |
||
258 | * @param Loom $start |
||
259 | * @param Loom $end |
||
260 | * @param bool $inclusive |
||
261 | * |
||
262 | * @return bool |
||
263 | */ |
||
264 | public function isBetween(Loom $start, Loom $end, $inclusive = false) |
||
274 | |||
275 | |||
276 | /* ----------------------------------------------------------------------------------------------------------------- |
||
277 | * Arithmetic |
||
278 | * ----------------------------------------------------------------------------------------------------------------- |
||
279 | * Add a Loom instance, or subtract a Loom instance. |
||
280 | */ |
||
281 | |||
282 | |||
283 | /** |
||
284 | * Add a Loom |
||
285 | * |
||
286 | * @param Loom|AbstractUnit $loom |
||
287 | * |
||
288 | * @return Loom |
||
289 | */ |
||
290 | public function add($loom) |
||
303 | |||
304 | |||
305 | /** |
||
306 | * Subtract a Loom |
||
307 | * |
||
308 | * @param Loom|AbstractUnit $loom |
||
309 | * |
||
310 | * @return Loom |
||
311 | */ |
||
312 | public function sub($loom) |
||
329 | |||
330 | |||
331 | /** |
||
332 | * Get time since |
||
333 | * |
||
334 | * @return Loom |
||
335 | */ |
||
336 | public function since() |
||
343 | |||
344 | |||
345 | /** |
||
346 | * Get time until |
||
347 | * |
||
348 | * @return Loom |
||
349 | */ |
||
350 | public function until() |
||
354 | } |
||
355 |
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.