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 OsDetector 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 OsDetector, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | class OsDetector implements DetectorInterface |
||
6 | { |
||
7 | /** |
||
8 | * Determine the user's operating system. |
||
9 | * |
||
10 | * @param Os $os |
||
11 | * @param UserAgent $userAgent |
||
12 | * |
||
13 | * @return bool |
||
14 | */ |
||
15 | 9 | public static function detect(Os $os, UserAgent $userAgent) |
|
46 | |||
47 | /** |
||
48 | * Determine if the user's browser is on a mobile device. |
||
49 | * |
||
50 | * @param Os $os |
||
51 | * @param UserAgent $userAgent |
||
52 | * |
||
53 | * @return bool |
||
54 | */ |
||
55 | 9 | public static function checkMobileBrowsers(Os $os, UserAgent $userAgent) |
|
65 | |||
66 | /** |
||
67 | * Determine if the user's operating system is iOS. |
||
68 | * |
||
69 | * @param Os $os |
||
70 | * @param UserAgent $userAgent |
||
71 | * |
||
72 | * @return bool |
||
73 | */ |
||
74 | 9 | private static function checkIOS(Os $os, UserAgent $userAgent) |
|
88 | |||
89 | /** |
||
90 | * Determine if the user's operating system is Chrome OS. |
||
91 | * |
||
92 | * @param Os $os |
||
93 | * @param UserAgent $userAgent |
||
94 | * |
||
95 | * @return bool |
||
96 | */ |
||
97 | 9 | View Code Duplication | private static function checkChromeOs(Os $os, UserAgent $userAgent) |
109 | |||
110 | /** |
||
111 | * Determine if the user's operating system is OS X. |
||
112 | * |
||
113 | * @param Os $os |
||
114 | * @param UserAgent $userAgent |
||
115 | * |
||
116 | * @return bool |
||
117 | */ |
||
118 | 7 | View Code Duplication | private static function checkOSX(Os $os, UserAgent $userAgent) |
133 | |||
134 | /** |
||
135 | * Determine if the user's operating system is Windows. |
||
136 | * |
||
137 | * @param Os $os |
||
138 | * @param UserAgent $userAgent |
||
139 | * |
||
140 | * @return bool |
||
141 | */ |
||
142 | 5 | private static function checkWindows(Os $os, UserAgent $userAgent) |
|
206 | |||
207 | /** |
||
208 | * Determine if the user's operating system is Windows Phone. |
||
209 | * |
||
210 | * @param Os $os |
||
211 | * @param UserAgent $userAgent |
||
212 | * |
||
213 | * @return bool |
||
214 | */ |
||
215 | 2 | View Code Duplication | private static function checkWindowsPhone(Os $os, UserAgent $userAgent) |
230 | |||
231 | /** |
||
232 | * Determine if the user's operating system is SymbOS. |
||
233 | * |
||
234 | * @param Os $os |
||
235 | * @param UserAgent $userAgent |
||
236 | * |
||
237 | * @return bool |
||
238 | */ |
||
239 | 5 | private static function checkSymbOS(Os $os, UserAgent $userAgent) |
|
249 | |||
250 | /** |
||
251 | * Determine if the user's operating system is Linux. |
||
252 | * |
||
253 | * @param Os $os |
||
254 | * @param UserAgent $userAgent |
||
255 | * |
||
256 | * @return bool |
||
257 | */ |
||
258 | 2 | View Code Duplication | private static function checkLinux(Os $os, UserAgent $userAgent) |
269 | |||
270 | /** |
||
271 | * Determine if the user's operating system is Nokia. |
||
272 | * |
||
273 | * @param Os $os |
||
274 | * @param UserAgent $userAgent |
||
275 | * |
||
276 | * @return bool |
||
277 | */ |
||
278 | 2 | View Code Duplication | private static function checkNokia(Os $os, UserAgent $userAgent) |
290 | |||
291 | /** |
||
292 | * Determine if the user's operating system is BlackBerry. |
||
293 | * |
||
294 | * @param Os $os |
||
295 | * @param UserAgent $userAgent |
||
296 | * |
||
297 | * @return bool |
||
298 | */ |
||
299 | 2 | View Code Duplication | private static function checkBlackBerry(Os $os, UserAgent $userAgent) |
311 | |||
312 | /** |
||
313 | * Determine if the user's operating system is Android. |
||
314 | * |
||
315 | * @param Os $os |
||
316 | * @param UserAgent $userAgent |
||
317 | * |
||
318 | * @return bool |
||
319 | */ |
||
320 | 2 | private static function checkAndroid(Os $os, UserAgent $userAgent) |
|
338 | |||
339 | /** |
||
340 | * Determine if the user's operating system is FreeBSD. |
||
341 | * |
||
342 | * @param Os $os |
||
343 | * @param UserAgent $userAgent |
||
344 | * |
||
345 | * @return bool |
||
346 | */ |
||
347 | 2 | View Code Duplication | private static function checkFreeBSD(Os $os, UserAgent $userAgent) |
358 | |||
359 | /** |
||
360 | * Determine if the user's operating system is OpenBSD. |
||
361 | * |
||
362 | * @param Os $os |
||
363 | * @param UserAgent $userAgent |
||
364 | * |
||
365 | * @return bool |
||
366 | */ |
||
367 | 2 | View Code Duplication | private static function checkOpenBSD(Os $os, UserAgent $userAgent) |
378 | |||
379 | /** |
||
380 | * Determine if the user's operating system is SunOS. |
||
381 | * |
||
382 | * @param Os $os |
||
383 | * @param UserAgent $userAgent |
||
384 | * |
||
385 | * @return bool |
||
386 | */ |
||
387 | 2 | View Code Duplication | private static function checkSunOS(Os $os, UserAgent $userAgent) |
398 | |||
399 | /** |
||
400 | * Determine if the user's operating system is NetBSD. |
||
401 | * |
||
402 | * @param Os $os |
||
403 | * @param UserAgent $userAgent |
||
404 | * |
||
405 | * @return bool |
||
406 | */ |
||
407 | 2 | View Code Duplication | private static function checkNetBSD(Os $os, UserAgent $userAgent) |
418 | |||
419 | /** |
||
420 | * Determine if the user's operating system is OpenSolaris. |
||
421 | * |
||
422 | * @param Os $os |
||
423 | * @param UserAgent $userAgent |
||
424 | * |
||
425 | * @return bool |
||
426 | */ |
||
427 | 2 | View Code Duplication | private static function checkOpenSolaris(Os $os, UserAgent $userAgent) |
438 | |||
439 | /** |
||
440 | * Determine if the user's operating system is OS2. |
||
441 | * |
||
442 | * @param Os $os |
||
443 | * @param UserAgent $userAgent |
||
444 | * |
||
445 | * @return bool |
||
446 | */ |
||
447 | 2 | View Code Duplication | private static function checkOS2(Os $os, UserAgent $userAgent) |
458 | |||
459 | /** |
||
460 | * Determine if the user's operating system is BeOS. |
||
461 | * |
||
462 | * @param Os $os |
||
463 | * @param UserAgent $userAgent |
||
464 | * |
||
465 | * @return bool |
||
466 | */ |
||
467 | 2 | View Code Duplication | private static function checkBeOS(Os $os, UserAgent $userAgent) |
478 | } |
||
479 |
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.