Passed
Push — v6 ( aac15a...ba732b )
by 光春
04:36
created

Requests   A

Complexity

Total Complexity 40

Size/Duplication

Total Lines 206
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 40
eloc 52
c 4
b 0
f 0
dl 0
loc 206
rs 9.2

16 Methods

Rating   Name   Duplication   Size   Complexity  
A isPost() 0 3 1
A isGet() 0 3 1
A isPut() 0 3 1
A isDelete() 0 3 1
A isAjax() 0 3 1
A getDeviceType() 0 9 4
A isWeiXin() 0 3 1
A isAliPay() 0 3 1
A isWeiXinMp() 0 3 1
A isQQBrowser() 0 6 2
A isEmptyRet() 0 8 3
B isMobile() 0 33 9
A getMobileType() 0 11 4
A isQQ() 0 3 2
A isEmpty() 0 8 3
A getWebsiteAddress() 0 4 5

How to fix   Complexity   

Complex Class

Complex classes like Requests 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.

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 Requests, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
// +----------------------------------------------------------------------
4
// | ThinkLibrary 6.0 for ThinkPhP 6.0
5
// +----------------------------------------------------------------------
6
// | 版权所有 2017~2020 [ https://www.dtapp.net ]
7
// +----------------------------------------------------------------------
8
// | 官方网站: https://gitee.com/liguangchun/ThinkLibrary
9
// +----------------------------------------------------------------------
10
// | 开源协议 ( https://mit-license.org )
11
// +----------------------------------------------------------------------
12
// | gitee 仓库地址 :https://gitee.com/liguangchun/ThinkLibrary
13
// | github 仓库地址 :https://github.com/GC0202/ThinkLibrary
14
// | gitlab 仓库地址 :https://gitlab.com/liguangchun/thinklibrary
15
// | weixin 仓库地址 :https://git.weixin.qq.com/liguangchun/ThinkLibrary
16
// | huaweicloud 仓库地址 :https://codehub-cn-south-1.devcloud.huaweicloud.com/composer00001/ThinkLibrary.git
17
// | Packagist 地址 :https://packagist.org/packages/liguangchun/think-library
18
// +----------------------------------------------------------------------
19
20
declare (strict_types=1);
21
22
namespace DtApp\ThinkLibrary\helper;
23
24
/**
25
 * 请求管理类
26
 * @mixin Requests
27
 * @package DtApp\ThinkLibrary\helper
28
 */
29
class Requests
30
{
31
    /**
32
     * 判断输入的参数
33
     * @param array $data
34
     * @param array $arr
35
     * @return array
36
     */
37
    public function isEmpty(array $data, array $arr): array
38
    {
39
        foreach ($arr as $k => $v) {
40
            if (empty($data[(string)$v] ?? '')) {
41
                return [];
42
            }
43
        }
44
        return $data;
45
    }
46
47
    /**
48
     * 判断输入的参数为空就返回Json错误
49
     * @param array $data
50
     * @param array $arr
51
     * @return array
52
     */
53
    public function isEmptyRet(array $data, array $arr): array
54
    {
55
        foreach ($arr as $k => $v) {
56
            if (empty($data[(string)$v] ?? '')) {
57
                (new Returns)->jsonError('请检查参数', 102);
58
            }
59
        }
60
        return $data;
61
    }
62
63
    /**
64
     * 判断是否为GET方式
65
     * @return bool
66
     */
67
    public function isGet(): bool
68
    {
69
        return request()->isGet();
70
    }
71
72
    /**
73
     * 判断是否为POST方式
74
     * @return bool
75
     */
76
    public function isPost(): bool
77
    {
78
        return request()->isPost();
79
    }
80
81
    /**
82
     * 判断是否为PUT方式
83
     * @return boolean
84
     */
85
    public function isPut(): bool
86
    {
87
        return request()->isPut();
88
    }
89
90
    /**
91
     * 判断是否为DELETE方式
92
     * @return boolean
93
     */
94
    public function isDelete(): bool
95
    {
96
        return request()->isDelete();
97
    }
98
99
    /**
100
     * 判断是否为Ajax方式
101
     * @return bool
102
     */
103
    public function isAjax(): bool
104
    {
105
        return request()->isAjax();
106
    }
107
108
    /**
109
     * 判断是否为移动端访问
110
     * @return bool
111
     */
112
    public function isMobile(): bool
113
    {
114
        // 如果有HTTP_X_WAP_PROFILE则一定是移动设备
115
        if (isset($_SERVER['HTTP_X_WAP_PROFILE'])) {
116
            return true;
117
        }
118
        //如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
119
        //找不到为flase,否则为true
120
        if (isset($_SERVER['HTTP_VIA'])) {
121
            return stripos(request()->server('HTTP_VIA'), "wap") !== false;
122
        }
123
        //判断手机发送的客户端标志
124
        if (isset($_SERVER['HTTP_USER_AGENT'])) {
125
            $clientkeywords = [
126
                'nokia', 'sony', 'ericsson', 'mot', 'samsung', 'htc', 'sgh', 'lg', 'sharp',
127
                'sie-', 'philips', 'panasonic', 'alcatel', 'lenovo', 'iphone', 'ipod', 'blackberry', 'meizu',
128
                'android', 'netfront', 'symbian', 'ucweb', 'windowsce', 'palm', 'operamini', 'operamobi',
129
                'openwave', 'nexusone', 'cldc', 'midp', 'wap', 'mobile', 'alipay'
130
            ];
131
            // 从HTTP_USER_AGENT中查找手机浏览器的关键字
132
            if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower(request()->server('HTTP_USER_AGENT')))) {
133
                return true;
134
            }
135
        }
