| Conditions | 10 |
| Paths | 15 |
| Total Lines | 61 |
| Code Lines | 36 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 30 | protected function decode(string $data) |
||
| 31 | { |
||
| 32 | if (empty($data)) { |
||
| 33 | return null; // close has been sent |
||
| 34 | } |
||
| 35 | |||
| 36 | $unmaskedPayload = ''; |
||
| 37 | $decodedData = []; |
||
| 38 | |||
| 39 | // estimate frame type: |
||
| 40 | $firstByteBinary = sprintf('%08b', ord($data[0])); |
||
| 41 | $secondByteBinary = sprintf('%08b', ord($data[1])); |
||
| 42 | $isMasked = $secondByteBinary[0] === '1'; |
||
| 43 | $payloadLength = ord($data[1]) & self::MASK_127; |
||
| 44 | |||
| 45 | // unmasked frame is received: |
||
| 46 | if (!$isMasked) { |
||
| 47 | return ['type' => '', 'payload' => '', 'error' => WebSocketServerContract::ERR_PROTOCOL]; |
||
| 48 | } |
||
| 49 | |||
| 50 | $this->getTypeByOpCode($firstByteBinary, $decodedData); |
||
| 51 | if (empty($decodedData['type'])) { |
||
| 52 | return ['type' => '', 'payload' => '', 'error' => WebSocketServerContract::ERR_UNKNOWN_OPCODE]; |
||
| 53 | } |
||
| 54 | |||
| 55 | $mask = substr($data, 2, 4); |
||
| 56 | $payloadOffset = WebSocketServerContract::PAYLOAD_OFFSET_6; |
||
| 57 | $dataLength = $payloadLength + $payloadOffset; |
||
| 58 | if ($payloadLength === self::MASK_126) { |
||
| 59 | $mask = substr($data, 4, 4); |
||
| 60 | $payloadOffset = WebSocketServerContract::PAYLOAD_OFFSET_8; |
||
| 61 | $dataLength = bindec(sprintf('%08b', ord($data[2])) . sprintf('%08b', ord($data[3]))) + $payloadOffset; |
||
| 62 | } elseif ($payloadLength === self::MASK_127) { |
||
| 63 | $mask = substr($data, 10, 4); |
||
| 64 | $payloadOffset = WebSocketServerContract::PAYLOAD_OFFSET_14; |
||
| 65 | $tmp = ''; |
||
| 66 | for ($i = 0; $i < 8; $i++) { |
||
| 67 | $tmp .= sprintf('%08b', ord($data[$i + 2])); |
||
| 68 | } |
||
| 69 | $dataLength = bindec($tmp) + $payloadOffset; |
||
| 70 | unset($tmp); |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * We have to check for large frames here. socket_recv cuts at 1024 bytes |
||
| 75 | * so if websocket-frame is > 1024 bytes we have to wait until whole |
||
| 76 | * data is transferd. |
||
| 77 | */ |
||
| 78 | if (strlen($data) < $dataLength) { |
||
| 79 | return false; |
||
| 80 | } |
||
| 81 | |||
| 82 | for ($i = $payloadOffset; $i < $dataLength; $i++) { |
||
| 83 | $j = $i - $payloadOffset; |
||
| 84 | if (isset($data[$i])) { |
||
| 85 | $unmaskedPayload .= $data[$i] ^ $mask[$j % 4]; |
||
| 86 | } |
||
| 87 | } |
||
| 88 | $decodedData['payload'] = $unmaskedPayload; |
||
| 89 | |||
| 90 | return $decodedData; |
||
| 91 | } |
||
| 175 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.