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 |
||
| 20 | class BufferUnpacker |
||
| 21 | { |
||
| 22 | private $buffer; |
||
| 23 | private $offset = 0; |
||
| 24 | private $isBigIntAsStr; |
||
| 25 | private $isBigIntAsGmp; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var Extension[] |
||
| 29 | */ |
||
| 30 | private $transformers; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @param string $buffer |
||
| 34 | * @param UnpackOptions|int|null $options |
||
| 35 | * |
||
| 36 | * @throws InvalidOptionException |
||
| 37 | */ |
||
| 38 | 123 | public function __construct($buffer = '', $options = null) |
|
| 39 | { |
||
| 40 | 123 | if (!$options instanceof UnpackOptions) { |
|
| 41 | 123 | $options = UnpackOptions::fromBitmask($options); |
|
| 42 | } |
||
| 43 | |||
| 44 | 123 | $this->isBigIntAsStr = $options->isBigIntAsStrMode(); |
|
| 45 | 123 | $this->isBigIntAsGmp = $options->isBigIntAsGmpMode(); |
|
| 46 | |||
| 47 | 123 | $this->buffer = $buffer; |
|
| 48 | 123 | } |
|
| 49 | |||
| 50 | 1 | public function registerTransformer(Extension $ext) |
|
| 51 | { |
||
| 52 | 1 | $this->transformers[$ext->getType()] = $ext; |
|
| 53 | 1 | } |
|
| 54 | |||
| 55 | /** |
||
| 56 | * @param string $data |
||
| 57 | * |
||
| 58 | * @return $this |
||
| 59 | */ |
||
| 60 | 6 | public function append($data) |
|
| 61 | { |
||
| 62 | 6 | $this->buffer .= $data; |
|
| 63 | |||
| 64 | 6 | return $this; |
|
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @param string $buffer |
||
| 69 | * |
||
| 70 | * @return $this |
||
| 71 | */ |
||
| 72 | 109 | public function reset($buffer = '') |
|
| 73 | { |
||
| 74 | 109 | $this->buffer = $buffer; |
|
| 75 | 109 | $this->offset = 0; |
|
| 76 | |||
| 77 | 109 | return $this; |
|
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @param int $length |
||
| 82 | * |
||
| 83 | * @return $this |
||
| 84 | */ |
||
| 85 | 1 | public function skip($length) |
|
| 86 | { |
||
| 87 | 1 | $this->offset += $length; |
|
| 88 | |||
| 89 | 1 | return $this; |
|
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @return array |
||
| 94 | */ |
||
| 95 | 3 | public function tryUnpack() |
|
| 96 | { |
||
| 97 | 3 | $data = []; |
|
| 98 | 3 | $offset = $this->offset; |
|
| 99 | |||
| 100 | try { |
||
| 101 | do { |
||
| 102 | 3 | $data[] = $this->unpack(); |
|
| 103 | 3 | $offset = $this->offset; |
|
| 104 | 3 | } while (isset($this->buffer[$this->offset])); |
|
| 105 | 1 | } catch (InsufficientDataException $e) { |
|
| 106 | 1 | $this->offset = $offset; |
|
| 107 | } |
||
| 108 | |||
| 109 | 3 | if ($this->offset) { |
|
| 110 | 3 | $this->buffer = isset($this->buffer[$this->offset]) ? \substr($this->buffer, $this->offset) : ''; |
|
| 111 | 3 | $this->offset = 0; |
|
| 112 | } |
||
| 113 | |||
| 114 | 3 | return $data; |
|
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @throws UnpackingFailedException |
||
| 119 | * |
||
| 120 | * @return mixed |
||
| 121 | */ |
||
| 122 | 119 | public function unpack() |
|
| 123 | { |
||
| 124 | 119 | if (!isset($this->buffer[$this->offset])) { |
|
| 125 | 4 | throw InsufficientDataException::fromOffset($this->buffer, $this->offset, 1); |
|
| 126 | } |
||
| 127 | |||
| 128 | 117 | $c = \ord($this->buffer[$this->offset]); |
|
| 129 | 117 | ++$this->offset; |
|
| 130 | |||
| 131 | // fixint |
||
| 132 | 117 | if ($c <= 0x7f) { |
|
| 133 | 19 | return $c; |
|
| 134 | } |
||
| 135 | // fixstr |
||
| 136 | 114 | if ($c >= 0xa0 && $c <= 0xbf) { |
|
| 137 | 13 | return ($c & 0x1f) ? $this->unpackStr($c & 0x1f) : ''; |
|
| 138 | } |
||
| 139 | // fixarray |
||
| 140 | 108 | if ($c >= 0x90 && $c <= 0x9f) { |
|
| 141 | 6 | return ($c & 0xf) ? $this->unpackArray($c & 0xf) : []; |
|
| 142 | } |
||
| 143 | // fixmap |
||
| 144 | 105 | if ($c >= 0x80 && $c <= 0x8f) { |
|
| 145 | 11 | return ($c & 0xf) ? $this->unpackMap($c & 0xf) : []; |
|
| 146 | } |
||
| 147 | // negfixint |
||
| 148 | 101 | if ($c >= 0xe0) { |
|
| 149 | 4 | return $c - 256; |
|
| 150 | } |
||
| 151 | |||
| 152 | 97 | switch ($c) { |
|
| 153 | 97 | case 0xc0: return null; |
|
| 154 | 96 | case 0xc2: return false; |
|
| 155 | 94 | case 0xc3: return true; |
|
| 156 | |||
| 157 | // MP_BIN |
||
| 158 | 88 | case 0xc4: return $this->unpackStr($this->unpackUint8()); |
|
| 159 | 84 | case 0xc5: return $this->unpackStr($this->unpackUint16()); |
|
| 160 | 83 | case 0xc6: return $this->unpackStr($this->unpackUint32()); |
|
| 161 | |||
| 162 | // MP_FLOAT |
||
| 163 | 82 | case 0xca: return $this->unpackFloat32(); |
|
| 164 | 79 | case 0xcb: return $this->unpackFloat64(); |
|
| 165 | |||
| 166 | // MP_UINT |
||
| 167 | 75 | case 0xcc: return $this->unpackUint8(); |
|
| 168 | 71 | case 0xcd: return $this->unpackUint16(); |
|
| 169 | 66 | case 0xce: return $this->unpackUint32(); |
|
| 170 | 62 | case 0xcf: return $this->unpackUint64(); |
|
| 171 | |||
| 172 | // MP_INT |
||
| 173 | 52 | case 0xd0: return $this->unpackInt8(); |
|
| 174 | 47 | case 0xd1: return $this->unpackInt16(); |
|
| 175 | 42 | case 0xd2: return $this->unpackInt32(); |
|
| 176 | 37 | case 0xd3: return $this->unpackInt64(); |
|
| 177 | |||
| 178 | // MP_STR |
||
| 179 | 32 | case 0xd9: return $this->unpackStr($this->unpackUint8()); |
|
| 180 | 28 | case 0xda: return $this->unpackStr($this->unpackUint16()); |
|
| 181 | 26 | case 0xdb: return $this->unpackStr($this->unpackUint32()); |
|
| 182 | |||
| 183 | // MP_ARRAY |
||
| 184 | 25 | case 0xdc: return $this->unpackArray($this->unpackUint16()); |
|
| 185 | 23 | case 0xdd: return $this->unpackArray($this->unpackUint32()); |
|
| 186 | |||
| 187 | // MP_MAP |
||
| 188 | 22 | case 0xde: return $this->unpackMap($this->unpackUint16()); |
|
| 189 | 20 | case 0xdf: return $this->unpackMap($this->unpackUint32()); |
|
| 190 | |||
| 191 | // MP_EXT |
||
| 192 | 19 | case 0xd4: return $this->unpackExt(1); |
|
| 193 | 16 | case 0xd5: return $this->unpackExt(2); |
|
| 194 | 14 | case 0xd6: return $this->unpackExt(4); |
|
| 195 | 12 | case 0xd7: return $this->unpackExt(8); |
|
| 196 | 10 | case 0xd8: return $this->unpackExt(16); |
|
| 197 | 8 | case 0xc7: return $this->unpackExt($this->unpackUint8()); |
|
| 198 | 5 | case 0xc8: return $this->unpackExt($this->unpackUint16()); |
|
| 199 | 3 | case 0xc9: return $this->unpackExt($this->unpackUint32()); |
|
| 200 | } |
||
| 201 | |||
| 202 | 1 | throw new UnpackingFailedException(\sprintf('Unknown code: 0x%x.', $c)); |
|
| 203 | } |
||
| 204 | |||
| 205 | 18 | public function unpackUint8() |
|
| 206 | { |
||
| 207 | 18 | if (!isset($this->buffer[$this->offset])) { |
|
| 208 | 2 | throw InsufficientDataException::fromOffset($this->buffer, $this->offset, 1); |
|
| 209 | } |
||
| 210 | |||
| 211 | 16 | $num = $this->buffer[$this->offset]; |
|
| 212 | 16 | ++$this->offset; |
|
| 213 | |||
| 214 | 16 | return \ord($num); |
|
| 215 | } |
||
| 216 | |||
| 217 | 15 | public function unpackUint16() |
|
| 218 | { |
||
| 219 | 15 | if (!isset($this->buffer[$this->offset + 1])) { |
|
| 220 | 2 | throw InsufficientDataException::fromOffset($this->buffer, $this->offset, 2); |
|
| 221 | } |
||
| 222 | |||
| 223 | 13 | $hi = \ord($this->buffer[$this->offset]); |
|
| 224 | 13 | $lo = \ord($this->buffer[$this->offset + 1]); |
|
| 225 | 13 | $this->offset += 2; |
|
| 226 | |||
| 227 | 13 | return $hi << 8 | $lo; |
|
| 228 | } |
||
| 229 | |||
| 230 | 11 | View Code Duplication | public function unpackUint32() |
| 231 | { |
||
| 232 | 11 | if (!isset($this->buffer[$this->offset + 3])) { |
|
| 233 | 2 | throw InsufficientDataException::fromOffset($this->buffer, $this->offset, 4); |
|
| 234 | } |
||
| 235 | |||
| 236 | 9 | $num = \substr($this->buffer, $this->offset, 4); |
|
| 237 | 9 | $this->offset += 4; |
|
| 238 | |||
| 239 | 9 | $num = \unpack('N', $num); |
|
| 240 | |||
| 241 | 9 | return $num[1]; |
|
| 242 | } |
||
| 243 | |||
| 244 | 10 | public function unpackUint64() |
|
| 245 | { |
||
| 246 | 10 | if (!isset($this->buffer[$this->offset + 7])) { |
|
| 247 | 1 | throw InsufficientDataException::fromOffset($this->buffer, $this->offset, 8); |
|
| 248 | } |
||
| 249 | |||
| 250 | 9 | $num = \substr($this->buffer, $this->offset, 8); |
|
| 251 | 9 | $this->offset += 8; |
|
| 252 | |||
| 253 | //$num = \unpack('J', $num); |
||
| 254 | |||
| 255 | 9 | $set = \unpack('N2', $num); |
|
| 256 | 9 | $value = $set[1] << 32 | $set[2]; |
|
| 257 | |||
| 258 | // PHP does not support unsigned integers. |
||
| 259 | // If a number is bigger than 2^63, it will be interpreted as a float. |
||
| 260 | // @link http://php.net/manual/en/language.types.integer.php#language.types.integer.overflow |
||
| 261 | |||
| 262 | 9 | return ($value < 0) ? $this->handleIntOverflow($value) : $value; |
|
| 263 | } |
||
| 264 | |||
| 265 | 15 | public function unpackInt8() |
|
| 266 | { |
||
| 267 | 15 | if (!isset($this->buffer[$this->offset])) { |
|
| 268 | 1 | throw InsufficientDataException::fromOffset($this->buffer, $this->offset, 1); |
|
| 269 | } |
||
| 270 | |||
| 271 | 14 | $num = \ord($this->buffer[$this->offset]); |
|
| 272 | 14 | ++$this->offset; |
|
| 273 | |||
| 274 | 14 | if ($num > 0x7f) { |
|
| 275 | 3 | return $num - 256; |
|
| 276 | } |
||
| 277 | |||
| 278 | 11 | return $num; |
|
| 279 | } |
||
| 280 | |||
| 281 | 5 | public function unpackInt16() |
|
| 282 | { |
||
| 283 | 5 | if (!isset($this->buffer[$this->offset + 1])) { |
|
| 284 | 1 | throw InsufficientDataException::fromOffset($this->buffer, $this->offset, 2); |
|
| 285 | } |
||
| 286 | |||
| 287 | 4 | $hi = \ord($this->buffer[$this->offset]); |
|
| 288 | 4 | $lo = \ord($this->buffer[$this->offset + 1]); |
|
| 289 | 4 | $this->offset += 2; |
|
| 290 | |||
| 291 | 4 | if ($hi > 0x7f) { |
|
| 292 | 3 | return -(0x010000 - ($hi << 8 | $lo)); |
|
| 293 | } |
||
| 294 | |||
| 295 | 1 | return $hi << 8 | $lo; |
|
| 296 | } |
||
| 297 | |||
| 298 | 5 | View Code Duplication | public function unpackInt32() |
| 299 | { |
||
| 300 | 5 | if (!isset($this->buffer[$this->offset + 3])) { |
|
| 301 | 1 | throw InsufficientDataException::fromOffset($this->buffer, $this->offset, 4); |
|
| 302 | } |
||
| 303 | |||
| 304 | 4 | $num = \substr($this->buffer, $this->offset, 4); |
|
| 305 | 4 | $this->offset += 4; |
|
| 306 | |||
| 307 | 4 | $num = \unpack('i', \strrev($num)); |
|
| 308 | |||
| 309 | 4 | return $num[1]; |
|
| 310 | } |
||
| 311 | |||
| 312 | 5 | View Code Duplication | public function unpackInt64() |
| 313 | { |
||
| 314 | 5 | if (!isset($this->buffer[$this->offset + 7])) { |
|
| 315 | 1 | throw InsufficientDataException::fromOffset($this->buffer, $this->offset, 8); |
|
| 316 | } |
||
| 317 | |||
| 318 | 4 | $num = \substr($this->buffer, $this->offset, 8); |
|
| 319 | 4 | $this->offset += 8; |
|
| 320 | |||
| 321 | 4 | $set = \unpack('N2', $num); |
|
| 322 | |||
| 323 | 4 | return $set[1] << 32 | $set[2]; |
|
| 324 | } |
||
| 325 | |||
| 326 | 3 | View Code Duplication | public function unpackFloat32() |
| 327 | { |
||
| 328 | 3 | if (!isset($this->buffer[$this->offset + 3])) { |
|
| 329 | 1 | throw InsufficientDataException::fromOffset($this->buffer, $this->offset, 4); |
|
| 330 | } |
||
| 331 | |||
| 332 | 2 | $num = \substr($this->buffer, $this->offset, 4); |
|
| 333 | 2 | $this->offset += 4; |
|
| 334 | |||
| 335 | 2 | $num = \unpack('f', \strrev($num)); |
|
| 336 | |||
| 337 | 2 | return $num[1]; |
|
| 338 | } |
||
| 339 | |||
| 340 | 4 | View Code Duplication | public function unpackFloat64() |
| 341 | { |
||
| 342 | 4 | if (!isset($this->buffer[$this->offset + 7])) { |
|
| 343 | 1 | throw InsufficientDataException::fromOffset($this->buffer, $this->offset, 8); |
|
| 344 | } |
||
| 345 | |||
| 346 | 3 | $num = \substr($this->buffer, $this->offset, 8); |
|
| 347 | 3 | $this->offset += 8; |
|
| 348 | |||
| 349 | 3 | $num = \unpack('d', \strrev($num)); |
|
| 350 | |||
| 351 | 3 | return $num[1]; |
|
| 352 | } |
||
| 353 | |||
| 354 | 25 | public function unpackStr($length) |
|
| 355 | { |
||
| 356 | 25 | if (!isset($this->buffer[$this->offset + $length - 1])) { |
|
| 357 | 1 | throw InsufficientDataException::fromOffset($this->buffer, $this->offset, $length); |
|
| 358 | } |
||
| 359 | |||
| 360 | 25 | $str = \substr($this->buffer, $this->offset, $length); |
|
| 361 | 25 | $this->offset += $length; |
|
| 362 | |||
| 363 | 25 | return $str; |
|
| 364 | } |
||
| 365 | |||
| 366 | 8 | public function unpackArray($size) |
|
| 367 | { |
||
| 368 | 8 | $array = []; |
|
| 369 | 8 | while ($size--) { |
|
| 370 | 8 | $array[] = $this->unpack(); |
|
| 371 | } |
||
| 372 | |||
| 373 | 8 | return $array; |
|
| 374 | } |
||
| 375 | |||
| 376 | 13 | public function unpackMap($size) |
|
| 377 | { |
||
| 378 | 13 | $map = []; |
|
| 379 | 13 | while ($size--) { |
|
| 380 | 13 | $map[$this->unpack()] = $this->unpack(); |
|
| 381 | } |
||
| 382 | |||
| 383 | 13 | return $map; |
|
| 384 | } |
||
| 385 | |||
| 386 | 15 | public function unpackExt($length) |
|
| 387 | { |
||
| 388 | 15 | if (!isset($this->buffer[$this->offset + $length - 1])) { |
|
| 389 | 5 | throw InsufficientDataException::fromOffset($this->buffer, $this->offset, $length); |
|
| 390 | } |
||
| 391 | |||
| 392 | 10 | $type = $this->unpackInt8(); |
|
| 393 | |||
| 394 | 10 | if (isset($this->transformers[$type])) { |
|
| 395 | 1 | return $this->transformers[$type]->unpack($this, $length); |
|
| 396 | } |
||
| 397 | |||
| 398 | 9 | $data = \substr($this->buffer, $this->offset, $length); |
|
| 399 | 9 | $this->offset += $length; |
|
| 400 | |||
| 401 | 9 | return new Ext($type, $data); |
|
| 402 | } |
||
| 403 | |||
| 404 | 5 | private function handleIntOverflow($value) |
|
| 415 | } |
||
| 416 |
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.