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