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() |
|
244 | |||
245 | 13 | View Code Duplication | private function unpackU16() |
256 | |||
257 | 9 | View Code Duplication | private function unpackU32() |
268 | |||
269 | 7 | private function unpackU64() |
|
287 | |||
288 | 13 | private function unpackI8() |
|
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.