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 Metadata |
||
20 | { |
||
21 | /** |
||
22 | * @var Stream |
||
23 | */ |
||
24 | protected $stream; |
||
25 | |||
26 | /** |
||
27 | * Create ID3v1 metadata. |
||
28 | * |
||
29 | * @param Stream $stream |
||
30 | */ |
||
31 | public function __construct(Stream $stream) |
||
35 | |||
36 | /** |
||
37 | * Returns whether ID3v1 metadata exists. |
||
38 | * |
||
39 | * @return bool |
||
40 | */ |
||
41 | View Code Duplication | public function exists() |
|
51 | |||
52 | /** |
||
53 | * Strip ID3v1 metadata. |
||
54 | * |
||
55 | * @return $this |
||
56 | */ |
||
57 | public function strip() |
||
68 | |||
69 | /** |
||
70 | * Read ID3v1 tag version. |
||
71 | * |
||
72 | * @return int |
||
73 | */ |
||
74 | protected function readVersion() |
||
83 | |||
84 | /** |
||
85 | * Trim data. |
||
86 | * |
||
87 | * @param string $data The data to trim |
||
88 | * |
||
89 | * @return string |
||
90 | */ |
||
91 | protected function trimData($data) |
||
95 | |||
96 | /** |
||
97 | * Read ID3v1 tag. |
||
98 | * |
||
99 | * @return null|Tag |
||
100 | */ |
||
101 | public function read() |
||
132 | |||
133 | /** |
||
134 | * Pad data. |
||
135 | * |
||
136 | * @param string $data The data to pad |
||
137 | * @param int $length The final length |
||
138 | * @param int $type The type of padding |
||
139 | * |
||
140 | * @return string |
||
141 | */ |
||
142 | protected function padData($data, $length, $type) |
||
146 | |||
147 | /** |
||
148 | * Write ID3v1 tag. |
||
149 | * |
||
150 | * @param Tag $tag The tag to write. |
||
151 | * |
||
152 | * @return $this |
||
153 | */ |
||
154 | public function write(Tag $tag) |
||
183 | } |
||
184 |
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.