Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like BufferUnpacker 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 BufferUnpacker, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class BufferUnpacker |
||
| 20 | { |
||
| 21 | const INT_AS_EXCEPTION = 0; |
||
| 22 | const INT_AS_STR = 1; |
||
| 23 | const INT_AS_GMP = 2; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var int |
||
| 27 | */ |
||
| 28 | private $intOverflowMode = self::INT_AS_EXCEPTION; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | private $buffer = ''; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var int |
||
| 37 | */ |
||
| 38 | private $offset = 0; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var Collection |
||
| 42 | */ |
||
| 43 | private $transformers; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @param int|null $intOverflowMode |
||
| 47 | */ |
||
| 48 | 119 | public function __construct($intOverflowMode = null) |
|
| 54 | |||
| 55 | /** |
||
| 56 | * @param Collection|null $transformers |
||
| 57 | */ |
||
| 58 | 2 | public function setTransformers(Collection $transformers = null) |
|
| 62 | |||
| 63 | /** |
||
| 64 | * @return Collection|null |
||
| 65 | */ |
||
| 66 | 1 | public function getTransformers() |
|
| 70 | |||
| 71 | /** |
||
| 72 | * @param int $intOverflowMode |
||
| 73 | * |
||
| 74 | * @throws \InvalidArgumentException |
||
| 75 | */ |
||
| 76 | 2 | public function setIntOverflowMode($intOverflowMode) |
|
| 88 | |||
| 89 | /** |
||
| 90 | * @return int |
||
| 91 | */ |
||
| 92 | 1 | public function getIntOverflowMode() |
|
| 96 | |||
| 97 | /** |
||
| 98 | * @param string $data |
||
| 99 | * |
||
| 100 | * @return $this |
||
| 101 | */ |
||
| 102 | 5 | public function append($data) |
|
| 108 | |||
| 109 | /** |
||
| 110 | * @param string $buffer |
||
| 111 | * |
||
| 112 | * @return $this |
||
| 113 | */ |
||
| 114 | 112 | public function reset($buffer = '') |
|
| 121 | |||
| 122 | /** |
||
| 123 | * @return array |
||
| 124 | */ |
||
| 125 | 3 | public function tryUnpack() |
|
| 146 | |||
| 147 | /** |
||
| 148 | * @return mixed |
||
| 149 | * |
||
| 150 | * @throws UnpackingFailedException |
||
| 151 | */ |
||
| 152 | 115 | public function unpack() |
|
| 153 | { |
||
| 154 | 115 | if (!isset($this->buffer[$this->offset])) { |
|
| 155 | 4 | throw new InsufficientDataException(1, \strlen($this->buffer) - $this->offset); |
|
| 156 | } |
||
| 157 | |||
| 158 | 113 | $c = \ord($this->buffer[$this->offset]); |
|
| 159 | 113 | ++$this->offset; |
|
| 160 | |||
| 161 | // fixint |
||
| 162 | 113 | if ($c <= 0x7f) { |
|
| 163 | 20 | return $c; |
|
| 164 | } |
||
| 165 | // fixstr |
||
| 166 | 110 | if ($c >= 0xa0 && $c <= 0xbf) { |
|
| 167 | 12 | return $this->unpackStr($c & 0x1f); |
|
| 168 | } |
||
| 169 | // fixarray |
||
| 170 | 105 | if ($c >= 0x90 && $c <= 0x9f) { |
|
| 171 | 6 | return $this->unpackArray($c & 0xf); |
|
| 172 | } |
||
| 173 | // fixmap |
||
| 174 | 102 | if ($c >= 0x80 && $c <= 0x8f) { |
|
| 175 | 11 | return $this->unpackMap($c & 0xf); |
|
| 176 | } |
||
| 177 | // negfixint |
||
| 178 | 98 | if ($c >= 0xe0) { |
|
| 179 | 4 | return $c - 256; |
|
| 180 | } |
||
| 181 | |||
| 182 | switch ($c) { |
||
| 183 | 94 | case 0xc0: return null; |
|
| 184 | 93 | case 0xc2: return false; |
|
| 185 | 91 | case 0xc3: return true; |
|
| 186 | |||
| 187 | // MP_BIN |
||
| 188 | 86 | case 0xc4: return $this->unpackStr($this->unpackUint8()); |
|
| 189 | 82 | case 0xc5: return $this->unpackStr($this->unpackUint16()); |
|
| 190 | 81 | case 0xc6: return $this->unpackStr($this->unpackUint32()); |
|
| 191 | |||
| 192 | // MP_FLOAT |
||
| 193 | 80 | case 0xca: return $this->unpackFloat32(); |
|
| 194 | 77 | case 0xcb: return $this->unpackFloat64(); |
|
| 195 | |||
| 196 | // MP_UINT |
||
| 197 | 73 | case 0xcc: return $this->unpackUint8(); |
|
| 198 | 69 | case 0xcd: return $this->unpackUint16(); |
|
| 199 | 64 | case 0xce: return $this->unpackUint32(); |
|
| 200 | 60 | case 0xcf: return $this->unpackUint64(); |
|
| 201 | |||
| 202 | // MP_INT |
||
| 203 | 52 | case 0xd0: return $this->unpackInt8(); |
|
| 204 | 47 | case 0xd1: return $this->unpackInt16(); |
|
| 205 | 42 | case 0xd2: return $this->unpackInt32(); |
|
| 206 | 37 | case 0xd3: return $this->unpackInt64(); |
|
| 207 | |||
| 208 | // MP_STR |
||
| 209 | 32 | case 0xd9: return $this->unpackStr($this->unpackUint8()); |
|
| 210 | 28 | case 0xda: return $this->unpackStr($this->unpackUint16()); |
|
| 211 | 26 | case 0xdb: return $this->unpackStr($this->unpackUint32()); |
|
| 212 | |||
| 213 | // MP_ARRAY |
||
| 214 | 25 | case 0xdc: return $this->unpackArray($this->unpackUint16()); |
|
| 215 | 23 | case 0xdd: return $this->unpackArray($this->unpackUint32()); |
|
| 216 | |||
| 217 | // MP_MAP |
||
| 218 | 22 | case 0xde: return $this->unpackMap($this->unpackUint16()); |
|
| 219 | 20 | case 0xdf: return $this->unpackMap($this->unpackUint32()); |
|
| 220 | |||
| 221 | // MP_EXT |
||
| 222 | 19 | case 0xd4: return $this->unpackExt(1); |
|
| 223 | 16 | case 0xd5: return $this->unpackExt(2); |
|
| 224 | 14 | case 0xd6: return $this->unpackExt(4); |
|
| 225 | 12 | case 0xd7: return $this->unpackExt(8); |
|
| 226 | 10 | case 0xd8: return $this->unpackExt(16); |
|
| 227 | 8 | case 0xc7: return $this->unpackExt($this->unpackUint8()); |
|
| 228 | 5 | case 0xc8: return $this->unpackExt($this->unpackUint16()); |
|
| 229 | 3 | case 0xc9: return $this->unpackExt($this->unpackUint32()); |
|
| 230 | } |
||
| 231 | |||
| 232 | 1 | throw new UnpackingFailedException(\sprintf('Unknown code: 0x%x.', $c)); |
|
| 233 | } |
||
| 234 | |||
| 235 | 18 | private function unpackUint8() |
|
| 236 | { |
||
| 237 | 18 | if (!isset($this->buffer[$this->offset])) { |
|
| 238 | 2 | throw new InsufficientDataException(1, \strlen($this->buffer) - $this->offset); |
|
| 239 | } |
||
| 240 | |||
| 241 | 16 | $num = $this->buffer[$this->offset]; |
|
| 242 | 16 | ++$this->offset; |
|
| 243 | |||
| 244 | 16 | return \ord($num); |
|
| 245 | } |
||
| 246 | |||
| 247 | 15 | private function unpackUint16() |
|
| 248 | { |
||
| 249 | 15 | if (!isset($this->buffer[$this->offset + 1])) { |
|
| 250 | 2 | throw new InsufficientDataException(2, \strlen($this->buffer) - $this->offset); |
|
| 251 | } |
||
| 252 | |||
| 253 | 13 | $hi = \ord($this->buffer[$this->offset]); |
|
| 254 | 13 | $lo = \ord($this->buffer[$this->offset + 1]); |
|
| 255 | 13 | $this->offset += 2; |
|
| 256 | |||
| 257 | 13 | return $hi << 8 | $lo; |
|
| 258 | } |
||
| 259 | |||
| 260 | 11 | View Code Duplication | private function unpackUint32() |
| 261 | { |
||
| 262 | 11 | if (!isset($this->buffer[$this->offset + 3])) { |
|
| 263 | 2 | throw new InsufficientDataException(4, \strlen($this->buffer) - $this->offset); |
|
| 264 | } |
||
| 265 | |||
| 266 | 9 | $num = \substr($this->buffer, $this->offset, 4); |
|
| 267 | 9 | $this->offset += 4; |
|
| 268 | |||
| 269 | 9 | $num = \unpack('N', $num); |
|
| 270 | |||
| 271 | 9 | return $num[1]; |
|
| 272 | } |
||
| 273 | |||
| 274 | 8 | private function unpackUint64() |
|
| 275 | { |
||
| 276 | 8 | if (!isset($this->buffer[$this->offset + 7])) { |
|
| 277 | 1 | throw new InsufficientDataException(8, \strlen($this->buffer) - $this->offset); |
|
| 278 | } |
||
| 279 | |||
| 280 | 7 | $num = \substr($this->buffer, $this->offset, 8); |
|
| 281 | 7 | $this->offset += 8; |
|
| 282 | |||
| 283 | //$num = \unpack('J', $num); |
||
| 284 | |||
| 285 | 7 | $set = \unpack('N2', $num); |
|
| 286 | 7 | $value = $set[1] << 32 | $set[2]; |
|
| 287 | |||
| 288 | // PHP does not support unsigned integers. |
||
| 289 | // If a number is bigger than 2^63, it will be interpreted as a float. |
||
| 290 | // @link http://php.net/manual/en/language.types.integer.php#language.types.integer.overflow |
||
| 291 | |||
| 292 | 7 | return ($value < 0) ? $this->handleIntOverflow($value) : $value; |
|
| 293 | } |
||
| 294 | |||
| 295 | 15 | private function unpackInt8() |
|
| 296 | { |
||
| 297 | 15 | if (!isset($this->buffer[$this->offset])) { |
|
| 298 | 1 | throw new InsufficientDataException(1, \strlen($this->buffer) - $this->offset); |
|
| 299 | } |
||
| 300 | |||
| 301 | 14 | $num = \ord($this->buffer[$this->offset]); |
|
| 302 | 14 | ++$this->offset; |
|
| 303 | |||
| 304 | 14 | if ($num > 0x7f) { |
|
| 305 | 3 | return $num - 256; |
|
| 306 | } |
||
| 307 | |||
| 308 | 11 | return $num; |
|
| 309 | } |
||
| 310 | |||
| 311 | 5 | private function unpackInt16() |
|
| 312 | { |
||
| 313 | 5 | if (!isset($this->buffer[$this->offset + 1])) { |
|
| 314 | 1 | throw new InsufficientDataException(2, \strlen($this->buffer) - $this->offset); |
|
| 315 | } |
||
| 316 | |||
| 317 | 4 | $hi = \ord($this->buffer[$this->offset]); |
|
| 318 | 4 | $lo = \ord($this->buffer[$this->offset + 1]); |
|
| 319 | 4 | $this->offset += 2; |
|
| 320 | |||
| 321 | 4 | if ($hi > 0x7f) { |
|
| 322 | 3 | return -(0x010000 - ($hi << 8 | $lo)); |
|
| 323 | } |
||
| 324 | |||
| 325 | 1 | return $hi << 8 | $lo; |
|
| 326 | } |
||
| 327 | |||
| 328 | 5 | View Code Duplication | private function unpackInt32() |
| 329 | { |
||
| 330 | 5 | if (!isset($this->buffer[$this->offset + 3])) { |
|
| 331 | 1 | throw new InsufficientDataException(4, \strlen($this->buffer) - $this->offset); |
|
| 332 | } |
||
| 333 | |||
| 334 | 4 | $num = \substr($this->buffer, $this->offset, 4); |
|
| 335 | 4 | $this->offset += 4; |
|
| 336 | |||
| 337 | 4 | $num = \unpack('i', \strrev($num)); |
|
| 338 | |||
| 339 | 4 | return $num[1]; |
|
| 340 | } |
||
| 341 | |||
| 342 | 5 | View Code Duplication | private function unpackInt64() |
| 343 | { |
||
| 344 | 5 | if (!isset($this->buffer[$this->offset + 7])) { |
|
| 345 | 1 | throw new InsufficientDataException(8, \strlen($this->buffer) - $this->offset); |
|
| 346 | } |
||
| 347 | |||
| 348 | 4 | $num = \substr($this->buffer, $this->offset, 8); |
|
| 349 | 4 | $this->offset += 8; |
|
| 350 | |||
| 351 | 4 | $set = \unpack('N2', $num); |
|
| 352 | |||
| 353 | 4 | return $set[1] << 32 | $set[2]; |
|
| 354 | } |
||
| 355 | |||
| 356 | 3 | View Code Duplication | private function unpackFloat32() |
| 357 | { |
||
| 358 | 3 | if (!isset($this->buffer[$this->offset + 3])) { |
|
| 359 | 1 | throw new InsufficientDataException(4, \strlen($this->buffer) - $this->offset); |
|
| 360 | } |
||
| 361 | |||
| 362 | 2 | $num = \substr($this->buffer, $this->offset, 4); |
|
| 363 | 2 | $this->offset += 4; |
|
| 364 | |||
| 365 | 2 | $num = \unpack('f', \strrev($num)); |
|
| 366 | |||
| 367 | 2 | return $num[1]; |
|
| 368 | } |
||
| 369 | |||
| 370 | 4 | View Code Duplication | private function unpackFloat64() |
| 371 | { |
||
| 372 | 4 | if (!isset($this->buffer[$this->offset + 7])) { |
|
| 373 | 1 | throw new InsufficientDataException(8, \strlen($this->buffer) - $this->offset); |
|
| 374 | } |
||
| 375 | |||
| 376 | 3 | $num = \substr($this->buffer, $this->offset, 8); |
|
| 377 | 3 | $this->offset += 8; |
|
| 378 | |||
| 379 | 3 | $num = \unpack('d', \strrev($num)); |
|
| 380 | |||
| 381 | 3 | return $num[1]; |
|
| 382 | } |
||
| 383 | |||
| 384 | 25 | private function unpackStr($length) |
|
| 399 | |||
| 400 | 9 | View Code Duplication | private function unpackArray($size) |
| 409 | |||
| 410 | 14 | View Code Duplication | private function unpackMap($size) |
| 419 | |||
| 420 | 15 | private function unpackExt($length) |
|
| 421 | { |
||
| 422 | 15 | if (!isset($this->buffer[$this->offset + $length - 1])) { |
|
| 423 | 5 | throw new InsufficientDataException($length, \strlen($this->buffer) - $this->offset); |
|
| 424 | } |
||
| 425 | |||
| 426 | 10 | $type = $this->unpackInt8(); |
|
| 427 | |||
| 428 | 10 | if ($this->transformers && $transformer = $this->transformers->find($type)) { |
|
| 429 | 1 | return $transformer->reverseTransform($this->unpack()); |
|
| 430 | } |
||
| 431 | |||
| 432 | 9 | $data = \substr($this->buffer, $this->offset, $length); |
|
| 433 | 9 | $this->offset += $length; |
|
| 434 | |||
| 435 | 9 | return new Ext($type, $data); |
|
| 436 | } |
||
| 437 | |||
| 438 | 3 | private function handleIntOverflow($value) |
|
| 449 | } |
||
| 450 |
According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.
}
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.