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 | 123 | public function __construct($buffer = '', $options = null) |
|
| 49 | |||
| 50 | /** |
||
| 51 | * @param Collection|null $transformers |
||
| 52 | */ |
||
| 53 | 2 | public function setTransformers(Collection $transformers = null) |
|
| 57 | |||
| 58 | /** |
||
| 59 | * @return Collection|null |
||
| 60 | */ |
||
| 61 | 1 | public function getTransformers() |
|
| 65 | |||
| 66 | /** |
||
| 67 | * @param string $data |
||
| 68 | * |
||
| 69 | * @return $this |
||
| 70 | */ |
||
| 71 | 5 | public function append($data) |
|
| 77 | |||
| 78 | /** |
||
| 79 | * @param string $buffer |
||
| 80 | * |
||
| 81 | * @return $this |
||
| 82 | */ |
||
| 83 | 109 | public function reset($buffer = '') |
|
| 90 | |||
| 91 | /** |
||
| 92 | * @return array |
||
| 93 | */ |
||
| 94 | 3 | public function tryUnpack() |
|
| 115 | |||
| 116 | /** |
||
| 117 | * @throws UnpackingFailedException |
||
| 118 | * |
||
| 119 | * @return mixed |
||
| 120 | */ |
||
| 121 | 118 | public function unpack() |
|
| 122 | { |
||
| 123 | 118 | if (!isset($this->buffer[$this->offset])) { |
|
| 124 | 4 | throw InsufficientDataException::fromOffset($this->buffer, $this->offset, 1); |
|
| 125 | } |
||
| 126 | |||
| 127 | 116 | $c = \ord($this->buffer[$this->offset]); |
|
| 128 | 116 | ++$this->offset; |
|
| 129 | |||
| 130 | // fixint |
||
| 131 | 116 | if ($c <= 0x7f) { |
|
| 132 | 20 | return $c; |
|
| 133 | } |
||
| 134 | // fixstr |
||
| 135 | 113 | if ($c >= 0xa0 && $c <= 0xbf) { |
|
| 136 | 13 | return $this->unpackStr($c & 0x1f); |
|
| 137 | } |
||
| 138 | // fixarray |
||
| 139 | 107 | if ($c >= 0x90 && $c <= 0x9f) { |
|
| 140 | 6 | return $this->unpackArray($c & 0xf); |
|
| 141 | } |
||
| 142 | // fixmap |
||
| 143 | 104 | if ($c >= 0x80 && $c <= 0x8f) { |
|
| 144 | 11 | return $this->unpackMap($c & 0xf); |
|
| 145 | } |
||
| 146 | // negfixint |
||
| 147 | 100 | if ($c >= 0xe0) { |
|
| 148 | 4 | return $c - 256; |
|
| 149 | } |
||
| 150 | |||
| 151 | 96 | switch ($c) { |
|
| 152 | 96 | case 0xc0: return null; |
|
| 153 | 95 | case 0xc2: return false; |
|
| 154 | 93 | case 0xc3: return true; |
|
| 155 | |||
| 156 | // MP_BIN |
||
| 157 | 88 | case 0xc4: return $this->unpackStr($this->unpackUint8()); |
|
| 158 | 84 | case 0xc5: return $this->unpackStr($this->unpackUint16()); |
|
| 159 | 83 | case 0xc6: return $this->unpackStr($this->unpackUint32()); |
|
| 160 | |||
| 161 | // MP_FLOAT |
||
| 162 | 82 | case 0xca: return $this->unpackFloat32(); |
|
| 163 | 79 | case 0xcb: return $this->unpackFloat64(); |
|
| 164 | |||
| 165 | // MP_UINT |
||
| 166 | 75 | case 0xcc: return $this->unpackUint8(); |
|
| 167 | 71 | case 0xcd: return $this->unpackUint16(); |
|
| 168 | 66 | case 0xce: return $this->unpackUint32(); |
|
| 169 | 62 | case 0xcf: return $this->unpackUint64(); |
|
| 170 | |||
| 171 | // MP_INT |
||
| 172 | 52 | case 0xd0: return $this->unpackInt8(); |
|
| 173 | 47 | case 0xd1: return $this->unpackInt16(); |
|
| 174 | 42 | case 0xd2: return $this->unpackInt32(); |
|
| 175 | 37 | case 0xd3: return $this->unpackInt64(); |
|
| 176 | |||
| 177 | // MP_STR |
||
| 178 | 32 | case 0xd9: return $this->unpackStr($this->unpackUint8()); |
|
| 179 | 28 | case 0xda: return $this->unpackStr($this->unpackUint16()); |
|
| 180 | 26 | case 0xdb: return $this->unpackStr($this->unpackUint32()); |
|
| 181 | |||
| 182 | // MP_ARRAY |
||
| 183 | 25 | case 0xdc: return $this->unpackArray($this->unpackUint16()); |
|
| 184 | 23 | case 0xdd: return $this->unpackArray($this->unpackUint32()); |
|
| 185 | |||
| 186 | // MP_MAP |
||
| 187 | 22 | case 0xde: return $this->unpackMap($this->unpackUint16()); |
|
| 188 | 20 | case 0xdf: return $this->unpackMap($this->unpackUint32()); |
|
| 189 | |||
| 190 | // MP_EXT |
||
| 191 | 19 | case 0xd4: return $this->unpackExt(1); |
|
| 192 | 16 | case 0xd5: return $this->unpackExt(2); |
|
| 193 | 14 | case 0xd6: return $this->unpackExt(4); |
|
| 194 | 12 | case 0xd7: return $this->unpackExt(8); |
|
| 195 | 10 | case 0xd8: return $this->unpackExt(16); |
|
| 196 | 8 | case 0xc7: return $this->unpackExt($this->unpackUint8()); |
|
| 197 | 5 | case 0xc8: return $this->unpackExt($this->unpackUint16()); |
|
| 198 | 3 | case 0xc9: return $this->unpackExt($this->unpackUint32()); |
|
| 199 | } |
||
| 200 | |||
| 201 | 1 | throw new UnpackingFailedException(\sprintf('Unknown code: 0x%x.', $c)); |
|
| 202 | } |
||
| 203 | |||
| 204 | 18 | private function unpackUint8() |
|
| 215 | |||
| 216 | 15 | private function unpackUint16() |
|
| 228 | |||
| 229 | 11 | View Code Duplication | private function unpackUint32() |
| 242 | |||
| 243 | 10 | private function unpackUint64() |
|
| 263 | |||
| 264 | 15 | private function unpackInt8() |
|
| 279 | |||
| 280 | 5 | private function unpackInt16() |
|
| 296 | |||
| 297 | 5 | View Code Duplication | private function unpackInt32() |
| 310 | |||
| 311 | 5 | View Code Duplication | private function unpackInt64() |
| 324 | |||
| 325 | 3 | View Code Duplication | private function unpackFloat32() |
| 338 | |||
| 339 | 4 | View Code Duplication | private function unpackFloat64() |
| 352 | |||
| 353 | 26 | private function unpackStr($length) |
|
| 368 | |||
| 369 | 9 | View Code Duplication | private function unpackArray($size) |
| 370 | { |
||
| 371 | 9 | $array = []; |
|
| 372 | 9 | for ($i = $size; $i; --$i) { |
|
| 373 | 8 | $array[] = $this->unpack(); |
|
| 374 | } |
||
| 375 | |||
| 376 | 9 | return $array; |
|
| 377 | } |
||
| 378 | |||
| 379 | 14 | View Code Duplication | private function unpackMap($size) |
| 380 | { |
||
| 381 | 14 | $map = []; |
|
| 382 | 14 | for ($i = $size; $i; --$i) { |
|
| 383 | 13 | $map[$this->unpack()] = $this->unpack(); |
|
| 384 | } |
||
| 385 | |||
| 386 | 14 | return $map; |
|
| 387 | } |
||
| 388 | |||
| 389 | 15 | private function unpackExt($length) |
|
| 406 | |||
| 407 | 5 | 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.