| Total Complexity | 46 |
| Total Lines | 283 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like IpIpReader 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 IpIpReader, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class IpIpReader |
||
| 24 | { |
||
| 25 | public const IPV4 = 1; |
||
| 26 | public const IPV6 = 2; |
||
| 27 | |||
| 28 | private $file; |
||
| 29 | private $fileSize = 0; |
||
| 30 | private $nodeCount = 0; |
||
| 31 | private $nodeOffset = 0; |
||
| 32 | |||
| 33 | private $meta = []; |
||
| 34 | |||
| 35 | private $database; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Reader constructor. |
||
| 39 | * @param $database |
||
| 40 | * @throws Exception |
||
| 41 | */ |
||
| 42 | public function __construct($database) |
||
| 43 | { |
||
| 44 | $this->database = $database; |
||
| 45 | |||
| 46 | $this->init(); |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @throws Exception |
||
| 51 | */ |
||
| 52 | private function init(): void |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @param $ip |
||
| 86 | * @param string $language |
||
| 87 | * @return array|NULL |
||
| 88 | */ |
||
| 89 | public function find($ip, $language): ?array |
||
| 90 | { |
||
| 91 | if (is_resource($this->file) === FALSE) { |
||
| 92 | throw new BadMethodCallException('IPIP DB closed.'); |
||
| 93 | } |
||
| 94 | |||
| 95 | if (isset($this->meta['languages'][$language]) === FALSE) { |
||
| 96 | throw new InvalidArgumentException("language : {$language} not support."); |
||
| 97 | } |
||
| 98 | |||
| 99 | if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6) === FALSE) { |
||
| 100 | throw new InvalidArgumentException("The value \"$ip\" is not a valid IP address."); |
||
| 101 | } |
||
| 102 | |||
| 103 | if (strpos($ip, '.') !== FALSE && !$this->supportV4()) { |
||
| 104 | throw new InvalidArgumentException("The Database not support IPv4 address."); |
||
| 105 | } |
||
| 106 | |||
| 107 | if (strpos($ip, ':') !== FALSE && !$this->supportV6()) { |
||
| 108 | throw new InvalidArgumentException("The Database not support IPv6 address."); |
||
| 109 | } |
||
| 110 | |||
| 111 | try { |
||
| 112 | $node = $this->findNode($ip); |
||
| 113 | |||
| 114 | if ($node > 0) { |
||
| 115 | $data = $this->resolve($node); |
||
| 116 | |||
| 117 | $values = explode("\t", $data); |
||
|
|
|||
| 118 | |||
| 119 | return array_slice($values, $this->meta['languages'][$language], count($this->meta['fields'])); |
||
| 120 | } |
||
| 121 | } catch (Exception $e) { |
||
| 122 | return NULL; |
||
| 123 | } |
||
| 124 | |||
| 125 | return NULL; |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @param $ip |
||
| 130 | * @param $language |
||
| 131 | * @return array|false|null |
||
| 132 | */ |
||
| 133 | public function findMap($ip, $language) |
||
| 134 | { |
||
| 135 | $array = $this->find($ip, $language); |
||
| 136 | if (NULL === $array) { |
||
| 137 | return NULL; |
||
| 138 | } |
||
| 139 | |||
| 140 | return array_combine($this->meta['fields'], $array); |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @var int |
||
| 145 | */ |
||
| 146 | private $v4offset = 0; |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @var array |
||
| 150 | */ |
||
| 151 | private $v6offsetCache = []; |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @param $ip |
||
| 155 | * @return int |
||
| 156 | * @throws Exception |
||
| 157 | */ |
||
| 158 | private function findNode($ip): int |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @param $node |
||
| 214 | * @param $index |
||
| 215 | * @return mixed |
||
| 216 | * @throws Exception |
||
| 217 | */ |
||
| 218 | private function readNode($node, $index) |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @param $node |
||
| 225 | * @return mixed |
||
| 226 | * @throws Exception |
||
| 227 | */ |
||
| 228 | private function resolve($node) |
||
| 229 | { |
||
| 230 | $resolved = $node - $this->nodeCount + $this->nodeCount * 8; |
||
| 231 | if ($resolved >= $this->fileSize) { |
||
| 232 | return NULL; |
||
| 233 | } |
||
| 234 | |||
| 235 | $bytes = $this->read($this->file, $resolved, 2); |
||
| 236 | $size = unpack('N', str_pad($bytes, 4, "\x00", STR_PAD_LEFT))[1]; |
||
| 237 | |||
| 238 | $resolved += 2; |
||
| 239 | |||
| 240 | return $this->read($this->file, $resolved, $size); |
||
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * |
||
| 245 | */ |
||
| 246 | public function close(): void |
||
| 247 | { |
||
| 248 | if (is_resource($this->file) === TRUE) { |
||
| 249 | fclose($this->file); |
||
| 250 | } |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @param $stream |
||
| 255 | * @param $offset |
||
| 256 | * @param $length |
||
| 257 | * @return bool|string |
||
| 258 | * @throws Exception |
||
| 259 | */ |
||
| 260 | private function read($stream, $offset, $length) |
||
| 261 | { |
||
| 262 | if ($length > 0) { |
||
| 263 | if (fseek($stream, $offset + $this->nodeOffset) === 0) { |
||
| 264 | $value = fread($stream, $length); |
||
| 265 | if (strlen($value) === $length) { |
||
| 266 | return $value; |
||
| 267 | } |
||
| 268 | } |
||
| 269 | |||
| 270 | throw new Exception("The Database file read bad data."); |
||
| 271 | } |
||
| 272 | |||
| 273 | return ''; |
||
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * @return bool |
||
| 278 | */ |
||
| 279 | public function supportV6(): bool |
||
| 280 | { |
||
| 281 | return ($this->meta['ip_version'] & self::IPV6) === self::IPV6; |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @return bool |
||
| 286 | */ |
||
| 287 | public function supportV4(): bool |
||
| 288 | { |
||
| 289 | return ($this->meta['ip_version'] & self::IPV4) === self::IPV4; |
||
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @return array |
||
| 294 | */ |
||
| 295 | public function getMeta() |
||
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * @return int UTC Timestamp |
||
| 302 | */ |
||
| 303 | public function getBuildTime() |
||
| 306 | } |
||
| 307 | } |
||
| 308 |