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 |
||
25 | class HeaderReader |
||
26 | { |
||
27 | /** |
||
28 | * @var StreamInterface |
||
29 | */ |
||
30 | protected $stream; |
||
31 | |||
32 | /** |
||
33 | * @var Integer8Reader |
||
34 | */ |
||
35 | protected $integer8Reader; |
||
36 | |||
37 | /** |
||
38 | * @var SynchsafeInteger32Reader |
||
39 | */ |
||
40 | protected $synchsafeInteger32Reader; |
||
41 | |||
42 | /** |
||
43 | * Create ID3v2 header reader. |
||
44 | * |
||
45 | * @param StreamInterface $stream |
||
46 | */ |
||
47 | public function __construct(StreamInterface $stream) |
||
51 | |||
52 | /** |
||
53 | * Get 8-bit integer reader. |
||
54 | * |
||
55 | * @return Integer8Reader |
||
56 | */ |
||
57 | public function getInteger8Reader() |
||
65 | |||
66 | /** |
||
67 | * Get synchsafe 32-bit integer reader |
||
68 | * |
||
69 | * @return SynchsafeInteger32Reader |
||
70 | */ |
||
71 | View Code Duplication | public function getSynchsafeInteger32Reader() |
|
80 | |||
81 | /** |
||
82 | * Read ID3v2 header version. |
||
83 | * |
||
84 | * @throws RuntimeException An exception is thrown on invalid versions. |
||
85 | * |
||
86 | * @return int |
||
87 | */ |
||
88 | protected function readVersion() |
||
102 | |||
103 | /** |
||
104 | * Read ID3v2 header revision. |
||
105 | * |
||
106 | * @return int |
||
107 | */ |
||
108 | protected function readRevision() |
||
114 | |||
115 | /** |
||
116 | * Read ID3v2 header flags. |
||
117 | * |
||
118 | * @param int $version |
||
119 | * |
||
120 | * @return array |
||
121 | */ |
||
122 | protected function readFlags($version) |
||
149 | |||
150 | /** |
||
151 | * Read ID3v2 header size. |
||
152 | * |
||
153 | * @return int |
||
154 | */ |
||
155 | public function readSize() |
||
161 | |||
162 | /** |
||
163 | * Read ID3v2 header. |
||
164 | * |
||
165 | * @return HeaderInterface |
||
166 | */ |
||
167 | public function read() |
||
177 | } |
||
178 |
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.