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 | * {@inheritdoc} |
||
| 119 | * @return self |
||
| 120 | */ |
||
| 121 | public function sendPackage( |
||
| 148 | |||
| 149 | /** |
||
| 150 | * {@inheritdoc} |
||
| 151 | * @return self |
||
| 152 | */ |
||
| 153 | public function send(string $payload, ?int $flags = null): self |
||
| 168 | |||
| 169 | /** |
||
| 170 | * {@inheritdoc} |
||
| 171 | */ |
||
| 172 | public function receiveSync(?int &$flags = null): ?string |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @return string |
||
| 208 | */ |
||
| 209 | public function getAddress(): string |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @return int|null |
||
| 216 | */ |
||
| 217 | public function getPort(): ?int |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @return int |
||
| 224 | */ |
||
| 225 | public function getType(): int |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @return bool |
||
| 232 | */ |
||
| 233 | public function isConnected(): bool |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Ensure socket connection. Returns true if socket successfully connected |
||
| 240 | * or have already been connected. |
||
| 241 | * |
||
| 242 | * @return bool |
||
| 243 | * |
||
| 244 | * @throws Exceptions\RelayException |
||
| 245 | * @throws Error When sockets are used in unsupported environment. |
||
| 246 | */ |
||
| 247 | public function connect(): bool |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Close connection. |
||
| 267 | * |
||
| 268 | * @throws Exceptions\RelayException |
||
| 269 | */ |
||
| 270 | public function close(): void |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @return array Prefix [flag, length] |
||
| 282 | * |
||
| 283 | * @throws Exceptions\PrefixException |
||
| 284 | */ |
||
| 285 | private function fetchPrefix(): array |
||
| 306 | |||
| 307 | /** |
||
| 308 | * @return resource |
||
| 309 | * @throws Exceptions\GoridgeException |
||
| 310 | */ |
||
| 311 | private function createSocket() |
||
| 319 | } |
||
| 320 |