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, SendPackageRelayInterface, StringableRelayInterface |
||
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 */ |
||
44 | private $socket; |
||
45 | |||
46 | /** @var bool */ |
||
47 | private $connected = false; |
||
48 | |||
49 | /** |
||
50 | * Example: |
||
51 | * $relay = new SocketRelay("localhost", 7000); |
||
52 | * $relay = new SocketRelay("/tmp/rpc.sock", null, Socket::UNIX_SOCKET); |
||
53 | * |
||
54 | * @param string $address Localhost, ip address or hostname. |
||
55 | * @param int|null $port Ignored for UNIX sockets. |
||
56 | * @param int $type Default: TCP_SOCKET |
||
57 | * |
||
58 | * @throws Exceptions\InvalidArgumentException |
||
59 | */ |
||
60 | public function __construct(string $address, ?int $port = null, int $type = self::SOCK_TCP) |
||
97 | |||
98 | /** |
||
99 | * Destruct connection and disconnect. |
||
100 | */ |
||
101 | public function __destruct() |
||
107 | |||
108 | /** |
||
109 | * @return string |
||
110 | */ |
||
111 | public function __toString(): string |
||
119 | |||
120 | /** |
||
121 | * Send message package with header and body. |
||
122 | * |
||
123 | * @param string $headerPayload |
||
124 | * @param int|null $headerFlags |
||
125 | * @param string $bodyPayload |
||
126 | * @param int|null $bodyFlags |
||
127 | * @return self |
||
128 | */ |
||
129 | public function sendPackage( |
||
156 | |||
157 | /** |
||
158 | * {@inheritdoc} |
||
159 | * @return self |
||
160 | */ |
||
161 | public function send(string $payload, ?int $flags = null): self |
||
176 | |||
177 | /** |
||
178 | * {@inheritdoc} |
||
179 | */ |
||
180 | public function receiveSync(?int &$flags = null): ?string |
||
213 | |||
214 | /** |
||
215 | * @return string |
||
216 | */ |
||
217 | public function getAddress(): string |
||
221 | |||
222 | /** |
||
223 | * @return int|null |
||
224 | */ |
||
225 | public function getPort(): ?int |
||
229 | |||
230 | /** |
||
231 | * @return int |
||
232 | */ |
||
233 | public function getType(): int |
||
237 | |||
238 | /** |
||
239 | * @return bool |
||
240 | */ |
||
241 | public function isConnected(): bool |
||
245 | |||
246 | /** |
||
247 | * Ensure socket connection. Returns true if socket successfully connected |
||
248 | * or have already been connected. |
||
249 | * |
||
250 | * @return bool |
||
251 | * |
||
252 | * @throws Exceptions\RelayException |
||
253 | * @throws Error When sockets are used in unsupported environment. |
||
254 | */ |
||
255 | public function connect(): bool |
||
279 | |||
280 | /** |
||
281 | * Close connection. |
||
282 | * |
||
283 | * @throws Exceptions\RelayException |
||
284 | */ |
||
285 | public function close(): void |
||
295 | |||
296 | /** |
||
297 | * @return array Prefix [flag, length] |
||
298 | * |
||
299 | * @throws Exceptions\PrefixException |
||
300 | */ |
||
301 | private function fetchPrefix(): array |
||
322 | |||
323 | /** |
||
324 | * @return resource|false |
||
325 | * @throws Exceptions\GoridgeException |
||
326 | */ |
||
327 | private function createSocket() |
||
335 | } |
||
336 |