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:
Complex classes like StreamReader often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use StreamReader, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
6 | class StreamReader { |
||
7 | |||
8 | /** |
||
9 | * @var string |
||
10 | */ |
||
11 | protected $data; |
||
12 | |||
13 | /** |
||
14 | * @var int |
||
15 | */ |
||
16 | protected $offset = 0; |
||
17 | |||
18 | public function __construct($data){ |
||
21 | |||
22 | /** |
||
23 | * Read data from stream. |
||
24 | * |
||
25 | * NOTICE When $this->offset == strlen($this->data), substr() will return false. You'd better avoid call read() when $length == 0. |
||
26 | * |
||
27 | * @param int $length $length should be > 0. |
||
28 | * @return string |
||
29 | */ |
||
30 | protected function read($length) { |
||
35 | |||
36 | public function offset($offset){ |
||
39 | |||
40 | public function reset(){ |
||
43 | |||
44 | /** |
||
45 | * Read single character. |
||
46 | * |
||
47 | * @return int |
||
48 | */ |
||
49 | public function readChar() { |
||
52 | |||
53 | /** |
||
54 | * Read unsigned short. |
||
55 | * |
||
56 | * @return int |
||
57 | */ |
||
58 | public function readShort() { |
||
61 | |||
62 | /** |
||
63 | * Read unsigned int. |
||
64 | * |
||
65 | * @return int |
||
66 | */ |
||
67 | public function readInt() { |
||
70 | |||
71 | /** |
||
72 | * Read string. |
||
73 | * |
||
74 | * @return string |
||
75 | */ |
||
76 | public function readString() { |
||
80 | |||
81 | /** |
||
82 | * Read long string. |
||
83 | * |
||
84 | * @return string |
||
85 | */ |
||
86 | public function readLongString() { |
||
90 | |||
91 | /** |
||
92 | * Read bytes. |
||
93 | * |
||
94 | * @return string |
||
95 | */ |
||
96 | public function readBytes() { |
||
104 | |||
105 | /** |
||
106 | * Read uuid. |
||
107 | * |
||
108 | * @return string |
||
109 | */ |
||
110 | public function readUuid() { |
||
115 | |||
116 | /** |
||
117 | * Read list. |
||
118 | * |
||
119 | * @param array $definition [$valueType] |
||
120 | * @return array |
||
121 | */ |
||
122 | View Code Duplication | public function readList(array $definition) { |
|
131 | |||
132 | /** |
||
133 | * Read map. |
||
134 | * |
||
135 | * @param array $definition [$keyType, $valueType] |
||
136 | * @return array |
||
137 | */ |
||
138 | View Code Duplication | public function readMap(array $definition) { |
|
147 | |||
148 | /** |
||
149 | * |
||
150 | * @param array $definition ['key1'=>$valueType1, 'key2'=>$valueType2, ...] |
||
151 | * @return array |
||
152 | */ |
||
153 | public function readTuple(array $definition) { |
||
164 | |||
165 | /** |
||
166 | * Read float. |
||
167 | * |
||
168 | * @return float |
||
169 | */ |
||
170 | public function readFloat() { |
||
173 | |||
174 | /** |
||
175 | * Read double. |
||
176 | * |
||
177 | * @return double |
||
178 | */ |
||
179 | public function readDouble() { |
||
182 | |||
183 | /** |
||
184 | * Read boolean. |
||
185 | * |
||
186 | * @return bool |
||
187 | */ |
||
188 | public function readBoolean() { |
||
191 | |||
192 | /** |
||
193 | * Read inet. |
||
194 | * |
||
195 | * @return string |
||
196 | */ |
||
197 | public function readInet() { |
||
200 | |||
201 | /** |
||
202 | * Read variable length integer. |
||
203 | * |
||
204 | * @return string |
||
205 | */ |
||
206 | public function readVarint() { |
||
210 | |||
211 | /** |
||
212 | * Read variable length decimal. |
||
213 | * |
||
214 | * @return string |
||
215 | */ |
||
216 | public function readDecimal() { |
||
222 | |||
223 | public function readStringMultimap(){ |
||
238 | |||
239 | /** |
||
240 | * alias of readValue() |
||
241 | * @deprecated |
||
242 | * |
||
243 | * @param int|array $type |
||
244 | * @return mixed |
||
245 | */ |
||
246 | public function readBytesAndConvertToType($type){ |
||
249 | |||
250 | /** |
||
251 | * read a [bytes] and read by type |
||
252 | * |
||
253 | * @param int|array $type |
||
254 | * @return mixed |
||
255 | */ |
||
256 | public function readValue($type){ |
||
280 | |||
281 | /** |
||
282 | * @return int|array |
||
283 | */ |
||
284 | public function readType(){ |
||
330 | } |
||
331 |
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.