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 Packet |
||
20 | { |
||
21 | private $content; |
||
22 | private $pos; |
||
23 | |||
24 | /** |
||
25 | * Constructor |
||
26 | * @param string $content |
||
27 | */ |
||
28 | public function __construct($content = null) |
||
33 | |||
34 | /** |
||
35 | * Set content |
||
36 | * |
||
37 | * @param string $content |
||
38 | */ |
||
39 | public function setContent($content) |
||
43 | |||
44 | /** |
||
45 | * @param string $content |
||
46 | */ |
||
47 | public function setContentFromPos($content) |
||
52 | |||
53 | /** |
||
54 | * Add content to the packet |
||
55 | * |
||
56 | * @param string $content |
||
57 | */ |
||
58 | public function addContent($content) |
||
71 | |||
72 | /** |
||
73 | * @param string $content |
||
74 | */ |
||
75 | public function pushContent($content) |
||
81 | |||
82 | /** |
||
83 | * Get content |
||
84 | * |
||
85 | * @return string |
||
86 | */ |
||
87 | public function __toString() |
||
91 | |||
92 | /** |
||
93 | * Get length of packet content |
||
94 | * |
||
95 | * @return int |
||
96 | */ |
||
97 | public function getLength() |
||
101 | |||
102 | public function key() |
||
106 | |||
107 | /** |
||
108 | * @param integer $pos |
||
109 | */ |
||
110 | public function setPos($pos) |
||
115 | |||
116 | public function rewind() |
||
121 | |||
122 | public function getContent() |
||
131 | |||
132 | /** |
||
133 | * Get a byte from the packet content |
||
134 | * |
||
135 | * @param bool $delByte If true, delete the byte that is return |
||
136 | * @return byte |
||
137 | * @throws EmptyPacketException |
||
138 | */ |
||
139 | View Code Duplication | public function getByte($delByte = true) |
|
152 | |||
153 | /** |
||
154 | * Get a short from the packet content |
||
155 | * |
||
156 | * @param bool $delShort If true, delete the short that is return |
||
157 | * @return short |
||
158 | * @throws EmptyPacketException |
||
159 | */ |
||
160 | View Code Duplication | public function getShort($delShort = true) |
|
173 | |||
174 | /** |
||
175 | * Get a long from the packet content |
||
176 | * |
||
177 | * @param bool $delLong If true, delete the long that is return |
||
178 | * @return long |
||
179 | * @throws EmptyPacketException |
||
180 | */ |
||
181 | View Code Duplication | public function getLong($delLong = true) |
|
194 | |||
195 | /** |
||
196 | * Get a integer from the packet content |
||
197 | * |
||
198 | * @param bool $delInt If true, delete the integer that is return |
||
199 | * @return integer |
||
200 | * @throws EmptyPacketException |
||
201 | */ |
||
202 | View Code Duplication | public function getInt($delInt = true) |
|
215 | |||
216 | /** |
||
217 | * Get a float from the packet content |
||
218 | * |
||
219 | * @param bool $delFloat If true, delete the float that is return |
||
220 | * @return float |
||
221 | * @throws EmptyPacketException |
||
222 | */ |
||
223 | View Code Duplication | public function getFloat($delFloat = true) |
|
236 | |||
237 | /** |
||
238 | * Get the first string inside the packet content |
||
239 | * |
||
240 | * @param bool $delString If true, delete the string that is return |
||
241 | * @return string |
||
242 | * @throws EmptyPacketException |
||
243 | */ |
||
244 | public function getString($delString = true) |
||
259 | |||
260 | /** |
||
261 | * Extract all vars depending on type specified |
||
262 | * |
||
263 | * @param array $vars |
||
264 | * @return array |
||
265 | */ |
||
266 | public function extract(array $vars) |
||
277 | |||
278 | public function isEmpty() |
||
282 | } |
||
283 |
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.