Complex classes like Input 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 Input, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class Input extends \EventBuffer |
||
| 13 | { |
||
| 14 | use \PHPDaemon\Traits\ClassWatchdog; |
||
| 15 | use \PHPDaemon\Traits\StaticObjectWatchdog; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * State: seek nearest boundary |
||
| 19 | */ |
||
| 20 | const STATE_SEEKBOUNDARY = 0; |
||
| 21 | /** |
||
| 22 | * State: headers |
||
| 23 | */ |
||
| 24 | const STATE_HEADERS = 1; |
||
| 25 | /** |
||
| 26 | * State: body |
||
| 27 | */ |
||
| 28 | const STATE_BODY = 2; |
||
| 29 | /** |
||
| 30 | * State: upload |
||
| 31 | */ |
||
| 32 | const STATE_UPLOAD = 3; |
||
| 33 | /** |
||
| 34 | * @var array Current Part |
||
| 35 | */ |
||
| 36 | public $curPart; |
||
| 37 | /** |
||
| 38 | * @var string Boundary |
||
| 39 | */ |
||
| 40 | protected $boundary; |
||
| 41 | /** |
||
| 42 | * @var integer Maximum file size from multi-part query |
||
| 43 | */ |
||
| 44 | protected $maxFileSize = 0; |
||
| 45 | /** |
||
| 46 | * @var integer Readed |
||
| 47 | */ |
||
| 48 | protected $readed = 0; |
||
| 49 | /** |
||
| 50 | * @var boolean Frozen |
||
| 51 | */ |
||
| 52 | protected $frozen = false; |
||
| 53 | /** |
||
| 54 | * @var boolean EOF |
||
| 55 | */ |
||
| 56 | protected $EOF = false; |
||
| 57 | /** |
||
| 58 | * @var array Content dispostion of current Part |
||
| 59 | */ |
||
| 60 | protected $curPartDisp = false; |
||
| 61 | /** |
||
| 62 | * @var Generic Related Request |
||
| 63 | */ |
||
| 64 | protected $req; |
||
| 65 | /** |
||
| 66 | * @var integer (self::STATE_*) State of multi-part processor |
||
| 67 | */ |
||
| 68 | protected $state = self::STATE_SEEKBOUNDARY; |
||
| 69 | /** |
||
| 70 | * @var integer Size of current upload chunk |
||
| 71 | */ |
||
| 72 | protected $curChunkSize; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Set boundary |
||
| 76 | * @param string $boundary Boundary |
||
| 77 | * @return void |
||
| 78 | */ |
||
| 79 | public function setBoundary($boundary) |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Freeze input |
||
| 86 | * @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 |
||
|
|
|||
| 87 | * @return void |
||
| 88 | */ |
||
| 89 | public function freeze($at_front = false) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Unfreeze input |
||
| 97 | * @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 |
||
| 98 | * @return void |
||
| 99 | */ |
||
| 100 | public function unfreeze($at_front = false) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * onRead |
||
| 114 | * @return void |
||
| 115 | */ |
||
| 116 | protected function onRead() |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Send EOF |
||
| 128 | * @return void |
||
| 129 | */ |
||
| 130 | public function sendEOF() |
||
| 137 | |||
| 138 | /** |
||
| 139 | * onEOF |
||
| 140 | * @return void |
||
| 141 | */ |
||
| 142 | protected function onEOF() |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Is frozen? |
||
| 171 | * @return boolean |
||
| 172 | */ |
||
| 173 | public function isFrozen() |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Is EOF? |
||
| 180 | * @return boolean |
||
| 181 | */ |
||
| 182 | public function isEof() |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Set request |
||
| 189 | * @param Generic $req Request |
||
| 190 | * @return void |
||
| 191 | */ |
||
| 192 | public function setRequest(Generic $req) |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Moves $n bytes from input buffer to arbitrary buffer |
||
| 199 | * @param \EventBuffer $buf Source nuffer |
||
| 200 | * @return integer |
||
| 201 | */ |
||
| 202 | public function readFromBuffer(\EventBuffer $buf) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Append string to input buffer |
||
| 223 | * @param string $chunk Piece of request input |
||
| 224 | * @param boolean $final Final call is THIS SEQUENCE of calls (not mandatory final in request)? |
||
| 225 | * @return void |
||
| 226 | */ |
||
| 227 | public function readFromString($chunk, $final = true) |
||
| 235 | |||
| 236 | |||
| 237 | /** |
||
| 238 | * Read from buffer without draining |
||
| 239 | * @param integer $n Number of bytes to read |
||
| 240 | * @param integer $o Offset |
||
| 241 | * @return string |
||
| 242 | */ |
||
| 243 | public function look($n, $o = 0) |
||
| 250 | |||
| 251 | |||
| 252 | /** |
||
| 253 | * Parses multipart |
||
| 254 | * @return void |
||
| 255 | */ |
||
| 256 | public function parseMultipart() |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Log |
||
| 398 | * @param string $msg Message |
||
| 399 | * @return void |
||
| 400 | */ |
||
| 401 | public function log($msg) |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Get current upload chunk as string |
||
| 408 | * @return string Chunk body |
||
| 409 | */ |
||
| 410 | public function getChunkString() |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Write current upload chunk to file descriptor |
||
| 422 | * @todo It is not supported yet (callback missing in EventBuffer->write()) |
||
| 423 | * @param mixed $fd File destriptor |
||
| 424 | * @param callable $cb Callback |
||
| 425 | * @return boolean Success |
||
| 426 | */ |
||
| 427 | public function writeChunkToFd($fd, $cb = null) |
||
| 437 | } |
||
| 438 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.