Conditions | 12 |
Paths | 14 |
Total Lines | 34 |
Code Lines | 21 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
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 |
||
95 | public static function checkIP6($requestIP, $ip) { |
||
96 | if (!((extension_loaded('sockets') && defined('AF_INET6')) || @inet_pton('::1'))) { |
||
97 | throw new \RuntimeException('Unable to check IPv6. Check that PHP was not compiled with option "disable-ipv6".'); |
||
98 | } |
||
99 | |||
100 | if (false !== strpos($ip, '/')) { |
||
101 | list($address, $netmask) = explode('/', $ip, 2); |
||
102 | |||
103 | if ($netmask < 1 || $netmask > 128) { |
||
104 | return false; |
||
105 | } |
||
106 | } else { |
||
107 | $address = $ip; |
||
108 | $netmask = 128; |
||
109 | } |
||
110 | |||
111 | $bytesAddr = unpack('n*', @inet_pton($address)); |
||
112 | $bytesTest = unpack('n*', @inet_pton($requestIP)); |
||
113 | |||
114 | if (!$bytesAddr || !$bytesTest) { |
||
115 | return false; |
||
116 | } |
||
117 | |||
118 | for ($i = 1, $ceil = ceil($netmask / 16); $i <= $ceil; ++$i) { |
||
119 | $left = $netmask - 16 * ($i - 1); |
||
120 | $left = ($left <= 16) ? $left : 16; |
||
121 | $mask = ~(0xffff >> $left) & 0xffff; |
||
122 | if (($bytesAddr[$i] & $mask) != ($bytesTest[$i] & $mask)) { |
||
123 | return false; |
||
124 | } |
||
125 | } |
||
126 | |||
127 | return true; |
||
128 | } |
||
129 | } |
||
130 |