Complex classes like SocketRelay 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 SocketRelay, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class SocketRelay implements RelayInterface |
||
| 26 | { |
||
| 27 | /** Supported socket types. */ |
||
| 28 | public const SOCK_TCP = 0; |
||
| 29 | public const SOCK_UNIX = 1; |
||
| 30 | |||
| 31 | // @deprecated |
||
| 32 | public const SOCK_TPC = self::SOCK_TCP; |
||
| 33 | |||
| 34 | /** @var string */ |
||
| 35 | private $address; |
||
| 36 | |||
| 37 | /** @var int|null */ |
||
| 38 | private $port; |
||
| 39 | |||
| 40 | /** @var int */ |
||
| 41 | private $type; |
||
| 42 | |||
| 43 | /** @var resource|null */ |
||
| 44 | private $socket; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Example: |
||
| 48 | * $relay = new SocketRelay("localhost", 7000); |
||
| 49 | * $relay = new SocketRelay("/tmp/rpc.sock", null, Socket::UNIX_SOCKET); |
||
| 50 | * |
||
| 51 | * @param string $address Localhost, ip address or hostname. |
||
| 52 | * @param int|null $port Ignored for UNIX sockets. |
||
| 53 | * @param int $type Default: TCP_SOCKET |
||
| 54 | * |
||
| 55 | * @throws Exceptions\InvalidArgumentException |
||
| 56 | */ |
||
| 57 | public function __construct(string $address, ?int $port = null, int $type = self::SOCK_TCP) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Destruct connection and disconnect. |
||
| 97 | */ |
||
| 98 | public function __destruct() |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @return string |
||
| 107 | */ |
||
| 108 | public function __toString(): string |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Send message package with header and body. |
||
| 119 | * |
||
| 120 | * @param string $headerPayload |
||
| 121 | * @param int|null $headerFlags |
||
| 122 | * @param string $bodyPayload |
||
| 123 | * @param int|null $bodyFlags |
||
| 124 | * @return self |
||
| 125 | */ |
||
| 126 | public function sendPackage( |
||
| 153 | |||
| 154 | /** |
||
| 155 | * {@inheritdoc} |
||
| 156 | * @return self |
||
| 157 | */ |
||
| 158 | public function send(string $payload, ?int $flags = null): self |
||
| 173 | |||
| 174 | /** |
||
| 175 | * {@inheritdoc} |
||
| 176 | */ |
||
| 177 | public function receiveSync(?int &$flags = null): ?string |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @return string |
||
| 213 | */ |
||
| 214 | public function getAddress(): string |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @return int|null |
||
| 221 | */ |
||
| 222 | public function getPort(): ?int |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @return int |
||
| 229 | */ |
||
| 230 | public function getType(): int |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @return bool |
||
| 237 | */ |
||
| 238 | public function isConnected(): bool |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Ensure socket connection. Returns true if socket successfully connected |
||
| 245 | * or have already been connected. |
||
| 246 | * |
||
| 247 | * @return bool |
||
| 248 | * |
||
| 249 | * @throws Exceptions\RelayException |
||
| 250 | * @throws Error When sockets are used in unsupported environment. |
||
| 251 | */ |
||
| 252 | public function connect(): bool |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Close connection. |
||
| 272 | * |
||
| 273 | * @throws Exceptions\RelayException |
||
| 274 | */ |
||
| 275 | public function close(): void |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @return array Prefix [flag, length] |
||
| 287 | * |
||
| 288 | * @throws Exceptions\PrefixException |
||
| 289 | */ |
||
| 290 | private function fetchPrefix(): array |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @return resource |
||
| 314 | * @throws Exceptions\GoridgeException |
||
| 315 | */ |
||
| 316 | private function createSocket() |
||
| 324 | } |
||
| 325 |