Conditions | 10 |
Paths | 15 |
Total Lines | 49 |
Code Lines | 28 |
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 namespace Zhuzhichao\IpLocationZh; |
||
18 | public static function find($ip) |
||
19 | { |
||
20 | if (empty( $ip ) === true) { |
||
21 | return 'N/A'; |
||
22 | } |
||
23 | |||
24 | $nip = gethostbyname($ip); |
||
25 | $ipdot = explode('.', $nip); |
||
26 | |||
27 | if ($ipdot[0] < 0 || $ipdot[0] > 255 || count($ipdot) !== 4) { |
||
28 | return 'N/A'; |
||
29 | } |
||
30 | |||
31 | if (isset( self::$cached[$nip] ) === true) { |
||
32 | return self::$cached[$nip]; |
||
33 | } |
||
34 | |||
35 | if (self::$fp === null) { |
||
36 | self::init(); |
||
37 | } |
||
38 | |||
39 | $nip2 = pack('N', ip2long($nip)); |
||
40 | |||
41 | $tmp_offset = (int) $ipdot[0] * 4; |
||
42 | $start = unpack('Vlen', |
||
43 | self::$index[$tmp_offset].self::$index[$tmp_offset + 1].self::$index[$tmp_offset + 2].self::$index[$tmp_offset + 3]); |
||
44 | |||
45 | $index_offset = $index_length = null; |
||
46 | $max_comp_len = self::$offset['len'] - 1024 - 4; |
||
47 | for ($start = $start['len'] * 8 + 1024; $start < $max_comp_len; $start += 8) { |
||
48 | if (self::$index{$start}.self::$index{$start + 1}.self::$index{$start + 2}.self::$index{$start + 3} >= $nip2) { |
||
49 | $index_offset = unpack('Vlen', |
||
50 | self::$index{$start + 4}.self::$index{$start + 5}.self::$index{$start + 6}."\x0"); |
||
51 | $index_length = unpack('Clen', self::$index{$start + 7}); |
||
52 | |||
53 | break; |
||
54 | } |
||
55 | } |
||
56 | |||
57 | if ($index_offset === null) { |
||
58 | return 'N/A'; |
||
59 | } |
||
60 | |||
61 | fseek(self::$fp, self::$offset['len'] + $index_offset['len'] - 1024); |
||
62 | |||
63 | self::$cached[$nip] = explode("\t", fread(self::$fp, $index_length['len'])); |
||
64 | |||
65 | return self::$cached[$nip]; |
||
66 | } |
||
67 | |||
93 | } |