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 Socket 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 Socket, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class Socket |
||
29 | { |
||
30 | private $ip; |
||
31 | private $port; |
||
32 | private $type; |
||
33 | private $timeout; |
||
34 | private $socket; |
||
35 | private $connected; |
||
36 | |||
37 | private $callbacks; |
||
38 | |||
39 | const MTU = 1400; |
||
40 | |||
41 | const MULTI_DETECTOR = 'isMultiResp'; |
||
42 | const MULTI_RECEIVER = 'recvMultiResp'; |
||
43 | |||
44 | /** |
||
45 | * Constructor |
||
46 | * |
||
47 | * @param string $ip |
||
48 | * @param int $port |
||
49 | * @param string $type (tcp/udp) |
||
50 | * @param array $timeout (0 => timeout sec, 1 => timeout usec) |
||
51 | * @param array $callbacks Array of callbacks |
||
52 | */ |
||
53 | public function __construct( |
||
70 | |||
71 | /** |
||
72 | * Connect to the server |
||
73 | * |
||
74 | * @throws CreateSocketException |
||
75 | * @throws ConnectionFailedException |
||
76 | */ |
||
77 | public function connect() |
||
119 | |||
120 | /** |
||
121 | * Disconnect the socket |
||
122 | */ |
||
123 | public function disconnect() |
||
134 | |||
135 | /** |
||
136 | * Send a packet |
||
137 | * |
||
138 | * @param Packet $packet |
||
139 | * |
||
140 | * @return integer Data length sended |
||
141 | * |
||
142 | * @throws NotConnectedException |
||
143 | * @throws SendDataException |
||
144 | */ |
||
145 | public function send(Packet $packet) |
||
160 | |||
161 | /** |
||
162 | * Receive a packet |
||
163 | * |
||
164 | * @param bool $multiPacket |
||
165 | * @param double $packetLength |
||
166 | * @return \DP\GameServer\GameServerBundle\Socket\Packet |
||
167 | * @throws NotConnectedException |
||
168 | * @throws RecvTimeoutException |
||
169 | * @throws RecvDataException |
||
170 | */ |
||
171 | public function recv($multiPacket = true, $packetLength = Socket::MTU) |
||
231 | |||
232 | /** |
||
233 | * Get socket buffer size |
||
234 | * @return int |
||
235 | */ |
||
236 | public function getSocketBufferSize() |
||
240 | |||
241 | /** |
||
242 | * Get the last error message |
||
243 | * @return string |
||
244 | */ |
||
245 | private function getLastError() |
||
249 | |||
250 | /** |
||
251 | * Set IP |
||
252 | * |
||
253 | * @param string $ip |
||
254 | */ |
||
255 | public function setIp($ip) |
||
259 | /** |
||
260 | * Get IP |
||
261 | * |
||
262 | * @return string |
||
263 | */ |
||
264 | public function getIp() |
||
268 | /** |
||
269 | * Verify if IP is v6 |
||
270 | * |
||
271 | * @todo Add IPv6 support |
||
272 | * @return bool |
||
273 | */ |
||
274 | public function isIPv6() |
||
278 | |||
279 | /** |
||
280 | * Set port |
||
281 | * |
||
282 | * @param int $port |
||
283 | */ |
||
284 | public function setPort($port) |
||
288 | /** |
||
289 | * Get port |
||
290 | * |
||
291 | * @return int |
||
292 | */ |
||
293 | public function getPort() |
||
297 | |||
298 | /** |
||
299 | * Set type (tcp or udp) |
||
300 | * |
||
301 | * @param int $type |
||
302 | */ |
||
303 | public function setType($type) |
||
307 | /** |
||
308 | * Get type (tcp or udp) |
||
309 | * |
||
310 | * @return int |
||
311 | */ |
||
312 | public function getType() |
||
316 | |||
317 | /** |
||
318 | * Get socket status (connected or not) |
||
319 | * |
||
320 | * @return bool |
||
321 | */ |
||
322 | public function isConnected() |
||
326 | } |
||
327 |
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.