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 SocketListener 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 SocketListener, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class SocketListener extends BaseEventEmitter implements SocketListenerInterface |
||
15 | { |
||
16 | use LoopAwareTrait; |
||
17 | |||
18 | /** |
||
19 | * @var int |
||
20 | */ |
||
21 | const DEFAULT_BACKLOG = 1024; |
||
22 | |||
23 | /** |
||
24 | * @var mixed |
||
25 | */ |
||
26 | const CONFIG_DEFAULT_SSL = false; |
||
27 | |||
28 | /** |
||
29 | * @var mixed |
||
30 | */ |
||
31 | const CONFIG_DEFAULT_SSL_METHOD = STREAM_CRYPTO_METHOD_TLSv1_2_SERVER; |
||
32 | |||
33 | /** |
||
34 | * @var mixed |
||
35 | */ |
||
36 | const CONFIG_DEFAULT_SSL_NAME = ''; |
||
37 | |||
38 | /** |
||
39 | * @var mixed |
||
40 | */ |
||
41 | const CONFIG_DEFAULT_SSL_VERIFY_SIGN = false; |
||
42 | |||
43 | /** |
||
44 | * @var mixed |
||
45 | */ |
||
46 | const CONFIG_DEFAULT_SSL_VERIFY_PEER = false; |
||
47 | |||
48 | /** |
||
49 | * @var mixed |
||
50 | */ |
||
51 | const CONFIG_DEFAULT_SSL_VERIFY_DEPTH = 10; |
||
52 | |||
53 | /** |
||
54 | * @var resource |
||
55 | */ |
||
56 | protected $socket; |
||
57 | |||
58 | /** |
||
59 | * @var bool |
||
60 | */ |
||
61 | protected $started; |
||
62 | |||
63 | /** |
||
64 | * @var bool |
||
65 | */ |
||
66 | protected $paused; |
||
67 | |||
68 | /** |
||
69 | * @var array |
||
70 | */ |
||
71 | protected $config; |
||
72 | |||
73 | /** |
||
74 | * @var string|resource |
||
75 | */ |
||
76 | protected $endpoint; |
||
77 | |||
78 | /** |
||
79 | * @param string|resource $endpointOrResource |
||
80 | * @param LoopInterface $loop |
||
81 | * @param mixed[] $config |
||
82 | * @throws InstantiationException |
||
83 | */ |
||
84 | 30 | public function __construct($endpointOrResource, LoopInterface $loop, $config = []) |
|
93 | |||
94 | /** |
||
95 | * |
||
96 | */ |
||
97 | 27 | public function __destruct() |
|
110 | |||
111 | /** |
||
112 | * @override |
||
113 | * @inheritDoc |
||
114 | */ |
||
115 | 16 | public function start() |
|
145 | |||
146 | /** |
||
147 | * @override |
||
148 | * @inheritDoc |
||
149 | */ |
||
150 | public function stop() |
||
154 | |||
155 | /** |
||
156 | * @override |
||
157 | * @inheritDoc |
||
158 | */ |
||
159 | 4 | public function getLocalEndpoint() |
|
163 | |||
164 | /** |
||
165 | * @override |
||
166 | * @inheritDoc |
||
167 | */ |
||
168 | 3 | public function getLocalAddress() |
|
174 | |||
175 | /** |
||
176 | * @override |
||
177 | * @inheritDoc |
||
178 | */ |
||
179 | 1 | public function getLocalHost() |
|
185 | |||
186 | /** |
||
187 | * @override |
||
188 | * @inheritDoc |
||
189 | */ |
||
190 | 1 | public function getLocalPort() |
|
196 | |||
197 | /** |
||
198 | * @override |
||
199 | * @inheritDoc |
||
200 | */ |
||
201 | public function getLocalProtocol() |
||
207 | |||
208 | /** |
||
209 | * @override |
||
210 | * @inheritDoc |
||
211 | */ |
||
212 | 1 | public function getResource() |
|
216 | |||
217 | /** |
||
218 | * @override |
||
219 | * @inheritDoc |
||
220 | */ |
||
221 | 1 | public function getResourceId() |
|
225 | |||
226 | /** |
||
227 | * @override |
||
228 | * @inheritDoc |
||
229 | */ |
||
230 | 16 | public function getMetadata() |
|
238 | |||
239 | /** |
||
240 | * @override |
||
241 | * @inheritDoc |
||
242 | */ |
||
243 | 14 | public function getStreamType() |
|
249 | |||
250 | /** |
||
251 | * @override |
||
252 | * @inheritDoc |
||
253 | */ |
||
254 | 1 | public function getWrapperType() |
|
260 | |||
261 | /** |
||
262 | * @override |
||
263 | * @inheritDoc |
||
264 | */ |
||
265 | 22 | public function isOpen() |
|
269 | |||
270 | /** |
||
271 | * @override |
||
272 | * @inheritDoc |
||
273 | */ |
||
274 | 5 | public function isPaused() |
|
278 | |||
279 | /** |
||
280 | * @override |
||
281 | * @inheritDoc |
||
282 | */ |
||
283 | public function isEncrypted() |
||
287 | |||
288 | /** |
||
289 | * @override |
||
290 | * @inheritDoc |
||
291 | */ |
||
292 | 5 | public function close() |
|
305 | |||
306 | /** |
||
307 | * @override |
||
308 | * @inheritDoc |
||
309 | */ |
||
310 | 30 | public function pause() |
|
322 | |||
323 | /** |
||
324 | * @override |
||
325 | * @inheritDoc |
||
326 | */ |
||
327 | 16 | public function resume() |
|
339 | |||
340 | /** |
||
341 | * Create the server resource. |
||
342 | * |
||
343 | * This method creates server resource for socket connections. |
||
344 | * |
||
345 | * @param string $endpoint |
||
346 | * @param mixed[] $config |
||
347 | * @return resource |
||
348 | * @throws LogicException |
||
349 | */ |
||
350 | 16 | protected function createServer($endpoint, $config = []) |
|
436 | |||
437 | /** |
||
438 | * Create the client resource. |
||
439 | * |
||
440 | * This method creates client resource for socket connections. |
||
441 | * |
||
442 | * @param resource $resource |
||
443 | * @param string[] $config |
||
444 | * @return SocketInterface |
||
445 | */ |
||
446 | 3 | protected function createClient($resource, $config = []) |
|
450 | |||
451 | /** |
||
452 | * Handle the new connection. |
||
453 | * |
||
454 | * @internal |
||
455 | */ |
||
456 | 3 | public function handleConnect() |
|
489 | |||
490 | private function handleDisconnect($resource) |
||
500 | |||
501 | /** |
||
502 | * Handle closing event. |
||
503 | * |
||
504 | * @internal |
||
505 | */ |
||
506 | 30 | public function handleClose() |
|
520 | |||
521 | /** |
||
522 | * Configure socket. |
||
523 | * |
||
524 | * @param string[] $config |
||
525 | */ |
||
526 | 30 | View Code Duplication | private function configure($config = []) |
537 | |||
538 | /** |
||
539 | * Configure static key |
||
540 | * |
||
541 | * @param $configKey |
||
542 | */ |
||
543 | 30 | private function configureVariable($configKey) |
|
548 | |||
549 | /** |
||
550 | * @return string |
||
551 | */ |
||
552 | 4 | private function parseEndpoint() |
|
598 | } |
||
599 |
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.