| Conditions | 13 |
| Paths | 102 |
| Total Lines | 68 |
| Code Lines | 35 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 14 | public static function details($ua) |
||
| 15 | { |
||
| 16 | $ua = is_null($ua) ? $_SERVER['HTTP_USER_AGENT'] : $ua; |
||
| 17 | // Enumerate all common platforms, this is usually placed in braces (order is important! First come first serve..) |
||
|
|
|||
| 18 | $platforms = 'Windows|iPad|iPhone|Macintosh|Android|BlackBerry|Unix|Linux'; |
||
| 19 | |||
| 20 | // All browsers except MSIE/Trident and.. |
||
| 21 | // NOT for browsers that use this syntax: Version/0.xx Browsername |
||
| 22 | $browsers = 'Firefox|Chrome|Opera'; |
||
| 23 | |||
| 24 | // Specifically for browsers that use this syntax: Version/0.xx Browername |
||
| 25 | $browsers_v = 'Safari|Mobile'; // Mobile is mentioned in Android and BlackBerry UA's |
||
| 26 | |||
| 27 | // Fill in your most common engines.. |
||
| 28 | $engines = 'Gecko|Trident|Webkit|Presto'; |
||
| 29 | |||
| 30 | // Regex the crap out of the user agent, making multiple selections and.. |
||
| 31 | $regex_pat = "/((Mozilla)\/[\d\.]+|(Opera)\/[\d\.]+)\s\(.*?((MSIE)\s([\d\.]+).*?(Windows)|({$platforms})).*?\s.*?({$engines})[\/\s]+[\d\.]+(\;\srv\:([\d\.]+)|.*?).*?(Version[\/\s]([\d\.]+)(.*?({$browsers_v})|$)|(({$browsers})[\/\s]+([\d\.]+))|$).*/i"; |
||
| 32 | |||
| 33 | // .. placing them in this order, delimited by | |
||
| 34 | $replace_pat = '$7$8|$2$3|$9|${17}${15}$5$3|${18}${13}$6${11}'; |
||
| 35 | |||
| 36 | // Run the preg_replace .. and explode on | |
||
| 37 | $ua_array = explode('|', preg_replace($regex_pat, $replace_pat, $ua, PREG_PATTERN_ORDER)); |
||
| 38 | |||
| 39 | if (count($ua_array) > 1) { |
||
| 40 | $return['platform'] = $ua_array[0]; // Windows / iPad / MacOS / BlackBerry |
||
| 41 | $return['type'] = $ua_array[1]; // Mozilla / Opera etc. |
||
| 42 | $return['renderer'] = $ua_array[2]; // WebKit / Presto / Trident / Gecko etc. |
||
| 43 | $return['browser'] = $ua_array[3]; // Chrome / Safari / MSIE / Firefox |
||
| 44 | |||
| 45 | /* |
||
| 46 | Not necessary but this will filter out Chromes ridiculously long version |
||
| 47 | numbers 31.0.1234.122 becomes 31.0, while a "normal" 3 digit version number |
||
| 48 | like 10.2.1 would stay 10.2.1, 11.0 stays 11.0. Non-match stays what it is. |
||
| 49 | */ |
||
| 50 | if (preg_match("/^[\d]+\.[\d]+(?:\.[\d]{0,2}$)?/", $ua_array[4], $matches)) { |
||
| 51 | $return['version'] = $matches[0]; |
||
| 52 | } else { |
||
| 53 | $return['version'] = $ua_array[4]; |
||
| 54 | } |
||
| 55 | } else { |
||
| 56 | return false; |
||
| 57 | } |
||
| 58 | |||
| 59 | // Replace some browsernames e.g. MSIE -> Internet Explorer |
||
| 60 | switch (strtolower($return['browser'])) { |
||
| 61 | case 'msie': |
||
| 62 | case 'trident': |
||
| 63 | $return['browser'] = 'Internet Explorer'; |
||
| 64 | break; |
||
| 65 | case '': // IE 11 is a steamy turd (thanks Microsoft...) |
||
| 66 | if (strtolower($return['renderer']) == 'trident') { |
||
| 67 | $return['browser'] = 'Internet Explorer'; |
||
| 68 | } |
||
| 69 | break; |
||
| 70 | } |
||
| 71 | |||
| 72 | switch (strtolower($return['platform'])) { |
||
| 73 | case 'android': // These browsers claim to be Safari but are BB Mobile |
||
| 74 | case 'blackberry': // and Android Mobile |
||
| 75 | if ($return['browser'] == 'Safari' || $return['browser'] == 'Mobile' || $return['browser'] == '') { |
||
| 76 | $return['browser'] = "{$return['platform']} mobile"; |
||
| 77 | } |
||
| 78 | break; |
||
| 79 | } |
||
| 80 | |||
| 81 | return $return; |
||
| 82 | } |
||
| 106 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.