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 |
||
14 | abstract class IOStream |
||
15 | { |
||
16 | use \PHPDaemon\Traits\ClassWatchdog; |
||
17 | use \PHPDaemon\Traits\StaticObjectWatchdog; |
||
18 | use \PHPDaemon\Traits\EventHandlers; |
||
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) |
||
200 | |||
201 | /** |
||
202 | * Getter |
||
203 | * @param string $name Name |
||
204 | * @return mixed |
||
205 | */ |
||
206 | public function __get($name) |
||
207 | { |
||
208 | if ($name === 'finished' |
||
209 | || $name === 'alive' |
||
210 | || $name === 'freed' |
||
211 | || $name === 'url' |
||
212 | ) { |
||
213 | return $this->{$name}; |
||
214 | } |
||
215 | return null; |
||
216 | } |
||
217 | |||
218 | |||
219 | /** |
||
220 | * Freed? |
||
221 | * @return boolean |
||
222 | */ |
||
223 | public function isFreed() |
||
227 | |||
228 | /** |
||
229 | * Finished? |
||
230 | * @return boolean |
||
231 | */ |
||
232 | public function isFinished() |
||
236 | |||
237 | /** |
||
238 | * Get EventBufferEvent |
||
239 | * @return EventBufferEvent |
||
240 | */ |
||
241 | public function getBev() |
||
245 | |||
246 | /** |
||
247 | * Get file descriptor |
||
248 | * @return resource File descriptor |
||
249 | */ |
||
250 | public function getFd() |
||
254 | |||
255 | /** |
||
256 | * Sets context mode |
||
257 | * @param object $ctx Context |
||
258 | * @param integer $mode Mode |
||
259 | * @return void |
||
260 | */ |
||
261 | |||
262 | public function setContext($ctx, $mode) |
||
267 | |||
268 | /** |
||
269 | * Sets fd |
||
270 | * @param resource $fd File descriptor |
||
271 | * @param object $bev EventBufferEvent |
||
272 | * @return void |
||
273 | */ |
||
274 | public function setFd($fd, $bev = null) |
||
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 | * Called when the session constructed |
||
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 | View Code Duplication | public function lookExact($n, $o = 0) |
|
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) |
|
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) |
||
789 | |||
790 | /** |
||
791 | * Called when new data received |
||
792 | * @return void |
||
793 | */ |
||
794 | protected function onRead() |
||
797 | |||
798 | /** |
||
799 | * Called when the stream is handshaked (at low-level), and peer is ready to recv. data |
||
800 | * @return void |
||
801 | */ |
||
802 | protected function onReady() |
||
805 | |||
806 | /** |
||
807 | * Push callback which will be called only once, when writing is available next time |
||
808 | * @param callable $cb Callback |
||
809 | * @return void |
||
810 | */ |
||
811 | public function onWriteOnce($cb) |
||
819 | |||
820 | /** |
||
821 | * Called when the connection is ready to accept new data |
||
822 | * @param object $bev EventBufferEvent |
||
823 | * @return void |
||
824 | */ |
||
825 | public function onWriteEv($bev) |
||
868 | |||
869 | /** |
||
870 | * Called when the connection state changed |
||
871 | * @param object $bev EventBufferEvent |
||
872 | * @param integer $events Events |
||
873 | * @return void |
||
874 | */ |
||
875 | public function onStateEv($bev, $events) |
||
906 | |||
907 | /** |
||
908 | * Moves arbitrary number of bytes from input buffer to given buffer |
||
909 | * @param \EventBuffer $dest Destination nuffer |
||
910 | * @param integer $n Max. number of bytes to move |
||
911 | * @return integer|false |
||
912 | */ |
||
913 | View Code Duplication | public function moveToBuffer(\EventBuffer $dest, $n) |
|
920 | |||
921 | /** |
||
922 | * Moves arbitrary number of bytes from given buffer to output buffer |
||
923 | * @param \EventBuffer $src Source buffer |
||
924 | * @param integer $n Max. number of bytes to move |
||
925 | * @return integer|false |
||
926 | */ |
||
927 | View Code Duplication | public function writeFromBuffer(\EventBuffer $src, $n) |
|
935 | |||
936 | /** |
||
937 | * Read data from the connection's buffer |
||
938 | * @param integer $n Max. number of bytes to read |
||
939 | * @return string|false Readed data |
||
940 | */ |
||
941 | public function read($n) |
||
955 | |||
956 | /** |
||
957 | * Reads all data from the connection's buffer |
||
958 | * @return string Readed data |
||
959 | */ |
||
960 | View Code Duplication | public function readUnlimited() |
|
971 | } |
||
972 |
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.