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 Packer 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 Packer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class Packer |
||
18 | { |
||
19 | /** |
||
20 | * @var Collection |
||
21 | */ |
||
22 | private $transformers; |
||
23 | |||
24 | /** |
||
25 | * @param Collection|null $transformers |
||
26 | */ |
||
27 | 2 | public function setTransformers(Collection $transformers = null) |
|
31 | |||
32 | /** |
||
33 | * @return Collection|null |
||
34 | */ |
||
35 | 1 | public function getTransformers() |
|
39 | |||
40 | 76 | public function pack($value) |
|
71 | |||
72 | 7 | public function packArray(array $array) |
|
73 | { |
||
74 | 7 | $size = count($array); |
|
75 | 7 | $data = self::packArrayHeader($size); |
|
76 | |||
77 | 7 | foreach ($array as $val) { |
|
78 | 6 | $data .= $this->pack($val); |
|
79 | 7 | } |
|
80 | |||
81 | 7 | return $data; |
|
82 | } |
||
83 | |||
84 | 7 | View Code Duplication | private static function packArrayHeader($size) |
|
|||
85 | { |
||
86 | 7 | if ($size <= 0xf) { |
|
87 | 4 | return chr(0x90 | $size); |
|
88 | } |
||
89 | 3 | if ($size <= 0xffff) { |
|
90 | 2 | return pack('Cn', 0xdc, $size); |
|
91 | } |
||
92 | |||
93 | 1 | return pack('CN', 0xdd, $size); |
|
94 | } |
||
95 | |||
96 | 8 | public function packMap(array $map) |
|
108 | |||
109 | 8 | View Code Duplication | private static function packMapHeader($size) |
120 | |||
121 | 17 | public function packStr($str) |
|
137 | |||
138 | 8 | public function packBin($str) |
|
151 | |||
152 | 10 | public function packExt(Ext $ext) |
|
175 | |||
176 | 3 | public function packNil() |
|
180 | |||
181 | 6 | public function packBool($val) |
|
185 | |||
186 | 3 | public function packDouble($num) |
|
190 | |||
191 | 42 | public function packInt($num) |
|
225 | |||
226 | 6 | private static function packU64($code, $num) |
|
233 | } |
||
234 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.