Completed
Push — master ( f836de...b537f9 )
by y
01:15
created
src/WebSocket/FrameHandler.php 1 patch
Spacing   +27 added lines, -27 removed lines patch added patch discarded remove patch
@@ -51,21 +51,21 @@  discard block
 block discarded – undo
51 51
     /**
52 52
      * @param WebSocketClient $client
53 53
      */
54
-    public function __construct (WebSocketClient $client) {
54
+    public function __construct(WebSocketClient $client) {
55 55
         $this->client = $client;
56 56
     }
57 57
 
58 58
     /**
59 59
      * @return int
60 60
      */
61
-    public function getFragmentSize (): int {
61
+    public function getFragmentSize(): int {
62 62
         return $this->fragmentSize;
63 63
     }
64 64
 
65 65
     /**
66 66
      * @return int
67 67
      */
68
-    public function getMaxLength (): int {
68
+    public function getMaxLength(): int {
69 69
         return $this->maxLength;
70 70
     }
71 71
 
@@ -75,7 +75,7 @@  discard block
 block discarded – undo
75 75
      * @param Frame $binary
76 76
      * @throws WebSocketError
77 77
      */
78
-    protected function onBinary (Frame $binary): void {
78
+    protected function onBinary(Frame $binary): void {
79 79
         $this->buffer .= $binary->getPayload();
80 80
         if ($binary->isFinal()) {
81 81
             $message = $this->buffer;
@@ -95,7 +95,7 @@  discard block
 block discarded – undo
95 95
      *
96 96
      * @param Frame $close
97 97
      */
98
-    protected function onClose (Frame $close): void {
98
+    protected function onClose(Frame $close): void {
99 99
         $this->client->close($close->getCloseCode());
100 100
     }
101 101
 
@@ -105,7 +105,7 @@  discard block
 block discarded – undo
105 105
      * @param Frame $fragment
106 106
      * @throws WebSocketError
107 107
      */
108
-    protected function onContinue (Frame $fragment): void {
108
+    protected function onContinue(Frame $fragment): void {
109 109
         if (!$this->continue) {
110 110
             throw new WebSocketError(
111 111
                 Frame::CLOSE_PROTOCOL_ERROR,
@@ -137,7 +137,7 @@  discard block
 block discarded – undo
137 137
      *
138 138
      * @param Frame $control
139 139
      */
140
-    protected function onControl (Frame $control): void {
140
+    protected function onControl(Frame $control): void {
141 141
         if ($control->isClose()) {
142 142
             $this->onClose($control);
143 143
         }
@@ -154,7 +154,7 @@  discard block
 block discarded – undo
154 154
      *
155 155
      * @param Frame $data
156 156
      */
157
-    protected function onData (Frame $data): void {
157
+    protected function onData(Frame $data): void {
158 158
         $this->onData_SetContinue($data);
159 159
         if ($data->isText()) {
160 160
             $this->onText($data);
@@ -168,11 +168,11 @@  discard block
 block discarded – undo
168 168
      * @param Frame $data
169 169
      * @throws WebSocketError
170 170
      */
171
-    protected function onData_SetContinue (Frame $data): void {
171
+    protected function onData_SetContinue(Frame $data): void {
172 172
         if ($this->continue) {
173 173
             throw new WebSocketError(
174 174
                 Frame::CLOSE_PROTOCOL_ERROR,
175
-                "Received interleaved {$data->getName()} against existing " . Frame::NAMES[$this->continue],
175
+                "Received interleaved {$data->getName()} against existing ".Frame::NAMES[$this->continue],
176 176
                 $data
177 177
             );
178 178
         }
@@ -188,7 +188,7 @@  discard block
 block discarded – undo
188 188
      *
189 189
      * @param Frame $frame
190 190
      */
191
-    public function onFrame (Frame $frame): void {
191
+    public function onFrame(Frame $frame): void {
192 192
         $this->onFrame_CheckRsv($frame);
193 193
         $this->onFrame_CheckLength($frame);
194 194
         if ($frame->isControl()) {
@@ -206,7 +206,7 @@  discard block
 block discarded – undo
206 206
      * @param Frame $frame
207 207
      * @throws WebSocketError
208 208
      */
209
-    protected function onFrame_CheckLength (Frame $frame): void {
209
+    protected function onFrame_CheckLength(Frame $frame): void {
210 210
         if ($frame->isData()) {
211 211
             $length = strlen($this->buffer);
212 212
             if ($length + $frame->getLength() > $this->maxLength) {
@@ -225,7 +225,7 @@  discard block
 block discarded – undo
225 225
      * @param Frame $frame
226 226
      * @throws WebSocketError
227 227
      */
228
-    protected function onFrame_CheckRsv (Frame $frame): void {
228
+    protected function onFrame_CheckRsv(Frame $frame): void {
229 229
         if ($badRsv = $frame->getRsv() & ~$this->client->getHandshake()->getRsv()) {
230 230
             $badRsv = str_pad(base_convert($badRsv >> 4, 10, 2), 3, '0', STR_PAD_LEFT);
231 231
             throw new WebSocketError(
@@ -243,7 +243,7 @@  discard block
 block discarded – undo
243 243
      *
244 244
      * @param Frame $ping
245 245
      */
246
-    protected function onPing (Frame $ping): void {
246
+    protected function onPing(Frame $ping): void {
247 247
         $this->writePong($ping->getPayload());
248 248
     }
249 249
 
@@ -254,7 +254,7 @@  discard block
 block discarded – undo
254 254
      *
255 255
      * @param Frame $pong
256 256
      */
257
-    protected function onPong (Frame $pong): void {
257
+    protected function onPong(Frame $pong): void {
258 258
         // stub
259 259
     }
260 260
 
@@ -264,7 +264,7 @@  discard block
 block discarded – undo
264 264
      * @param Frame $text
265 265
      * @throws WebSocketError
266 266
      */
267
-    protected function onText (Frame $text): void {
267
+    protected function onText(Frame $text): void {
268 268
         $this->buffer .= $text->getPayload();
269 269
         if ($text->isFinal()) {
270 270
             $message = $this->buffer;
@@ -277,7 +277,7 @@  discard block
 block discarded – undo
277 277
      * @param int $bytes
278 278
      * @return $this
279 279
      */
280
-    public function setFragmentSize (int $bytes) {
280
+    public function setFragmentSize(int $bytes) {
281 281
         $this->fragmentSize = $bytes;
282 282
         return $this;
283 283
     }
@@ -286,7 +286,7 @@  discard block
 block discarded – undo
286 286
      * @param int $bytes
287 287
      * @return $this
288 288
      */
289
-    public function setMaxLength (int $bytes) {
289
+    public function setMaxLength(int $bytes) {
290 290
         $this->maxLength = $bytes;
291 291
         return $this;
292 292
     }
@@ -297,7 +297,7 @@  discard block
 block discarded – undo
297 297
      * @param int $opCode
298 298
      * @param string $payload
299 299
      */
300
-    public function write (int $opCode, string $payload): void {
300
+    public function write(int $opCode, string $payload): void {
301 301
         $offset = 0;
302 302
         $total = strlen($payload);
303 303
         do {
@@ -313,7 +313,7 @@  discard block
 block discarded – undo
313 313
     /**
314 314
      * @param string $payload
315 315
      */
316
-    public function writeBinary (string $payload): void {
316
+    public function writeBinary(string $payload): void {
317 317
         $this->write(Frame::OP_BINARY, $payload);
318 318
     }
319 319
 
@@ -321,8 +321,8 @@  discard block
 block discarded – undo
321 321
      * @param int $code
322 322
      * @param string $reason
323 323
      */
324
-    public function writeClose (int $code = Frame::CLOSE_NORMAL, string $reason = ''): void {
325
-        $this->writeFrame(true, Frame::OP_CLOSE, pack('n', $code) . $reason);
324
+    public function writeClose(int $code = Frame::CLOSE_NORMAL, string $reason = ''): void {
325
+        $this->writeFrame(true, Frame::OP_CLOSE, pack('n', $code).$reason);
326 326
     }
327 327
 
328 328
     /**
@@ -332,7 +332,7 @@  discard block
 block discarded – undo
332 332
      * @param int $opCode
333 333
      * @param string $payload
334 334
      */
335
-    protected function writeFrame (bool $final, int $opCode, string $payload): void {
335
+    protected function writeFrame(bool $final, int $opCode, string $payload): void {
336 336
         if ($opCode & 0x08 and !$final) {
337 337
             throw new LogicException("Would have sent a fragmented control frame ({$opCode}) {$payload}");
338 338
         }
@@ -349,27 +349,27 @@  discard block
 block discarded – undo
349 349
         else {
350 350
             $head .= chr($length);
351 351
         }
352
-        $this->client->write($head . $payload);
352
+        $this->client->write($head.$payload);
353 353
     }
354 354
 
355 355
     /**
356 356
      * @param string $payload
357 357
      */
358
-    public function writePing (string $payload = ''): void {
358
+    public function writePing(string $payload = ''): void {
359 359
         $this->writeFrame(true, Frame::OP_PING, $payload);
360 360
     }
361 361
 
362 362
     /**
363 363
      * @param string $payload
364 364
      */
365
-    public function writePong (string $payload = ''): void {
365
+    public function writePong(string $payload = ''): void {
366 366
         $this->writeFrame(true, Frame::OP_PONG, $payload);
367 367
     }
368 368
 
369 369
     /**
370 370
      * @param string $payload
371 371
      */
372
-    public function writeText (string $payload): void {
372
+    public function writeText(string $payload): void {
373 373
         $this->write(Frame::OP_TEXT, $payload);
374 374
     }
375 375
 }
376 376
\ No newline at end of file
Please login to merge, or discard this patch.
src/Reactor.php 2 patches
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) {
83
+    public function onError(int $channel, $socket, Throwable $error) {
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.
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.