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 |
||
18 | class Metadata |
||
19 | { |
||
20 | /** |
||
21 | * @var Stream |
||
22 | */ |
||
23 | protected $stream; |
||
24 | |||
25 | /** |
||
26 | * Create ID3v2 metadata object. |
||
27 | * |
||
28 | * @param Stream $stream |
||
29 | */ |
||
30 | public function __construct(Stream $stream) |
||
34 | |||
35 | /** |
||
36 | * Read synchsafe unsigned 32-bit integer (long) data from the stream. |
||
37 | * |
||
38 | * @return int |
||
39 | */ |
||
40 | public function readSynchsafeUInt32() |
||
46 | |||
47 | /** |
||
48 | * Returns whether ID3v2 metadata exists. |
||
49 | * |
||
50 | * @return bool |
||
51 | */ |
||
52 | View Code Duplication | public function exists() |
|
62 | |||
63 | /** |
||
64 | * Strip ID3v2 metadata. |
||
65 | * |
||
66 | * @return $this |
||
67 | */ |
||
68 | public function strip() |
||
74 | |||
75 | /** |
||
76 | * Read ID3v2 header version. |
||
77 | * |
||
78 | * @throws RuntimeException An exception is thrown on invalid versions. |
||
79 | * |
||
80 | * @return int |
||
81 | */ |
||
82 | protected function readHeaderVersion() |
||
97 | |||
98 | /** |
||
99 | * Read ID3v2 header revision. |
||
100 | * |
||
101 | * @return int |
||
102 | */ |
||
103 | protected function readHeaderRevision() |
||
109 | |||
110 | /** |
||
111 | * Read ID3v2 header flags. |
||
112 | * |
||
113 | * @param int $version |
||
114 | * |
||
115 | * @return array |
||
116 | */ |
||
117 | protected function readHeaderFlags($version) |
||
145 | |||
146 | /** |
||
147 | * Read ID3v2 header size. |
||
148 | * |
||
149 | * @return int |
||
150 | */ |
||
151 | public function readHeaderSize() |
||
157 | |||
158 | /** |
||
159 | * Read data. |
||
160 | * |
||
161 | * @param Header $header |
||
162 | * |
||
163 | * @return string |
||
164 | */ |
||
165 | protected function readData(Header $header) |
||
180 | |||
181 | /** |
||
182 | * Create data stream. |
||
183 | * |
||
184 | * @param string $data |
||
185 | * |
||
186 | * @return Stream |
||
187 | */ |
||
188 | protected function createDataStream($data) |
||
197 | |||
198 | /** |
||
199 | * Read ID3v2 tag. |
||
200 | * |
||
201 | * @return null|Tag |
||
202 | */ |
||
203 | public function read() |
||
249 | |||
250 | /** |
||
251 | * Write ID3v2 tag. |
||
252 | * |
||
253 | * @param Tag $tag The tag to write. |
||
254 | * |
||
255 | * @return $this |
||
256 | */ |
||
257 | public function write(Tag $tag) |
||
263 | } |
||
264 |
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.