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 | 97 | public function __construct($intOverflowMode = null) |
|
49 | { |
||
50 | 97 | if (null !== $intOverflowMode) { |
|
51 | 2 | $this->intOverflowMode = $intOverflowMode; |
|
52 | 2 | } |
|
53 | 97 | } |
|
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() |
|
67 | { |
||
68 | 1 | return $this->transformers; |
|
69 | } |
||
70 | |||
71 | /** |
||
72 | * @param int $intOverflowMode |
||
73 | * |
||
74 | * @throws \InvalidArgumentException |
||
75 | */ |
||
76 | 2 | public function setIntOverflowMode($intOverflowMode) |
|
77 | { |
||
78 | 2 | if (!\in_array($intOverflowMode, [ |
|
79 | 2 | self::INT_AS_EXCEPTION, |
|
80 | 2 | self::INT_AS_STR, |
|
81 | 2 | self::INT_AS_GMP, |
|
82 | 2 | ], true)) { |
|
83 | 1 | throw new \InvalidArgumentException(\sprintf('Invalid integer overflow mode: %s.', $intOverflowMode)); |
|
84 | } |
||
85 | |||
86 | 1 | $this->intOverflowMode = $intOverflowMode; |
|
87 | 1 | } |
|
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 | 89 | 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 | 93 | public function unpack() |
|
232 | |||
233 | 16 | private function unpackU8() |
|
234 | { |
||
235 | 16 | $this->ensureLength(1); |
|
236 | |||
237 | 16 | $num = $this->buffer[$this->offset]; |
|
238 | 16 | $this->offset += 1; |
|
239 | |||
240 | 16 | return \ord($num); |
|
241 | } |
||
242 | |||
243 | 13 | View Code Duplication | private function unpackU16() |
244 | { |
||
245 | 13 | $this->ensureLength(2); |
|
246 | |||
247 | 13 | $num = $this->buffer[$this->offset].$this->buffer[$this->offset + 1]; |
|
248 | 13 | $this->offset += 2; |
|
249 | |||
250 | 13 | $num = \unpack('n', $num); |
|
251 | |||
252 | 13 | return $num[1]; |
|
253 | } |
||
254 | |||
255 | 9 | View Code Duplication | private function unpackU32() |
256 | { |
||
257 | 9 | $this->ensureLength(4); |
|
258 | |||
259 | 9 | $num = \substr($this->buffer, $this->offset, 4); |
|
260 | 9 | $this->offset += 4; |
|
261 | |||
262 | 9 | $num = \unpack('N', $num); |
|
263 | |||
264 | 9 | return $num[1]; |
|
265 | } |
||
266 | |||
267 | 7 | private function unpackU64() |
|
268 | { |
||
269 | 7 | $this->ensureLength(8); |
|
270 | |||
271 | 7 | $num = \substr($this->buffer, $this->offset, 8); |
|
272 | 7 | $this->offset += 8; |
|
273 | |||
274 | //$num = \unpack('J', $num); |
||
275 | |||
276 | 7 | $set = \unpack('N2', $num); |
|
277 | 7 | $value = $set[1] << 32 | $set[2]; |
|
278 | |||
279 | // PHP does not support unsigned integers. |
||
280 | // If a number is bigger than 2^63, it will be interpreted as a float. |
||
281 | // @link http://php.net/manual/en/language.types.integer.php#language.types.integer.overflow |
||
282 | |||
283 | 7 | return ($value < 0) ? $this->handleIntOverflow($value) : $value; |
|
284 | } |
||
285 | |||
286 | 13 | View Code Duplication | private function unpackI8() |
287 | { |
||
288 | 13 | $this->ensureLength(1); |
|
289 | |||
290 | 13 | $num = \ord($this->buffer[$this->offset]); |
|
291 | 13 | $this->offset += 1; |
|
292 | |||
293 | 13 | if ($num & 128) { |
|
294 | 3 | return $num - 256; |
|
295 | } |
||
296 | |||
297 | 10 | return $num; |
|
298 | } |
||
299 | |||
300 | 3 | View Code Duplication | private function unpackI16() |
311 | |||
312 | 3 | private function unpackI32() |
|
323 | |||
324 | 3 | View Code Duplication | private function unpackI64() |
335 | |||
336 | 2 | private function unpackFloat() |
|
347 | |||
348 | 3 | private function unpackDouble() |
|
359 | |||
360 | 25 | private function unpackStr($length) |
|
373 | |||
374 | 9 | private function unpackArray($size) |
|
384 | |||
385 | 14 | private function unpackMap($size) |
|
398 | |||
399 | 10 | private function unpackExt($length) |
|
414 | |||
415 | 93 | private function ensureLength($length) |
|
421 | |||
422 | 3 | private function handleIntOverflow($value) |
|
433 | } |
||
434 |
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.