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 FrameHandler extends AbstractHandler |
||
19 | { |
||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | private $name; |
||
24 | |||
25 | /** |
||
26 | * @var int |
||
27 | */ |
||
28 | private $dataLength; |
||
29 | |||
30 | /** |
||
31 | * Read ID3v2 frame name. |
||
32 | * |
||
33 | * @return string |
||
34 | */ |
||
35 | protected function readName() |
||
45 | |||
46 | /** |
||
47 | * Get ID3v2 frame name. |
||
48 | * |
||
49 | * @return string |
||
50 | */ |
||
51 | public function getName() |
||
59 | |||
60 | /** |
||
61 | * Set ID3v2 frame name. |
||
62 | * |
||
63 | * @param string $name |
||
64 | * |
||
65 | * @return $this |
||
66 | */ |
||
67 | public function setName($name) |
||
73 | |||
74 | /** |
||
75 | * Read ID3v2 frame size. |
||
76 | * |
||
77 | * @return int |
||
78 | */ |
||
79 | protected function readSize() |
||
97 | |||
98 | /** |
||
99 | * Read ID3v2 frame flags. |
||
100 | * |
||
101 | * @return bool[] |
||
102 | */ |
||
103 | protected function readFlags() |
||
135 | |||
136 | /** |
||
137 | * Read ID3v2 frame data length. |
||
138 | * |
||
139 | * @return int |
||
140 | */ |
||
141 | protected function readDataLength() |
||
151 | |||
152 | /** |
||
153 | * Get ID3v2 frame data length. |
||
154 | * |
||
155 | * @return int |
||
156 | */ |
||
157 | public function getDataLength() |
||
165 | |||
166 | /** |
||
167 | * Write ID3v2 frame data data. |
||
168 | * |
||
169 | * @return $this |
||
170 | */ |
||
171 | public function write() |
||
179 | } |
||
180 |
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.