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 | use EventLoopContainer; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var object Associated pool |
||
| 23 | */ |
||
| 24 | public $pool; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var string EOL |
||
| 28 | */ |
||
| 29 | protected $EOL = "\n"; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var integer EOLS_* switch |
||
| 33 | */ |
||
| 34 | protected $EOLS; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var object EventBufferEvent |
||
| 38 | */ |
||
| 39 | protected $bev; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var resource File descriptor |
||
| 43 | */ |
||
| 44 | protected $fd; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var boolean Finished? |
||
| 48 | */ |
||
| 49 | protected $finished = false; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var boolean Ready? |
||
| 53 | */ |
||
| 54 | protected $ready = false; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var boolean Writing? |
||
| 58 | */ |
||
| 59 | protected $writing = true; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var boolean Timeout? |
||
| 63 | */ |
||
| 64 | protected $timedout = false; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var integer Default low mark. Minimum number of bytes in buffer |
||
| 68 | */ |
||
| 69 | protected $lowMark = 1; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var integer Default high mark. Maximum number of bytes in buffer |
||
| 73 | */ |
||
| 74 | protected $highMark = 0xFFFF; // initial value of the maximum amout of bytes in buffer |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var integer Priority |
||
| 78 | */ |
||
| 79 | protected $priority; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var boolean Initialized? |
||
| 83 | */ |
||
| 84 | protected $inited = false; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var integer Current state |
||
| 88 | */ |
||
| 89 | protected $state = 0; // stream state of the connection (application protocol level) |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Alias of STATE_STANDBY |
||
| 93 | */ |
||
| 94 | const STATE_ROOT = 0; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Standby state (default state) |
||
| 98 | */ |
||
| 99 | const STATE_STANDBY = 0; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var object Stack of callbacks called when writing is done |
||
| 103 | */ |
||
| 104 | protected $onWriteOnce; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @var integer Timeout |
||
| 108 | */ |
||
| 109 | protected $timeout = null; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @var string URL |
||
| 113 | */ |
||
| 114 | protected $url; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @var boolean Alive? |
||
| 118 | */ |
||
| 119 | protected $alive = false; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @var boolean Is bevConnect used? |
||
| 123 | */ |
||
| 124 | protected $bevConnect = false; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @var boolean Should we can onReadEv() in next onWriteEv()? |
||
| 128 | */ |
||
| 129 | protected $wRead = false; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @var boolean Freed? |
||
| 133 | */ |
||
| 134 | protected $freed = false; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @var object Context |
||
| 138 | */ |
||
| 139 | protected $ctx; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @var object Context name |
||
| 143 | */ |
||
| 144 | protected $ctxname; |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @var integer Defines context-related flag |
||
| 148 | */ |
||
| 149 | protected $ctxMode; |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @var boolean SSL? |
||
| 153 | */ |
||
| 154 | protected $ssl = false; |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @var float Read timeout |
||
| 158 | */ |
||
| 159 | protected $timeoutRead; |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @var float Write timeout |
||
| 163 | */ |
||
| 164 | protected $timeoutWrite; |
||
| 165 | |||
| 166 | /** |
||
| 167 | * IOStream constructor |
||
| 168 | * @param resource $fd File descriptor. Optional |
||
|
|
|||
| 169 | * @param object $pool Pool. Optional |
||
| 170 | */ |
||
| 171 | public function __construct($fd = null, $pool = null) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Getter |
||
| 205 | * @param string $name Name |
||
| 206 | * @return mixed |
||
| 207 | */ |
||
| 208 | public function __get($name) |
||
| 219 | |||
| 220 | |||
| 221 | /** |
||
| 222 | * Freed? |
||
| 223 | * @return boolean |
||
| 224 | */ |
||
| 225 | public function isFreed() |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Finished? |
||
| 232 | * @return boolean |
||
| 233 | */ |
||
| 234 | public function isFinished() |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Get EventBufferEvent |
||
| 241 | * @return EventBufferEvent |
||
| 242 | */ |
||
| 243 | public function getBev() |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Get file descriptor |
||
| 250 | * @return resource File descriptor |
||
| 251 | */ |
||
| 252 | public function getFd() |
||
| 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) |
||
| 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) |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Set timeout |
||
| 359 | * @param integer $rw Timeout |
||
| 360 | * @return void |
||
| 361 | */ |
||
| 362 | public function setTimeout($rw) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Set timeouts |
||
| 369 | * @param integer $read Read timeout in seconds |
||
| 370 | * @param integer $write Write timeout in seconds |
||
| 371 | * @return void |
||
| 372 | */ |
||
| 373 | public function setTimeouts($read, $write) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Sets priority |
||
| 384 | * @param integer $p Priority |
||
| 385 | * @return void |
||
| 386 | */ |
||
| 387 | public function setPriority($p) |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Sets watermark |
||
| 395 | * @param integer|null $low Low |
||
| 396 | * @param integer|null $high High |
||
| 397 | * @return void |
||
| 398 | */ |
||
| 399 | public function setWatermark($low = null, $high = null) |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Constructor |
||
| 415 | * @return void |
||
| 416 | */ |
||
| 417 | protected function init() |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Reads line from buffer |
||
| 423 | * @param integer $eol EOLS_* |
||
| 424 | * @return string|null |
||
| 425 | */ |
||
| 426 | public function readLine($eol = null) |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Drains buffer |
||
| 436 | * @param integer $n Numbers of bytes to drain |
||
| 437 | * @return boolean Success |
||
| 438 | */ |
||
| 439 | public function drain($n) |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Drains buffer it matches the string |
||
| 446 | * @param string $str Data |
||
| 447 | * @return boolean|null Success |
||
| 448 | */ |
||
| 449 | public function drainIfMatch($str) |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Reads exact $n bytes of buffer without draining |
||
| 477 | * @param integer $n Number of bytes to read |
||
| 478 | * @param integer $o Offset |
||
| 479 | * @return string|false |
||
| 480 | */ |
||
| 481 | public function lookExact($n, $o = 0) |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Prepends data to input buffer |
||
| 494 | * @param string $str Data |
||
| 495 | * @return boolean Success |
||
| 496 | */ |
||
| 497 | public function prependInput($str) |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Prepends data to output buffer |
||
| 507 | * @param string $str Data |
||
| 508 | * @return boolean Success |
||
| 509 | */ |
||
| 510 | public function prependOutput($str) |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Read from buffer without draining |
||
| 520 | * @param integer $n Number of bytes to read |
||
| 521 | * @param integer $o Offset |
||
| 522 | * @return string|false |
||
| 523 | */ |
||
| 524 | View Code Duplication | public function look($n, $o = 0) |
|
| 534 | |||
| 535 | /** |
||
| 536 | * Read from buffer without draining |
||
| 537 | * @param integer $o Offset |
||
| 538 | * @param integer $n Number of bytes to read |
||
| 539 | * @return string|false |
||
| 540 | */ |
||
| 541 | public function substr($o, $n = -1) |
||
| 548 | |||
| 549 | /** |
||
| 550 | * Searches first occurence of the string in input buffer |
||
| 551 | * @param string $what Needle |
||
| 552 | * @param integer $start Offset start |
||
| 553 | * @param integer $end Offset end |
||
| 554 | * @return integer Position |
||
| 555 | */ |
||
| 556 | public function search($what, $start = 0, $end = -1) |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Reads exact $n bytes from buffer |
||
| 563 | * @param integer $n Number of bytes to read |
||
| 564 | * @return string|false |
||
| 565 | */ |
||
| 566 | View Code Duplication | public function readExact($n) |
|
| 576 | |||
| 577 | /** |
||
| 578 | * Returns length of input buffer |
||
| 579 | * @return integer |
||
| 580 | */ |
||
| 581 | public function getInputLength() |
||
| 585 | |||
| 586 | /** |
||
| 587 | * Called when the worker is going to shutdown |
||
| 588 | * @return boolean Ready to shutdown? |
||
| 589 | */ |
||
| 590 | public function gracefulShutdown() |
||
| 595 | |||
| 596 | /** |
||
| 597 | * Freeze input |
||
| 598 | * @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 |
||
| 599 | * @return boolean Success |
||
| 600 | */ |
||
| 601 | public function freezeInput($at_front = true) |
||
| 608 | |||
| 609 | /** |
||
| 610 | * Unfreeze input |
||
| 611 | * @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 |
||
| 612 | * @return boolean Success |
||
| 613 | */ |
||
| 614 | public function unfreezeInput($at_front = true) |
||
| 621 | |||
| 622 | /** |
||
| 623 | * Freeze output |
||
| 624 | * @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 |
||
| 625 | * @return boolean Success |
||
| 626 | */ |
||
| 627 | public function freezeOutput($at_front = true) |
||
| 634 | |||
| 635 | /** |
||
| 636 | * Unfreeze output |
||
| 637 | * @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 |
||
| 638 | * @return boolean Success |
||
| 639 | */ |
||
| 640 | public function unfreezeOutput($at_front = true) |
||
| 647 | |||
| 648 | /** |
||
| 649 | * Called when the connection is ready to accept new data |
||
| 650 | * @return void |
||
| 651 | */ |
||
| 652 | public function onWrite() |
||
| 655 | |||
| 656 | /** |
||
| 657 | * Send data to the connection. Note that it just writes to buffer that flushes at every baseloop |
||
| 658 | * @param string $data Data to send |
||
| 659 | * @return boolean Success |
||
| 660 | */ |
||
| 661 | View Code Duplication | public function write($data) |
|
| 681 | |||
| 682 | /** |
||
| 683 | * Send data and appending \n to connection. Note that it just writes to buffer flushed at every baseloop |
||
| 684 | * @param string $data Data to send |
||
| 685 | * @return boolean Success |
||
| 686 | */ |
||
| 687 | View Code Duplication | public function writeln($data) |
|
| 704 | |||
| 705 | /** |
||
| 706 | * Finish the session. You should not worry about buffers, they are going to be flushed properly |
||
| 707 | * @return void |
||
| 708 | */ |
||
| 709 | public function finish() |
||
| 721 | |||
| 722 | /** |
||
| 723 | * Called when the session finished |
||
| 724 | * @return void |
||
| 725 | */ |
||
| 726 | protected function onFinish() |
||
| 729 | |||
| 730 | /** |
||
| 731 | * Close the connection |
||
| 732 | * @return void |
||
| 733 | */ |
||
| 734 | public function close() |
||
| 748 | |||
| 749 | /** |
||
| 750 | * Unsets pointers of associated EventBufferEvent and File descriptr |
||
| 751 | * @return void |
||
| 752 | */ |
||
| 753 | public function unsetFd() |
||
| 758 | |||
| 759 | /** |
||
| 760 | * Send message to log |
||
| 761 | * @param string $m Message |
||
| 762 | * @return void |
||
| 763 | */ |
||
| 764 | protected function log($m) |
||
| 768 | |||
| 769 | /** |
||
| 770 | * Called when the connection has got new data |
||
| 771 | * @param object $bev EventBufferEvent |
||
| 772 | * @return void |
||
| 773 | */ |
||
| 774 | 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) |
||
| 865 | |||
| 866 | /** |
||
| 867 | * Called when the connection state changed |
||
| 868 | * @param object $bev EventBufferEvent |
||
| 869 | * @param integer $events Events |
||
| 870 | * @return void |
||
| 871 | */ |
||
| 872 | public function onStateEv($bev, $events) |
||
| 903 | |||
| 904 | /** |
||
| 905 | * Moves arbitrary number of bytes from input buffer to given buffer |
||
| 906 | * @param \EventBuffer $dest Destination nuffer |
||
| 907 | * @param integer $n Max. number of bytes to move |
||
| 908 | * @return integer|false |
||
| 909 | */ |
||
| 910 | View Code Duplication | public function moveToBuffer(\EventBuffer $dest, $n) |
|
| 917 | |||
| 918 | /** |
||
| 919 | * Moves arbitrary number of bytes from given buffer to output buffer |
||
| 920 | * @param \EventBuffer $src Source buffer |
||
| 921 | * @param integer $n Max. number of bytes to move |
||
| 922 | * @return integer|false |
||
| 923 | */ |
||
| 924 | View Code Duplication | public function writeFromBuffer(\EventBuffer $src, $n) |
|
| 932 | |||
| 933 | /** |
||
| 934 | * Read data from the connection's buffer |
||
| 935 | * @param integer $n Max. number of bytes to read |
||
| 936 | * @return string|false Readed data |
||
| 937 | */ |
||
| 938 | public function read($n) |
||
| 952 | |||
| 953 | /** |
||
| 954 | * Reads all data from the connection's buffer |
||
| 955 | * @return string Readed data |
||
| 956 | */ |
||
| 957 | public function readUnlimited() |
||
| 968 | } |
||
| 969 |
This check looks for
@paramannotations 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.