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 | use \PHPDaemon\Traits\ClassWatchdog; |
||
| 16 | use \PHPDaemon\Traits\StaticObjectWatchdog; |
||
| 17 | use \PHPDaemon\Traits\EventHandlers; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var object Associated pool |
||
| 21 | */ |
||
| 22 | public $pool; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var string EOL |
||
| 26 | */ |
||
| 27 | protected $EOL = "\n"; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var integer EOLS_* switch |
||
| 31 | */ |
||
| 32 | protected $EOLS; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var object EventBufferEvent |
||
| 36 | */ |
||
| 37 | protected $bev; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var resource File descriptor |
||
| 41 | */ |
||
| 42 | protected $fd; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var boolean Finished? |
||
| 46 | */ |
||
| 47 | protected $finished = false; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var boolean Ready? |
||
| 51 | */ |
||
| 52 | protected $ready = false; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var boolean Writing? |
||
| 56 | */ |
||
| 57 | protected $writing = true; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var boolean Timeout? |
||
| 61 | */ |
||
| 62 | protected $timedout = false; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var integer Default low mark. Minimum number of bytes in buffer |
||
| 66 | */ |
||
| 67 | protected $lowMark = 1; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var integer Default high mark. Maximum number of bytes in buffer |
||
| 71 | */ |
||
| 72 | protected $highMark = 0xFFFF; // initial value of the maximum amout of bytes in buffer |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var integer Priority |
||
| 76 | */ |
||
| 77 | protected $priority; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var boolean Initialized? |
||
| 81 | */ |
||
| 82 | protected $inited = false; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var integer Current state |
||
| 86 | */ |
||
| 87 | protected $state = 0; // stream state of the connection (application protocol level) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Alias of STATE_STANDBY |
||
| 91 | */ |
||
| 92 | const STATE_ROOT = 0; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Standby state (default state) |
||
| 96 | */ |
||
| 97 | const STATE_STANDBY = 0; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var object Stack of callbacks called when writing is done |
||
| 101 | */ |
||
| 102 | protected $onWriteOnce; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @var integer Timeout |
||
| 106 | */ |
||
| 107 | protected $timeout = null; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @var string URL |
||
| 111 | */ |
||
| 112 | protected $url; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @var boolean Alive? |
||
| 116 | */ |
||
| 117 | protected $alive = false; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @var boolean Is bevConnect used? |
||
| 121 | */ |
||
| 122 | protected $bevConnect = false; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @var boolean Should we can onReadEv() in next onWriteEv()? |
||
| 126 | */ |
||
| 127 | protected $wRead = false; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @var boolean Freed? |
||
| 131 | */ |
||
| 132 | protected $freed = false; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @var object Context |
||
| 136 | */ |
||
| 137 | protected $ctx; |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @var object Context name |
||
| 141 | */ |
||
| 142 | protected $ctxname; |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @var integer Defines context-related flag |
||
| 146 | */ |
||
| 147 | protected $ctxMode; |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @var boolean SSL? |
||
| 151 | */ |
||
| 152 | protected $ssl = false; |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @var float Read timeout |
||
| 156 | */ |
||
| 157 | protected $timeoutRead; |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @var float Write timeout |
||
| 161 | */ |
||
| 162 | protected $timeoutWrite; |
||
| 163 | |||
| 164 | /** |
||
| 165 | * IOStream constructor |
||
| 166 | * @param resource $fd File descriptor. Optional |
||
|
|
|||
| 167 | * @param object $pool Pool. Optional |
||
| 168 | */ |
||
| 169 | 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) { |
||
| 216 | |||
| 217 | |||
| 218 | /** |
||
| 219 | * Freed? |
||
| 220 | * @return boolean |
||
| 221 | */ |
||
| 222 | public function isFreed() { |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Finished? |
||
| 228 | * @return boolean |
||
| 229 | */ |
||
| 230 | public function isFinished() { |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Get EventBufferEvent |
||
| 236 | * @return EventBufferEvent |
||
| 237 | */ |
||
| 238 | public function getBev() { |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Get file descriptor |
||
| 244 | * @return resource File descriptor |
||
| 245 | */ |
||
| 246 | public function getFd() { |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Sets context mode |
||
| 252 | * @param object $ctx Context |
||
| 253 | * @param integer $mode Mode |
||
| 254 | * @return void |
||
| 255 | */ |
||
| 256 | |||
| 257 | public function setContext($ctx, $mode) { |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Sets fd |
||
| 264 | * @param resource $fd File descriptor |
||
| 265 | * @param object $bev EventBufferEvent |
||
| 266 | * @return void |
||
| 267 | */ |
||
| 268 | public function setFd($fd, $bev = null) { |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Set timeout |
||
| 336 | * @param integer $rw Timeout |
||
| 337 | * @return void |
||
| 338 | */ |
||
| 339 | public function setTimeout($rw) { |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Set timeouts |
||
| 345 | * @param integer $read Read timeout in seconds |
||
| 346 | * @param integer $write Write timeout in seconds |
||
| 347 | * @return void |
||
| 348 | */ |
||
| 349 | public function setTimeouts($read, $write) { |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Sets priority |
||
| 359 | * @param integer $p Priority |
||
| 360 | * @return void |
||
| 361 | */ |
||
| 362 | public function setPriority($p) { |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Sets watermark |
||
| 369 | * @param integer|null $low Low |
||
| 370 | * @param integer|null $high High |
||
| 371 | * @return void |
||
| 372 | */ |
||
| 373 | public function setWatermark($low = null, $high = null) { |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Called when the session constructed |
||
| 388 | * @return void |
||
| 389 | */ |
||
| 390 | protected function init() { |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Reads line from buffer |
||
| 395 | * @param integer $eol EOLS_* |
||
| 396 | * @return string|null |
||
| 397 | */ |
||
| 398 | public function readLine($eol = null) { |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Drains buffer |
||
| 407 | * @param integer $n Numbers of bytes to drain |
||
| 408 | * @return boolean Success |
||
| 409 | */ |
||
| 410 | public function drain($n) { |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Drains buffer it matches the string |
||
| 416 | * @param string $str Data |
||
| 417 | * @return boolean|null Success |
||
| 418 | */ |
||
| 419 | public function drainIfMatch($str) { |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Reads exact $n bytes of buffer without draining |
||
| 447 | * @param integer $n Number of bytes to read |
||
| 448 | * @param integer $o Offset |
||
| 449 | * @return string|false |
||
| 450 | */ |
||
| 451 | View Code Duplication | public function lookExact($n, $o = 0) { |
|
| 460 | |||
| 461 | /** |
||
| 462 | * Prepends data to input buffer |
||
| 463 | * @param string $str Data |
||
| 464 | * @return boolean Success |
||
| 465 | */ |
||
| 466 | public function prependInput($str) { |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Prepends data to output buffer |
||
| 475 | * @param string $str Data |
||
| 476 | * @return boolean Success |
||
| 477 | */ |
||
| 478 | public function prependOutput($str) { |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Read from buffer without draining |
||
| 487 | * @param integer $n Number of bytes to read |
||
| 488 | * @param integer $o Offset |
||
| 489 | * @return string|false |
||
| 490 | */ |
||
| 491 | View Code Duplication | public function look($n, $o = 0) { |
|
| 500 | |||
| 501 | /** |
||
| 502 | * Read from buffer without draining |
||
| 503 | * @param integer $o Offset |
||
| 504 | * @param integer $n Number of bytes to read |
||
| 505 | * @return string|false |
||
| 506 | */ |
||
| 507 | public function substr($o, $n = -1) { |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Searches first occurence of the string in input buffer |
||
| 516 | * @param string $what Needle |
||
| 517 | * @param integer $start Offset start |
||
| 518 | * @param integer $end Offset end |
||
| 519 | * @return integer Position |
||
| 520 | */ |
||
| 521 | public function search($what, $start = 0, $end = -1) { |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Reads exact $n bytes from buffer |
||
| 527 | * @param integer $n Number of bytes to read |
||
| 528 | * @return string|false |
||
| 529 | */ |
||
| 530 | public function readExact($n) { |
||
| 539 | |||
| 540 | /** |
||
| 541 | * Returns length of input buffer |
||
| 542 | * @return integer |
||
| 543 | */ |
||
| 544 | public function getInputLength() { |
||
| 547 | |||
| 548 | /** |
||
| 549 | * Called when the worker is going to shutdown |
||
| 550 | * @return boolean Ready to shutdown? |
||
| 551 | */ |
||
| 552 | public function gracefulShutdown() { |
||
| 556 | |||
| 557 | /** |
||
| 558 | * Freeze input |
||
| 559 | * @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 |
||
| 560 | * @return boolean Success |
||
| 561 | */ |
||
| 562 | public function freezeInput($at_front = true) { |
||
| 568 | |||
| 569 | /** |
||
| 570 | * Unfreeze input |
||
| 571 | * @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 |
||
| 572 | * @return boolean Success |
||
| 573 | */ |
||
| 574 | public function unfreezeInput($at_front = true) { |
||
| 580 | |||
| 581 | /** |
||
| 582 | * Freeze output |
||
| 583 | * @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 |
||
| 584 | * @return boolean Success |
||
| 585 | */ |
||
| 586 | public function freezeOutput($at_front = true) { |
||
| 592 | |||
| 593 | /** |
||
| 594 | * Unfreeze output |
||
| 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 unfreezeOutput($at_front = true) { |
||
| 604 | |||
| 605 | /** |
||
| 606 | * Called when the connection is ready to accept new data |
||
| 607 | * @return void |
||
| 608 | */ |
||
| 609 | public function onWrite() { |
||
| 611 | |||
| 612 | /** |
||
| 613 | * Send data to the connection. Note that it just writes to buffer that flushes at every baseloop |
||
| 614 | * @param string $data Data to send |
||
| 615 | * @return boolean Success |
||
| 616 | */ |
||
| 617 | View Code Duplication | public function write($data) { |
|
| 636 | |||
| 637 | /** |
||
| 638 | * Send data and appending \n to connection. Note that it just writes to buffer flushed at every baseloop |
||
| 639 | * @param string $data Data to send |
||
| 640 | * @return boolean Success |
||
| 641 | */ |
||
| 642 | View Code Duplication | public function writeln($data) { |
|
| 658 | |||
| 659 | /** |
||
| 660 | * Finish the session. You should not worry about buffers, they are going to be flushed properly |
||
| 661 | * @return void |
||
| 662 | */ |
||
| 663 | public function finish() { |
||
| 674 | |||
| 675 | /** |
||
| 676 | * Called when the session finished |
||
| 677 | * @return void |
||
| 678 | */ |
||
| 679 | protected function onFinish() { |
||
| 681 | |||
| 682 | /** |
||
| 683 | * Close the connection |
||
| 684 | * @return void |
||
| 685 | */ |
||
| 686 | public function close() { |
||
| 699 | |||
| 700 | /** |
||
| 701 | * Unsets pointers of associated EventBufferEvent and File descriptr |
||
| 702 | * @return void |
||
| 703 | */ |
||
| 704 | public function unsetFd() { |
||
| 708 | |||
| 709 | /** |
||
| 710 | * Send message to log |
||
| 711 | * @param string $m Message |
||
| 712 | * @return void |
||
| 713 | */ |
||
| 714 | protected function log($m) { |
||
| 717 | |||
| 718 | /** |
||
| 719 | * Called when the connection has got new data |
||
| 720 | * @param object $bev EventBufferEvent |
||
| 721 | * @return void |
||
| 722 | */ |
||
| 723 | public function onReadEv($bev) { |
||
| 740 | |||
| 741 | /** |
||
| 742 | * Called when new data received |
||
| 743 | * @return void |
||
| 744 | */ |
||
| 745 | protected function onRead() { |
||
| 747 | |||
| 748 | /** |
||
| 749 | * Called when the stream is handshaked (at low-level), and peer is ready to recv. data |
||
| 750 | * @return void |
||
| 751 | */ |
||
| 752 | protected function onReady() { |
||
| 754 | |||
| 755 | /** |
||
| 756 | * Push callback which will be called only once, when writing is available next time |
||
| 757 | * @param callable $cb Callback |
||
| 758 | * @return void |
||
| 759 | */ |
||
| 760 | public function onWriteOnce($cb) { |
||
| 767 | |||
| 768 | /** |
||
| 769 | * Called when the connection is ready to accept new data |
||
| 770 | * @param object $bev EventBufferEvent |
||
| 771 | * @return void |
||
| 772 | */ |
||
| 773 | public function onWriteEv($bev) { |
||
| 816 | |||
| 817 | /** |
||
| 818 | * Called when the connection state changed |
||
| 819 | * @param object $bev EventBufferEvent |
||
| 820 | * @param integer $events Events |
||
| 821 | * @return void |
||
| 822 | */ |
||
| 823 | public function onStateEv($bev, $events) { |
||
| 855 | |||
| 856 | /** |
||
| 857 | * Moves arbitrary number of bytes from input buffer to given buffer |
||
| 858 | * @param \EventBuffer $dest Destination nuffer |
||
| 859 | * @param integer $n Max. number of bytes to move |
||
| 860 | * @return integer|false |
||
| 861 | */ |
||
| 862 | public function moveToBuffer(\EventBuffer $dest, $n) { |
||
| 868 | |||
| 869 | /** |
||
| 870 | * Moves arbitrary number of bytes from given buffer to output buffer |
||
| 871 | * @param \EventBuffer $src Source buffer |
||
| 872 | * @param integer $n Max. number of bytes to move |
||
| 873 | * @return integer|false |
||
| 874 | */ |
||
| 875 | public function writeFromBuffer(\EventBuffer $src, $n) { |
||
| 882 | |||
| 883 | /** |
||
| 884 | * Read data from the connection's buffer |
||
| 885 | * @param integer $n Max. number of bytes to read |
||
| 886 | * @return string|false Readed data |
||
| 887 | */ |
||
| 888 | public function read($n) { |
||
| 901 | |||
| 902 | /** |
||
| 903 | * Reads all data from the connection's buffer |
||
| 904 | * @return string Readed data |
||
| 905 | */ |
||
| 906 | View Code Duplication | public function readUnlimited() { |
|
| 916 | } |
||
| 917 |
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.