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:
1 | <?php |
||
19 | class ExtendedHeaderReader extends AbstractReader |
||
20 | { |
||
21 | /** |
||
22 | * @var int |
||
23 | */ |
||
24 | private $padding; |
||
25 | |||
26 | /** |
||
27 | * @var int |
||
28 | */ |
||
29 | private $crc32; |
||
30 | |||
31 | /** |
||
32 | * @var int[] |
||
33 | */ |
||
34 | private $restrictions; |
||
35 | |||
36 | /** |
||
37 | * Read ID3v2 extended header size. |
||
38 | * |
||
39 | * @return int |
||
40 | */ |
||
41 | protected function readSize() |
||
53 | |||
54 | /** |
||
55 | * Read ID3v2 extended header flags. |
||
56 | * |
||
57 | * @return bool[] |
||
58 | */ |
||
59 | public function readFlags() |
||
81 | |||
82 | /** |
||
83 | * Read ID3v2 extended header padding. |
||
84 | * |
||
85 | * @return int |
||
86 | */ |
||
87 | protected function readPadding() |
||
97 | |||
98 | /** |
||
99 | * Get ID3v2 extended header padding. |
||
100 | * |
||
101 | * @return int |
||
102 | */ |
||
103 | public function getPadding() |
||
111 | |||
112 | /** |
||
113 | * Read ID3v2 extended header CRC-32 data. |
||
114 | * |
||
115 | * @return int |
||
116 | */ |
||
117 | protected function readCrc32() |
||
139 | |||
140 | /** |
||
141 | * Get ID3v2 extended header CRC-32 data. |
||
142 | * |
||
143 | * @return int |
||
144 | */ |
||
145 | public function getCrc32() |
||
153 | |||
154 | /** |
||
155 | * Read ID3v2 extended header restrictions. |
||
156 | * |
||
157 | * @return int[] |
||
158 | */ |
||
159 | protected function readRestrictions() |
||
185 | |||
186 | /** |
||
187 | * Get ID3v2 extended header restrictions. |
||
188 | * |
||
189 | * @return int[] |
||
190 | */ |
||
191 | public function getRestrictions() |
||
199 | } |
||
200 |
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.