Complex classes like SecureSocket 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 SecureSocket, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class SecureSocket implements SocketInterface |
||
| 9 | { |
||
| 10 | const MAX32BIT = 4294967295; |
||
| 11 | const MAX16BIT = 65535; |
||
| 12 | |||
| 13 | const CONNECTION_TIMEOUT = 3; |
||
| 14 | const SOCKET_TIMEOUT = 3; |
||
| 15 | |||
| 16 | private static $acceptableOptions = array( |
||
| 17 | 'autoconnect', |
||
| 18 | 'socket_timeout', |
||
| 19 | 'connection_timeout', |
||
| 20 | 'persistent', |
||
| 21 | 'ssl_allow_self_signed', |
||
| 22 | 'ssl_cafile', |
||
| 23 | 'ssl_peer_name', |
||
| 24 | 'ssl_disable_compression', |
||
| 25 | 'ssl_tls_only', |
||
| 26 | 'ssl_verify_peer_name', |
||
| 27 | ); |
||
| 28 | |||
| 29 | private $options = array( |
||
| 30 | 'autoconnect' => true, |
||
| 31 | 'socket_timeout' => self::SOCKET_TIMEOUT, |
||
| 32 | 'connection_timeout' => self::CONNECTION_TIMEOUT, |
||
| 33 | 'ssl_allow_self_signed' => false, |
||
| 34 | 'ssl_disable_compression' => true, |
||
| 35 | 'ssl_tls_only' => false, |
||
| 36 | 'persistent' => false, |
||
| 37 | ); |
||
| 38 | |||
| 39 | /** @var resource */ |
||
| 40 | private $socket; |
||
| 41 | |||
| 42 | /** @var string */ |
||
| 43 | private $uri; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @param string $host |
||
| 47 | * @param int $port |
||
| 48 | * @param array $options |
||
| 49 | * @throws InvalidArgumentException |
||
| 50 | */ |
||
| 51 | public function __construct($host, $port, array $options = array()) |
||
| 70 | |||
| 71 | public function __destruct() |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @param int $code |
||
| 80 | * @param string $message |
||
| 81 | * @param string $file |
||
| 82 | * @param int $line |
||
| 83 | * @throws \ErrorException |
||
| 84 | */ |
||
| 85 | public function convertErrorToException($code, $message, $file, $line) |
||
| 89 | |||
| 90 | /** |
||
| 91 | * set options |
||
| 92 | * |
||
| 93 | * @param array $options |
||
| 94 | * @throws \InvalidArgumentException |
||
| 95 | */ |
||
| 96 | public function setOptions(array $options) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @return bool |
||
| 104 | */ |
||
| 105 | public function isConnected() |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @throws Exception |
||
| 112 | */ |
||
| 113 | public function connect() |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @throws Exception |
||
| 178 | */ |
||
| 179 | public function disconnect() |
||
| 198 | |||
| 199 | /** |
||
| 200 | * recreate a connection. |
||
| 201 | * |
||
| 202 | * @throws Exception |
||
| 203 | */ |
||
| 204 | public function reconnect() |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @param int $length |
||
| 212 | * @return string |
||
| 213 | * @throws Exception |
||
| 214 | */ |
||
| 215 | public function read($length) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @param mixed $buffer |
||
| 240 | * @return int |
||
| 241 | * @throws Exception |
||
| 242 | */ |
||
| 243 | public function write($buffer) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * merge options |
||
| 302 | * |
||
| 303 | * @param array $options |
||
| 304 | * @throws InvalidArgumentException |
||
| 305 | */ |
||
| 306 | private function mergeOptions(array $options) |
||
| 315 | |||
| 316 | /** |
||
| 317 | * @throws InvalidArgumentException |
||
| 318 | */ |
||
| 319 | private function validateOptions() |
||
| 345 | |||
| 346 | /** |
||
| 347 | * get specified option's value |
||
| 348 | * |
||
| 349 | * @param string $key |
||
| 350 | * @param mixed $default |
||
| 351 | * @return mixed |
||
| 352 | */ |
||
| 353 | private function getOption($key, $default = null) |
||
| 359 | } |
||
| 360 |