Completed
Push — master ( 1f28ab...083bb9 )
by y
01:17
created
src/StreamServer.php 1 patch
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -12,7 +12,7 @@  discard block
 block discarded – undo
12 12
      *
13 13
      * @return int
14 14
      */
15
-    final public static function getType (): int {
15
+    final public static function getType(): int {
16 16
         return SOCK_STREAM;
17 17
     }
18 18
 
@@ -25,7 +25,7 @@  discard block
 block discarded – undo
25 25
      * @return StreamClient
26 26
      * @throws SocketError
27 27
      */
28
-    public function accept (): StreamClient {
28
+    public function accept(): StreamClient {
29 29
         if (!$resource = @socket_accept($this->resource)) {
30 30
             throw new SocketError($this->resource); // reliable errno
31 31
         }
@@ -45,7 +45,7 @@  discard block
 block discarded – undo
45 45
      * @return $this
46 46
      * @throws SocketError
47 47
      */
48
-    public function listen (int $backlog = 0) {
48
+    public function listen(int $backlog = 0) {
49 49
         if (!@socket_listen($this->resource, $backlog)) {
50 50
             throw new SocketError($this->resource); // reliable errno
51 51
         }
@@ -58,7 +58,7 @@  discard block
 block discarded – undo
58 58
      * @param resource $resource The accepted connection.
59 59
      * @return StreamClient
60 60
      */
61
-    protected function newClient ($resource): StreamClient {
61
+    protected function newClient($resource): StreamClient {
62 62
         return new StreamClient($resource);
63 63
     }
64 64
 
Please login to merge, or discard this patch.
src/Reactor.php 2 patches
Braces   +3 added lines, -6 removed lines patch added patch discarded remove patch
@@ -84,8 +84,7 @@  discard block
 block discarded – undo
84 84
         unset($channel);
85 85
         if ($socket instanceof WebSocketClient and $error instanceof WebSocketError) {
86 86
             $socket->close($error->getCode(), $error->getMessage());
87
-        }
88
-        else {
87
+        } else {
89 88
             $socket->close();
90 89
         }
91 90
         echo $error;
@@ -109,11 +108,9 @@  discard block
 block discarded – undo
109 108
             foreach ($rwe[$channel] as $id => $socket) {
110 109
                 try {
111 110
                     $socket->{$method}();
112
-                }
113
-                catch (Throwable $error) {
111
+                } catch (Throwable $error) {
114 112
                     $this->onError($channel, $socket, $error);
115
-                }
116
-                finally {
113
+                } finally {
117 114
                     if (!$socket->isOpen()) {
118 115
                         $this->remove($socket);
119 116
                     }
Please login to merge, or discard this patch.
Spacing   +8 added lines, -8 removed lines patch added patch discarded remove patch
@@ -31,12 +31,12 @@  discard block
 block discarded – undo
31 31
      * @return int
32 32
      * @throws SocketError
33 33
      */
34
-    public static function select (array &$read, array &$write, array &$except, ?float $timeout = null): int {
34
+    public static function select(array &$read, array &$write, array &$except, ?float $timeout = null): int {
35 35
         $rwe = [$read, $write, $except];
36 36
         array_walk_recursive($rwe, function(SocketInterface &$each) {
37 37
             $each = $each->getResource();
38 38
         });
39
-        $uSec = (int)(fmod($timeout, 1) * 1000000); // ignored if timeout is null
39
+        $uSec = (int) (fmod($timeout, 1) * 1000000); // ignored if timeout is null
40 40
         $count = @socket_select($rwe[0], $rwe[1], $rwe[2], $timeout, $uSec); // keys are preserved
41 41
         if ($count === false) {
42 42
             $read = $write = $except = [];
@@ -54,7 +54,7 @@  discard block
 block discarded – undo
54 54
      * @param ReactiveInterface $socket
55 55
      * @return $this
56 56
      */
57
-    public function add (ReactiveInterface $socket) {
57
+    public function add(ReactiveInterface $socket) {
58 58
         $this->sockets[$socket->getId()] = $socket;
59 59
         return $this;
60 60
     }
@@ -64,14 +64,14 @@  discard block
 block discarded – undo
64 64
      *
65 65
      * @return int
66 66
      */
67
-    public function count (): int {
67
+    public function count(): int {
68 68
         return count($this->sockets);
69 69
     }
70 70
 
71 71
     /**
72 72
      * @return ReactiveInterface[]
73 73
      */
74
-    public function getSockets () {
74
+    public function getSockets() {
75 75
         return $this->sockets;
76 76
     }
77 77
 
@@ -80,7 +80,7 @@  discard block
 block discarded – undo
80 80
      * @param ReactiveInterface $socket
81 81
      * @param Throwable $error
82 82
      */
83
-    public function onError (int $channel, $socket, Throwable $error): void {
83
+    public function onError(int $channel, $socket, Throwable $error): void {
84 84
         unset($channel);
85 85
         if ($socket instanceof WebSocketClient and $error instanceof WebSocketError) {
86 86
             $socket->close($error->getCode(), $error->getMessage());
@@ -101,7 +101,7 @@  discard block
 block discarded – undo
101 101
      * @param float|null $timeout Maximum seconds to block. `NULL` blocks forever.
102 102
      * @return int Number of sockets selected.
103 103
      */
104
-    public function react (?float $timeout = null): int {
104
+    public function react(?float $timeout = null): int {
105 105
         /** @var ReactiveInterface[][] $rwe */
106 106
         $rwe = [$this->sockets, [], $this->sockets];
107 107
         $count = static::select($rwe[0], $rwe[1], $rwe[2], $timeout);
@@ -129,7 +129,7 @@  discard block
 block discarded – undo
129 129
      * @param ReactiveInterface $socket
130 130
      * @return $this
131 131
      */
132
-    public function remove (ReactiveInterface $socket) {
132
+    public function remove(ReactiveInterface $socket) {
133 133
         unset($this->sockets[$socket->getId()]);
134 134
         return $this;
135 135
     }
Please login to merge, or discard this patch.
src/WebSocket/WebSocketServer.php 2 patches
Spacing   +13 added lines, -13 removed lines patch added patch discarded remove patch
@@ -31,7 +31,7 @@  discard block
 block discarded – undo
31 31
      * @param $resource
32 32
      * @param Reactor $reactor
33 33
      */
34
-    public function __construct ($resource, Reactor $reactor) {
34
+    public function __construct($resource, Reactor $reactor) {
35 35
         parent::__construct($resource);
36 36
         $reactor->add($this);
37 37
         $this->reactor = $reactor;
@@ -40,7 +40,7 @@  discard block
 block discarded – undo
40 40
     /**
41 41
      * @return WebSocketClient
42 42
      */
43
-    public function accept (): WebSocketClient {
43
+    public function accept(): WebSocketClient {
44 44
         /**
45 45
          * @see newClient()
46 46
          * @var WebSocketClient $client
@@ -57,7 +57,7 @@  discard block
 block discarded – undo
57 57
      * @param int $opCode
58 58
      * @param string $payload
59 59
      */
60
-    public function broadcast (int $opCode, string $payload) {
60
+    public function broadcast(int $opCode, string $payload) {
61 61
         foreach ($this->clients as $client) {
62 62
             if ($client->isOk()) {
63 63
                 $client->getFrameHandler()->write($opCode, $payload);
@@ -68,7 +68,7 @@  discard block
 block discarded – undo
68 68
     /**
69 69
      * @param string $payload
70 70
      */
71
-    public function broadcastBinary (string $payload) {
71
+    public function broadcastBinary(string $payload) {
72 72
         $this->broadcast(Frame::OP_BINARY, $payload);
73 73
     }
74 74
 
@@ -77,7 +77,7 @@  discard block
 block discarded – undo
77 77
      *
78 78
      * @param string $payload
79 79
      */
80
-    public function broadcastPing (string $payload = '') {
80
+    public function broadcastPing(string $payload = '') {
81 81
         $this->broadcast(Frame::OP_PING, $payload);
82 82
     }
83 83
 
@@ -86,7 +86,7 @@  discard block
 block discarded – undo
86 86
      *
87 87
      * @param string $text
88 88
      */
89
-    public function broadcastText (string $text) {
89
+    public function broadcastText(string $text) {
90 90
         $this->broadcast(Frame::OP_TEXT, $text);
91 91
     }
92 92
 
@@ -97,7 +97,7 @@  discard block
 block discarded – undo
97 97
      * @param string $reason
98 98
      * @return $this
99 99
      */
100
-    public function close (int $code = Frame::CLOSE_INTERRUPT, $reason = '') {
100
+    public function close(int $code = Frame::CLOSE_INTERRUPT, $reason = '') {
101 101
         foreach ($this->clients as $client) {
102 102
             try {
103 103
                 $client->close($code, $reason); // clients remove themselves
@@ -115,14 +115,14 @@  discard block
 block discarded – undo
115 115
      *
116 116
      * @return int
117 117
      */
118
-    public function count (): int {
118
+    public function count(): int {
119 119
         return count($this->clients);
120 120
     }
121 121
 
122 122
     /**
123 123
      * @return WebSocketClient[]
124 124
      */
125
-    public function getClients () {
125
+    public function getClients() {
126 126
         return $this->clients;
127 127
     }
128 128
 
@@ -130,21 +130,21 @@  discard block
 block discarded – undo
130 130
      * @param resource $resource
131 131
      * @return WebSocketClient
132 132
      */
133
-    protected function newClient ($resource): WebSocketClient {
133
+    protected function newClient($resource): WebSocketClient {
134 134
         return new WebSocketClient($resource, $this);
135 135
     }
136 136
 
137 137
     /**
138 138
      * WebSocket servers never get OOB data.
139 139
      */
140
-    final public function onOutOfBand (): void {
140
+    final public function onOutOfBand(): void {
141 141
         // do nothing
142 142
     }
143 143
 
144 144
     /**
145 145
      * Auto-accept.
146 146
      */
147
-    public function onReadable (): void {
147
+    public function onReadable(): void {
148 148
         $this->accept();
149 149
     }
150 150
 
@@ -153,7 +153,7 @@  discard block
 block discarded – undo
153 153
      *
154 154
      * @param WebSocketClient $client
155 155
      */
156
-    public function remove ($client): void {
156
+    public function remove($client): void {
157 157
         unset($this->clients[$client->getId()]);
158 158
         $this->reactor->remove($client);
159 159
     }
Please login to merge, or discard this patch.
Braces   +1 added lines, -2 removed lines patch added patch discarded remove patch
@@ -101,8 +101,7 @@
 block discarded – undo
101 101
         foreach ($this->clients as $client) {
102 102
             try {
103 103
                 $client->close($code, $reason); // clients remove themselves
104
-            }
105
-            catch (Exception $e) {
104
+            } catch (Exception $e) {
106 105
                 continue;
107 106
             }
108 107
         }
Please login to merge, or discard this patch.
src/WebSocket/Handshake.php 2 patches
Spacing   +8 added lines, -8 removed lines patch added patch discarded remove patch
@@ -43,21 +43,21 @@  discard block
 block discarded – undo
43 43
     /**
44 44
      * @param WebSocketClient $client
45 45
      */
46
-    public function __construct (WebSocketClient $client) {
46
+    public function __construct(WebSocketClient $client) {
47 47
         $this->client = $client;
48 48
     }
49 49
 
50 50
     /**
51 51
      * @return string[]
52 52
      */
53
-    public function getHeaders () {
53
+    public function getHeaders() {
54 54
         return $this->headers;
55 55
     }
56 56
 
57 57
     /**
58 58
      * @return string
59 59
      */
60
-    public function getMethod (): string {
60
+    public function getMethod(): string {
61 61
         return $this->method;
62 62
     }
63 63
 
@@ -68,7 +68,7 @@  discard block
 block discarded – undo
68 68
      * @throws WebSocketError
69 69
      * @throws Throwable
70 70
      */
71
-    public function onReadable (): bool {
71
+    public function onReadable(): bool {
72 72
         // read into the buffer
73 73
         $this->buffer .= $bytes = $this->client->recvAll();
74 74
 
@@ -100,7 +100,7 @@  discard block
 block discarded – undo
100 100
                 $key = strtolower(trim($key));
101 101
                 $value = trim($value);
102 102
                 if (isset($this->headers[$key])) {
103
-                    $this->headers[$key] .= ', ' . $value;
103
+                    $this->headers[$key] .= ', '.$value;
104 104
                 }
105 105
                 else {
106 106
                     $this->headers[$key] = $value;
@@ -129,8 +129,8 @@  discard block
 block discarded – undo
129 129
     /**
130 130
      * Sends the connection upgrade headers.
131 131
      */
132
-    protected function upgrade (): void {
133
-        $key = base64_encode(sha1($this->headers['sec-websocket-key'] . self::RFC_GUID, true));
132
+    protected function upgrade(): void {
133
+        $key = base64_encode(sha1($this->headers['sec-websocket-key'].self::RFC_GUID, true));
134 134
         $this->client->write(implode("\r\n", [
135 135
             "HTTP/1.1 101 Switching Protocols",
136 136
             "Connection: Upgrade",
@@ -144,7 +144,7 @@  discard block
 block discarded – undo
144 144
      *
145 145
      * @throws WebSocketError
146 146
      */
147
-    protected function validate (): void {
147
+    protected function validate(): void {
148 148
         if (!(
149 149
             $check = 'method = http 1.1'
150 150
             and preg_match('/HTTP\/1\.1$/i', $this->method)
Please login to merge, or discard this patch.
Braces   +3 added lines, -6 removed lines patch added patch discarded remove patch
@@ -101,19 +101,16 @@
 block discarded – undo
101 101
                 $value = trim($value);
102 102
                 if (isset($this->headers[$key])) {
103 103
                     $this->headers[$key] .= ', ' . $value;
104
-                }
105
-                else {
104
+                } else {
106 105
                     $this->headers[$key] = $value;
107 106
                 }
108 107
             }
109 108
             $this->buffer = ''; // wipe the buffer
110 109
             $this->validate();
111
-        }
112
-        catch (WebSocketError $e) { // catch and respond with HTTP error and rethrow
110
+        } catch (WebSocketError $e) { // catch and respond with HTTP error and rethrow
113 111
             $this->client->write("HTTP/1.1 {$e->getCode()} WebSocket Handshake Failure\r\n\r\n");
114 112
             throw $e;
115
-        }
116
-        catch (Throwable $e) { // catch everything else and respond with HTTP 500 and rethrow
113
+        } catch (Throwable $e) { // catch everything else and respond with HTTP 500 and rethrow
117 114
             $this->client->write("HTTP/1.1 500 WebSocket Internal Error\r\n\r\n");
118 115
             throw $e;
119 116
         }
Please login to merge, or discard this patch.
src/WebSocket/WebSocketClient.php 2 patches
Braces   +4 added lines, -8 removed lines patch added patch discarded remove patch
@@ -82,8 +82,7 @@  discard block
 block discarded – undo
82 82
             if ($code >= 1000 and $this->isOk()) {
83 83
                 $this->frameHandler->writeClose($code, $reason);
84 84
             }
85
-        }
86
-        finally {
85
+        } finally {
87 86
             $this->server->remove($this);
88 87
             parent::close();
89 88
             $this->state = self::STATE_CLOSED;
@@ -200,16 +199,13 @@  discard block
 block discarded – undo
200 199
                     $this->state = self::STATE_OK;
201 200
                     $this->onStateOk();
202 201
                 }
203
-            }
204
-            elseif ($this->isOk()) {
202
+            } elseif ($this->isOk()) {
205 203
                 $this->frameHandler->onReadable();
206 204
             }
207
-        }
208
-        catch (WebSocketError $e) {
205
+        } catch (WebSocketError $e) {
209 206
             $this->close($e->getCode(), $e->getMessage());
210 207
             throw $e;
211
-        }
212
-        catch (Throwable $e) {
208
+        } catch (Throwable $e) {
213 209
             $this->close(Frame::CLOSE_INTERNAL_ERROR);
214 210
             throw $e;
215 211
         }
Please login to merge, or discard this patch.
Spacing   +17 added lines, -17 removed lines patch added patch discarded remove patch
@@ -52,7 +52,7 @@  discard block
 block discarded – undo
52 52
      * @param resource $resource
53 53
      * @param WebSocketServer $server
54 54
      */
55
-    public function __construct ($resource, WebSocketServer $server) {
55
+    public function __construct($resource, WebSocketServer $server) {
56 56
         parent::__construct($resource);
57 57
         $this->server = $server;
58 58
         $this->handshake = new Handshake($this);
@@ -77,7 +77,7 @@  discard block
 block discarded – undo
77 77
      * @param string $reason Sent to the peer, if code is >= 1000
78 78
      * @return $this
79 79
      */
80
-    public function close (int $code = null, string $reason = '') {
80
+    public function close(int $code = null, string $reason = '') {
81 81
         try {
82 82
             if ($code >= 1000 and $this->isOk()) {
83 83
                 $this->frameHandler->writeClose($code, $reason);
@@ -94,32 +94,32 @@  discard block
 block discarded – undo
94 94
     /**
95 95
      * @return FrameHandler
96 96
      */
97
-    public function getFrameHandler (): FrameHandler {
97
+    public function getFrameHandler(): FrameHandler {
98 98
         return $this->frameHandler;
99 99
     }
100 100
 
101 101
     /**
102 102
      * @return WebSocketServer
103 103
      */
104
-    public function getServer (): WebSocketServer {
104
+    public function getServer(): WebSocketServer {
105 105
         return $this->server;
106 106
     }
107 107
 
108 108
     /**
109 109
      * @return int
110 110
      */
111
-    public function getState (): int {
111
+    public function getState(): int {
112 112
         return $this->state;
113 113
     }
114 114
 
115
-    final public function isNegotiating (): bool {
115
+    final public function isNegotiating(): bool {
116 116
         return $this->state === self::STATE_HANDSHAKE;
117 117
     }
118 118
 
119 119
     /**
120 120
      * @return bool
121 121
      */
122
-    final public function isOk (): bool {
122
+    final public function isOk(): bool {
123 123
         return $this->state === self::STATE_OK;
124 124
     }
125 125
 
@@ -131,7 +131,7 @@  discard block
 block discarded – undo
131 131
      * @param string $binary
132 132
      * @throws WebSocketError
133 133
      */
134
-    public function onBinary (string $binary): void {
134
+    public function onBinary(string $binary): void {
135 135
         unset($binary);
136 136
         throw new WebSocketError(Frame::CLOSE_UNHANDLED_DATA, "I don't handle binary data.");
137 137
     }
@@ -142,7 +142,7 @@  discard block
 block discarded – undo
142 142
      * @param int $code
143 143
      * @param string $reason
144 144
      */
145
-    public function onClose (int $code, string $reason): void {
145
+    public function onClose(int $code, string $reason): void {
146 146
         unset($code, $reason);
147 147
         $this->close();
148 148
     }
@@ -154,7 +154,7 @@  discard block
 block discarded – undo
154 154
      *
155 155
      * Closes the connection with a protocol-error frame.
156 156
      */
157
-    final public function onOutOfBand (): void {
157
+    final public function onOutOfBand(): void {
158 158
         $this->close(Frame::CLOSE_PROTOCOL_ERROR, "Received out-of-band data.");
159 159
     }
160 160
 
@@ -165,7 +165,7 @@  discard block
 block discarded – undo
165 165
      *
166 166
      * @param string $message
167 167
      */
168
-    public function onPing (string $message): void {
168
+    public function onPing(string $message): void {
169 169
         $this->frameHandler->writePong($message);
170 170
     }
171 171
 
@@ -176,7 +176,7 @@  discard block
 block discarded – undo
176 176
      *
177 177
      * @param string $message
178 178
      */
179
-    public function onPong (string $message): void {
179
+    public function onPong(string $message): void {
180 180
         // stub
181 181
     }
182 182
 
@@ -186,7 +186,7 @@  discard block
 block discarded – undo
186 186
      * @throws WebSocketError
187 187
      * @throws Throwable
188 188
      */
189
-    public function onReadable (): void {
189
+    public function onReadable(): void {
190 190
         try {
191 191
             if ($this->isNegotiating()) {
192 192
                 if ($this->handshake->onReadable()) {
@@ -216,7 +216,7 @@  discard block
 block discarded – undo
216 216
      * If you have negotiated an extension during {@link Handshake},
217 217
      * claim the RSV bits here via {@link FrameReader::setRsv()}
218 218
      */
219
-    protected function onStateOk (): void {
219
+    protected function onStateOk(): void {
220 220
         // stub
221 221
     }
222 222
 
@@ -228,7 +228,7 @@  discard block
 block discarded – undo
228 228
      * @param string $text
229 229
      * @throws WebSocketError
230 230
      */
231
-    public function onText (string $text): void {
231
+    public function onText(string $text): void {
232 232
         unset($text);
233 233
         throw new WebSocketError(Frame::CLOSE_UNHANDLED_DATA, "I don't handle text.");
234 234
     }
@@ -238,7 +238,7 @@  discard block
 block discarded – undo
238 238
      *
239 239
      * @param string $binary
240 240
      */
241
-    public function writeBinary (string $binary): void {
241
+    public function writeBinary(string $binary): void {
242 242
         $this->frameHandler->writeBinary($binary);
243 243
     }
244 244
 
@@ -247,7 +247,7 @@  discard block
 block discarded – undo
247 247
      *
248 248
      * @param string $text
249 249
      */
250
-    public function writeText (string $text): void {
250
+    public function writeText(string $text): void {
251 251
         $this->frameHandler->writeText($text);
252 252
     }
253 253
 
Please login to merge, or discard this patch.
src/WebSocket/Frame.php 1 patch
Spacing   +33 added lines, -33 removed lines patch added patch discarded remove patch
@@ -43,15 +43,15 @@  discard block
 block discarded – undo
43 43
         0x0f => 'RESERVED CONTROL 0x0f',
44 44
     ];
45 45
 
46
-    const CLOSE_NORMAL = 1000;              // mutual closure
47
-    const CLOSE_INTERRUPT = 1001;           // abrupt closure due to hangups, reboots, "going away"
48
-    const CLOSE_PROTOCOL_ERROR = 1002;      // invalid behavior / framing
49
-    const CLOSE_UNHANDLED_DATA = 1003;      // message handler doesn't want the payload
50
-    const CLOSE_BAD_DATA = 1007;            // message handler can't understand the payload
51
-    const CLOSE_POLICY_VIOLATION = 1008;    // generic "access denied"
52
-    const CLOSE_TOO_LARGE = 1009;           // unacceptable payload size
53
-    const CLOSE_EXPECTATION = 1010;         // peer closed because it wants extensions (listed in the reason)
54
-    const CLOSE_INTERNAL_ERROR = 1011;      // like http 500
46
+    const CLOSE_NORMAL = 1000; // mutual closure
47
+    const CLOSE_INTERRUPT = 1001; // abrupt closure due to hangups, reboots, "going away"
48
+    const CLOSE_PROTOCOL_ERROR = 1002; // invalid behavior / framing
49
+    const CLOSE_UNHANDLED_DATA = 1003; // message handler doesn't want the payload
50
+    const CLOSE_BAD_DATA = 1007; // message handler can't understand the payload
51
+    const CLOSE_POLICY_VIOLATION = 1008; // generic "access denied"
52
+    const CLOSE_TOO_LARGE = 1009; // unacceptable payload size
53
+    const CLOSE_EXPECTATION = 1010; // peer closed because it wants extensions (listed in the reason)
54
+    const CLOSE_INTERNAL_ERROR = 1011; // like http 500
55 55
 
56 56
     /**
57 57
      * @var bool
@@ -81,7 +81,7 @@  discard block
 block discarded – undo
81 81
      * @param int $opCode
82 82
      * @param string $payload
83 83
      */
84
-    public function __construct (bool $final, int $rsv, int $opCode, string $payload) {
84
+    public function __construct(bool $final, int $rsv, int $opCode, string $payload) {
85 85
         $this->final = $final;
86 86
         $this->rsv = $rsv;
87 87
         $this->opCode = $opCode;
@@ -93,7 +93,7 @@  discard block
 block discarded – undo
93 93
      *
94 94
      * @return string
95 95
      */
96
-    public function __toString () {
96
+    public function __toString() {
97 97
         if ($this->isClose()) {
98 98
             return $this->getCloseReason();
99 99
         }
@@ -107,7 +107,7 @@  discard block
 block discarded – undo
107 107
      *
108 108
      * @return int
109 109
      */
110
-    final public function getCloseCode (): int {
110
+    final public function getCloseCode(): int {
111 111
         $code = substr($this->payload, 0, 2);
112 112
         return isset($code[1]) ? unpack('n', $code)[1] : self::CLOSE_NORMAL;
113 113
     }
@@ -119,126 +119,126 @@  discard block
 block discarded – undo
119 119
      *
120 120
      * @return string
121 121
      */
122
-    final public function getCloseReason (): string {
122
+    final public function getCloseReason(): string {
123 123
         return substr($this->payload, 2);
124 124
     }
125 125
 
126 126
     /**
127 127
      * @return int
128 128
      */
129
-    final public function getLength (): int {
129
+    final public function getLength(): int {
130 130
         return strlen($this->payload);
131 131
     }
132 132
 
133 133
     /**
134 134
      * @return string
135 135
      */
136
-    public function getName (): string {
136
+    public function getName(): string {
137 137
         return self::NAMES[$this->opCode];
138 138
     }
139 139
 
140 140
     /**
141 141
      * @return int
142 142
      */
143
-    final public function getOpCode (): int {
143
+    final public function getOpCode(): int {
144 144
         return $this->opCode;
145 145
     }
146 146
 
147 147
     /**
148 148
      * @return string
149 149
      */
150
-    final public function getPayload (): string {
150
+    final public function getPayload(): string {
151 151
         return $this->payload;
152 152
     }
153 153
 
154 154
     /**
155 155
      * @return int
156 156
      */
157
-    final public function getRsv (): int {
157
+    final public function getRsv(): int {
158 158
         return $this->rsv;
159 159
     }
160 160
 
161 161
     /**
162 162
      * @return bool
163 163
      */
164
-    final public function hasRsv1 (): bool {
165
-        return (bool)($this->rsv & self::RSV1);
164
+    final public function hasRsv1(): bool {
165
+        return (bool) ($this->rsv & self::RSV1);
166 166
     }
167 167
 
168 168
     /**
169 169
      * @return bool
170 170
      */
171
-    final public function hasRsv2 (): bool {
172
-        return (bool)($this->rsv & self::RSV2);
171
+    final public function hasRsv2(): bool {
172
+        return (bool) ($this->rsv & self::RSV2);
173 173
     }
174 174
 
175 175
     /**
176 176
      * @return bool
177 177
      */
178
-    final public function hasRsv3 (): bool {
179
-        return (bool)($this->rsv & self::RSV3);
178
+    final public function hasRsv3(): bool {
179
+        return (bool) ($this->rsv & self::RSV3);
180 180
     }
181 181
 
182 182
     /**
183 183
      * @return bool
184 184
      */
185
-    final public function isBinary (): bool {
185
+    final public function isBinary(): bool {
186 186
         return $this->opCode === self::OP_BINARY;
187 187
     }
188 188
 
189 189
     /**
190 190
      * @return bool
191 191
      */
192
-    final public function isClose (): bool {
192
+    final public function isClose(): bool {
193 193
         return $this->opCode === self::OP_CLOSE;
194 194
     }
195 195
 
196 196
     /**
197 197
      * @return bool
198 198
      */
199
-    final public function isContinuation (): bool {
199
+    final public function isContinuation(): bool {
200 200
         return $this->opCode === self::OP_CONTINUATION;
201 201
     }
202 202
 
203 203
     /**
204 204
      * @return bool
205 205
      */
206
-    final public function isControl (): bool {
206
+    final public function isControl(): bool {
207 207
         return $this->opCode >= self::OP_CLOSE;
208 208
     }
209 209
 
210 210
     /**
211 211
      * @return bool
212 212
      */
213
-    final public function isData (): bool {
213
+    final public function isData(): bool {
214 214
         return $this->opCode < self::OP_CLOSE;
215 215
     }
216 216
 
217 217
     /**
218 218
      * @return bool
219 219
      */
220
-    final public function isFinal (): bool {
220
+    final public function isFinal(): bool {
221 221
         return $this->final;
222 222
     }
223 223
 
224 224
     /**
225 225
      * @return bool
226 226
      */
227
-    final public function isPing (): bool {
227
+    final public function isPing(): bool {
228 228
         return $this->opCode === self::OP_PING;
229 229
     }
230 230
 
231 231
     /**
232 232
      * @return bool
233 233
      */
234
-    final public function isPong (): bool {
234
+    final public function isPong(): bool {
235 235
         return $this->opCode === self::OP_PONG;
236 236
     }
237 237
 
238 238
     /**
239 239
      * @return bool
240 240
      */
241
-    final public function isText (): bool {
241
+    final public function isText(): bool {
242 242
         return $this->opCode === self::OP_TEXT;
243 243
     }
244 244
 
Please login to merge, or discard this patch.
src/WebSocket/FrameReader.php 2 patches
Spacing   +9 added lines, -9 removed lines patch added patch discarded remove patch
@@ -62,7 +62,7 @@  discard block
 block discarded – undo
62 62
     /**
63 63
      * @param WebSocketClient $client
64 64
      */
65
-    public function __construct (WebSocketClient $client) {
65
+    public function __construct(WebSocketClient $client) {
66 66
         $this->client = $client;
67 67
     }
68 68
 
@@ -72,7 +72,7 @@  discard block
 block discarded – undo
72 72
      * @return null|Frame
73 73
      * @throws WebSocketError
74 74
      */
75
-    protected function getFrame (): ?Frame {
75
+    protected function getFrame(): ?Frame {
76 76
         // wait for the header
77 77
         if (!$this->header ??= $this->getFrame_header()) {
78 78
             return null;
@@ -110,7 +110,7 @@  discard block
 block discarded – undo
110 110
      * https://tools.ietf.org/html/rfc6455#section-5.2
111 111
      * @return null|array
112 112
      */
113
-    protected function getFrame_header (): ?array {
113
+    protected function getFrame_header(): ?array {
114 114
         if (!preg_match(self::REGEXP, $this->buffer, $match)) {
115 115
             if (strlen($this->buffer) >= 14) { // max head room
116 116
                 throw new WebSocketError(Frame::CLOSE_PROTOCOL_ERROR, 'Bad frame.');
@@ -166,7 +166,7 @@  discard block
 block discarded – undo
166 166
      *
167 167
      * @return Generator|Frame[]
168 168
      */
169
-    public function getFrames () {
169
+    public function getFrames() {
170 170
         // read into the buffer
171 171
         $this->buffer .= $bytes = $this->client->recvAll();
172 172
 
@@ -184,14 +184,14 @@  discard block
 block discarded – undo
184 184
     /**
185 185
      * @return int
186 186
      */
187
-    public function getMaxLength (): int {
187
+    public function getMaxLength(): int {
188 188
         return $this->maxLength;
189 189
     }
190 190
 
191 191
     /**
192 192
      * @return int
193 193
      */
194
-    public function getRsv (): int {
194
+    public function getRsv(): int {
195 195
         return $this->rsv;
196 196
     }
197 197
 
@@ -201,7 +201,7 @@  discard block
 block discarded – undo
201 201
      * @param string $payload
202 202
      * @return Frame
203 203
      */
204
-    protected function newFrame (string $payload): Frame {
204
+    protected function newFrame(string $payload): Frame {
205 205
         return new Frame($this->header['final'], $this->header['rsv'], $this->header['opCode'], $payload);
206 206
     }
207 207
 
@@ -209,7 +209,7 @@  discard block
 block discarded – undo
209 209
      * @param int $bytes
210 210
      * @return $this
211 211
      */
212
-    public function setMaxLength (int $bytes) {
212
+    public function setMaxLength(int $bytes) {
213 213
         $this->maxLength = min(max(125, $bytes), 2 ** 63 - 1);
214 214
         return $this;
215 215
     }
@@ -218,7 +218,7 @@  discard block
 block discarded – undo
218 218
      * @param int $rsv
219 219
      * @return $this
220 220
      */
221
-    public function setRsv (int $rsv) {
221
+    public function setRsv(int $rsv) {
222 222
         $this->rsv = $rsv;
223 223
         return $this;
224 224
     }
Please login to merge, or discard this patch.
Braces   +4 added lines, -8 removed lines patch added patch discarded remove patch
@@ -141,19 +141,15 @@
 block discarded – undo
141 141
         if ($badRsv = $rsv & ~$this->rsv) {
142 142
             $badRsv = str_pad(base_convert($badRsv >> 4, 10, 2), 3, '0', STR_PAD_LEFT);
143 143
             throw new WebSocketError(Frame::CLOSE_PROTOCOL_ERROR, "Received unknown RSV bits: 0b{$badRsv}");
144
-        }
145
-        elseif ($opCode >= Frame::OP_CLOSE) {
144
+        } elseif ($opCode >= Frame::OP_CLOSE) {
146 145
             if ($opCode > Frame::OP_PONG) {
147 146
                 throw new WebSocketError(Frame::CLOSE_PROTOCOL_ERROR, "Received unsupported control frame ({$opCode})");
148
-            }
149
-            elseif (!$final) {
147
+            } elseif (!$final) {
150 148
                 throw new WebSocketError(Frame::CLOSE_PROTOCOL_ERROR, "Received fragmented control frame ({$opCode})");
151 149
             }
152
-        }
153
-        elseif ($opCode > Frame::OP_BINARY) {
150
+        } elseif ($opCode > Frame::OP_BINARY) {
154 151
             throw new WebSocketError(Frame::CLOSE_PROTOCOL_ERROR, "Received unsupported data frame ({$opCode})");
155
-        }
156
-        elseif ($length > $this->maxLength) {
152
+        } elseif ($length > $this->maxLength) {
157 153
             throw new WebSocketError(Frame::CLOSE_TOO_LARGE, "Payload would exceed {$this->maxLength} bytes");
158 154
         }
159 155
 
Please login to merge, or discard this patch.
src/WebSocket/FrameHandler.php 2 patches
Spacing   +28 added lines, -28 removed lines patch added patch discarded remove patch
@@ -67,7 +67,7 @@  discard block
 block discarded – undo
67 67
     /**
68 68
      * @param WebSocketClient $client
69 69
      */
70
-    public function __construct (WebSocketClient $client) {
70
+    public function __construct(WebSocketClient $client) {
71 71
         $this->client = $client;
72 72
         $this->reader = new FrameReader($client);
73 73
     }
@@ -75,21 +75,21 @@  discard block
 block discarded – undo
75 75
     /**
76 76
      * @return int
77 77
      */
78
-    public function getFragmentSize (): int {
78
+    public function getFragmentSize(): int {
79 79
         return $this->fragmentSize;
80 80
     }
81 81
 
82 82
     /**
83 83
      * @return int
84 84
      */
85
-    public function getMaxLength (): int {
85
+    public function getMaxLength(): int {
86 86
         return $this->maxLength;
87 87
     }
88 88
 
89 89
     /**
90 90
      * @return bool
91 91
      */
92
-    public function isStream (): bool {
92
+    public function isStream(): bool {
93 93
         return $this->stream;
94 94
     }
95 95
 
@@ -102,7 +102,7 @@  discard block
 block discarded – undo
102 102
      * @param Frame $frame
103 103
      * @throws WebSocketError
104 104
      */
105
-    protected function onBinary (Frame $frame): void {
105
+    protected function onBinary(Frame $frame): void {
106 106
         if ($this->stream) {
107 107
             $this->client->onBinary($frame->getPayload());
108 108
         }
@@ -128,7 +128,7 @@  discard block
 block discarded – undo
128 128
      *
129 129
      * @param Frame $frame
130 130
      */
131
-    protected function onClose (Frame $frame): void {
131
+    protected function onClose(Frame $frame): void {
132 132
         $this->client->onClose($frame->getCloseCode(), $frame->getCloseReason());
133 133
     }
134 134
 
@@ -138,7 +138,7 @@  discard block
 block discarded – undo
138 138
      * @param Frame $frame
139 139
      * @throws WebSocketError
140 140
      */
141
-    protected function onContinuation (Frame $frame): void {
141
+    protected function onContinuation(Frame $frame): void {
142 142
         if (!$this->continue) {
143 143
             throw new WebSocketError(
144 144
                 Frame::CLOSE_PROTOCOL_ERROR,
@@ -170,7 +170,7 @@  discard block
 block discarded – undo
170 170
      *
171 171
      * @param Frame $frame
172 172
      */
173
-    protected function onControl (Frame $frame): void {
173
+    protected function onControl(Frame $frame): void {
174 174
         if ($frame->isClose()) {
175 175
             $this->onClose($frame);
176 176
         }
@@ -190,7 +190,7 @@  discard block
 block discarded – undo
190 190
      *
191 191
      * @param Frame $frame
192 192
      */
193
-    protected function onData (Frame $frame): void {
193
+    protected function onData(Frame $frame): void {
194 194
         // did we get a continuation?
195 195
         if ($frame->isContinuation()) {
196 196
             $this->onContinuation($frame);
@@ -199,7 +199,7 @@  discard block
 block discarded – undo
199 199
         elseif ($this->continue) {
200 200
             throw new WebSocketError(
201 201
                 Frame::CLOSE_PROTOCOL_ERROR,
202
-                "Received interleaved {$frame->getName()} against existing " . Frame::NAMES[$this->continue],
202
+                "Received interleaved {$frame->getName()} against existing ".Frame::NAMES[$this->continue],
203 203
                 $frame
204 204
             );
205 205
         }
@@ -226,7 +226,7 @@  discard block
 block discarded – undo
226 226
      * @param Frame $frame
227 227
      * @throws WebSocketError
228 228
      */
229
-    protected function onData_CheckLength (Frame $frame): void {
229
+    protected function onData_CheckLength(Frame $frame): void {
230 230
         if (strlen($this->buffer) + $frame->getLength() > $this->maxLength) {
231 231
             throw new WebSocketError(
232 232
                 Frame::CLOSE_TOO_LARGE,
@@ -245,7 +245,7 @@  discard block
 block discarded – undo
245 245
      *
246 246
      * @param Frame $frame
247 247
      */
248
-    public function onFrame (Frame $frame): void {
248
+    public function onFrame(Frame $frame): void {
249 249
         if ($frame->isControl()) {
250 250
             $this->onControl($frame);
251 251
         }
@@ -259,7 +259,7 @@  discard block
 block discarded – undo
259 259
      *
260 260
      * @param Frame $frame
261 261
      */
262
-    protected function onPing (Frame $frame): void {
262
+    protected function onPing(Frame $frame): void {
263 263
         $this->client->onPing($frame->getPayload());
264 264
     }
265 265
 
@@ -268,14 +268,14 @@  discard block
 block discarded – undo
268 268
      *
269 269
      * @param Frame $frame
270 270
      */
271
-    protected function onPong (Frame $frame): void {
271
+    protected function onPong(Frame $frame): void {
272 272
         $this->client->onPong($frame->getPayload());
273 273
     }
274 274
 
275 275
     /**
276 276
      * Uses {@link FrameReader} to read frames and passes them off to {@link onFrame()}
277 277
      */
278
-    public function onReadable (): void {
278
+    public function onReadable(): void {
279 279
         foreach ($this->reader->getFrames() as $frame) {
280 280
             $this->onFrame($frame);
281 281
         }
@@ -288,7 +288,7 @@  discard block
 block discarded – undo
288 288
      * @param Frame $frame
289 289
      * @throws WebSocketError
290 290
      */
291
-    protected function onText (Frame $frame): void {
291
+    protected function onText(Frame $frame): void {
292 292
         $this->onData_CheckLength($frame);
293 293
         $this->buffer .= $frame->getPayload();
294 294
         if ($frame->isFinal()) {
@@ -305,7 +305,7 @@  discard block
 block discarded – undo
305 305
      * @param int $bytes
306 306
      * @return $this
307 307
      */
308
-    public function setFragmentSize (int $bytes) {
308
+    public function setFragmentSize(int $bytes) {
309 309
         $this->fragmentSize = $bytes;
310 310
         return $this;
311 311
     }
@@ -314,7 +314,7 @@  discard block
 block discarded – undo
314 314
      * @param int $bytes
315 315
      * @return $this
316 316
      */
317
-    public function setMaxLength (int $bytes) {
317
+    public function setMaxLength(int $bytes) {
318 318
         $this->maxLength = $bytes;
319 319
         return $this;
320 320
     }
@@ -323,7 +323,7 @@  discard block
 block discarded – undo
323 323
      * @param bool $stream
324 324
      * @return $this
325 325
      */
326
-    public function setStream (bool $stream) {
326
+    public function setStream(bool $stream) {
327 327
         $this->stream = $stream;
328 328
         return $this;
329 329
     }
@@ -334,7 +334,7 @@  discard block
 block discarded – undo
334 334
      * @param int $opCode
335 335
      * @param string $payload
336 336
      */
337
-    public function write (int $opCode, string $payload): void {
337
+    public function write(int $opCode, string $payload): void {
338 338
         $offset = 0;
339 339
         $total = strlen($payload);
340 340
         do {
@@ -350,7 +350,7 @@  discard block
 block discarded – undo
350 350
     /**
351 351
      * @param string $payload
352 352
      */
353
-    public function writeBinary (string $payload): void {
353
+    public function writeBinary(string $payload): void {
354 354
         $this->write(Frame::OP_BINARY, $payload);
355 355
     }
356 356
 
@@ -358,8 +358,8 @@  discard block
 block discarded – undo
358 358
      * @param int $code
359 359
      * @param string $reason
360 360
      */
361
-    public function writeClose (int $code = Frame::CLOSE_NORMAL, string $reason = ''): void {
362
-        $this->writeFrame(true, Frame::OP_CLOSE, pack('n', $code) . $reason);
361
+    public function writeClose(int $code = Frame::CLOSE_NORMAL, string $reason = ''): void {
362
+        $this->writeFrame(true, Frame::OP_CLOSE, pack('n', $code).$reason);
363 363
     }
364 364
 
365 365
     /**
@@ -369,7 +369,7 @@  discard block
 block discarded – undo
369 369
      * @param int $opCode
370 370
      * @param string $payload
371 371
      */
372
-    protected function writeFrame (bool $final, int $opCode, string $payload): void {
372
+    protected function writeFrame(bool $final, int $opCode, string $payload): void {
373 373
         if ($opCode & 0x08 and !$final) {
374 374
             throw new LogicException("Would have sent a fragmented control frame ({$opCode}) {$payload}");
375 375
         }
@@ -386,27 +386,27 @@  discard block
 block discarded – undo
386 386
         else {
387 387
             $head .= chr($length);
388 388
         }
389
-        $this->client->write($head . $payload);
389
+        $this->client->write($head.$payload);
390 390
     }
391 391
 
392 392
     /**
393 393
      * @param string $payload
394 394
      */
395
-    public function writePing (string $payload = ''): void {
395
+    public function writePing(string $payload = ''): void {
396 396
         $this->writeFrame(true, Frame::OP_PING, $payload);
397 397
     }
398 398
 
399 399
     /**
400 400
      * @param string $payload
401 401
      */
402
-    public function writePong (string $payload = ''): void {
402
+    public function writePong(string $payload = ''): void {
403 403
         $this->writeFrame(true, Frame::OP_PONG, $payload);
404 404
     }
405 405
 
406 406
     /**
407 407
      * @param string $payload
408 408
      */
409
-    public function writeText (string $payload): void {
409
+    public function writeText(string $payload): void {
410 410
         $this->write(Frame::OP_TEXT, $payload);
411 411
     }
412 412
 }
413 413
\ No newline at end of file
Please login to merge, or discard this patch.
Braces   +9 added lines, -18 removed lines patch added patch discarded remove patch
@@ -105,8 +105,7 @@  discard block
 block discarded – undo
105 105
     protected function onBinary (Frame $frame): void {
106 106
         if ($this->stream) {
107 107
             $this->client->onBinary($frame->getPayload());
108
-        }
109
-        else {
108
+        } else {
110 109
             $this->onData_CheckLength($frame);
111 110
             $this->buffer .= $frame->getPayload();
112 111
             if ($frame->isFinal()) {
@@ -149,12 +148,10 @@  discard block
 block discarded – undo
149 148
         try {
150 149
             if ($this->continue === Frame::OP_TEXT) {
151 150
                 $this->onText($frame);
152
-            }
153
-            else {
151
+            } else {
154 152
                 $this->onBinary($frame);
155 153
             }
156
-        }
157
-        finally {
154
+        } finally {
158 155
             if ($frame->isFinal()) {
159 156
                 $this->continue = null;
160 157
             }
@@ -173,14 +170,11 @@  discard block
 block discarded – undo
173 170
     protected function onControl (Frame $frame): void {
174 171
         if ($frame->isClose()) {
175 172
             $this->onClose($frame);
176
-        }
177
-        elseif ($frame->isPing()) {
173
+        } elseif ($frame->isPing()) {
178 174
             $this->onPing($frame);
179
-        }
180
-        elseif ($frame->isPong()) {
175
+        } elseif ($frame->isPong()) {
181 176
             $this->onPong($frame);
182
-        }
183
-        else {
177
+        } else {
184 178
             throw new WebSocketError(Frame::CLOSE_PROTOCOL_ERROR, "Unsupported control frame.", $frame);
185 179
         }
186 180
     }
@@ -248,8 +242,7 @@  discard block
 block discarded – undo
248 242
     public function onFrame (Frame $frame): void {
249 243
         if ($frame->isControl()) {
250 244
             $this->onControl($frame);
251
-        }
252
-        else {
245
+        } else {
253 246
             $this->onData($frame);
254 247
         }
255 248
     }
@@ -378,12 +371,10 @@  discard block
 block discarded – undo
378 371
         if ($length > 65535) {
379 372
             $head .= chr(127);
380 373
             $head .= pack('J', $length);
381
-        }
382
-        elseif ($length >= 126) {
374
+        } elseif ($length >= 126) {
383 375
             $head .= chr(126);
384 376
             $head .= pack('n', $length);
385
-        }
386
-        else {
377
+        } else {
387 378
             $head .= chr($length);
388 379
         }
389 380
         $this->client->write($head . $payload);
Please login to merge, or discard this patch.