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 Browser 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 Browser, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
45 | class Browser |
||
46 | { |
||
47 | private $_agent = ''; |
||
48 | private $_browser_name = ''; |
||
49 | private $_version = ''; |
||
50 | private $_platform = ''; |
||
51 | private $_os = ''; |
||
52 | private $_is_aol = false; |
||
53 | private $_is_mobile = false; |
||
54 | private $_is_tablet = false; |
||
55 | private $_is_robot = false; |
||
56 | private $_is_facebook = false; |
||
57 | private $_aol_version = ''; |
||
58 | |||
59 | const BROWSER_UNKNOWN = 'unknown'; |
||
60 | const VERSION_UNKNOWN = 'unknown'; |
||
61 | |||
62 | const BROWSER_OPERA = 'Opera'; // http://www.opera.com/ |
||
63 | const BROWSER_OPERA_MINI = 'Opera Mini'; // http://www.opera.com/mini/ |
||
64 | const BROWSER_WEBTV = 'WebTV'; // http://www.webtv.net/pc/ |
||
65 | const BROWSER_IE = 'Internet Explorer'; // http://www.microsoft.com/ie/ |
||
66 | const BROWSER_POCKET_IE = 'Pocket Internet Explorer'; // http://en.wikipedia.org/wiki/Internet_Explorer_Mobile |
||
67 | const BROWSER_KONQUEROR = 'Konqueror'; // http://www.konqueror.org/ |
||
68 | const BROWSER_ICAB = 'iCab'; // http://www.icab.de/ |
||
69 | const BROWSER_OMNIWEB = 'OmniWeb'; // http://www.omnigroup.com/applications/omniweb/ |
||
70 | const BROWSER_FIREBIRD = 'Firebird'; // http://www.ibphoenix.com/ |
||
71 | const BROWSER_FIREFOX = 'Firefox'; // http://www.mozilla.com/en-US/firefox/firefox.html |
||
72 | const BROWSER_ICEWEASEL = 'Iceweasel'; // http://www.geticeweasel.org/ |
||
73 | const BROWSER_SHIRETOKO = 'Shiretoko'; // http://wiki.mozilla.org/Projects/shiretoko |
||
74 | const BROWSER_MOZILLA = 'Mozilla'; // http://www.mozilla.com/en-US/ |
||
75 | const BROWSER_AMAYA = 'Amaya'; // http://www.w3.org/Amaya/ |
||
76 | const BROWSER_LYNX = 'Lynx'; // http://en.wikipedia.org/wiki/Lynx |
||
77 | const BROWSER_SAFARI = 'Safari'; // http://apple.com |
||
78 | const BROWSER_IPHONE = 'iPhone'; // http://apple.com |
||
79 | const BROWSER_IPOD = 'iPod'; // http://apple.com |
||
80 | const BROWSER_IPAD = 'iPad'; // http://apple.com |
||
81 | const BROWSER_CHROME = 'Chrome'; // http://www.google.com/chrome |
||
82 | const BROWSER_ANDROID = 'Android'; // http://www.android.com/ |
||
83 | const BROWSER_GOOGLEBOT = 'GoogleBot'; // http://en.wikipedia.org/wiki/Googlebot |
||
84 | const BROWSER_SLURP = 'Yahoo! Slurp'; // http://en.wikipedia.org/wiki/Yahoo!_Slurp |
||
85 | const BROWSER_W3CVALIDATOR = 'W3C Validator'; // http://validator.w3.org/ |
||
86 | const BROWSER_BLACKBERRY = 'BlackBerry'; // http://www.blackberry.com/ |
||
87 | const BROWSER_ICECAT = 'IceCat'; // http://en.wikipedia.org/wiki/GNU_IceCat |
||
88 | const BROWSER_NOKIA_S60 = 'Nokia S60 OSS Browser'; // http://en.wikipedia.org/wiki/Web_Browser_for_S60 |
||
89 | const BROWSER_NOKIA = 'Nokia Browser'; // * all other WAP-based browsers on the Nokia Platform |
||
90 | const BROWSER_MSN = 'MSN Browser'; // http://explorer.msn.com/ |
||
91 | const BROWSER_MSNBOT = 'MSN Bot'; // http://search.msn.com/msnbot.htm |
||
92 | const BROWSER_BINGBOT = 'Bing Bot'; // http://en.wikipedia.org/wiki/Bingbot |
||
93 | |||
94 | const BROWSER_NETSCAPE_NAVIGATOR = 'Netscape Navigator'; // http://browser.netscape.com/ (DEPRECATED) |
||
95 | const BROWSER_GALEON = 'Galeon'; // http://galeon.sourceforge.net/ (DEPRECATED) |
||
96 | const BROWSER_NETPOSITIVE = 'NetPositive'; // http://en.wikipedia.org/wiki/NetPositive (DEPRECATED) |
||
97 | const BROWSER_PHOENIX = 'Phoenix'; // http://en.wikipedia.org/wiki/History_of_Mozilla_Firefox (DEPRECATED) |
||
98 | |||
99 | const PLATFORM_UNKNOWN = 'unknown'; |
||
100 | const PLATFORM_WINDOWS = 'Windows'; |
||
101 | const PLATFORM_WINDOWS_CE = 'Windows CE'; |
||
102 | const PLATFORM_APPLE = 'Apple'; |
||
103 | const PLATFORM_LINUX = 'Linux'; |
||
104 | const PLATFORM_OS2 = 'OS/2'; |
||
105 | const PLATFORM_BEOS = 'BeOS'; |
||
106 | const PLATFORM_IPHONE = 'iPhone'; |
||
107 | const PLATFORM_IPOD = 'iPod'; |
||
108 | const PLATFORM_IPAD = 'iPad'; |
||
109 | const PLATFORM_BLACKBERRY = 'BlackBerry'; |
||
110 | const PLATFORM_NOKIA = 'Nokia'; |
||
111 | const PLATFORM_FREEBSD = 'FreeBSD'; |
||
112 | const PLATFORM_OPENBSD = 'OpenBSD'; |
||
113 | const PLATFORM_NETBSD = 'NetBSD'; |
||
114 | const PLATFORM_SUNOS = 'SunOS'; |
||
115 | const PLATFORM_OPENSOLARIS = 'OpenSolaris'; |
||
116 | const PLATFORM_ANDROID = 'Android'; |
||
117 | |||
118 | const OPERATING_SYSTEM_UNKNOWN = 'unknown'; |
||
119 | |||
120 | public function __construct($userAgent = "") |
||
129 | |||
130 | /** |
||
131 | * Reset all properties |
||
132 | */ |
||
133 | public function reset() |
||
147 | |||
148 | /** |
||
149 | * Check to see if the specific browser is valid |
||
150 | * @param string $browserName |
||
151 | * @return bool True if the browser is the specified browser |
||
152 | */ |
||
153 | function isBrowser($browserName) |
||
157 | |||
158 | /** |
||
159 | * The name of the browser. All return types are from the class contants |
||
160 | * @return string Name of the browser |
||
161 | */ |
||
162 | public function getBrowser() |
||
166 | |||
167 | /** |
||
168 | * Set the name of the browser |
||
169 | * @param $browser string The name of the Browser |
||
170 | */ |
||
171 | public function setBrowser($browser) |
||
175 | |||
176 | /** |
||
177 | * The name of the platform. All return types are from the class contants |
||
178 | * @return string Name of the browser |
||
179 | */ |
||
180 | public function getPlatform() |
||
184 | |||
185 | /** |
||
186 | * Set the name of the platform |
||
187 | * @param string $platform The name of the Platform |
||
188 | */ |
||
189 | public function setPlatform($platform) |
||
193 | |||
194 | /** |
||
195 | * The version of the browser. |
||
196 | * @return string Version of the browser (will only contain alpha-numeric characters and a period) |
||
197 | */ |
||
198 | public function getVersion() |
||
202 | |||
203 | /** |
||
204 | * Set the version of the browser |
||
205 | * @param string $version The version of the Browser |
||
206 | */ |
||
207 | public function setVersion($version) |
||
211 | |||
212 | /** |
||
213 | * The version of AOL. |
||
214 | * @return string Version of AOL (will only contain alpha-numeric characters and a period) |
||
215 | */ |
||
216 | public function getAolVersion() |
||
220 | |||
221 | /** |
||
222 | * Set the version of AOL |
||
223 | * @param string $version The version of AOL |
||
224 | */ |
||
225 | public function setAolVersion($version) |
||
229 | |||
230 | /** |
||
231 | * Is the browser from AOL? |
||
232 | * @return boolean True if the browser is from AOL otherwise false |
||
233 | */ |
||
234 | public function isAol() |
||
238 | |||
239 | /** |
||
240 | * Is the browser from a mobile device? |
||
241 | * @return boolean True if the browser is from a mobile device otherwise false |
||
242 | */ |
||
243 | public function isMobile() |
||
247 | |||
248 | /** |
||
249 | * Is the browser from a tablet device? |
||
250 | * @return boolean True if the browser is from a tablet device otherwise false |
||
251 | */ |
||
252 | public function isTablet() |
||
256 | |||
257 | /** |
||
258 | * Is the browser from a robot (ex Slurp,GoogleBot)? |
||
259 | * @return boolean True if the browser is from a robot otherwise false |
||
260 | */ |
||
261 | public function isRobot() |
||
265 | |||
266 | /** |
||
267 | * Is the browser from facebook? |
||
268 | * @return boolean True if the browser is from facebook otherwise false |
||
269 | */ |
||
270 | public function isFacebook() |
||
274 | |||
275 | /** |
||
276 | * Set the browser to be from AOL |
||
277 | * @param $isAol |
||
278 | */ |
||
279 | public function setAol($isAol) |
||
283 | |||
284 | /** |
||
285 | * Set the Browser to be mobile |
||
286 | * @param boolean $value is the browser a mobile browser or not |
||
287 | */ |
||
288 | protected function setMobile($value = true) |
||
292 | |||
293 | /** |
||
294 | * Set the Browser to be tablet |
||
295 | * @param boolean $value is the browser a tablet browser or not |
||
296 | */ |
||
297 | protected function setTablet($value = true) |
||
301 | |||
302 | /** |
||
303 | * Set the Browser to be a robot |
||
304 | * @param boolean $value is the browser a robot or not |
||
305 | */ |
||
306 | protected function setRobot($value = true) |
||
310 | |||
311 | /** |
||
312 | * Set the Browser to be a Facebook request |
||
313 | * @param boolean $value is the browser a robot or not |
||
314 | */ |
||
315 | protected function setFacebook($value = true) |
||
319 | |||
320 | /** |
||
321 | * Get the user agent value in use to determine the browser |
||
322 | * @return string The user agent from the HTTP header |
||
323 | */ |
||
324 | public function getUserAgent() |
||
328 | |||
329 | /** |
||
330 | * Set the user agent value (the construction will use the HTTP header value - this will overwrite it) |
||
331 | * @param string $agent_string The value for the User Agent |
||
332 | */ |
||
333 | public function setUserAgent($agent_string) |
||
339 | |||
340 | /** |
||
341 | * Used to determine if the browser is actually "chromeframe" |
||
342 | * @since 1.7 |
||
343 | * @return boolean True if the browser is using chromeframe |
||
344 | */ |
||
345 | public function isChromeFrame() |
||
349 | |||
350 | /** |
||
351 | * Returns a formatted string with a summary of the details of the browser. |
||
352 | * @return string formatted string with a summary of the browser |
||
353 | */ |
||
354 | public function __toString() |
||
361 | |||
362 | /** |
||
363 | * Protected routine to calculate and determine what the browser is in use (including platform) |
||
364 | */ |
||
365 | protected function determine() |
||
371 | |||
372 | /** |
||
373 | * Protected routine to determine the browser type |
||
374 | * @return boolean True if the browser was detected otherwise false |
||
375 | */ |
||
376 | protected function checkBrowsers() |
||
435 | |||
436 | /** |
||
437 | * Determine if the user is using a BlackBerry (last updated 1.7) |
||
438 | * @return boolean True if the browser is the BlackBerry browser otherwise false |
||
439 | */ |
||
440 | protected function checkBrowserBlackBerry() |
||
454 | |||
455 | /** |
||
456 | * Determine if the user is using an AOL User Agent (last updated 1.7) |
||
457 | * @return boolean True if the browser is from AOL otherwise false |
||
458 | */ |
||
459 | protected function checkForAol() |
||
474 | |||
475 | /** |
||
476 | * Determine if the browser is the GoogleBot or not (last updated 1.7) |
||
477 | * @return boolean True if the browser is the GoogletBot otherwise false |
||
478 | */ |
||
479 | protected function checkBrowserGoogleBot() |
||
493 | |||
494 | /** |
||
495 | * Determine if the browser is the MSNBot or not (last updated 1.9) |
||
496 | * @return boolean True if the browser is the MSNBot otherwise false |
||
497 | */ |
||
498 | protected function checkBrowserMSNBot() |
||
512 | |||
513 | /** |
||
514 | * Determine if the browser is the BingBot or not (last updated 1.9) |
||
515 | * @return boolean True if the browser is the BingBot otherwise false |
||
516 | */ |
||
517 | protected function checkBrowserBingBot() |
||
531 | |||
532 | /** |
||
533 | * Determine if the browser is the W3C Validator or not (last updated 1.7) |
||
534 | * @return boolean True if the browser is the W3C Validator otherwise false |
||
535 | */ |
||
536 | protected function checkBrowserW3CValidator() |
||
563 | |||
564 | /** |
||
565 | * Determine if the browser is the Yahoo! Slurp Robot or not (last updated 1.7) |
||
566 | * @return boolean True if the browser is the Yahoo! Slurp Robot otherwise false |
||
567 | */ |
||
568 | protected function checkBrowserSlurp() |
||
583 | |||
584 | /** |
||
585 | * Determine if the browser is Internet Explorer or not (last updated 1.7) |
||
586 | * @return boolean True if the browser is Internet Explorer otherwise false |
||
587 | */ |
||
588 | protected function checkBrowserInternetExplorer() |
||
654 | |||
655 | /** |
||
656 | * Determine if the browser is Opera or not (last updated 1.7) |
||
657 | * @return boolean True if the browser is Opera otherwise false |
||
658 | */ |
||
659 | protected function checkBrowserOpera() |
||
714 | |||
715 | /** |
||
716 | * Determine if the browser is Chrome or not (last updated 1.7) |
||
717 | * @return boolean True if the browser is Chrome otherwise false |
||
718 | */ |
||
719 | protected function checkBrowserChrome() |
||
740 | |||
741 | |||
742 | /** |
||
743 | * Determine if the browser is WebTv or not (last updated 1.7) |
||
744 | * @return boolean True if the browser is WebTv otherwise false |
||
745 | */ |
||
746 | View Code Duplication | protected function checkBrowserWebTv() |
|
759 | |||
760 | /** |
||
761 | * Determine if the browser is NetPositive or not (last updated 1.7) |
||
762 | * @return boolean True if the browser is NetPositive otherwise false |
||
763 | */ |
||
764 | protected function checkBrowserNetPositive() |
||
777 | |||
778 | /** |
||
779 | * Determine if the browser is Galeon or not (last updated 1.7) |
||
780 | * @return boolean True if the browser is Galeon otherwise false |
||
781 | */ |
||
782 | View Code Duplication | protected function checkBrowserGaleon() |
|
795 | |||
796 | /** |
||
797 | * Determine if the browser is Konqueror or not (last updated 1.7) |
||
798 | * @return boolean True if the browser is Konqueror otherwise false |
||
799 | */ |
||
800 | View Code Duplication | protected function checkBrowserKonqueror() |
|
813 | |||
814 | /** |
||
815 | * Determine if the browser is iCab or not (last updated 1.7) |
||
816 | * @return boolean True if the browser is iCab otherwise false |
||
817 | */ |
||
818 | View Code Duplication | protected function checkBrowserIcab() |
|
830 | |||
831 | /** |
||
832 | * Determine if the browser is OmniWeb or not (last updated 1.7) |
||
833 | * @return boolean True if the browser is OmniWeb otherwise false |
||
834 | */ |
||
835 | View Code Duplication | protected function checkBrowserOmniWeb() |
|
846 | |||
847 | /** |
||
848 | * Determine if the browser is Phoenix or not (last updated 1.7) |
||
849 | * @return boolean True if the browser is Phoenix otherwise false |
||
850 | */ |
||
851 | View Code Duplication | protected function checkBrowserPhoenix() |
|
863 | |||
864 | /** |
||
865 | * Determine if the browser is Firebird or not (last updated 1.7) |
||
866 | * @return boolean True if the browser is Firebird otherwise false |
||
867 | */ |
||
868 | View Code Duplication | protected function checkBrowserFirebird() |
|
880 | |||
881 | /** |
||
882 | * Determine if the browser is Netscape Navigator 9+ or not (last updated 1.7) |
||
883 | * NOTE: (http://browser.netscape.com/ - Official support ended on March 1st, 2008) |
||
884 | * @return boolean True if the browser is Netscape Navigator 9+ otherwise false |
||
885 | */ |
||
886 | protected function checkBrowserNetscapeNavigator9Plus() |
||
899 | |||
900 | /** |
||
901 | * Determine if the browser is Shiretoko or not (https://wiki.mozilla.org/Projects/shiretoko) (last updated 1.7) |
||
902 | * @return boolean True if the browser is Shiretoko otherwise false |
||
903 | */ |
||
904 | View Code Duplication | protected function checkBrowserShiretoko() |
|
913 | |||
914 | /** |
||
915 | * Determine if the browser is Ice Cat or not (http://en.wikipedia.org/wiki/GNU_IceCat) (last updated 1.7) |
||
916 | * @return boolean True if the browser is Ice Cat otherwise false |
||
917 | */ |
||
918 | View Code Duplication | protected function checkBrowserIceCat() |
|
927 | |||
928 | /** |
||
929 | * Determine if the browser is Nokia or not (last updated 1.7) |
||
930 | * @return boolean True if the browser is Nokia otherwise false |
||
931 | */ |
||
932 | protected function checkBrowserNokia() |
||
946 | |||
947 | /** |
||
948 | * Determine if the browser is Firefox or not (last updated 1.7) |
||
949 | * @return boolean True if the browser is Firefox otherwise false |
||
950 | */ |
||
951 | protected function checkBrowserFirefox() |
||
974 | |||
975 | /** |
||
976 | * Determine if the browser is Firefox or not (last updated 1.7) |
||
977 | * @return boolean True if the browser is Firefox otherwise false |
||
978 | */ |
||
979 | View Code Duplication | protected function checkBrowserIceweasel() |
|
992 | |||
993 | /** |
||
994 | * Determine if the browser is Mozilla or not (last updated 1.7) |
||
995 | * @return boolean True if the browser is Mozilla otherwise false |
||
996 | */ |
||
997 | protected function checkBrowserMozilla() |
||
1017 | |||
1018 | /** |
||
1019 | * Determine if the browser is Lynx or not (last updated 1.7) |
||
1020 | * @return boolean True if the browser is Lynx otherwise false |
||
1021 | */ |
||
1022 | View Code Duplication | protected function checkBrowserLynx() |
|
1033 | |||
1034 | /** |
||
1035 | * Determine if the browser is Amaya or not (last updated 1.7) |
||
1036 | * @return boolean True if the browser is Amaya otherwise false |
||
1037 | */ |
||
1038 | View Code Duplication | protected function checkBrowserAmaya() |
|
1051 | |||
1052 | /** |
||
1053 | * Determine if the browser is Safari or not (last updated 1.7) |
||
1054 | * @return boolean True if the browser is Safari otherwise false |
||
1055 | */ |
||
1056 | protected function checkBrowserSafari() |
||
1075 | |||
1076 | /** |
||
1077 | * Detect if URL is loaded from FacebookExternalHit |
||
1078 | * @return boolean True if it detects FacebookExternalHit otherwise false |
||
1079 | */ |
||
1080 | protected function checkFacebookExternalHit() |
||
1089 | |||
1090 | /** |
||
1091 | * Detect if URL is being loaded from internal Facebook browser |
||
1092 | * @return boolean True if it detects internal Facebook browser otherwise false |
||
1093 | */ |
||
1094 | protected function checkForFacebookIos() |
||
1102 | |||
1103 | /** |
||
1104 | * Detect Version for the Safari browser on iOS devices |
||
1105 | * @return boolean True if it detects the version correctly otherwise false |
||
1106 | */ |
||
1107 | View Code Duplication | protected function getSafariVersionOnIos() |
|
1117 | |||
1118 | /** |
||
1119 | * Detect Version for the Chrome browser on iOS devices |
||
1120 | * @return boolean True if it detects the version correctly otherwise false |
||
1121 | */ |
||
1122 | View Code Duplication | protected function getChromeVersionOnIos() |
|
1133 | |||
1134 | /** |
||
1135 | * Determine if the browser is iPhone or not (last updated 1.7) |
||
1136 | * @return boolean True if the browser is iPhone otherwise false |
||
1137 | */ |
||
1138 | View Code Duplication | protected function checkBrowseriPhone() |
|
1151 | |||
1152 | /** |
||
1153 | * Determine if the browser is iPad or not (last updated 1.7) |
||
1154 | * @return boolean True if the browser is iPad otherwise false |
||
1155 | */ |
||
1156 | View Code Duplication | protected function checkBrowseriPad() |
|
1169 | |||
1170 | /** |
||
1171 | * Determine if the browser is iPod or not (last updated 1.7) |
||
1172 | * @return boolean True if the browser is iPod otherwise false |
||
1173 | */ |
||
1174 | View Code Duplication | protected function checkBrowseriPod() |
|
1187 | |||
1188 | /** |
||
1189 | * Determine if the browser is Android or not (last updated 1.7) |
||
1190 | * @return boolean True if the browser is Android otherwise false |
||
1191 | */ |
||
1192 | protected function checkBrowserAndroid() |
||
1212 | |||
1213 | /** |
||
1214 | * Determine the user's platform (last updated 1.7) |
||
1215 | */ |
||
1216 | protected function checkPlatform() |
||
1255 | } |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: