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