| Total Complexity | 40 |
| Total Lines | 268 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like WSClientTrait 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.
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 WSClientTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | trait WSClientTrait |
||
| 20 | { |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Validates whether server sent valid upgrade response |
||
| 24 | * |
||
| 25 | * @param WebSocketConfig $config |
||
| 26 | * @param string $pathWithQuery |
||
| 27 | * @param string $key |
||
| 28 | * @throws ConnectionException |
||
| 29 | */ |
||
| 30 | private function validateResponse(WebSocketConfig $config, string $pathWithQuery, string $key): void |
||
| 31 | { |
||
| 32 | $response = stream_get_line($this->socket, self::DEFAULT_RESPONSE_HEADER, "\r\n\r\n"); |
||
|
|
|||
| 33 | if (!preg_match(self::SEC_WEBSOCKET_ACCEPT_PTTRN, $response, $matches)) { |
||
| 34 | $address = $config->getScheme() . '://' . $config->getHost() . ':' . $config->getPort() . $pathWithQuery; |
||
| 35 | throw new ConnectionException( |
||
| 36 | "Connection to '{$address}' failed: Server sent invalid upgrade response:\n" |
||
| 37 | . $response, CommonsContract::CLIENT_INVALID_UPGRADE_RESPONSE |
||
| 38 | ); |
||
| 39 | } |
||
| 40 | |||
| 41 | $keyAccept = trim($matches[1]); |
||
| 42 | $expectedResponse = base64_encode(pack('H*', sha1($key . self::SERVER_KEY_ACCEPT))); |
||
| 43 | if ($keyAccept !== $expectedResponse) { |
||
| 44 | throw new ConnectionException( |
||
| 45 | 'Server sent bad upgrade response.', |
||
| 46 | CommonsContract::CLIENT_INVALID_UPGRADE_RESPONSE |
||
| 47 | ); |
||
| 48 | } |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Gets host uri based on protocol |
||
| 53 | * |
||
| 54 | * @param WebSocketConfig $config |
||
| 55 | * @return string |
||
| 56 | * @throws BadUriException |
||
| 57 | */ |
||
| 58 | private function getHostUri(WebSocketConfig $config): string |
||
| 59 | { |
||
| 60 | if (in_array($config->getScheme(), ['ws', 'wss'], true) === false) { |
||
| 61 | throw new BadUriException( |
||
| 62 | "Url should have scheme ws or wss, not '{$config->getScheme()}' from URI '$this->socketUrl' .", |
||
| 63 | CommonsContract::CLIENT_INCORRECT_SCHEME |
||
| 64 | ); |
||
| 65 | } |
||
| 66 | |||
| 67 | return ($config->getScheme() === 'wss' ? 'ssl' : 'tcp') . '://' . $config->getHost(); |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @param string $data |
||
| 72 | * @return float|int |
||
| 73 | * @throws ConnectionException |
||
| 74 | */ |
||
| 75 | private function getPayloadLength(string $data): float|int |
||
| 76 | { |
||
| 77 | $payloadLength = (int)ord($data[1]) & self::MASK_127; // Bits 1-7 in byte 1 |
||
| 78 | if ($payloadLength > self::MASK_125) { |
||
| 79 | if ($payloadLength === self::MASK_126) { |
||
| 80 | $data = $this->read(2); // 126: Payload is a 16-bit unsigned int |
||
| 81 | } else { |
||
| 82 | $data = $this->read(8); // 127: Payload is a 64-bit unsigned int |
||
| 83 | } |
||
| 84 | $payloadLength = bindec(Toolkit::sprintB($data)); |
||
| 85 | } |
||
| 86 | |||
| 87 | return $payloadLength; |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @param string $data |
||
| 92 | * @param int $payloadLength |
||
| 93 | * @return string |
||
| 94 | * @throws ConnectionException |
||
| 95 | */ |
||
| 96 | private function getPayloadData(string $data, int $payloadLength): string |
||
| 97 | { |
||
| 98 | // Masking? |
||
| 99 | $mask = (bool)(ord($data[1]) >> 7); // Bit 0 in byte 1 |
||
| 100 | $payload = ''; |
||
| 101 | $maskingKey = ''; |
||
| 102 | |||
| 103 | // Get masking key. |
||
| 104 | if ($mask) { |
||
| 105 | $maskingKey = $this->read(4); |
||
| 106 | } |
||
| 107 | |||
| 108 | // Get the actual payload, if any (might not be for e.g. close frames. |
||
| 109 | if ($payloadLength > 0) { |
||
| 110 | $data = $this->read($payloadLength); |
||
| 111 | |||
| 112 | if ($mask) { |
||
| 113 | // Unmask payload. |
||
| 114 | for ($i = 0; $i < $payloadLength; $i++) { |
||
| 115 | $payload .= ($data[$i] ^ $maskingKey[$i % 4]); |
||
| 116 | } |
||
| 117 | } else { |
||
| 118 | $payload = $data; |
||
| 119 | } |
||
| 120 | } |
||
| 121 | |||
| 122 | return $payload; |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @return string|null |
||
| 127 | * @throws \Exception |
||
| 128 | */ |
||
| 129 | protected function receiveFragment(): string|null |
||
| 130 | { |
||
| 131 | $data = $this->read(2); |
||
| 132 | if (is_string($data) === false) { |
||
| 133 | return null; |
||
| 134 | } |
||
| 135 | |||
| 136 | $final = (bool)(ord($data[0]) & 1 << 7); |
||
| 137 | |||
| 138 | $opcodeInt = ord($data[0]) & 31; |
||
| 139 | $opcodeInts = array_flip(self::$opcodes); |
||
| 140 | if (!array_key_exists($opcodeInt, $opcodeInts)) { |
||
| 141 | throw new ConnectionException( |
||
| 142 | "Bad opcode in websocket frame: $opcodeInt", |
||
| 143 | CommonsContract::CLIENT_BAD_OPCODE |
||
| 144 | ); |
||
| 145 | } |
||
| 146 | |||
| 147 | $opcode = $opcodeInts[$opcodeInt]; |
||
| 148 | |||
| 149 | if ($opcode !== 'continuation') { |
||
| 150 | $this->lastOpcode = $opcode; |
||
| 151 | } |
||
| 152 | |||
| 153 | $payloadLength = $this->getPayloadLength($data); |
||
| 154 | $payload = $this->getPayloadData($data, $payloadLength); |
||
| 155 | |||
| 156 | if ($opcode === CommonsContract::EVENT_TYPE_CLOSE) { |
||
| 157 | if ($payloadLength >= 2) { |
||
| 158 | $statusBin = $payload[0] . $payload[1]; |
||
| 159 | $status = bindec(sprintf('%08b%08b', ord($payload[0]), ord($payload[1]))); |
||
| 160 | $this->closeStatus = $status; |
||
| 161 | $payload = substr($payload, 2); |
||
| 162 | |||
| 163 | if (!$this->isClosing) { |
||
| 164 | $this->send($statusBin . 'Close acknowledged: ' . $status, |
||
| 165 | CommonsContract::EVENT_TYPE_CLOSE); // Respond. |
||
| 166 | } |
||
| 167 | } |
||
| 168 | |||
| 169 | if ($this->isClosing) { |
||
| 170 | $this->isClosing = false; // A close response, all done. |
||
| 171 | } |
||
| 172 | |||
| 173 | fclose($this->socket); |
||
| 174 | $this->isConnected = false; |
||
| 175 | } |
||
| 176 | |||
| 177 | if (!$final) { |
||
| 178 | $this->hugePayload .= $payload; |
||
| 179 | |||
| 180 | return null; |
||
| 181 | } |
||
| 182 | |||
| 183 | if ($this->hugePayload) { |
||
| 184 | $payload = $this->hugePayload .= $payload; |
||
| 185 | $this->hugePayload = null; |
||
| 186 | } |
||
| 187 | |||
| 188 | return $payload; |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @param $final |
||
| 193 | * @param $payload |
||
| 194 | * @param $opcode |
||
| 195 | * @param $masked |
||
| 196 | * @throws \Exception |
||
| 197 | */ |
||
| 198 | protected function sendFragment($final, $payload, $opcode, $masked): void |
||
| 199 | { |
||
| 200 | // Binary string for header. |
||
| 201 | $frameHeadBin = ''; |
||
| 202 | // Write FIN, final fragment bit. |
||
| 203 | $frameHeadBin .= (bool)$final ? '1' : '0'; |
||
| 204 | // RSV 1, 2, & 3 false and unused. |
||
| 205 | $frameHeadBin .= '000'; |
||
| 206 | // Opcode rest of the byte. |
||
| 207 | $frameHeadBin .= sprintf('%04b', self::$opcodes[$opcode]); |
||
| 208 | // Use masking? |
||
| 209 | $frameHeadBin .= $masked ? '1' : '0'; |
||
| 210 | |||
| 211 | // 7 bits of payload length... |
||
| 212 | $payloadLen = strlen($payload); |
||
| 213 | if ($payloadLen > self::MAX_BYTES_READ) { |
||
| 214 | $frameHeadBin .= decbin(self::MASK_127); |
||
| 215 | $frameHeadBin .= sprintf('%064b', $payloadLen); |
||
| 216 | } else if ($payloadLen > self::MASK_125) { |
||
| 217 | $frameHeadBin .= decbin(self::MASK_126); |
||
| 218 | $frameHeadBin .= sprintf('%016b', $payloadLen); |
||
| 219 | } else { |
||
| 220 | $frameHeadBin .= sprintf('%07b', $payloadLen); |
||
| 221 | } |
||
| 222 | |||
| 223 | $frame = ''; |
||
| 224 | |||
| 225 | // Write frame head to frame. |
||
| 226 | foreach (str_split($frameHeadBin, 8) as $binstr) { |
||
| 227 | $frame .= chr(bindec($binstr)); |
||
| 228 | } |
||
| 229 | // Handle masking |
||
| 230 | if ($masked) { |
||
| 231 | // generate a random mask: |
||
| 232 | $mask = ''; |
||
| 233 | for ($i = 0; $i < 4; $i++) { |
||
| 234 | $mask .= chr(random_int(0, 255)); |
||
| 235 | } |
||
| 236 | $frame .= $mask; |
||
| 237 | } |
||
| 238 | |||
| 239 | // Append payload to frame: |
||
| 240 | for ($i = 0; $i < $payloadLen; $i++) { |
||
| 241 | $frame .= ($masked === true) ? $payload[$i] ^ $mask[$i % 4] : $payload[$i]; |
||
| 242 | } |
||
| 243 | |||
| 244 | $this->write($frame); |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Sec-WebSocket-Key generator |
||
| 249 | * |
||
| 250 | * @return string the 16 character length key |
||
| 251 | * @throws \Exception |
||
| 252 | */ |
||
| 253 | private function generateKey(): string |
||
| 254 | { |
||
| 255 | $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"$&/()=[]{}0123456789'; |
||
| 256 | $key = ''; |
||
| 257 | $chLen = strlen($chars); |
||
| 258 | for ($i = 0; $i < self::KEY_GEN_LENGTH; $i++) { |
||
| 259 | $key .= $chars[random_int(0, $chLen - 1)]; |
||
| 260 | } |
||
| 261 | |||
| 262 | return base64_encode($key); |
||
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @param int $len |
||
| 267 | * @return string|null |
||
| 268 | * @throws ConnectionException |
||
| 269 | */ |
||
| 270 | protected function read(int $len): string|null |
||
| 271 | { |
||
| 272 | if ($this->socket && $this->isConnected()) { |
||
| 273 | return Middleware::stream_read($this->socket, $len); |
||
| 274 | } |
||
| 275 | |||
| 276 | return null; |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @param string $data |
||
| 281 | * @return void |
||
| 282 | * @throws ConnectionException |
||
| 283 | */ |
||
| 284 | protected function write(string $data): void |
||
| 287 | } |
||
| 288 | |||
| 289 | } |