| Conditions | 13 |
| Paths | 136 |
| Total Lines | 112 |
| Code Lines | 85 |
| 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 |
||
| 151 | public function getLocation(string $ip = '') |
||
| 152 | { |
||
| 153 | if (empty($ip)) { |
||
| 154 | $ip = get_ip(); |
||
| 155 | } |
||
| 156 | if (strpos($ip, 'http://') === 0) { |
||
| 157 | $ip = substr($ip, 7); |
||
| 158 | $ip = gethostbyname($ip); |
||
| 159 | } |
||
| 160 | static $locationData = []; |
||
| 161 | if (!isset($locationData[$ip])) { |
||
| 162 | if (!$this->fp) { |
||
| 163 | // 如果数据文件没有被正确打开,则直接返回错误 |
||
| 164 | throw new DtaException('数据库文件不存在!'); |
||
| 165 | } |
||
| 166 | $location['ip'] = $ip; // 将输入的域名转化为IP地址 |
||
| 167 | $ip = $this->packIp($location['ip']); // 将输入的IP地址转化为可比较的IP地址 |
||
| 168 | // 不合法的IP地址会被转化为255.255.255.255 |
||
| 169 | // 对分搜索 |
||
| 170 | $l = 0; // 搜索的下边界 |
||
| 171 | $u = $this->totalIp; // 搜索的上边界 |
||
| 172 | $findip = $this->lastIp; // 如果没有找到就返回最后一条IP记录(QQWry.Dat的版本信息) |
||
| 173 | while ($l <= $u) { // 当上边界小于下边界时,查找失败 |
||
| 174 | $i = floor(($l + $u) / 2); // 计算近似中间记录 |
||
| 175 | fseek($this->fp, $this->firstIp + $i * 7); |
||
| 176 | $beginip = strrev(fread($this->fp, 4)); // 获取中间记录的开始IP地址 |
||
| 177 | // strrev函数在这里的作用是将little-endian的压缩IP地址转化为big-endian的格式 |
||
| 178 | // 以便用于比较,后面相同。 |
||
| 179 | if ($ip < $beginip) { // 用户的IP小于中间记录的开始IP地址时 |
||
| 180 | $u = $i - 1; // 将搜索的上边界修改为中间记录减一 |
||
| 181 | } else { |
||
| 182 | fseek($this->fp, $this->getLong3()); |
||
| 183 | $endip = strrev(fread($this->fp, 4)); // 获取中间记录的结束IP地址 |
||
| 184 | if ($ip > $endip) { // 用户的IP大于中间记录的结束IP地址时 |
||
| 185 | $l = $i + 1; // 将搜索的下边界修改为中间记录加一 |
||
| 186 | } else { // 用户的IP在中间记录的IP范围内时 |
||
| 187 | $findip = $this->firstIp + $i * 7; |
||
| 188 | break; // 则表示找到结果,退出循环 |
||
| 189 | } |
||
| 190 | } |
||
| 191 | } |
||
| 192 | //获取查找到的IP地理位置信息 |
||
| 193 | fseek($this->fp, $findip); |
||
| 194 | $location['beginip'] = long2ip($this->getLong()); // 用户IP所在范围的开始地址 |
||
| 195 | $offset = $this->getLong3(); |
||
| 196 | fseek($this->fp, $offset); |
||
| 197 | $location['endip'] = long2ip($this->getLong()); // 用户IP所在范围的结束地址 |
||
| 198 | $byte = fread($this->fp, 1); // 标志字节 |
||
| 199 | switch (ord($byte)) { |
||
| 200 | case 1: // 标志字节为1,表示国家和区域信息都被同时重定向 |
||
| 201 | $countryOffset = $this->getLong3(); // 重定向地址 |
||
| 202 | fseek($this->fp, $countryOffset); |
||
| 203 | $byte = fread($this->fp, 1); // 标志字节 |
||
| 204 | switch (ord($byte)) { |
||
| 205 | case 2: // 标志字节为2,表示国家信息又被重定向 |
||
| 206 | fseek($this->fp, $this->getLong3()); |
||
| 207 | $location['all'] = $this->getString(); |
||
| 208 | fseek($this->fp, $countryOffset + 4); |
||
| 209 | $location['extend'] = $this->getExtendString(); |
||
| 210 | break; |
||
| 211 | default: // 否则,表示国家信息没有被重定向 |
||
| 212 | $location['all'] = $this->getString($byte); |
||
| 213 | $location['extend'] = $this->getExtendString(); |
||
| 214 | break; |
||
| 215 | } |
||
| 216 | break; |
||
| 217 | case 2: // 标志字节为2,表示国家信息被重定向 |
||
| 218 | fseek($this->fp, $this->getLong3()); |
||
| 219 | $location['all'] = $this->getString(); |
||
| 220 | fseek($this->fp, $offset + 8); |
||
| 221 | $location['extend'] = $this->getExtendString(); |
||
| 222 | break; |
||
| 223 | default: // 否则,表示国家信息没有被重定向 |
||
| 224 | $location['all'] = $this->getString($byte); |
||
| 225 | $location['extend'] = $this->getExtendString(); |
||
| 226 | break; |
||
| 227 | } |
||
| 228 | // CZ88.NET表示没有有效信息 |
||
| 229 | if (trim($location['all']) === 'CZ88.NET') { |
||
| 230 | $location['all'] = $this->unknown; |
||
| 231 | } |
||
| 232 | if (trim($location['extend']) === 'CZ88.NET') { |
||
| 233 | $location['extend'] = ''; |
||
| 234 | } |
||
| 235 | $location['all'] = iconv("gb2312", "UTF-8//IGNORE", $location['all']); |
||
| 236 | $location['extend'] = iconv("gb2312", "UTF-8//IGNORE", $location['extend']); |
||
| 237 | $location['extend'] = $location['extend'] ?? ''; |
||
| 238 | $parseData = $this->parseLocation($location['all']); |
||
| 239 | $location['state'] = $parseData[0]; |
||
| 240 | $location['city'] = $parseData[1]; |
||
| 241 | $location['area'] = $parseData[2]; |
||
| 242 | |||
| 243 | // 全部地址 |
||
| 244 | $res['location_all'] = $location['all']; |
||
| 245 | // 运营商 |
||
| 246 | $res['isp']['name'] = $location['extend']; |
||
| 247 | // IP |
||
| 248 | $res['ip']['ipv4'] = $location['ip']; |
||
| 249 | $res['ip']['beginip'] = $location['beginip']; |
||
| 250 | $res['ip']['endip'] = $location['endip']; |
||
| 251 | $res['ip']['trueip'] = ip2long($location['ip']); |
||
| 252 | $res['ip']['ipv6'] = $this->getNormalizedIP($location['ip']); |
||
| 253 | $getAdCodeLatLng = $this->getNameAdCodeLatLng($location['state'], $location['city'], $location['area']); |
||
| 254 | // 省份 |
||
| 255 | $res['province'] = $getAdCodeLatLng['province']; |
||
| 256 | // 城市 |
||
| 257 | $res['city'] = $getAdCodeLatLng['city']; |
||
| 258 | // 地区 |
||
| 259 | $res['district'] = $getAdCodeLatLng['district']; |
||
| 260 | $locationData[$ip] = $location; |
||
| 261 | } |
||
| 262 | return $res; |
||
| 263 | } |
||
| 467 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..