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 |
||
29 | class Socket |
||
30 | { |
||
31 | use InstanceConfigTrait; |
||
32 | |||
33 | /** |
||
34 | * Object description |
||
35 | * |
||
36 | * @var string |
||
37 | */ |
||
38 | public $description = 'Remote DataSource Network Socket Interface'; |
||
39 | |||
40 | /** |
||
41 | * Default configuration settings for the socket connection |
||
42 | * |
||
43 | * @var array |
||
44 | */ |
||
45 | protected $_defaultConfig = [ |
||
46 | 'persistent' => false, |
||
47 | 'host' => 'localhost', |
||
48 | 'protocol' => 'tcp', |
||
49 | 'port' => 80, |
||
50 | 'timeout' => 30, |
||
51 | ]; |
||
52 | |||
53 | /** |
||
54 | * Reference to socket connection resource |
||
55 | * |
||
56 | * @var resource|null |
||
57 | */ |
||
58 | public $connection; |
||
59 | |||
60 | /** |
||
61 | * This boolean contains the current state of the Socket class |
||
62 | * |
||
63 | * @var bool |
||
64 | */ |
||
65 | public $connected = false; |
||
66 | |||
67 | /** |
||
68 | * This variable contains an array with the last error number (num) and string (str) |
||
69 | * |
||
70 | * @var array |
||
71 | */ |
||
72 | public $lastError = []; |
||
73 | |||
74 | /** |
||
75 | * True if the socket stream is encrypted after a Cake\Network\Socket::enableCrypto() call |
||
76 | * |
||
77 | * @var bool |
||
78 | */ |
||
79 | public $encrypted = false; |
||
80 | |||
81 | /** |
||
82 | * Contains all the encryption methods available |
||
83 | * |
||
84 | * SSLv2 and SSLv3 are deprecated, and should not be used as they |
||
85 | * have several published vulnerablilities. |
||
86 | * |
||
87 | * @var array |
||
88 | */ |
||
89 | protected $_encryptMethods = [ |
||
90 | // @codingStandardsIgnoreStart |
||
91 | // @deprecated Will be removed in 4.0.0 |
||
92 | 'sslv2_client' => STREAM_CRYPTO_METHOD_SSLv2_CLIENT, |
||
93 | // @deprecated Will be removed in 4.0.0 |
||
94 | 'sslv3_client' => STREAM_CRYPTO_METHOD_SSLv3_CLIENT, |
||
95 | 'sslv23_client' => STREAM_CRYPTO_METHOD_SSLv23_CLIENT, |
||
96 | 'tls_client' => STREAM_CRYPTO_METHOD_TLS_CLIENT, |
||
97 | 'tlsv10_client' => STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT, |
||
98 | 'tlsv11_client' => STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT, |
||
99 | 'tlsv12_client' => STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT, |
||
100 | // @deprecated Will be removed in 4.0.0 |
||
101 | 'sslv2_server' => STREAM_CRYPTO_METHOD_SSLv2_SERVER, |
||
102 | // @deprecated Will be removed in 4.0.0 |
||
103 | 'sslv3_server' => STREAM_CRYPTO_METHOD_SSLv3_SERVER, |
||
104 | 'sslv23_server' => STREAM_CRYPTO_METHOD_SSLv23_SERVER, |
||
105 | 'tls_server' => STREAM_CRYPTO_METHOD_TLS_SERVER, |
||
106 | 'tlsv10_server' => STREAM_CRYPTO_METHOD_TLSv1_0_SERVER, |
||
107 | 'tlsv11_server' => STREAM_CRYPTO_METHOD_TLSv1_1_SERVER, |
||
108 | 'tlsv12_server' => STREAM_CRYPTO_METHOD_TLSv1_2_SERVER |
||
109 | // @codingStandardsIgnoreEnd |
||
110 | ]; |
||
111 | |||
112 | /** |
||
113 | * Used to capture connection warnings which can happen when there are |
||
114 | * SSL errors for example. |
||
115 | * |
||
116 | * @var array |
||
117 | */ |
||
118 | protected $_connectionErrors = []; |
||
119 | |||
120 | /** |
||
121 | * Constructor. |
||
122 | * |
||
123 | * @param array $config Socket configuration, which will be merged with the base configuration |
||
124 | * @see \Cake\Network\Socket::$_baseConfig |
||
125 | */ |
||
126 | public function __construct(array $config = []) |
||
130 | |||
131 | /** |
||
132 | * Connect the socket to the given host and port. |
||
133 | * |
||
134 | * @return bool Success |
||
135 | * @throws \Cake\Network\Exception\SocketException |
||
136 | */ |
||
137 | public function connect() |
||
197 | |||
198 | /** |
||
199 | * Configure the SSL context options. |
||
200 | * |
||
201 | * @param string $host The host name being connected to. |
||
202 | * @return void |
||
203 | */ |
||
204 | protected function _setSslContext($host) |
||
232 | |||
233 | /** |
||
234 | * socket_stream_client() does not populate errNum, or $errStr when there are |
||
235 | * connection errors, as in the case of SSL verification failure. |
||
236 | * |
||
237 | * Instead we need to handle those errors manually. |
||
238 | * |
||
239 | * @param int $code Code number. |
||
240 | * @param string $message Message. |
||
241 | * @return void |
||
242 | */ |
||
243 | protected function _connectionErrorHandler($code, $message) |
||
247 | |||
248 | /** |
||
249 | * Get the connection context. |
||
250 | * |
||
251 | * @return array|null Null when there is no connection, an array when there is. |
||
252 | */ |
||
253 | public function context() |
||
261 | |||
262 | /** |
||
263 | * Get the host name of the current connection. |
||
264 | * |
||
265 | * @return string Host name |
||
266 | */ |
||
267 | public function host() |
||
275 | |||
276 | /** |
||
277 | * Get the IP address of the current connection. |
||
278 | * |
||
279 | * @return string IP address |
||
280 | */ |
||
281 | public function address() |
||
289 | |||
290 | /** |
||
291 | * Get all IP addresses associated with the current connection. |
||
292 | * |
||
293 | * @return array IP addresses |
||
294 | */ |
||
295 | public function addresses() |
||
303 | |||
304 | /** |
||
305 | * Get the last error as a string. |
||
306 | * |
||
307 | * @return string|null Last error |
||
308 | */ |
||
309 | public function lastError() |
||
317 | |||
318 | /** |
||
319 | * Set the last error. |
||
320 | * |
||
321 | * @param int $errNum Error code |
||
322 | * @param string $errStr Error string |
||
323 | * @return void |
||
324 | */ |
||
325 | public function setLastError($errNum, $errStr) |
||
329 | |||
330 | /** |
||
331 | * Write data to the socket. |
||
332 | * |
||
333 | * The bool false return value is deprecated and will be int 0 in the next major. |
||
334 | * Please code respectively to be future proof. |
||
335 | * |
||
336 | * @param string $data The data to write to the socket. |
||
337 | * @return int|false Bytes written. |
||
338 | */ |
||
339 | public function write($data) |
||
356 | |||
357 | /** |
||
358 | * Read data from the socket. Returns false if no data is available or no connection could be |
||
359 | * established. |
||
360 | * |
||
361 | * The bool false return value is deprecated and will be null in the next major. |
||
362 | * Please code respectively to be future proof. |
||
363 | * |
||
364 | * @param int $length Optional buffer length to read; defaults to 1024 |
||
365 | * @return mixed Socket data |
||
366 | */ |
||
367 | public function read($length = 1024) |
||
387 | |||
388 | /** |
||
389 | * Disconnect the socket from the current connection. |
||
390 | * |
||
391 | * @return bool Success |
||
392 | */ |
||
393 | public function disconnect() |
||
408 | |||
409 | /** |
||
410 | * Destructor, used to disconnect from current connection. |
||
411 | */ |
||
412 | public function __destruct() |
||
416 | |||
417 | /** |
||
418 | * Resets the state of this Socket instance to it's initial state (before Object::__construct got executed) |
||
419 | * |
||
420 | * @param array|null $state Array with key and values to reset |
||
421 | * @return bool True on success |
||
422 | */ |
||
423 | public function reset($state = null) |
||
439 | |||
440 | /** |
||
441 | * Encrypts current stream socket, using one of the defined encryption methods |
||
442 | * |
||
443 | * @param string $type can be one of 'ssl2', 'ssl3', 'ssl23' or 'tls' |
||
444 | * @param string $clientOrServer can be one of 'client', 'server'. Default is 'client' |
||
445 | * @param bool $enable enable or disable encryption. Default is true (enable) |
||
446 | * @return bool True on success |
||
447 | * @throws \InvalidArgumentException When an invalid encryption scheme is chosen. |
||
448 | * @throws \Cake\Network\Exception\SocketException When attempting to enable SSL/TLS fails |
||
449 | * @see stream_socket_enable_crypto |
||
450 | */ |
||
451 | public function enableCrypto($type, $clientOrServer = 'client', $enable = true) |
||
493 | } |
||
494 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.