| Conditions | 10 |
| Paths | 10 |
| Total Lines | 61 |
| Code Lines | 44 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 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 |
||
| 59 | public static function isMobile() |
||
| 60 | { |
||
| 61 | // 如果有HTTP_X_WAP_PROFILE则一定是移动设备 |
||
| 62 | if (isset ($_SERVER['HTTP_X_WAP_PROFILE'])) { |
||
| 63 | return true; |
||
| 64 | } |
||
| 65 | // 如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息 |
||
| 66 | if (isset ($_SERVER['HTTP_VIA'])) { |
||
| 67 | // 找不到为flase,否则为true |
||
| 68 | return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false; |
||
| 69 | } |
||
| 70 | // 脑残法,判断手机发送的客户端标志,兼容性有待提高 |
||
| 71 | if (isset ($_SERVER['HTTP_USER_AGENT'])) { |
||
| 72 | $clientKeyWords = array( |
||
| 73 | 'nokia', |
||
| 74 | 'sony', |
||
| 75 | 'ericsson', |
||
| 76 | 'mot', |
||
| 77 | 'samsung', |
||
| 78 | 'htc', |
||
| 79 | 'sgh', |
||
| 80 | 'lg', |
||
| 81 | 'sharp', |
||
| 82 | 'sie-', |
||
| 83 | 'philips', |
||
| 84 | 'panasonic', |
||
| 85 | 'alcatel', |
||
| 86 | 'lenovo', |
||
| 87 | 'iphone', |
||
| 88 | 'ipod', |
||
| 89 | 'blackberry', |
||
| 90 | 'meizu', |
||
| 91 | 'android', |
||
| 92 | 'netfront', |
||
| 93 | 'symbian', |
||
| 94 | 'ucweb', |
||
| 95 | 'windowsce', |
||
| 96 | 'palm', |
||
| 97 | 'operamini', |
||
| 98 | 'operamobi', |
||
| 99 | 'openwave', |
||
| 100 | 'nexusone', |
||
| 101 | 'cldc', |
||
| 102 | 'midp', |
||
| 103 | 'wap', |
||
| 104 | 'mobile' |
||
| 105 | ); |
||
| 106 | // 从HTTP_USER_AGENT中查找手机浏览器的关键字 |
||
| 107 | if (preg_match("/(" . implode('|', $clientKeyWords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) { |
||
| 108 | return true; |
||
| 109 | } |
||
| 110 | } |
||
| 111 | // 协议法,因为有可能不准确,放到最后判断 |
||
| 112 | if (isset ($_SERVER['HTTP_ACCEPT'])) { |
||
| 113 | // 如果只支持wml并且不支持html那一定是移动设备 |
||
| 114 | // 如果支持wml和html但是wml在html之前则是移动设备 |
||
| 115 | if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html')))) { |
||
| 116 | return true; |
||
| 117 | } |
||
| 118 | } |
||
| 119 | return false; |
||
| 120 | } |
||
| 149 |