Complex classes like SocketClient 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 SocketClient, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class SocketClient |
||
17 | { |
||
18 | /** |
||
19 | * @var int |
||
20 | */ |
||
21 | private $addressType; |
||
22 | |||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | private $transport; |
||
27 | |||
28 | /** |
||
29 | * @var string |
||
30 | */ |
||
31 | private $target; |
||
32 | |||
33 | /** |
||
34 | * @var int |
||
35 | */ |
||
36 | private $targetPort; |
||
37 | |||
38 | /** |
||
39 | * @var array |
||
40 | */ |
||
41 | private $contextOptions = array(); |
||
42 | |||
43 | /** |
||
44 | * @var bool |
||
45 | */ |
||
46 | private $blockingMode = true; |
||
47 | |||
48 | /** |
||
49 | * @var int |
||
50 | */ |
||
51 | private $socketTimeout; |
||
52 | |||
53 | /** |
||
54 | * @var array |
||
55 | */ |
||
56 | private $connectionFlags = array(); |
||
57 | |||
58 | /** |
||
59 | * @var resource |
||
60 | */ |
||
61 | private $streamResource; |
||
62 | |||
63 | /** |
||
64 | * @param string $target |
||
65 | * @param int $targetPort |
||
66 | * @param int $addressType |
||
67 | */ |
||
68 | public function __construct($target, $targetPort = null, $addressType = AF_INET) |
||
77 | |||
78 | /** |
||
79 | * Returns address to the socket to connect to |
||
80 | * |
||
81 | * @return string |
||
82 | */ |
||
83 | public function getAddress() |
||
94 | |||
95 | /** |
||
96 | * Gets address type |
||
97 | * |
||
98 | * @return int |
||
99 | */ |
||
100 | public function getAddressType() |
||
104 | |||
105 | /** |
||
106 | * Sets address type |
||
107 | * |
||
108 | * @param int $addressType |
||
109 | * @return $this |
||
110 | */ |
||
111 | public function setAddressType($addressType) |
||
126 | |||
127 | /** |
||
128 | * Gets socket transport |
||
129 | * |
||
130 | * @return string |
||
131 | */ |
||
132 | public function getTransport() |
||
136 | |||
137 | /** |
||
138 | * Defines socket transport |
||
139 | * |
||
140 | * @param string $transport |
||
141 | * @return $this |
||
142 | */ |
||
143 | public function setTransport($transport) |
||
152 | |||
153 | /** |
||
154 | * Gets target |
||
155 | * |
||
156 | * @return string |
||
157 | */ |
||
158 | public function getTarget() |
||
162 | |||
163 | /** |
||
164 | * Sets target portion of the remote socket parameter |
||
165 | * |
||
166 | * @param string $target |
||
167 | * @return $this |
||
168 | */ |
||
169 | public function setTarget($target) |
||
174 | |||
175 | /** |
||
176 | * Gets target port |
||
177 | * |
||
178 | * @return int |
||
179 | */ |
||
180 | public function getTargetPort() |
||
184 | |||
185 | /** |
||
186 | * Sets target port |
||
187 | * |
||
188 | * @param int $targetPort |
||
189 | * @return $this |
||
190 | */ |
||
191 | public function setTargetPort($targetPort) |
||
203 | |||
204 | /** |
||
205 | * Sets stream context parameters |
||
206 | * |
||
207 | * @param array $contextOptions |
||
208 | * @return $this |
||
209 | */ |
||
210 | public function setContextOptions(array $contextOptions) |
||
215 | |||
216 | /** |
||
217 | * Gets stream context parameters |
||
218 | * |
||
219 | * @return array |
||
220 | */ |
||
221 | public function getContextOptions() |
||
225 | |||
226 | /** |
||
227 | * Gets blocking mode |
||
228 | * |
||
229 | * @return boolean |
||
230 | */ |
||
231 | public function isBlockingMode() |
||
235 | |||
236 | /** |
||
237 | * Sets blocking/non-blocking mode on a stream |
||
238 | * |
||
239 | * @param boolean $blockingMode |
||
240 | * @return $this |
||
241 | */ |
||
242 | public function setBlockingMode($blockingMode) |
||
248 | |||
249 | /** |
||
250 | * Gets a socket timeout |
||
251 | * |
||
252 | * @return int |
||
253 | */ |
||
254 | public function getSocketTimeout() |
||
262 | |||
263 | /** |
||
264 | * Sets a timeout for reading/writing data over the socket |
||
265 | * |
||
266 | * @param int $socketTimeout |
||
267 | * @return $this |
||
268 | */ |
||
269 | public function setSocketTimeout($socketTimeout) |
||
278 | |||
279 | /** |
||
280 | * Gets combination of connection flags |
||
281 | * @return array |
||
282 | */ |
||
283 | public function getConnectionFlags() |
||
291 | |||
292 | /** |
||
293 | * Adds a connection flag |
||
294 | * |
||
295 | * @param int $connectionFlag |
||
296 | * @return $this |
||
297 | */ |
||
298 | public function addConnectionFlag($connectionFlag) |
||
315 | |||
316 | /** |
||
317 | * Drops all connection flags |
||
318 | * |
||
319 | * @return $this |
||
320 | */ |
||
321 | public function dropConnectionFlags() |
||
326 | |||
327 | /** |
||
328 | * Returns a stream resource |
||
329 | * |
||
330 | * @return resource |
||
331 | */ |
||
332 | public function getStreamResource() |
||
336 | |||
337 | /** |
||
338 | * Sets a stream resource |
||
339 | * |
||
340 | * @param resource $streamResource |
||
341 | * @return $this |
||
342 | */ |
||
343 | public function setStreamResource($streamResource) |
||
352 | |||
353 | /** |
||
354 | * Checks if stream resource exists |
||
355 | * |
||
356 | * @return bool |
||
357 | */ |
||
358 | public function isAlive() |
||
362 | |||
363 | /** |
||
364 | * Establishes a socket connection |
||
365 | * |
||
366 | * @return $this |
||
367 | */ |
||
368 | public function connect() |
||
397 | |||
398 | /** |
||
399 | * Writes data |
||
400 | * |
||
401 | * @param string $data |
||
402 | * @return $this |
||
403 | */ |
||
404 | public function write($data) |
||
419 | |||
420 | /** |
||
421 | * Reads data |
||
422 | * |
||
423 | * @param int $length |
||
424 | * @return string |
||
425 | */ |
||
426 | public function read($length) |
||
448 | |||
449 | /** |
||
450 | * Shutdowns a connection |
||
451 | * |
||
452 | * @return $this |
||
453 | */ |
||
454 | public function disconnect() |
||
465 | } |
||
466 |