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 integer Default low mark. Minimum number of bytes in buffer |
||
61 | */ |
||
62 | protected $lowMark = 1; |
||
63 | |||
64 | /** |
||
65 | * @var integer Default high mark. Maximum number of bytes in buffer |
||
66 | */ |
||
67 | protected $highMark = 0xFFFF; // initial value of the maximum amout of bytes in buffer |
||
68 | |||
69 | /** |
||
70 | * @var integer Priority |
||
71 | */ |
||
72 | protected $priority; |
||
73 | |||
74 | /** |
||
75 | * @var boolean Initialized? |
||
76 | */ |
||
77 | protected $inited = false; |
||
78 | |||
79 | /** |
||
80 | * @var integer Current state |
||
81 | */ |
||
82 | protected $state = 0; // stream state of the connection (application protocol level) |
||
83 | |||
84 | /** |
||
85 | * Alias of STATE_STANDBY |
||
86 | */ |
||
87 | const STATE_ROOT = 0; |
||
88 | |||
89 | /** |
||
90 | * Standby state (default state) |
||
91 | */ |
||
92 | const STATE_STANDBY = 0; |
||
93 | |||
94 | /** |
||
95 | * @var object Stack of callbacks called when writing is done |
||
96 | */ |
||
97 | protected $onWriteOnce; |
||
98 | |||
99 | /** |
||
100 | * @var integer Timeout |
||
101 | */ |
||
102 | protected $timeout = null; |
||
103 | |||
104 | /** |
||
105 | * @var string URL |
||
106 | */ |
||
107 | protected $url; |
||
108 | |||
109 | /** |
||
110 | * @var boolean Alive? |
||
111 | */ |
||
112 | protected $alive = false; |
||
113 | |||
114 | /** |
||
115 | * @var boolean Is bevConnect used? |
||
116 | */ |
||
117 | protected $bevConnect = false; |
||
118 | |||
119 | /** |
||
120 | * @var boolean Should we can onReadEv() in next onWriteEv()? |
||
121 | */ |
||
122 | protected $wRead = false; |
||
123 | |||
124 | /** |
||
125 | * @var boolean Freed? |
||
126 | */ |
||
127 | protected $freed = false; |
||
128 | |||
129 | /** |
||
130 | * @var object Context |
||
131 | */ |
||
132 | protected $ctx; |
||
133 | |||
134 | /** |
||
135 | * @var object Context name |
||
136 | */ |
||
137 | protected $ctxname; |
||
138 | |||
139 | /** |
||
140 | * @var integer Defines context-related flag |
||
141 | */ |
||
142 | protected $ctxMode; |
||
143 | |||
144 | /** |
||
145 | * @var boolean SSL? |
||
146 | */ |
||
147 | protected $ssl = false; |
||
148 | |||
149 | /** |
||
150 | * @var float Read timeout |
||
151 | */ |
||
152 | protected $timeoutRead; |
||
153 | |||
154 | /** |
||
155 | * @var float Write timeout |
||
156 | */ |
||
157 | protected $timeoutWrite; |
||
158 | |||
159 | /** |
||
160 | * IOStream constructor |
||
161 | * @param resource $fd File descriptor. Optional |
||
|
|||
162 | * @param object $pool Pool. Optional |
||
163 | */ |
||
164 | public function __construct($fd = null, $pool = null) { |
||
165 | if ($pool) { |
||
166 | $this->pool = $pool; |
||
167 | $this->pool->attach($this); |
||
168 | if (isset($this->pool->config->timeout->value)) { |
||
169 | $this->timeout = $this->pool->config->timeout->value; |
||
170 | } |
||
171 | if (isset($this->pool->config->timeoutread->value)) { |
||
172 | $this->timeoutRead = $this->pool->config->timeoutread->value; |
||
173 | } |
||
174 | if (isset($this->pool->config->timeoutwrite->value)) { |
||
175 | $this->timeoutWrite = $this->pool->config->timeoutwrite->value; |
||
176 | } |
||
177 | } |
||
178 | |||
179 | if ($fd !== null) { |
||
180 | $this->setFd($fd); |
||
181 | } |
||
182 | |||
183 | if ($this->EOL === "\n") { |
||
184 | $this->EOLS = \EventBuffer::EOL_LF; |
||
185 | } |
||
186 | elseif ($this->EOL === "\r\n") { |
||
187 | $this->EOLS = \EventBuffer::EOL_CRLF; |
||
188 | } |
||
189 | else { |
||
190 | $this->EOLS = \EventBuffer::EOL_ANY; |
||
191 | } |
||
192 | |||
193 | $this->onWriteOnce = new StackCallbacks; |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * Getter |
||
198 | * @param string $name Name |
||
199 | * @return mixed |
||
200 | */ |
||
201 | public function __get($name) { |
||
202 | if ( $name === 'finished' |
||
203 | || $name === 'alive' |
||
204 | || $name === 'freed' |
||
205 | || $name === 'url' |
||
206 | ) { |
||
207 | return $this->{$name}; |
||
208 | } |
||
209 | return NULL; |
||
210 | } |
||
211 | |||
212 | |||
213 | /** |
||
214 | * Freed? |
||
215 | * @return boolean |
||
216 | */ |
||
217 | public function isFreed() { |
||
220 | |||
221 | /** |
||
222 | * Finished? |
||
223 | * @return boolean |
||
224 | */ |
||
225 | public function isFinished() { |
||
228 | |||
229 | /** |
||
230 | * Get EventBufferEvent |
||
231 | * @return EventBufferEvent |
||
232 | */ |
||
233 | public function getBev() { |
||
236 | |||
237 | /** |
||
238 | * Get file descriptor |
||
239 | * @return resource File descriptor |
||
240 | */ |
||
241 | public function getFd() { |
||
244 | |||
245 | /** |
||
246 | * Sets context mode |
||
247 | * @param object $ctx Context |
||
248 | * @param integer $mode Mode |
||
249 | * @return void |
||
250 | */ |
||
251 | |||
252 | public function setContext($ctx, $mode) { |
||
256 | |||
257 | /** |
||
258 | * Sets fd |
||
259 | * @param resource $fd File descriptor |
||
260 | * @param object $bev EventBufferEvent |
||
261 | * @return void |
||
262 | */ |
||
263 | public function setFd($fd, $bev = null) { |
||
264 | $this->fd = $fd; |
||
265 | if ($this->fd === false) { |
||
266 | $this->finish(); |
||
267 | return; |
||
268 | } |
||
269 | if ($bev !== null) { |
||
270 | $this->bev = $bev; |
||
271 | $this->bev->setCallbacks([$this, 'onReadEv'], [$this, 'onWriteEv'], [$this, 'onStateEv']); |
||
272 | if (!$this->bev) { |
||
273 | return; |
||
274 | } |
||
275 | $this->ready = true; |
||
276 | $this->alive = true; |
||
277 | } |
||
278 | else { |
||
279 | $flags = !is_resource($this->fd) ? \EventBufferEvent::OPT_CLOSE_ON_FREE : 0; |
||
280 | $flags |= \EventBufferEvent::OPT_DEFER_CALLBACKS; /* buggy option */ |
||
281 | if ($this->ctx) { |
||
282 | if ($this->ctx instanceof \EventSslContext) { |
||
283 | $this->bev = \EventBufferEvent::sslSocket(Daemon::$process->eventBase, $this->fd, $this->ctx, $this->ctxMode, $flags); |
||
284 | if ($this->bev) { |
||
285 | $this->bev->setCallbacks([$this, 'onReadEv'], [$this, 'onWriteEv'], [$this, 'onStateEv']); |
||
286 | } |
||
287 | $this->ssl = true; |
||
288 | } |
||
289 | else { |
||
290 | $this->log('unsupported type of context: ' . ($this->ctx ? get_class($this->ctx) : 'undefined')); |
||
291 | return; |
||
292 | } |
||
293 | } |
||
294 | else { |
||
295 | $this->bev = new \EventBufferEvent(Daemon::$process->eventBase, $this->fd, $flags, [$this, 'onReadEv'], [$this, 'onWriteEv'], [$this, 'onStateEv']); |
||
296 | } |
||
297 | if (!$this->bev) { |
||
298 | return; |
||
299 | } |
||
300 | } |
||
301 | if ($this->priority !== null) { |
||
302 | $this->bev->priority = $this->priority; |
||
303 | } |
||
304 | $this->setTimeouts($this->timeoutRead !== null ? $this->timeoutRead : $this->timeout, |
||
305 | $this->timeoutWrite!== null ? $this->timeoutWrite : $this->timeout); |
||
306 | if ($this->bevConnect && ($this->fd === null)) { |
||
307 | //$this->bev->connectHost(Daemon::$process->dnsBase, $this->hostReal, $this->port); |
||
308 | $this->bev->connect($this->addr); |
||
309 | } |
||
310 | if (!$this->bev) { |
||
311 | $this->finish(); |
||
312 | return; |
||
313 | } |
||
314 | View Code Duplication | if (!$this->bev->enable(\Event::READ | \Event::WRITE | \Event::TIMEOUT | \Event::PERSIST)) { |
|
315 | $this->finish(); |
||
316 | return; |
||
317 | } |
||
318 | $this->bev->setWatermark(\Event::READ, $this->lowMark, $this->highMark); |
||
319 | init: |
||
320 | if ($this->keepalive) { |
||
321 | $this->setKeepalive(true); |
||
322 | } |
||
323 | if (!$this->inited) { |
||
324 | $this->inited = true; |
||
325 | $this->init(); |
||
326 | } |
||
327 | } |
||
328 | |||
329 | /** |
||
330 | * Set timeout |
||
331 | * @param integer $rw Timeout |
||
332 | * @return void |
||
333 | */ |
||
334 | public function setTimeout($rw) { |
||
337 | |||
338 | /** |
||
339 | * Set timeouts |
||
340 | * @param integer $read Read timeout in seconds |
||
341 | * @param integer $write Write timeout in seconds |
||
342 | * @return void |
||
343 | */ |
||
344 | public function setTimeouts($read, $write) { |
||
345 | $this->timeoutRead = $read; |
||
346 | $this->timeoutWrite = $write; |
||
347 | if ($this->bev) { |
||
348 | $this->bev->setTimeouts($this->timeoutRead, $this->timeoutWrite); |
||
349 | } |
||
350 | } |
||
351 | |||
352 | /** |
||
353 | * Sets priority |
||
354 | * @param integer $p Priority |
||
355 | * @return void |
||
356 | */ |
||
357 | public function setPriority($p) { |
||
361 | |||
362 | /** |
||
363 | * Sets watermark |
||
364 | * @param integer|null $low Low |
||
365 | * @param integer|null $high High |
||
366 | * @return void |
||
367 | */ |
||
368 | public function setWatermark($low = null, $high = null) { |
||
369 | if ($low !== null) { |
||
370 | $this->lowMark = $low; |
||
371 | } |
||
372 | if ($high !== null) { |
||
373 | $this->highMark = $high; |
||
374 | } |
||
375 | if ($this->highMark !== null && $this->lowMark > $this->highMark) { |
||
376 | $this->highMark = $this->lowMark; |
||
377 | } |
||
378 | $this->bev->setWatermark(\Event::READ, $this->lowMark, $this->highMark); |
||
379 | } |
||
380 | |||
381 | /** |
||
382 | * Called when the session constructed |
||
383 | * @return void |
||
384 | */ |
||
385 | protected function init() { |
||
387 | |||
388 | /** |
||
389 | * Reads line from buffer |
||
390 | * @param integer $eol EOLS_* |
||
391 | * @return string|null |
||
392 | */ |
||
393 | public function readLine($eol = null) { |
||
399 | |||
400 | /** |
||
401 | * Drains buffer |
||
402 | * @param integer $n Numbers of bytes to drain |
||
403 | * @return boolean Success |
||
404 | */ |
||
405 | public function drain($n) { |
||
408 | |||
409 | /** |
||
410 | * Drains buffer it matches the string |
||
411 | * @param string $str Data |
||
412 | * @return boolean|null Success |
||
413 | */ |
||
414 | public function drainIfMatch($str) { |
||
439 | |||
440 | /** |
||
441 | * Reads exact $n bytes of buffer without draining |
||
442 | * @param integer $n Number of bytes to read |
||
443 | * @param integer $o Offset |
||
444 | * @return string|false |
||
445 | */ |
||
446 | View Code Duplication | public function lookExact($n, $o = 0) { |
|
455 | |||
456 | /** |
||
457 | * Prepends data to input buffer |
||
458 | * @param string $str Data |
||
459 | * @return boolean Success |
||
460 | */ |
||
461 | public function prependInput($str) { |
||
467 | |||
468 | /** |
||
469 | * Prepends data to output buffer |
||
470 | * @param string $str Data |
||
471 | * @return boolean Success |
||
472 | */ |
||
473 | public function prependOutput($str) { |
||
479 | |||
480 | /** |
||
481 | * Read from buffer without draining |
||
482 | * @param integer $n Number of bytes to read |
||
483 | * @param integer $o Offset |
||
484 | * @return string|false |
||
485 | */ |
||
486 | View Code Duplication | public function look($n, $o = 0) { |
|
495 | |||
496 | /** |
||
497 | * Read from buffer without draining |
||
498 | * @param integer $o Offset |
||
499 | * @param integer $n Number of bytes to read |
||
500 | * @return string|false |
||
501 | */ |
||
502 | public function substr($o, $n = -1) { |
||
508 | |||
509 | /** |
||
510 | * Searches first occurence of the string in input buffer |
||
511 | * @param string $what Needle |
||
512 | * @param integer $start Offset start |
||
513 | * @param integer $end Offset end |
||
514 | * @return integer Position |
||
515 | */ |
||
516 | public function search($what, $start = 0, $end = -1) { |
||
519 | |||
520 | /** |
||
521 | * Reads exact $n bytes from buffer |
||
522 | * @param integer $n Number of bytes to read |
||
523 | * @return string|false |
||
524 | */ |
||
525 | public function readExact($n) { |
||
534 | |||
535 | /** |
||
536 | * Returns length of input buffer |
||
537 | * @return integer |
||
538 | */ |
||
539 | public function getInputLength() { |
||
542 | |||
543 | /** |
||
544 | * Called when the worker is going to shutdown |
||
545 | * @return boolean Ready to shutdown? |
||
546 | */ |
||
547 | public function gracefulShutdown() { |
||
551 | |||
552 | /** |
||
553 | * Freeze input |
||
554 | * @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 |
||
555 | * @return boolean Success |
||
556 | */ |
||
557 | public function freezeInput($at_front = true) { |
||
563 | |||
564 | /** |
||
565 | * Unfreeze input |
||
566 | * @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 |
||
567 | * @return boolean Success |
||
568 | */ |
||
569 | public function unfreezeInput($at_front = true) { |
||
575 | |||
576 | /** |
||
577 | * Freeze output |
||
578 | * @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 |
||
579 | * @return boolean Success |
||
580 | */ |
||
581 | public function freezeOutput($at_front = true) { |
||
587 | |||
588 | /** |
||
589 | * Unfreeze output |
||
590 | * @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 |
||
591 | * @return boolean Success |
||
592 | */ |
||
593 | public function unfreezeOutput($at_front = true) { |
||
599 | |||
600 | /** |
||
601 | * Called when the connection is ready to accept new data |
||
602 | * @return void |
||
603 | */ |
||
604 | public function onWrite() { |
||
606 | |||
607 | /** |
||
608 | * Send data to the connection. Note that it just writes to buffer that flushes at every baseloop |
||
609 | * @param string $data Data to send |
||
610 | * @return boolean Success |
||
611 | */ |
||
612 | View Code Duplication | public function write($data) { |
|
631 | |||
632 | /** |
||
633 | * Send data and appending \n to connection. Note that it just writes to buffer flushed at every baseloop |
||
634 | * @param string $data Data to send |
||
635 | * @return boolean Success |
||
636 | */ |
||
637 | View Code Duplication | public function writeln($data) { |
|
653 | |||
654 | /** |
||
655 | * Finish the session. You should not worry about buffers, they are going to be flushed properly |
||
656 | * @return void |
||
657 | */ |
||
658 | public function finish() { |
||
669 | |||
670 | /** |
||
671 | * Called when the session finished |
||
672 | * @return void |
||
673 | */ |
||
674 | protected function onFinish() { |
||
676 | |||
677 | /** |
||
678 | * Close the connection |
||
679 | * @return void |
||
680 | */ |
||
681 | public function close() { |
||
694 | |||
695 | /** |
||
696 | * Unsets pointers of associated EventBufferEvent and File descriptr |
||
697 | * @return void |
||
698 | */ |
||
699 | public function unsetFd() { |
||
703 | |||
704 | /** |
||
705 | * Send message to log |
||
706 | * @param string $m Message |
||
707 | * @return void |
||
708 | */ |
||
709 | protected function log($m) { |
||
712 | |||
713 | /** |
||
714 | * Called when the connection has got new data |
||
715 | * @param object $bev EventBufferEvent |
||
716 | * @return void |
||
717 | */ |
||
718 | public function onReadEv($bev) { |
||
735 | |||
736 | /** |
||
737 | * Called when new data received |
||
738 | * @return void |
||
739 | */ |
||
740 | protected function onRead() { |
||
742 | |||
743 | /** |
||
744 | * Called when the stream is handshaked (at low-level), and peer is ready to recv. data |
||
745 | * @return void |
||
746 | */ |
||
747 | protected function onReady() { |
||
749 | |||
750 | /** |
||
751 | * Push callback which will be called only once, when writing is available next time |
||
752 | * @param callable $cb Callback |
||
753 | * @return void |
||
754 | */ |
||
755 | public function onWriteOnce($cb) { |
||
762 | |||
763 | /** |
||
764 | * Called when the connection is ready to accept new data |
||
765 | * @param object $bev EventBufferEvent |
||
766 | * @return void |
||
767 | */ |
||
768 | public function onWriteEv($bev) { |
||
811 | |||
812 | /** |
||
813 | * Called when the connection state changed |
||
814 | * @param object $bev EventBufferEvent |
||
815 | * @param integer $events Events |
||
816 | * @return void |
||
817 | */ |
||
818 | public function onStateEv($bev, $events) { |
||
846 | |||
847 | /** |
||
848 | * Moves arbitrary number of bytes from input buffer to given buffer |
||
849 | * @param \EventBuffer $dest Destination nuffer |
||
850 | * @param integer $n Max. number of bytes to move |
||
851 | * @return integer|false |
||
852 | */ |
||
853 | public function moveToBuffer(\EventBuffer $dest, $n) { |
||
859 | |||
860 | /** |
||
861 | * Moves arbitrary number of bytes from given buffer to output buffer |
||
862 | * @param \EventBuffer $src Source buffer |
||
863 | * @param integer $n Max. number of bytes to move |
||
864 | * @return integer|false |
||
865 | */ |
||
866 | public function writeFromBuffer(\EventBuffer $src, $n) { |
||
873 | |||
874 | /** |
||
875 | * Read data from the connection's buffer |
||
876 | * @param integer $n Max. number of bytes to read |
||
877 | * @return string|false Readed data |
||
878 | */ |
||
879 | public function read($n) { |
||
892 | |||
893 | /** |
||
894 | * Reads all data from the connection's buffer |
||
895 | * @return string Readed data |
||
896 | */ |
||
897 | View Code Duplication | public function readUnlimited() { |
|
907 | } |
||
908 |
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.