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 |
||
38 | class StreamSocket implements SocketInterface |
||
39 | { |
||
40 | |||
41 | /** |
||
42 | * Holds the connection resource itself. |
||
43 | * |
||
44 | * @var resource |
||
45 | */ |
||
46 | protected $connectionResource; |
||
47 | |||
48 | /** |
||
49 | * Holds the actual resource id |
||
50 | * |
||
51 | * @var int |
||
52 | */ |
||
53 | protected $connectionResourceId; |
||
54 | |||
55 | /** |
||
56 | * Holds the peer name of the client who connected |
||
57 | * |
||
58 | * @var string |
||
59 | */ |
||
60 | protected $connectionPeername; |
||
61 | |||
62 | /** |
||
63 | * Creates a stream socket server and returns a instance of Stream implementation with server socket in it. |
||
64 | * |
||
65 | * @param string $socket The address incl. transport the server should be listening to. For example 0.0.0.0:8080 |
||
66 | * @param string $flags The flags to be set on server create |
||
67 | * @param resource $context The context to be set on stream create |
||
68 | * |
||
69 | * @return \AppserverIo\Server\Sockets\StreamSocket The Stream instance with a server socket created. |
||
70 | * |
||
71 | * @throws \AppserverIo\Psr\Socket\SocketServerException |
||
72 | */ |
||
73 | public static function getServerInstance($socket, $flags = null, $context = null) |
||
108 | |||
109 | /** |
||
110 | * Creates a stream socket client and returns a instance of Stream implementation with client socket in it. |
||
111 | * |
||
112 | * @param string $socket The address incl. transport the server should be listening to. For example 0.0.0.0:8080 |
||
113 | * @param string $flags The flags to be set on client create |
||
114 | * @param resource $context The context to be set on stream create |
||
115 | * |
||
116 | * @return \AppserverIo\Server\Sockets\StreamSocket The Stream instance with a client socket created. |
||
117 | * |
||
118 | * @throws \AppserverIo\Psr\Socket\SocketServerException |
||
119 | */ |
||
120 | public static function getClientInstance($socket, $flags = null, $context = null) |
||
146 | |||
147 | /** |
||
148 | * Returns an instance of Stream with preset resource in it. |
||
149 | * |
||
150 | * @param resource $connectionResource The resource to use |
||
151 | * |
||
152 | * @return \AppserverIo\Server\Sockets\StreamSocket |
||
153 | */ |
||
154 | public static function getInstance($connectionResource) |
||
160 | |||
161 | /** |
||
162 | * Accepts connections from clients and build up a instance of Stream with connection resource in it. |
||
163 | * |
||
164 | * @param int $acceptTimeout The timeout in seconds to wait for accepting connections. |
||
165 | * @param int $receiveTimeout The timeout in seconds to wait for read a line. |
||
166 | * |
||
167 | * @return \AppserverIo\Server\Sockets\StreamSocket|bool The Stream instance with the connection socket |
||
168 | * accepted or bool false if timeout or error occurred. |
||
169 | */ |
||
170 | public function accept($acceptTimeout = 600, $receiveTimeout = 60) |
||
186 | |||
187 | /** |
||
188 | * Returns the line read from connection resource |
||
189 | * |
||
190 | * @param int $readLength The max length to read for a line. |
||
191 | * @param int $receiveTimeout The max time to wait for read the next line |
||
192 | * |
||
193 | * @return string |
||
194 | * @throws \AppserverIo\Psr\Socket\SocketReadTimeoutException |
||
195 | */ |
||
196 | View Code Duplication | public function readLine($readLength = 1024, $receiveTimeout = null) |
|
216 | |||
217 | /** |
||
218 | * Reads the given length from connection resource |
||
219 | * |
||
220 | * @param int $readLength The max length to read for a line. |
||
221 | * @param int $receiveTimeout The max time to wait for read the next line |
||
222 | * |
||
223 | * @return string |
||
224 | * |
||
225 | * @throws \AppserverIo\Psr\Socket\SocketReadTimeoutException |
||
226 | * @throws \AppserverIo\Psr\Socket\SocketReadException |
||
227 | */ |
||
228 | View Code Duplication | public function read($readLength = 1024, $receiveTimeout = null) |
|
247 | |||
248 | /** |
||
249 | * Writes the given message to the connection resource. |
||
250 | * |
||
251 | * @param string $message The message to write to the connection resource. |
||
252 | * |
||
253 | * @return int |
||
254 | */ |
||
255 | public function write($message) |
||
259 | |||
260 | /** |
||
261 | * Receives data from a socket, connected or not. |
||
262 | * |
||
263 | * This method HAS to be used when working with an UDP socket. |
||
264 | * |
||
265 | * @param integer $length The number of bytes to receive from the |
||
266 | * @param integer $flags The value of flags, can be STREAM_OOB or STREAM_PEEK |
||
267 | * |
||
268 | * @return string Returns the read data, as a string |
||
269 | * @see http://docs.php.net/manual/de/function.stream-socket-recvfrom.php |
||
270 | */ |
||
271 | public function receiveFrom($length = 512, $flags = null) |
||
292 | |||
293 | /** |
||
294 | * Sends a message to a socket, whether it is connected or not. |
||
295 | * |
||
296 | *This method HAS to be used when working with an UDP socket. |
||
297 | * |
||
298 | * @param string $message The data to be sent |
||
299 | * @param integer $flags The value of flags can be STREAM_OOB |
||
300 | * |
||
301 | * @return integer Returns a result code, as an integer |
||
302 | * @see http://docs.php.net/manual/de/function.stream-socket-sendto.php |
||
303 | */ |
||
304 | public function sendTo($message, $flags = 0) |
||
308 | |||
309 | /** |
||
310 | * Copies data from a stream |
||
311 | * |
||
312 | * @param resource $sourceResource The source stream |
||
313 | * |
||
314 | * @return int The total count of bytes copied. |
||
315 | */ |
||
316 | public function copyStream($sourceResource) |
||
323 | |||
324 | /** |
||
325 | * Closes the connection resource |
||
326 | * |
||
327 | * @return bool |
||
328 | */ |
||
329 | public function close() |
||
337 | |||
338 | /** |
||
339 | * Returns the stream socket's status |
||
340 | * |
||
341 | * @return bool|array The status information as array or false in case of an invalid stream socket resource |
||
342 | */ |
||
343 | public function getStatus() |
||
347 | |||
348 | /** |
||
349 | * Returns the meta information of the stream socket |
||
350 | * |
||
351 | * @return bool|array The meta informations of the stream socket |
||
352 | */ |
||
353 | public function getMetaInfo() |
||
357 | |||
358 | /** |
||
359 | * Sets the connection resource |
||
360 | * |
||
361 | * @param resource $connectionResource The resource to socket file descriptor |
||
362 | * |
||
363 | * @return void |
||
364 | */ |
||
365 | public function setConnectionResource($connectionResource) |
||
370 | |||
371 | /** |
||
372 | * Sets the peer name |
||
373 | * |
||
374 | * @param string $peername The peername in format ip:port |
||
375 | * |
||
376 | * @return void |
||
377 | */ |
||
378 | public function setPeername($peername) |
||
382 | |||
383 | /** |
||
384 | * Returns the peer name in format ip:port (e.g. 10.20.30.40:57128) |
||
385 | * |
||
386 | * @return string |
||
387 | */ |
||
388 | public function getPeername() |
||
392 | |||
393 | /** |
||
394 | * Returns the address of connection |
||
395 | * |
||
396 | * @return string |
||
397 | */ |
||
398 | public function getAddress() |
||
402 | |||
403 | /** |
||
404 | * Returns the port of connection |
||
405 | * |
||
406 | * @return string |
||
407 | */ |
||
408 | public function getPort() |
||
412 | |||
413 | /** |
||
414 | * Returns the connection resource |
||
415 | * |
||
416 | * @return mixed |
||
417 | */ |
||
418 | public function getConnectionResource() |
||
422 | |||
423 | /** |
||
424 | * Returns connection resource id |
||
425 | * |
||
426 | * @return int |
||
427 | */ |
||
428 | public function getConnectionResourceId() |
||
432 | } |
||
433 |
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.