136
        //协议法,因为有可能不准确,放到最后判断
137
        if (isset($_SERVER['HTTP_ACCEPT'])) {
138
            // 如果只支持wml并且不支持html那一定是移动设备
139
            // 如果支持wml和html但是wml在html之前则是移动设备
140
            if ((strpos(request()->server('HTTP_ACCEPT'), 'vnd.wap.wml') !== false) && (strpos(request()->server('HTTP_ACCEPT'), 'text/html') === false || (strpos(request()->server('HTTP_ACCEPT'), 'vnd.wap.wml') < strpos(request()->server('HTTP_ACCEPT'), 'text/html')))) {
141
                return true;
142
            }
143
        }
144
        return false;
145
    }
146
147
    /**
148
     * 判断是否微信内置浏览器访问
149
     * @return bool
150
     */
151
    public function isWeiXin(): bool
152
    {
153
        return strpos(request()->server('HTTP_USER_AGENT'), 'MicroMessenger') !== false;
154
    }
155
156
    /**
157
     * 判断是否为微信小程序访问
158
     * @return bool
159
     */
160
    public function isWeiXinMp(): bool
161
    {
162
        return strpos(request()->server('HTTP_USER_AGENT'), 'miniProgram') !== false;
163
    }
164
165
    /**
166
     * 判断是否支付宝内置浏览器访问
167
     * @return bool
168
     */
169
    public function isAliPay(): bool
170
    {
171
        return strpos(request()->server('HTTP_USER_AGENT'), 'Alipay') !== false;
172
    }
173
174
    /**
175
     * 判断是否QQ内置浏览器访问
176
     * @return bool
177
     */
178
    public function isQQ(): bool
179
    {
180
        return (strpos(request()->server('HTTP_USER_AGENT'), 'QQ') !== false) && strpos(request()->server('HTTP_USER_AGENT'), '_SQ_') !== false;
181
    }
182
183
    /**
184
     * 判断是否QQ浏览器访问
185
     * @return bool
186
     */
187
    public function isQQBrowser(): bool
188
    {
189
        if (strpos(request()->server('HTTP_USER_AGENT'), 'QQ') !== false) {
190
            return !(strpos(request()->server('HTTP_USER_AGENT'), '_SQ_') !== false);
191
        }
192
        return false;
193
    }
194
195
    /**
196
     * 获取客户端类型
197
     * @return string
198
     */
199
    public function getDeviceType()
200
    {
201
        $agent = strtolower(request()->server('HTTP_USER_AGENT'));
202
        if (strpos($agent, 'iphone') || strpos($agent, 'ipad') || strpos($agent, 'android')) {
203
            $type = 'mobile';
204
        } else {
205
            $type = 'computer';
206
        }
207
        return $type;
208
    }
209
210
    /**
211
     * 获取手机设备类型
212
     * @return string
213
     */
214
    public function getMobileType()
215
    {
216
        $agent = strtolower(request()->server('HTTP_USER_AGENT'));
217
        $type = 'other';
218
        if (strpos($agent, 'iphone') || strpos($agent, 'ipad')) {
219
            $type = 'ios';
220
        }
221
        if (strpos($agent, 'android')) {
222
            $type = 'android';
223
        }
224
        return $type;
225
    }
226
227
    /**
228
     * 获取域名地址
229
     * @return string
230
     */
231
    public function getWebsiteAddress(): string
232
    {
233
        $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
234
        return $http_type . $_SERVER['HTTP_HOST'] . "/";
235
    }
236
}
237