Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like IOStream 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 IOStream, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | abstract class IOStream |
||
14 | { |
||
15 | use \PHPDaemon\Traits\ClassWatchdog; |
||
16 | use \PHPDaemon\Traits\StaticObjectWatchdog; |
||
17 | use \PHPDaemon\Traits\EventHandlers; |
||
18 | use EventLoopContainer; |
||
19 | |||
20 | /** |
||
21 | * @var object Associated pool |
||
22 | */ |
||
23 | public $pool; |
||
24 | |||
25 | /** |
||
26 | * @var string EOL |
||
27 | */ |
||
28 | protected $EOL = "\n"; |
||
29 | |||
30 | /** |
||
31 | * @var integer EOLS_* switch |
||
32 | */ |
||
33 | protected $EOLS; |
||
34 | |||
35 | /** |
||
36 | * @var object EventBufferEvent |
||
37 | */ |
||
38 | protected $bev; |
||
39 | |||
40 | /** |
||
41 | * @var resource File descriptor |
||
42 | */ |
||
43 | protected $fd; |
||
44 | |||
45 | /** |
||
46 | * @var boolean Finished? |
||
47 | */ |
||
48 | protected $finished = false; |
||
49 | |||
50 | /** |
||
51 | * @var boolean Ready? |
||
52 | */ |
||
53 | protected $ready = false; |
||
54 | |||
55 | /** |
||
56 | * @var boolean Writing? |
||
57 | */ |
||
58 | protected $writing = true; |
||
59 | |||
60 | /** |
||
61 | * @var boolean Timeout? |
||
62 | */ |
||
63 | protected $timedout = false; |
||
64 | |||
65 | /** |
||
66 | * @var integer Default low mark. Minimum number of bytes in buffer |
||
67 | */ |
||
68 | protected $lowMark = 1; |
||
69 | |||
70 | /** |
||
71 | * @var integer Default high mark. Maximum number of bytes in buffer |
||
72 | */ |
||
73 | protected $highMark = 0xFFFF; // initial value of the maximum amout of bytes in buffer |
||
74 | |||
75 | /** |
||
76 | * @var integer Priority |
||
77 | */ |
||
78 | protected $priority; |
||
79 | |||
80 | /** |
||
81 | * @var boolean Initialized? |
||
82 | */ |
||
83 | protected $inited = false; |
||
84 | |||
85 | /** |
||
86 | * @var integer Current state |
||
87 | */ |
||
88 | protected $state = 0; // stream state of the connection (application protocol level) |
||
89 | |||
90 | /** |
||
91 | * Alias of STATE_STANDBY |
||
92 | */ |
||
93 | const STATE_ROOT = 0; |
||
94 | |||
95 | /** |
||
96 | * Standby state (default state) |
||
97 | */ |
||
98 | const STATE_STANDBY = 0; |
||
99 | |||
100 | /** |
||
101 | * @var object Stack of callbacks called when writing is done |
||
102 | */ |
||
103 | protected $onWriteOnce; |
||
104 | |||
105 | /** |
||
106 | * @var integer Timeout |
||
107 | */ |
||
108 | protected $timeout = null; |
||
109 | |||
110 | /** |
||
111 | * @var string URL |
||
112 | */ |
||
113 | protected $url; |
||
114 | |||
115 | /** |
||
116 | * @var boolean Alive? |
||
117 | */ |
||
118 | protected $alive = false; |
||
119 | |||
120 | /** |
||
121 | * @var boolean Is bevConnect used? |
||
122 | */ |
||
123 | protected $bevConnect = false; |
||
124 | |||
125 | /** |
||
126 | * @var boolean Should we can onReadEv() in next onWriteEv()? |
||
127 | */ |
||
128 | protected $wRead = false; |
||
129 | |||
130 | /** |
||
131 | * @var boolean Freed? |
||
132 | */ |
||
133 | protected $freed = false; |
||
134 | |||
135 | /** |
||
136 | * @var object Context |
||
137 | */ |
||
138 | protected $ctx; |
||
139 | |||
140 | /** |
||
141 | * @var object Context name |
||
142 | */ |
||
143 | protected $ctxname; |
||
144 | |||
145 | /** |
||
146 | * @var integer Defines context-related flag |
||
147 | */ |
||
148 | protected $ctxMode; |
||
149 | |||
150 | /** |
||
151 | * @var boolean SSL? |
||
152 | */ |
||
153 | protected $ssl = false; |
||
154 | |||
155 | /** |
||
156 | * @var float Read timeout |
||
157 | */ |
||
158 | protected $timeoutRead; |
||
159 | |||
160 | /** |
||
161 | * @var float Write timeout |
||
162 | */ |
||
163 | protected $timeoutWrite; |
||
164 | |||
165 | /** |
||
166 | * IOStream constructor |
||
167 | * @param resource $fd File descriptor. Optional |
||
|
|||
168 | * @param object $pool Pool. Optional |
||
169 | */ |
||
170 | public function __construct($fd = null, $pool = null) |
||
171 | { |
||
172 | if ($pool) { |
||
173 | $this->pool = $pool; |
||
174 | $this->eventLoop = $pool->eventLoop; |
||
175 | var_dump(['iostream pool' => md5(spl_object_hash($pool)), 'eventLoop' => $this->eventLoop]); |
||
176 | $this->pool->attach($this); |
||
177 | if (isset($this->pool->config->timeout->value)) { |
||
178 | $this->timeout = $this->pool->config->timeout->value; |
||
179 | } |
||
180 | if (isset($this->pool->config->timeoutread->value)) { |
||
181 | $this->timeoutRead = $this->pool->config->timeoutread->value; |
||
182 | } |
||
183 | if (isset($this->pool->config->timeoutwrite->value)) { |
||
184 | $this->timeoutWrite = $this->pool->config->timeoutwrite->value; |
||
185 | } |
||
186 | } |
||
187 | |||
188 | if ($fd !== null) { |
||
189 | $this->setFd($fd); |
||
190 | } |
||
191 | |||
192 | if ($this->EOL === "\n") { |
||
193 | $this->EOLS = \EventBuffer::EOL_LF; |
||
194 | } elseif ($this->EOL === "\r\n") { |
||
195 | $this->EOLS = \EventBuffer::EOL_CRLF; |
||
196 | } else { |
||
197 | $this->EOLS = \EventBuffer::EOL_ANY; |
||
198 | } |
||
199 | |||
200 | $this->onWriteOnce = new StackCallbacks; |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * Getter |
||
205 | * @param string $name Name |
||
206 | * @return mixed |
||
207 | */ |
||
208 | public function __get($name) |
||
209 | { |
||
210 | if ($name === 'finished' |
||
211 | || $name === 'alive' |
||
212 | || $name === 'freed' |
||
213 | || $name === 'url' |
||
214 | ) { |
||
215 | return $this->{$name}; |
||
216 | } |
||
217 | return null; |
||
218 | } |
||
219 | |||
220 | |||
221 | /** |
||
222 | * Freed? |
||
223 | * @return boolean |
||
224 | */ |
||
225 | public function isFreed() |
||
226 | { |
||
227 | return $this->freed; |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Finished? |
||
232 | * @return boolean |
||
233 | */ |
||
234 | public function isFinished() |
||
235 | { |
||
236 | return $this->finished; |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * Get EventBufferEvent |
||
241 | * @return EventBufferEvent |
||
242 | */ |
||
243 | public function getBev() |
||
244 | { |
||
245 | return $this->bev; |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * Get file descriptor |
||
250 | * @return resource File descriptor |
||
251 | */ |
||
252 | public function getFd() |
||
253 | { |
||
254 | return $this->fd; |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * Sets context mode |
||
259 | * @param object $ctx Context |
||
260 | * @param integer $mode Mode |
||
261 | * @return void |
||
262 | */ |
||
263 | |||
264 | public function setContext($ctx, $mode) |
||
265 | { |
||
266 | $this->ctx = $ctx; |
||
267 | $this->ctxMode = $mode; |
||
268 | } |
||
269 | |||
270 | /** |
||
271 | * Sets fd |
||
272 | * @param resource $fd File descriptor |
||
273 | * @param object $bev EventBufferEvent |
||
274 | * @return void |
||
275 | */ |
||
276 | public function setFd($fd, $bev = null) |
||
277 | { |
||
278 | $this->fd = $fd; |
||
279 | if ($this->fd === false) { |
||
280 | $this->finish(); |
||
281 | return; |
||
282 | } |
||
283 | if ($bev !== null) { |
||
284 | $this->bev = $bev; |
||
285 | $this->bev->setCallbacks([$this, 'onReadEv'], [$this, 'onWriteEv'], [$this, 'onStateEv']); |
||
286 | if (!$this->bev) { |
||
287 | return; |
||
288 | } |
||
289 | $this->ready = true; |
||
290 | $this->alive = true; |
||
291 | } else { |
||
292 | $flags = !is_resource($this->fd) ? \EventBufferEvent::OPT_CLOSE_ON_FREE : 0; |
||
293 | $flags |= \EventBufferEvent::OPT_DEFER_CALLBACKS; /* buggy option */ |
||
294 | if ($this->ctx) { |
||
295 | if ($this->ctx instanceof \EventSslContext) { |
||
296 | $this->bev = $this->eventLoop->bufferEventSsl( |
||
297 | $this->fd, |
||
298 | $this->ctx, |
||
299 | $this->ctxMode, |
||
300 | $flags |
||
301 | ); |
||
302 | |||
303 | View Code Duplication | if ($this->bev) { |
|
304 | $this->bev->setCallbacks([$this, 'onReadEv'], [$this, 'onWriteEv'], [$this, 'onStateEv']); |
||
305 | } |
||
306 | $this->ssl = true; |
||
307 | } else { |
||
308 | $this->log('unsupported type of context: ' . ($this->ctx ? get_class($this->ctx) : 'undefined')); |
||
309 | return; |
||
310 | } |
||
311 | View Code Duplication | } else { |
|
312 | $this->bev = $this->eventLoop->bufferEvent( |
||
313 | $this->fd, |
||
314 | $flags, |
||
315 | [$this, 'onReadEv'], |
||
316 | [$this, 'onWriteEv'], |
||
317 | [$this, 'onStateEv'] |
||
318 | ); |
||
319 | } |
||
320 | if (!$this->bev) { |
||
321 | return; |
||
322 | } |
||
323 | } |
||
324 | if ($this->priority !== null) { |
||
325 | $this->bev->priority = $this->priority; |
||
326 | } |
||
327 | $this->setTimeouts( |
||
328 | $this->timeoutRead !== null ? $this->timeoutRead : $this->timeout, |
||
329 | $this->timeoutWrite !== null ? $this->timeoutWrite : $this->timeout |
||
330 | ); |
||
331 | if ($this->bevConnect && ($this->fd === null)) { |
||
332 | //$this->bev->connectHost(Daemon::$process->dnsBase, $this->hostReal, $this->port); |
||
333 | $this->bev->connect($this->addr); |
||
334 | } |
||
335 | if (!$this->bev) { |
||
336 | $this->finish(); |
||
337 | return; |
||
338 | } |
||
339 | View Code Duplication | if (!$this->bev->enable(\Event::READ | \Event::WRITE | \Event::TIMEOUT | \Event::PERSIST)) { |
|
340 | $this->finish(); |
||
341 | return; |
||
342 | } |
||
343 | $this->bev->setWatermark(\Event::READ, $this->lowMark, $this->highMark); |
||
344 | init: |
||
345 | if ($this->keepalive && $this->fd != null) { |
||
346 | $this->setKeepalive(true); |
||
347 | } |
||
348 | if (!$this->inited) { |
||
349 | $this->inited = true; |
||
350 | $this->init(); |
||
351 | } |
||
352 | } |
||
353 | |||
354 | /** |
||
355 | * Set timeout |
||
356 | * @param integer $rw Timeout |
||
357 | * @return void |
||
358 | */ |
||
359 | public function setTimeout($rw) |
||
363 | |||
364 | /** |
||
365 | * Set timeouts |
||
366 | * @param integer $read Read timeout in seconds |
||
367 | * @param integer $write Write timeout in seconds |
||
368 | * @return void |
||
369 | */ |
||
370 | public function setTimeouts($read, $write) |
||
378 | |||
379 | /** |
||
380 | * Sets priority |
||
381 | * @param integer $p Priority |
||
382 | * @return void |
||
383 | */ |
||
384 | public function setPriority($p) |
||
389 | |||
390 | /** |
||
391 | * Sets watermark |
||
392 | * @param integer|null $low Low |
||
393 | * @param integer|null $high High |
||
394 | * @return void |
||
395 | */ |
||
396 | public function setWatermark($low = null, $high = null) |
||
409 | |||
410 | /** |
||
411 | * Constructor |
||
412 | * @return void |
||
413 | */ |
||
414 | protected function init() |
||
417 | |||
418 | /** |
||
419 | * Reads line from buffer |
||
420 | * @param integer $eol EOLS_* |
||
421 | * @return string|null |
||
422 | */ |
||
423 | public function readLine($eol = null) |
||
430 | |||
431 | /** |
||
432 | * Drains buffer |
||
433 | * @param integer $n Numbers of bytes to drain |
||
434 | * @return boolean Success |
||
435 | */ |
||
436 | public function drain($n) |
||
440 | |||
441 | /** |
||
442 | * Drains buffer it matches the string |
||
443 | * @param string $str Data |
||
444 | * @return boolean|null Success |
||
445 | */ |
||
446 | public function drainIfMatch($str) |
||
471 | |||
472 | /** |
||
473 | * Reads exact $n bytes of buffer without draining |
||
474 | * @param integer $n Number of bytes to read |
||
475 | * @param integer $o Offset |
||
476 | * @return string|false |
||
477 | */ |
||
478 | public function lookExact($n, $o = 0) |
||
479 | { |
||
480 | if (!isset($this->bev)) { |
||
481 | return false; |
||
482 | } |
||
483 | if ($o + $n > $this->bev->input->length) { |
||
484 | return false; |
||
485 | } |
||
486 | return $this->bev->input->substr($o, $n); |
||
487 | } |
||
488 | |||
489 | /** |
||
490 | * Prepends data to input buffer |
||
491 | * @param string $str Data |
||
492 | * @return boolean Success |
||
493 | */ |
||
494 | public function prependInput($str) |
||
501 | |||
502 | /** |
||
503 | * Prepends data to output buffer |
||
504 | * @param string $str Data |
||
505 | * @return boolean Success |
||
506 | */ |
||
507 | public function prependOutput($str) |
||
514 | |||
515 | /** |
||
516 | * Read from buffer without draining |
||
517 | * @param integer $n Number of bytes to read |
||
518 | * @param integer $o Offset |
||
519 | * @return string|false |
||
520 | */ |
||
521 | View Code Duplication | public function look($n, $o = 0) |
|
522 | { |
||
523 | if (!isset($this->bev)) { |
||
524 | return false; |
||
525 | } |
||
526 | if ($this->bev->input->length <= $o) { |
||
527 | return ''; |
||
528 | } |
||
529 | return $this->bev->input->substr($o, $n); |
||
530 | } |
||
531 | |||
532 | /** |
||
533 | * Read from buffer without draining |
||
534 | * @param integer $o Offset |
||
535 | * @param integer $n Number of bytes to read |
||
536 | * @return string|false |
||
537 | */ |
||
538 | public function substr($o, $n = -1) |
||
545 | |||
546 | /** |
||
547 | * Searches first occurence of the string in input buffer |
||
548 | * @param string $what Needle |
||
549 | * @param integer $start Offset start |
||
550 | * @param integer $end Offset end |
||
551 | * @return integer Position |
||
552 | */ |
||
553 | public function search($what, $start = 0, $end = -1) |
||
557 | |||
558 | /** |
||
559 | * Reads exact $n bytes from buffer |
||
560 | * @param integer $n Number of bytes to read |
||
561 | * @return string|false |
||
562 | */ |
||
563 | View Code Duplication | public function readExact($n) |
|
573 | |||
574 | /** |
||
575 | * Returns length of input buffer |
||
576 | * @return integer |
||
577 | */ |
||
578 | public function getInputLength() |
||
582 | |||
583 | /** |
||
584 | * Called when the worker is going to shutdown |
||
585 | * @return boolean Ready to shutdown? |
||
586 | */ |
||
587 | public function gracefulShutdown() |
||
592 | |||
593 | /** |
||
594 | * Freeze input |
||
595 | * @param boolean $at_front At front. Default is true. If the front of a buffer is frozen, operations that drain data from the front of the buffer, or that prepend data to the buffer, will fail until it is unfrozen. If the back a buffer is frozen, operations that append data from the buffer will fail until it is unfrozen |
||
596 | * @return boolean Success |
||
597 | */ |
||
598 | public function freezeInput($at_front = true) |
||
605 | |||
606 | /** |
||
607 | * Unfreeze input |
||
608 | * @param boolean $at_front At front. Default is true. If the front of a buffer is frozen, operations that drain data from the front of the buffer, or that prepend data to the buffer, will fail until it is unfrozen. If the back a buffer is frozen, operations that append data from the buffer will fail until it is unfrozen |
||
609 | * @return boolean Success |
||
610 | */ |
||
611 | public function unfreezeInput($at_front = true) |
||
618 | |||
619 | /** |
||
620 | * Freeze output |
||
621 | * @param boolean $at_front At front. Default is true. If the front of a buffer is frozen, operations that drain data from the front of the buffer, or that prepend data to the buffer, will fail until it is unfrozen. If the back a buffer is frozen, operations that append data from the buffer will fail until it is unfrozen |
||
622 | * @return boolean Success |
||
623 | */ |
||
624 | public function freezeOutput($at_front = true) |
||
631 | |||
632 | /** |
||
633 | * Unfreeze output |
||
634 | * @param boolean $at_front At front. Default is true. If the front of a buffer is frozen, operations that drain data from the front of the buffer, or that prepend data to the buffer, will fail until it is unfrozen. If the back a buffer is frozen, operations that append data from the buffer will fail until it is unfrozen |
||
635 | * @return boolean Success |
||
636 | */ |
||
637 | public function unfreezeOutput($at_front = true) |
||
644 | |||
645 | /** |
||
646 | * Called when the connection is ready to accept new data |
||
647 | * @return void |
||
648 | */ |
||
649 | public function onWrite() |
||
652 | |||
653 | /** |
||
654 | * Send data to the connection. Note that it just writes to buffer that flushes at every baseloop |
||
655 | * @param string $data Data to send |
||
656 | * @return boolean Success |
||
657 | */ |
||
658 | View Code Duplication | public function write($data) |
|
678 | |||
679 | /** |
||
680 | * Send data and appending \n to connection. Note that it just writes to buffer flushed at every baseloop |
||
681 | * @param string $data Data to send |
||
682 | * @return boolean Success |
||
683 | */ |
||
684 | View Code Duplication | public function writeln($data) |
|
701 | |||
702 | /** |
||
703 | * Finish the session. You should not worry about buffers, they are going to be flushed properly |
||
704 | * @return void |
||
705 | */ |
||
706 | public function finish() |
||
718 | |||
719 | /** |
||
720 | * Called when the session finished |
||
721 | * @return void |
||
722 | */ |
||
723 | protected function onFinish() |
||
726 | |||
727 | /** |
||
728 | * Close the connection |
||
729 | * @return void |
||
730 | */ |
||
731 | public function close() |
||
745 | |||
746 | /** |
||
747 | * Unsets pointers of associated EventBufferEvent and File descriptr |
||
748 | * @return void |
||
749 | */ |
||
750 | public function unsetFd() |
||
755 | |||
756 | /** |
||
757 | * Send message to log |
||
758 | * @param string $m Message |
||
759 | * @return void |
||
760 | */ |
||
761 | protected function log($m) |
||
765 | |||
766 | /** |
||
767 | * Called when the connection has got new data |
||
768 | * @param object $bev EventBufferEvent |
||
769 | * @return void |
||
770 | */ |
||
771 | public function onReadEv($bev) |
||
772 | { |
||
773 | if (!$this->ready) { |
||
774 | $this->wRead = true; |
||
775 | return; |
||
776 | } |
||
777 | if ($this->finished) { |
||
778 | return; |
||
779 | } |
||
780 | try { |
||
781 | $this->onRead(); |
||
782 | } catch (\Exception $e) { |
||
783 | Daemon::uncaughtExceptionHandler($e); |
||
784 | } |
||
785 | } |
||
786 | |||
787 | /** |
||
788 | * Called when new data received |
||
789 | * @return void |
||
790 | */ |
||
791 | protected function onRead() |
||
794 | |||
795 | /** |
||
796 | * Called when the stream is handshaked (at low-level), and peer is ready to recv. data |
||
797 | * @return void |
||
798 | */ |
||
799 | protected function onReady() |
||
802 | |||
803 | /** |
||
804 | * Push callback which will be called only once, when writing is available next time |
||
805 | * @param callable $cb Callback |
||
806 | * @return void |
||
807 | */ |
||
808 | public function onWriteOnce($cb) |
||
816 | |||
817 | /** |
||
818 | * Called when the connection is ready to accept new data |
||
819 | * @param object $bev EventBufferEvent |
||
820 | * @return void |
||
821 | */ |
||
822 | public function onWriteEv($bev) |
||
823 | { |
||
824 | $this->writing = false; |
||
825 | if ($this->finished) { |
||
862 | |||
863 | /** |
||
864 | * Called when the connection state changed |
||
865 | * @param object $bev EventBufferEvent |
||
866 | * @param integer $events Events |
||
867 | * @return void |
||
868 | */ |
||
869 | public function onStateEv($bev, $events) |
||
900 | |||
901 | /** |
||
902 | * Moves arbitrary number of bytes from input buffer to given buffer |
||
903 | * @param \EventBuffer $dest Destination nuffer |
||
904 | * @param integer $n Max. number of bytes to move |
||
905 | * @return integer|false |
||
906 | */ |
||
907 | View Code Duplication | public function moveToBuffer(\EventBuffer $dest, $n) |
|
914 | |||
915 | /** |
||
916 | * Moves arbitrary number of bytes from given buffer to output buffer |
||
917 | * @param \EventBuffer $src Source buffer |
||
918 | * @param integer $n Max. number of bytes to move |
||
919 | * @return integer|false |
||
920 | */ |
||
921 | View Code Duplication | public function writeFromBuffer(\EventBuffer $src, $n) |
|
929 | |||
930 | /** |
||
931 | * Read data from the connection's buffer |
||
932 | * @param integer $n Max. number of bytes to read |
||
933 | * @return string|false Readed data |
||
934 | */ |
||
935 | public function read($n) |
||
949 | |||
950 | /** |
||
951 | * Reads all data from the connection's buffer |
||
952 | * @return string Readed data |
||
953 | */ |
||
954 | public function readUnlimited() |
||
965 | } |
||
966 |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.