Conditions | 7 |
Paths | 73 |
Total Lines | 67 |
Code Lines | 46 |
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 |
||
71 | public function connect(string $socketUrl, ?WebSocketConfig $config = null): void |
||
72 | { |
||
73 | try { |
||
74 | $this->config = $config ?? new WebSocketConfig(); |
||
|
|||
75 | $this->socketUrl = $socketUrl; |
||
76 | $urlParts = parse_url($this->socketUrl); |
||
77 | |||
78 | $this->config->setScheme($urlParts['scheme']); |
||
79 | $this->config->setHost($urlParts['host']); |
||
80 | $this->config->setUser($urlParts); |
||
81 | $this->config->setPassword($urlParts); |
||
82 | $this->config->setPort($urlParts); |
||
83 | |||
84 | $pathWithQuery = $this->getPathWithQuery($urlParts); |
||
85 | $hostUri = $this->getHostUri($this->config); |
||
86 | |||
87 | $context = $this->getStreamContext(); |
||
88 | if ($this->config->hasProxy()) { |
||
89 | $this->socket = $this->proxy(); |
||
90 | } else { |
||
91 | $this->socket = @stream_socket_client( |
||
92 | $hostUri . ':' . $this->config->getPort(), |
||
93 | $errno, |
||
94 | $errstr, |
||
95 | $this->config->getTimeout(), |
||
96 | STREAM_CLIENT_CONNECT, |
||
97 | $context |
||
98 | ); |
||
99 | } |
||
100 | |||
101 | if ($this->socket === false) { |
||
102 | throw new ConnectionException( |
||
103 | "Could not open socket to \"{$this->config->getHost()}:{$this->config->getPort()}\": $errstr ($errno).", |
||
104 | CommonsContract::CLIENT_COULD_NOT_OPEN_SOCKET |
||
105 | ); |
||
106 | } |
||
107 | |||
108 | stream_set_timeout($this->socket, $this->config->getTimeout()); |
||
109 | |||
110 | $key = $this->generateKey(); |
||
111 | $headers = array_merge($this->defaultHeaders, [ |
||
112 | 'Host' => $this->config->getHost() . ':' . $this->config->getPort(), |
||
113 | 'User-Agent' => 'Easy-Http/' . self::VERSION . ' (PHP/' . PHP_VERSION . ')', |
||
114 | 'Sec-WebSocket-Key' => $key, |
||
115 | ]); |
||
116 | |||
117 | if ($this->config->getUser() || $this->config->getPassword()) { |
||
118 | $headers['authorization'] = 'Basic ' . base64_encode($this->config->getUser() . ':' . $this->config->getPassword()) . "\r\n"; |
||
119 | } |
||
120 | |||
121 | if (!empty($this->config->getHeaders())) { |
||
122 | $headers = array_merge($headers, $this->config->getHeaders()); |
||
123 | } |
||
124 | |||
125 | $header = $this->getHeaders($pathWithQuery, $headers); |
||
126 | |||
127 | $this->write($header); |
||
128 | |||
129 | $this->validateResponse($this->config, $pathWithQuery, $key); |
||
130 | $this->isConnected = true; |
||
131 | $this->whileIsConnected(); |
||
132 | |||
133 | } catch (\Exception $e) { |
||
134 | $this->safeCall($this->onError, $this, new WebSocketException( |
||
135 | $e->getMessage(), |
||
136 | $e->getCode(), |
||
137 | $e->getPrevious() |
||
138 | )); |
||
268 | } |