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 |
||
13 | class Socket extends Stream implements SocketInterface |
||
14 | { |
||
15 | /** |
||
16 | * @var string |
||
17 | */ |
||
18 | const TYPE_UNIX = 'unix_socket'; |
||
19 | |||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | const TYPE_TCP = 'tcp_socket/ssl'; |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | const TYPE_UDP = 'udp_socket'; |
||
29 | |||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | const TYPE_UNKNOWN = 'Unknown'; |
||
34 | |||
35 | /** |
||
36 | * @var int |
||
37 | */ |
||
38 | const CRYPTO_TYPE_UNKNOWN = 0; |
||
39 | |||
40 | /** |
||
41 | * @var int |
||
42 | */ |
||
43 | const CRYPTO_TYPE_SERVER = 1; |
||
44 | |||
45 | /** |
||
46 | * @var int |
||
47 | */ |
||
48 | const CRYPTO_TYPE_CLIENT = 2; |
||
49 | |||
50 | /** |
||
51 | * @var mixed |
||
52 | */ |
||
53 | const CONFIG_DEFAULT_SSL = false; |
||
54 | |||
55 | /** |
||
56 | * @var mixed |
||
57 | */ |
||
58 | const CONFIG_DEFAULT_SSL_METHOD = STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT; |
||
59 | |||
60 | /** |
||
61 | * @var mixed |
||
62 | */ |
||
63 | const CONFIG_DEFAULT_SSL_NAME = ''; |
||
64 | |||
65 | /** |
||
66 | * @var mixed |
||
67 | */ |
||
68 | const CONFIG_DEFAULT_SSL_VERIFY_SIGN = false; |
||
69 | |||
70 | /** |
||
71 | * @var mixed |
||
72 | */ |
||
73 | const CONFIG_DEFAULT_SSL_VERIFY_PEER = false; |
||
74 | |||
75 | /** |
||
76 | * @var mixed |
||
77 | */ |
||
78 | const CONFIG_DEFAULT_SSL_VERIFY_DEPTH = 10; |
||
79 | |||
80 | /** |
||
81 | * @var int |
||
82 | */ |
||
83 | protected $crypto = 0; |
||
84 | |||
85 | /** |
||
86 | * @var int |
||
87 | */ |
||
88 | protected $cryptoMethod = 0; |
||
89 | |||
90 | /** |
||
91 | * @var array |
||
92 | */ |
||
93 | protected $config = []; |
||
94 | |||
95 | /** |
||
96 | * @var bool[] |
||
97 | */ |
||
98 | private $cachedEndpoint = []; |
||
99 | |||
100 | /** |
||
101 | * @param string|resource $endpointOrResource |
||
102 | * @param LoopInterface $loop |
||
103 | * @param mixed[] $config |
||
104 | * @throws InstantiationException |
||
105 | */ |
||
106 | 18 | public function __construct($endpointOrResource, LoopInterface $loop, $config = []) |
|
130 | |||
131 | /** |
||
132 | * @override |
||
133 | * @inheritDoc |
||
134 | */ |
||
135 | 1 | public function stop() |
|
139 | |||
140 | /** |
||
141 | * @override |
||
142 | * @inheritDoc |
||
143 | */ |
||
144 | 5 | public function getLocalEndpoint() |
|
148 | |||
149 | /** |
||
150 | * @override |
||
151 | * @inheritDoc |
||
152 | */ |
||
153 | 5 | public function getRemoteEndpoint() |
|
157 | |||
158 | /** |
||
159 | * @override |
||
160 | * @inheritDoc |
||
161 | */ |
||
162 | 3 | public function getLocalAddress() |
|
168 | |||
169 | /** |
||
170 | * @override |
||
171 | * @inheritDoc |
||
172 | */ |
||
173 | 1 | public function getLocalHost() |
|
179 | |||
180 | /** |
||
181 | * @override |
||
182 | * @inheritDoc |
||
183 | */ |
||
184 | 1 | public function getLocalPort() |
|
190 | |||
191 | /** |
||
192 | * @override |
||
193 | * @inheritDoc |
||
194 | */ |
||
195 | 1 | public function getLocalProtocol() |
|
201 | |||
202 | /** |
||
203 | * @override |
||
204 | * @inheritDoc |
||
205 | */ |
||
206 | 3 | public function getRemoteAddress() |
|
212 | |||
213 | /** |
||
214 | * @override |
||
215 | * @inheritDoc |
||
216 | */ |
||
217 | 1 | public function getRemoteHost() |
|
223 | |||
224 | /** |
||
225 | * @override |
||
226 | * @inheritDoc |
||
227 | */ |
||
228 | 1 | public function getRemotePort() |
|
234 | |||
235 | /** |
||
236 | * @override |
||
237 | * @inheritDoc |
||
238 | */ |
||
239 | 1 | public function getRemoteProtocol() |
|
245 | |||
246 | /** |
||
247 | * @override |
||
248 | * @inheritDoc |
||
249 | */ |
||
250 | 1 | public function isEncrypted() |
|
254 | |||
255 | /** |
||
256 | * Create the client resource. |
||
257 | * |
||
258 | * This method creates client resource for socket connections. |
||
259 | * |
||
260 | * @param string $endpoint |
||
261 | * @param mixed[] $config |
||
262 | * @return resource |
||
263 | * @throws LogicException |
||
264 | */ |
||
265 | 18 | protected function createClient($endpoint, $config = []) |
|
266 | { |
||
267 | 18 | $ssl = $this->config['ssl']; |
|
268 | 18 | $name = $this->config['ssl_name']; |
|
269 | 18 | $verifySign = $this->config['ssl_verify_sign']; |
|
270 | 18 | $verifyPeer = $this->config['ssl_verify_peer']; |
|
271 | 18 | $verifyDepth = $this->config['ssl_verify_depth']; |
|
272 | |||
273 | 18 | $context = []; |
|
274 | 18 | $context['socket'] = [ |
|
275 | 18 | 'connect' => $endpoint |
|
276 | ]; |
||
277 | 18 | $context['ssl'] = [ |
|
278 | 18 | 'capture_peer_cert' => true, |
|
279 | 'capture_peer_chain' => true, |
||
280 | 'capture_peer_cert_chain' => true, |
||
281 | 18 | 'allow_self_signed' => !$verifySign, |
|
282 | 18 | 'verify_peer' => $verifyPeer, |
|
283 | 18 | 'verify_peer_name' => $verifyPeer, |
|
284 | 18 | 'verify_depth' => $verifyDepth, |
|
285 | 18 | 'SNI_enabled' => $name !== '', |
|
286 | 18 | 'SNI_server_name' => $name, |
|
287 | 18 | 'peer_name' => $name, |
|
288 | 'disable_compression' => true, |
||
289 | 'honor_cipher_order' => true, |
||
290 | ]; |
||
291 | |||
292 | 18 | if ($ssl && isset($config['ssl_cafile'])) |
|
293 | { |
||
294 | $context['ssl']['cafile'] = $config['ssl_cafile']; |
||
295 | } |
||
296 | |||
297 | 18 | if ($ssl && isset($config['ssl_capath'])) |
|
298 | { |
||
299 | $context['ssl']['capath'] = $config['ssl_capath']; |
||
300 | } |
||
301 | |||
302 | 18 | $context = stream_context_create($context); |
|
303 | // Error reporting suppressed since stream_socket_client() emits an E_WARNING on failure. |
||
304 | 18 | $socket = @stream_socket_client( |
|
305 | 18 | $endpoint, |
|
306 | 18 | $errno, |
|
307 | 18 | $errstr, |
|
308 | 18 | 0, // Timeout does not apply for async connect. |
|
309 | 18 | STREAM_CLIENT_CONNECT | STREAM_CLIENT_ASYNC_CONNECT, |
|
310 | 18 | $context |
|
311 | ); |
||
312 | |||
313 | 18 | View Code Duplication | if (!$socket || $errno > 0) |
314 | { |
||
315 | 2 | throw new LogicException( |
|
316 | 2 | sprintf('Could not connect socket [%s] because of error [%d; %s]', $endpoint, $errno, $errstr) |
|
317 | ); |
||
318 | } |
||
319 | |||
320 | 16 | stream_set_blocking($socket, false); |
|
321 | |||
322 | 16 | return $socket; |
|
323 | } |
||
324 | |||
325 | /** |
||
326 | * Handle socket encryption. |
||
327 | * |
||
328 | * @internal |
||
329 | */ |
||
330 | 1 | public function handleEncrypt() |
|
360 | |||
361 | /** |
||
362 | * @internal |
||
363 | * @override |
||
364 | * @inheritDoc |
||
365 | */ |
||
366 | 3 | public function handleRead() |
|
395 | |||
396 | /** |
||
397 | * @internal |
||
398 | * @override |
||
399 | * @inheritDoc |
||
400 | */ |
||
401 | 3 | public function handleWrite() |
|
419 | |||
420 | /** |
||
421 | * @internal |
||
422 | * @override |
||
423 | * @inheritDoc |
||
424 | */ |
||
425 | 16 | public function handleClose() |
|
437 | |||
438 | /** |
||
439 | * Get function that should be invoked on read event. |
||
440 | * |
||
441 | * @return callable |
||
442 | */ |
||
443 | 16 | protected function getHandleReadFunction() |
|
449 | |||
450 | /** |
||
451 | * Get function that should be invoked on write event. |
||
452 | * |
||
453 | * @return callable |
||
454 | */ |
||
455 | 3 | protected function getHandleWriteFunction() |
|
461 | |||
462 | /** |
||
463 | * Configure socket. |
||
464 | * |
||
465 | * @param string[] $config |
||
466 | */ |
||
467 | 18 | View Code Duplication | private function configure($config = []) |
478 | |||
479 | /** |
||
480 | * Configure config variable. |
||
481 | * |
||
482 | * @param $configKey |
||
483 | */ |
||
484 | 18 | private function configureVariable($configKey) |
|
489 | |||
490 | /** |
||
491 | * @param bool $wantPeer |
||
492 | * @return string |
||
493 | */ |
||
494 | 10 | private function parseEndpoint($wantPeer = false) |
|
547 | |||
548 | /** |
||
549 | * @override |
||
550 | * @inheritDoc |
||
551 | */ |
||
552 | 1 | private function encrypt($method) |
|
612 | |||
613 | /** |
||
614 | * Checks type of crypto. |
||
615 | * |
||
616 | * @param int $version |
||
617 | * @return int |
||
618 | */ |
||
619 | 1 | private function selectCryptoType($version) |
|
639 | |||
640 | /** |
||
641 | * Returns highest supported crypto method constant based on protocol version identifier. |
||
642 | * |
||
643 | * @param int $version |
||
644 | * @return int |
||
645 | */ |
||
646 | 1 | private function selectCryptoVersion($version) |
|
656 | } |
||
657 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.