Complex classes like StreamIO 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 StreamIO, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class StreamIO extends AbstractIO |
||
11 | { |
||
12 | /** @var string */ |
||
13 | protected $protocol; |
||
14 | |||
15 | /** @var string */ |
||
16 | protected $host; |
||
17 | |||
18 | /** @var int */ |
||
19 | protected $port; |
||
20 | |||
21 | /** @var int */ |
||
22 | protected $connection_timeout; |
||
23 | |||
24 | /** @var int */ |
||
25 | protected $read_write_timeout; |
||
26 | |||
27 | /** @var resource */ |
||
28 | protected $context; |
||
29 | |||
30 | /** @var bool */ |
||
31 | protected $keepalive; |
||
32 | |||
33 | /** @var int */ |
||
34 | protected $heartbeat; |
||
35 | |||
36 | /** @var float */ |
||
37 | protected $last_read; |
||
38 | |||
39 | /** @var float */ |
||
40 | protected $last_write; |
||
41 | |||
42 | /** @var array */ |
||
43 | protected $last_error; |
||
44 | |||
45 | /** @var resource */ |
||
46 | private $sock; |
||
47 | |||
48 | /** @var bool */ |
||
49 | private $canSelectNull; |
||
50 | |||
51 | /** @var bool */ |
||
52 | private $canDispatchPcntlSignal; |
||
53 | |||
54 | /** |
||
55 | * @param string $host |
||
56 | * @param int $port |
||
57 | * @param int $connection_timeout |
||
58 | * @param int $read_write_timeout |
||
59 | * @param null $context |
||
60 | * @param bool $keepalive |
||
61 | * @param int $heartbeat |
||
62 | */ |
||
63 | 36 | public function __construct( |
|
64 | $host, |
||
65 | $port, |
||
66 | $connection_timeout, |
||
67 | $read_write_timeout, |
||
68 | $context = null, |
||
69 | $keepalive = false, |
||
70 | $heartbeat = 0 |
||
71 | ) { |
||
72 | 36 | $this->protocol = 'tcp'; |
|
73 | 36 | $this->host = $host; |
|
74 | 36 | $this->port = $port; |
|
75 | 36 | $this->connection_timeout = $connection_timeout; |
|
76 | 36 | $this->read_write_timeout = $read_write_timeout; |
|
77 | 36 | $this->context = $context; |
|
78 | 36 | $this->keepalive = $keepalive; |
|
79 | 36 | $this->heartbeat = $heartbeat; |
|
80 | 36 | $this->canSelectNull = true; |
|
81 | 36 | $this->canDispatchPcntlSignal = $this->isPcntlSignalEnabled(); |
|
82 | |||
83 | 36 | if (is_null($this->context)) { |
|
84 | 36 | $this->context = stream_context_create(); |
|
85 | 24 | } else { |
|
86 | $this->protocol = 'ssl'; |
||
87 | // php bugs 41631 & 65137 prevent select null from working on ssl streams |
||
88 | if (PHP_VERSION_ID < 50436) { |
||
89 | $this->canSelectNull = false; |
||
90 | } |
||
91 | } |
||
92 | 36 | } |
|
93 | |||
94 | /** |
||
95 | * @return bool |
||
96 | */ |
||
97 | 36 | private function isPcntlSignalEnabled() |
|
103 | |||
104 | /** |
||
105 | * Sets up the stream connection |
||
106 | * |
||
107 | * @throws \PhpAmqpLib\Exception\AMQPRuntimeException |
||
108 | * @throws \Exception |
||
109 | */ |
||
110 | 36 | public function connect() |
|
179 | |||
180 | 12 | /** |
|
181 | 12 | * Reconnects the socket |
|
182 | 12 | */ |
|
183 | public function reconnect() |
||
188 | |||
189 | 36 | /** |
|
190 | * @param $len |
||
191 | 36 | * @throws \PhpAmqpLib\Exception\AMQPIOException |
|
192 | 36 | * @return mixed|string |
|
193 | */ |
||
194 | 36 | public function read($len) |
|
250 | 36 | ||
251 | /** |
||
252 | 36 | * @param $data |
|
253 | * @return mixed|void |
||
254 | 36 | * @throws \PhpAmqpLib\Exception\AMQPRuntimeException |
|
255 | * @throws \PhpAmqpLib\Exception\AMQPTimeoutException |
||
256 | */ |
||
257 | public function write($data) |
||
305 | |||
306 | /** |
||
307 | * Internal error handler to deal with stream and socket errors that need to be ignored |
||
308 | * |
||
309 | * @param int $errno |
||
310 | * @param string $errstr |
||
311 | * @param string $errfile |
||
312 | * @param int $errline |
||
313 | * @param array $errcontext |
||
314 | * @return null |
||
315 | * @throws \ErrorException |
||
316 | */ |
||
317 | public function error_handler($errno, $errstr, $errfile, $errline, $errcontext = null) |
||
336 | |||
337 | /** |
||
338 | * Heartbeat logic: check connection health here |
||
339 | */ |
||
340 | protected function check_heartbeat() |
||
359 | |||
360 | 36 | /** |
|
361 | * Sends a heartbeat message |
||
362 | 36 | */ |
|
363 | 36 | protected function write_heartbeat() |
|
372 | |||
373 | public function close() |
||
380 | |||
381 | /** |
||
382 | * @return resource |
||
383 | */ |
||
384 | public function get_socket() |
||
388 | |||
389 | 36 | /** |
|
390 | * @return resource |
||
391 | 36 | */ |
|
392 | 36 | public function getSocket() |
|
396 | 36 | ||
397 | 36 | /** |
|
398 | 36 | * @param $sec |
|
399 | * @param $usec |
||
400 | 36 | * @return int|mixed |
|
401 | */ |
||
402 | public function select($sec, $usec) |
||
420 | |||
421 | /** |
||
422 | * @return mixed |
||
423 | */ |
||
424 | protected function timed_out() |
||
431 | |||
432 | /** |
||
433 | * @throws \PhpAmqpLib\Exception\AMQPIOException |
||
434 | 36 | */ |
|
435 | protected function enable_keepalive() |
||
448 | |||
449 | /** |
||
450 | * @return $this |
||
451 | */ |
||
452 | public function disableHeartbeat() |
||
458 | } |
||
459 |