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) |
|
16 | { |
||
17 | 9 | $os->setName($os::UNKNOWN); |
|
18 | 9 | $os->setVersion($os::VERSION_UNKNOWN); |
|
19 | 9 | $os->setIsMobile(false); |
|
20 | |||
21 | 9 | self::checkMobileBrowsers($os, $userAgent); |
|
22 | |||
23 | return ( |
||
24 | // Chrome OS before OS X |
||
25 | 9 | self::checkChromeOs($os, $userAgent) || |
|
26 | // iOS before OS X |
||
27 | 9 | self::checkIOS($os, $userAgent) || |
|
28 | 8 | self::checkOSX($os, $userAgent) || |
|
29 | 6 | self::checkSymbOS($os, $userAgent) || |
|
30 | 6 | self::checkWindows($os, $userAgent) || |
|
31 | 4 | self::checkWindowsPhone($os, $userAgent) || |
|
32 | 3 | self::checkFreeBSD($os, $userAgent) || |
|
33 | 3 | self::checkOpenBSD($os, $userAgent) || |
|
34 | 3 | self::checkNetBSD($os, $userAgent) || |
|
35 | 3 | self::checkOpenSolaris($os, $userAgent) || |
|
36 | 3 | self::checkSunOS($os, $userAgent) || |
|
37 | 3 | self::checkOS2($os, $userAgent) || |
|
38 | 3 | self::checkBeOS($os, $userAgent) || |
|
39 | // Android before Linux |
||
40 | 3 | self::checkAndroid($os, $userAgent) || |
|
41 | 3 | self::checkLinux($os, $userAgent) || |
|
42 | 3 | self::checkNokia($os, $userAgent) || |
|
43 | 3 | self::checkBlackBerry($os, $userAgent) |
|
44 | 9 | ); |
|
45 | } |
||
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) |
|
56 | { |
||
57 | // Check for Opera Mini |
||
58 | 9 | if (stripos($userAgent->getUserAgentString(), 'opera mini') !== false) { |
|
59 | $os->setIsMobile(true); |
||
60 | } // Set is mobile for Pocket IE |
||
61 | 9 | elseif (stripos($userAgent->getUserAgentString(), 'mspie') !== false || |
|
62 | 9 | stripos($userAgent->getUserAgentString(), 'pocket') !== false) { |
|
63 | $os->setIsMobile(true); |
||
64 | } |
||
65 | 9 | } |
|
66 | |||
67 | /** |
||
68 | * Determine if the user's operating system is iOS. |
||
69 | * |
||
70 | * @param Os $os |
||
71 | * @param UserAgent $userAgent |
||
72 | * |
||
73 | * @return bool |
||
74 | */ |
||
75 | 9 | private static function checkIOS(Os $os, UserAgent $userAgent) |
|
76 | { |
||
77 | 9 | if (stripos($userAgent->getUserAgentString(), 'CPU OS') !== false || |
|
78 | 8 | stripos($userAgent->getUserAgentString(), 'iPhone OS') !== false && |
|
79 | 9 | stripos($userAgent->getUserAgentString(), 'OS X')) { |
|
80 | 3 | $os->setName($os::IOS); |
|
81 | 3 | if (preg_match('/CPU( iPhone)? OS ([\d_]*)/i', $userAgent->getUserAgentString(), $matches)) { |
|
82 | 3 | $os->setVersion(str_replace('_', '.', $matches[2])); |
|
83 | 3 | } |
|
84 | 3 | $os->setIsMobile(true); |
|
85 | |||
86 | 3 | return true; |
|
87 | } |
||
88 | |||
89 | 8 | return false; |
|
90 | } |
||
91 | |||
92 | /** |
||
93 | * Determine if the user's operating system is Chrome OS. |
||
94 | * |
||
95 | * @param Os $os |
||
96 | * @param UserAgent $userAgent |
||
97 | * |
||
98 | * @return bool |
||
99 | */ |
||
100 | 9 | private static function checkChromeOs(Os $os, UserAgent $userAgent) |
|
101 | { |
||
102 | 9 | if (stripos($userAgent->getUserAgentString(), ' CrOS') !== false || |
|
103 | 9 | stripos($userAgent->getUserAgentString(), 'CrOS ') !== false |
|
104 | 9 | ) { |
|
105 | 1 | $os->setName($os::CHROME_OS); |
|
106 | 1 | if (preg_match('/Chrome\/([\d\.]*)/i', $userAgent->getUserAgentString(), $matches)) { |
|
107 | 1 | $os->setVersion($matches[1]); |
|
108 | 1 | } |
|
109 | 1 | return true; |
|
110 | } |
||
111 | |||
112 | 9 | return false; |
|
113 | } |
||
114 | |||
115 | /** |
||
116 | * Determine if the user's operating system is OS X. |
||
117 | * |
||
118 | * @param Os $os |
||
119 | * @param UserAgent $userAgent |
||
120 | * |
||
121 | * @return bool |
||
122 | */ |
||
123 | 8 | View Code Duplication | private static function checkOSX(Os $os, UserAgent $userAgent) |
|
|||
124 | { |
||
125 | 8 | if (stripos($userAgent->getUserAgentString(), 'OS X') !== false) { |
|
126 | 3 | $os->setName($os::OSX); |
|
127 | 3 | if (preg_match('/OS X ([\d\._]*)/i', $userAgent->getUserAgentString(), $matches)) { |
|
128 | 3 | if (isset($matches[1])) { |
|
129 | 3 | $os->setVersion(str_replace('_', '.', $matches[1])); |
|
130 | 3 | } |
|
131 | 3 | } |
|
132 | |||
133 | 3 | return true; |
|
134 | } |
||
135 | |||
136 | 6 | return false; |
|
137 | } |
||
138 | |||
139 | /** |
||
140 | * Determine if the user's operating system is Windows. |
||
141 | * |
||
142 | * @param Os $os |
||
143 | * @param UserAgent $userAgent |
||
144 | * |
||
145 | * @return bool |
||
146 | */ |
||
147 | 6 | private static function checkWindows(Os $os, UserAgent $userAgent) |
|
148 | { |
||
149 | 6 | if (stripos($userAgent->getUserAgentString(), 'Windows NT') !== false) { |
|
150 | 3 | $os->setName($os::WINDOWS); |
|
151 | // Windows version |
||
152 | 3 | if (preg_match('/Windows NT ([\d\.]*)/i', $userAgent->getUserAgentString(), $matches)) { |
|
153 | 3 | if (isset($matches[1])) { |
|
154 | 3 | switch (str_replace('_', '.', $matches[1])) { |
|
155 | 3 | case '6.3': |
|
156 | $os->setVersion('8.1'); |
||
157 | break; |
||
158 | 3 | case '6.2': |
|
159 | 1 | $os->setVersion('8'); |
|
160 | 1 | break; |
|
161 | 3 | case '6.1': |
|
162 | 3 | $os->setVersion('7'); |
|
163 | 3 | break; |
|
164 | 1 | case '6.0': |
|
165 | $os->setVersion('Vista'); |
||
166 | break; |
||
167 | 1 | case '5.2': |
|
168 | 1 | case '5.1': |
|
169 | $os->setVersion('XP'); |
||
170 | break; |
||
171 | 1 | case '5.01': |
|
172 | 1 | case '5.0': |
|
173 | $os->setVersion('2000'); |
||
174 | break; |
||
175 | 1 | case '4.0': |
|
176 | $os->setVersion('NT 4.0'); |
||
177 | break; |
||
178 | 1 | default: |
|
179 | 1 | if ((float)$matches[1] >= 10.0) { |
|
180 | 1 | $os->setVersion($matches[1]); |
|
181 | 1 | } |
|
182 | 1 | break; |
|
183 | 3 | } |
|
184 | 3 | } |
|
185 | 3 | } |
|
186 | |||
187 | 3 | return true; |
|
188 | } // Windows Me, Windows 98, Windows 95, Windows CE |
||
189 | 4 | elseif (preg_match( |
|
190 | 4 | '/(Windows 98; Win 9x 4\.90|Windows 98|Windows 95|Windows CE)/i', |
|
191 | 4 | $userAgent->getUserAgentString(), |
|
192 | $matches |
||
193 | 4 | )) { |
|
194 | $os->setName($os::WINDOWS); |
||
195 | switch (strtolower($matches[0])) { |
||
196 | case 'windows 98; win 9x 4.90': |
||
197 | $os->setVersion('Me'); |
||
198 | break; |
||
199 | case 'windows 98': |
||
200 | $os->setVersion('98'); |
||
201 | break; |
||
202 | case 'windows 95': |
||
203 | $os->setVersion('95'); |
||
204 | break; |
||
205 | case 'windows ce': |
||
206 | $os->setVersion('CE'); |
||
207 | break; |
||
208 | } |
||
209 | |||
210 | return true; |
||
211 | } |
||
212 | |||
213 | 4 | return false; |
|
214 | } |
||
215 | |||
216 | /** |
||
217 | * Determine if the user's operating system is Windows Phone. |
||
218 | * |
||
219 | * @param Os $os |
||
220 | * @param UserAgent $userAgent |
||
221 | * |
||
222 | * @return bool |
||
223 | */ |
||
224 | 4 | View Code Duplication | private static function checkWindowsPhone(Os $os, UserAgent $userAgent) |
225 | { |
||
226 | 4 | if (stripos($userAgent->getUserAgentString(), 'Windows Phone') !== false) { |
|
227 | 2 | $os->setIsMobile(true); |
|
228 | 2 | $os->setName($os::WINDOWS_PHONE); |
|
229 | // Windows version |
||
230 | 2 | if (preg_match('/Windows Phone ([\d\.]*)/i', $userAgent->getUserAgentString(), $matches)) { |
|
231 | 2 | if (isset($matches[1])) { |
|
232 | 2 | $os->setVersion((float)$matches[1]); |
|
233 | 2 | } |
|
234 | 2 | } |
|
235 | |||
236 | 2 | return true; |
|
237 | } |
||
238 | 3 | return false; |
|
239 | } |
||
240 | |||
241 | /** |
||
242 | * Determine if the user's operating system is SymbOS. |
||
243 | * |
||
244 | * @param Os $os |
||
245 | * @param UserAgent $userAgent |
||
246 | * |
||
247 | * @return bool |
||
248 | */ |
||
249 | 6 | private static function checkSymbOS(Os $os, UserAgent $userAgent) |
|
259 | |||
260 | /** |
||
261 | * Determine if the user's operating system is Linux. |
||
262 | * |
||
263 | * @param Os $os |
||
264 | * @param UserAgent $userAgent |
||
265 | * |
||
266 | * @return bool |
||
267 | */ |
||
268 | 3 | View Code Duplication | private static function checkLinux(Os $os, UserAgent $userAgent) |
279 | |||
280 | /** |
||
281 | * Determine if the user's operating system is Nokia. |
||
282 | * |
||
283 | * @param Os $os |
||
284 | * @param UserAgent $userAgent |
||
285 | * |
||
286 | * @return bool |
||
287 | */ |
||
288 | 3 | View Code Duplication | private static function checkNokia(Os $os, UserAgent $userAgent) |
300 | |||
301 | /** |
||
302 | * Determine if the user's operating system is BlackBerry. |
||
303 | * |
||
304 | * @param Os $os |
||
305 | * @param UserAgent $userAgent |
||
306 | * |
||
307 | * @return bool |
||
308 | */ |
||
309 | 3 | private static function checkBlackBerry(Os $os, UserAgent $userAgent) |
|
341 | |||
342 | /** |
||
343 | * Determine if the user's operating system is Android. |
||
344 | * |
||
345 | * @param Os $os |
||
346 | * @param UserAgent $userAgent |
||
347 | * |
||
348 | * @return bool |
||
349 | */ |
||
350 | 3 | private static function checkAndroid(Os $os, UserAgent $userAgent) |
|
368 | |||
369 | /** |
||
370 | * Determine if the user's operating system is FreeBSD. |
||
371 | * |
||
372 | * @param Os $os |
||
373 | * @param UserAgent $userAgent |
||
374 | * |
||
375 | * @return bool |
||
376 | */ |
||
377 | 3 | View Code Duplication | private static function checkFreeBSD(Os $os, UserAgent $userAgent) |
388 | |||
389 | /** |
||
390 | * Determine if the user's operating system is OpenBSD. |
||
391 | * |
||
392 | * @param Os $os |
||
393 | * @param UserAgent $userAgent |
||
394 | * |
||
395 | * @return bool |
||
396 | */ |
||
397 | 3 | View Code Duplication | private static function checkOpenBSD(Os $os, UserAgent $userAgent) |
408 | |||
409 | /** |
||
410 | * Determine if the user's operating system is SunOS. |
||
411 | * |
||
412 | * @param Os $os |
||
413 | * @param UserAgent $userAgent |
||
414 | * |
||
415 | * @return bool |
||
416 | */ |
||
417 | 3 | View Code Duplication | private static function checkSunOS(Os $os, UserAgent $userAgent) |
428 | |||
429 | /** |
||
430 | * Determine if the user's operating system is NetBSD. |
||
431 | * |
||
432 | * @param Os $os |
||
433 | * @param UserAgent $userAgent |
||
434 | * |
||
435 | * @return bool |
||
436 | */ |
||
437 | 3 | View Code Duplication | private static function checkNetBSD(Os $os, UserAgent $userAgent) |
448 | |||
449 | /** |
||
450 | * Determine if the user's operating system is OpenSolaris. |
||
451 | * |
||
452 | * @param Os $os |
||
453 | * @param UserAgent $userAgent |
||
454 | * |
||
455 | * @return bool |
||
456 | */ |
||
457 | 3 | View Code Duplication | private static function checkOpenSolaris(Os $os, UserAgent $userAgent) |
468 | |||
469 | /** |
||
470 | * Determine if the user's operating system is OS2. |
||
471 | * |
||
472 | * @param Os $os |
||
473 | * @param UserAgent $userAgent |
||
474 | * |
||
475 | * @return bool |
||
476 | */ |
||
477 | 3 | View Code Duplication | private static function checkOS2(Os $os, UserAgent $userAgent) |
488 | |||
489 | /** |
||
490 | * Determine if the user's operating system is BeOS. |
||
491 | * |
||
492 | * @param Os $os |
||
493 | * @param UserAgent $userAgent |
||
494 | * |
||
495 | * @return bool |
||
496 | */ |
||
497 | 3 | View Code Duplication | private static function checkBeOS(Os $os, UserAgent $userAgent) |
508 | } |
||
509 |
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.