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 |
||
15 | class ServerSocket extends Socket |
||
16 | { |
||
17 | |||
18 | /** |
||
19 | * Create a new server socket |
||
20 | * |
||
21 | * @param Endpoint $endpoint |
||
22 | * The endpoint to use |
||
23 | * |
||
24 | * @throws SocketException In case of creation of socket has failed or socket options could not be set. |
||
25 | */ |
||
26 | 1 | public function __construct(Endpoint $endpoint) |
|
34 | |||
35 | /** |
||
36 | * Creates a service at the given endpoint |
||
37 | * |
||
38 | * @throws SocketException in case of it is not possible to serve due to binding or listening error |
||
39 | */ |
||
40 | public function serve(ServiceCallback $callback) |
||
68 | |||
69 | /** |
||
70 | * Bind the server socket to the given endpoint |
||
71 | * |
||
72 | * @throws SocketException in case of binding has failed |
||
73 | */ |
||
74 | private function bind() |
||
81 | |||
82 | /** |
||
83 | * Listen to the binded socket endpoint |
||
84 | * |
||
85 | * @throws SocketException in case of listening is not possible |
||
86 | */ |
||
87 | private function listen() |
||
94 | } |
||
95 |
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.