| Conditions | 11 |
| Paths | 12 |
| Total Lines | 67 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 26 | protected function decode(string $data) |
||
| 27 | { |
||
| 28 | if (empty($data)) { |
||
| 29 | return NULL; // close has been sent |
||
| 30 | } |
||
| 31 | |||
| 32 | $unmaskedPayload = ''; |
||
| 33 | $decodedData = []; |
||
| 34 | |||
| 35 | // estimate frame type: |
||
| 36 | $firstByteBinary = sprintf('%08b', ord($data[0])); |
||
| 37 | $secondByteBinary = sprintf('%08b', ord($data[1])); |
||
| 38 | $isMasked = $secondByteBinary[0] === '1'; |
||
| 39 | $payloadLength = ord($data[1]) & self::MASK_127; |
||
| 40 | |||
| 41 | // unmasked frame is received: |
||
| 42 | if (!$isMasked) { |
||
| 43 | return ['type' => '', 'payload' => '', 'error' => WebSocketServerContract::ERR_PROTOCOL]; |
||
| 44 | } |
||
| 45 | |||
| 46 | $this->getTypeByOpCode($firstByteBinary, $decodedData); |
||
| 47 | if (empty($decodedData['type'])) { |
||
| 48 | return ['type' => '', 'payload' => '', 'error' => WebSocketServerContract::ERR_UNKNOWN_OPCODE]; |
||
| 49 | } |
||
| 50 | |||
| 51 | $mask = substr($data, 2, 4); |
||
| 52 | $payloadOffset = WebSocketServerContract::PAYLOAD_OFFSET_6; |
||
| 53 | $dataLength = $payloadLength + $payloadOffset; |
||
| 54 | if ($payloadLength === self::MASK_126) { |
||
| 55 | $mask = substr($data, 4, 4); |
||
| 56 | $payloadOffset = WebSocketServerContract::PAYLOAD_OFFSET_8; |
||
| 57 | $dataLength = bindec(sprintf('%08b', ord($data[2])) . sprintf('%08b', ord($data[3]))) + $payloadOffset; |
||
| 58 | } elseif ($payloadLength === self::MASK_127) { |
||
| 59 | $mask = substr($data, 10, 4); |
||
| 60 | $payloadOffset = WebSocketServerContract::PAYLOAD_OFFSET_14; |
||
| 61 | $tmp = ''; |
||
| 62 | for ($i = 0; $i < 8; $i++) { |
||
| 63 | $tmp .= sprintf('%08b', ord($data[$i + 2])); |
||
| 64 | } |
||
| 65 | $dataLength = bindec($tmp) + $payloadOffset; |
||
| 66 | unset($tmp); |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * We have to check for large frames here. socket_recv cuts at 1024 bytes |
||
| 71 | * so if websocket-frame is > 1024 bytes we have to wait until whole |
||
| 72 | * data is transferd. |
||
| 73 | */ |
||
| 74 | if (strlen($data) < $dataLength) { |
||
| 75 | return false; |
||
| 76 | } |
||
| 77 | |||
| 78 | if ($isMasked) { |
||
| 79 | for ($i = $payloadOffset; $i < $dataLength; $i++) { |
||
| 80 | $j = $i - $payloadOffset; |
||
| 81 | if (isset($data[$i])) { |
||
| 82 | $unmaskedPayload .= $data[$i] ^ $mask[$j % 4]; |
||
| 83 | } |
||
| 84 | } |
||
| 85 | $decodedData['payload'] = $unmaskedPayload; |
||
| 86 | } else { |
||
| 87 | $payloadOffset -= 4; |
||
| 88 | $decodedData['payload'] = substr($data, $payloadOffset); |
||
| 89 | } |
||
| 90 | |||
| 91 | return $decodedData; |
||
| 92 | } |
||
| 93 | |||
| 146 |