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 IntegerReader extends Reader |
||
19 | { |
||
20 | /** |
||
21 | * Use byte order aware trait |
||
22 | */ |
||
23 | use ByteOrderAwareTrait; |
||
24 | |||
25 | /** |
||
26 | * Read unsigned 8-bit integer (char) data from the stream |
||
27 | * |
||
28 | * @return int |
||
29 | */ |
||
30 | public function readUnsignedInteger8() |
||
35 | |||
36 | /** |
||
37 | * Read signed 8-bit integer (char) data from the stream |
||
38 | * |
||
39 | * @return int |
||
40 | */ |
||
41 | public function readSignedInteger8() |
||
46 | |||
47 | /** |
||
48 | * Read unsigned 16-bit integer (short) data from the stream |
||
49 | * |
||
50 | * @return int |
||
51 | */ |
||
52 | View Code Duplication | public function readUnsignedInteger16() |
|
68 | |||
69 | /** |
||
70 | * Read signed 16-bit integer (short) data from the stream |
||
71 | * |
||
72 | * @return int |
||
73 | */ |
||
74 | View Code Duplication | public function readSignedInteger16() |
|
87 | |||
88 | /** |
||
89 | * Read unsigned 24-bit integer (short) data from the stream |
||
90 | * |
||
91 | * @return int |
||
92 | */ |
||
93 | public function readUnsignedInteger24() |
||
109 | |||
110 | /** |
||
111 | * Read signed 24-bit integer (short) data from the stream |
||
112 | * |
||
113 | * @return int |
||
114 | */ |
||
115 | public function readSignedInteger24() |
||
125 | |||
126 | /** |
||
127 | * Read unsigned 32-bit integer (long) data from the stream |
||
128 | * |
||
129 | * @return int |
||
130 | */ |
||
131 | View Code Duplication | public function readUnsignedInteger32() |
|
147 | |||
148 | /** |
||
149 | * Read signed 32-bit integer (long) data from the stream |
||
150 | * |
||
151 | * @return int |
||
152 | */ |
||
153 | View Code Duplication | public function readSignedInteger32() |
|
166 | |||
167 | /** |
||
168 | * Read unsigned 64-bit integer (long long) data from the stream |
||
169 | * |
||
170 | * @return int |
||
171 | */ |
||
172 | View Code Duplication | public function readUnsignedInteger64() |
|
188 | |||
189 | /** |
||
190 | * Read signed 64-bit integer (long long) data from the stream |
||
191 | * |
||
192 | * @return int |
||
193 | */ |
||
194 | View Code Duplication | public function readSignedInteger64() |
|
207 | } |
||
208 |
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.