Complex classes like FrameHandler 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 FrameHandler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class FrameHandler { |
||
| 15 | |||
| 16 | /** |
||
| 17 | * The message buffer for data frames. |
||
| 18 | * |
||
| 19 | * @var string |
||
| 20 | */ |
||
| 21 | protected $buffer = ''; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var WebSocketClient |
||
| 25 | */ |
||
| 26 | protected $client; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Resume opCode for the `CONTINUATION` handler. |
||
| 30 | * |
||
| 31 | * @var int|null |
||
| 32 | */ |
||
| 33 | protected $continue; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Max outgoing fragment size. |
||
| 37 | * |
||
| 38 | * Each browser has its own standard, so this is generalized. |
||
| 39 | * |
||
| 40 | * Defaults to 128 KiB. |
||
| 41 | * |
||
| 42 | * @var int |
||
| 43 | */ |
||
| 44 | protected $fragmentSize = 128 * 1024; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Maximum inbound message length (complete payload). |
||
| 48 | * |
||
| 49 | * Defaults to 10 MiB. |
||
| 50 | * |
||
| 51 | * @var int |
||
| 52 | */ |
||
| 53 | protected $maxLength = 10 * 1024 * 1024; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var FrameReader |
||
| 57 | */ |
||
| 58 | protected $reader; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Whether binary I/O should bypass buffers. |
||
| 62 | * |
||
| 63 | * @var bool |
||
| 64 | */ |
||
| 65 | protected $stream = false; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @param WebSocketClient $client |
||
| 69 | */ |
||
| 70 | public function __construct (WebSocketClient $client) { |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @return int |
||
| 77 | */ |
||
| 78 | public function getFragmentSize (): int { |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @return int |
||
| 84 | */ |
||
| 85 | public function getMaxLength (): int { |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @return bool |
||
| 91 | */ |
||
| 92 | public function isStream (): bool { |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Progressively receives `BINARY` data into the buffer until the payload is complete. |
||
| 98 | * Passes the complete payload up to {@link WebSocketClient::onBinary()} |
||
| 99 | * |
||
| 100 | * When {@link $stream} is `true`, this bypasses the buffer. |
||
| 101 | * |
||
| 102 | * @param Frame $frame |
||
| 103 | * @throws WebSocketError |
||
| 104 | */ |
||
| 105 | protected function onBinary (Frame $frame): void { |
||
| 119 | |||
| 120 | /** |
||
| 121 | * When a `CLOSE` frame is received. Calls {@link WebSocketClient::onClose()} |
||
| 122 | * |
||
| 123 | * https://tools.ietf.org/html/rfc6455#section-5.5.1 |
||
| 124 | * > If an endpoint receives a Close frame and did not previously send a |
||
| 125 | * > Close frame, the endpoint MUST send a Close frame in response. (When |
||
| 126 | * > sending a Close frame in response, the endpoint typically echos the |
||
| 127 | * > status code it received.) |
||
| 128 | * |
||
| 129 | * @param Frame $frame |
||
| 130 | */ |
||
| 131 | protected function onClose (Frame $frame): void { |
||
| 134 | |||
| 135 | /** |
||
| 136 | * When a `CONTINUATION` frame (data fragment) is received. |
||
| 137 | * |
||
| 138 | * @param Frame $frame |
||
| 139 | * @throws WebSocketError |
||
| 140 | */ |
||
| 141 | protected function onContinuation (Frame $frame): void { |
||
| 163 | |||
| 164 | /** |
||
| 165 | * When a control frame is received. |
||
| 166 | * |
||
| 167 | * https://tools.ietf.org/html/rfc6455#section-5.4 |
||
| 168 | * > Control frames (see Section 5.5) MAY be injected in the middle of |
||
| 169 | * > a fragmented message. |
||
| 170 | * |
||
| 171 | * @param Frame $frame |
||
| 172 | */ |
||
| 173 | protected function onControl (Frame $frame): void { |
||
| 187 | |||
| 188 | /** |
||
| 189 | * When an initial data frame (not `CONTINUATION`) is received. |
||
| 190 | * |
||
| 191 | * @param Frame $frame |
||
| 192 | */ |
||
| 193 | protected function onData (Frame $frame): void { |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Validates the message length, but only when the buffer is in use. |
||
| 225 | * |
||
| 226 | * @param Frame $frame |
||
| 227 | * @throws WebSocketError |
||
| 228 | */ |
||
| 229 | protected function onData_CheckLength (Frame $frame): void { |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Called by {@link WebSocketClient} when a complete frame has been received. |
||
| 241 | * |
||
| 242 | * Delegates to the other handler methods using the program logic outlined in the RFC. |
||
| 243 | * |
||
| 244 | * Eventually calls back to the {@link WebSocketClient} when payloads are complete. |
||
| 245 | * |
||
| 246 | * @param Frame $frame |
||
| 247 | */ |
||
| 248 | public function onFrame (Frame $frame): void { |
||
| 256 | |||
| 257 | /** |
||
| 258 | * When a `PING` is received. Calls {@link WebSocketClient::onPing()} |
||
| 259 | * |
||
| 260 | * @param Frame $frame |
||
| 261 | */ |
||
| 262 | protected function onPing (Frame $frame): void { |
||
| 265 | |||
| 266 | /** |
||
| 267 | * When a `PONG` is received. Calls {@link WebSocketClient::onPong()} |
||
| 268 | * |
||
| 269 | * @param Frame $frame |
||
| 270 | */ |
||
| 271 | protected function onPong (Frame $frame): void { |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Uses {@link FrameReader} to read frames and passes them off to {@link onFrame()} |
||
| 277 | */ |
||
| 278 | public function onReadable (): void { |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Progressively receives `TEXT` data until the payload is complete. |
||
| 286 | * Validates the complete payload as UTF-8 and passes it up to {@link WebSocketClient::onText()} |
||
| 287 | * |
||
| 288 | * @param Frame $frame |
||
| 289 | * @throws WebSocketError |
||
| 290 | */ |
||
| 291 | protected function onText (Frame $frame): void { |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @param int $bytes |
||
| 306 | * @return $this |
||
| 307 | */ |
||
| 308 | public function setFragmentSize (int $bytes) { |
||
| 312 | |||
| 313 | /** |
||
| 314 | * @param int $bytes |
||
| 315 | * @return $this |
||
| 316 | */ |
||
| 317 | public function setMaxLength (int $bytes) { |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @param bool $stream |
||
| 324 | * @return $this |
||
| 325 | */ |
||
| 326 | public function setStream (bool $stream) { |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Sends a complete message to the peer, fragmenting if needed. |
||
| 333 | * |
||
| 334 | * @param int $opCode |
||
| 335 | * @param string $payload |
||
| 336 | */ |
||
| 337 | public function write (int $opCode, string $payload): void { |
||
| 349 | |||
| 350 | /** |
||
| 351 | * @param string $payload |
||
| 352 | */ |
||
| 353 | public function writeBinary (string $payload): void { |
||
| 356 | |||
| 357 | /** |
||
| 358 | * @param int $code |
||
| 359 | * @param string $reason |
||
| 360 | */ |
||
| 361 | public function writeClose (int $code = Frame::CLOSE_NORMAL, string $reason = ''): void { |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Writes a single frame. |
||
| 367 | * |
||
| 368 | * @param bool $final |
||
| 369 | * @param int $opCode |
||
| 370 | * @param string $payload |
||
| 371 | */ |
||
| 372 | protected function writeFrame (bool $final, int $opCode, string $payload): void { |
||
| 391 | |||
| 392 | /** |
||
| 393 | * @param string $payload |
||
| 394 | */ |
||
| 395 | public function writePing (string $payload = ''): void { |
||
| 398 | |||
| 399 | /** |
||
| 400 | * @param string $payload |
||
| 401 | */ |
||
| 402 | public function writePong (string $payload = ''): void { |
||
| 405 | |||
| 406 | /** |
||
| 407 | * @param string $payload |
||
| 408 | */ |
||
| 409 | public function writeText (string $payload): void { |
||
| 412 | } |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: