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