1 | <?php |
||
11 | class FrameReader { |
||
12 | |||
13 | // todo? doesn't allow unmasked frames. |
||
14 | // op((char )|(short )|(bigint ))(mask ) |
||
15 | protected const REGEXP = '/^.([\x80-\xfd]|\xfe(?<n>..)|\xff(?<J>.{8}))(?<mask>.{4})/s'; |
||
16 | |||
17 | const MAX_LENGTH_RANGE = [125, 2 ** 63 - 1]; |
||
18 | |||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $buffer = ''; |
||
23 | |||
24 | /** |
||
25 | * @var WebSocketClient |
||
26 | */ |
||
27 | protected $client; |
||
28 | |||
29 | /** |
||
30 | * @var array |
||
31 | */ |
||
32 | protected $head = []; |
||
33 | |||
34 | /** |
||
35 | * Payload size limit. |
||
36 | * |
||
37 | * Must fall within {@see MAX_LENGTH_RANGE} (inclusive). |
||
38 | * |
||
39 | * Defaults to 10 MiB. |
||
40 | * |
||
41 | * https://tools.ietf.org/html/rfc6455#section-5.2 |
||
42 | * > ... interpreted as a 64-bit unsigned integer (the |
||
43 | * > most significant bit MUST be 0) ... |
||
44 | * |
||
45 | * @var int |
||
46 | */ |
||
47 | protected $maxLength = 10 * 1024 * 1024; |
||
48 | |||
49 | public function __construct (WebSocketClient $client) { |
||
52 | |||
53 | /** |
||
54 | * @return Frame|null |
||
55 | */ |
||
56 | protected function getFrame () { |
||
92 | |||
93 | /** |
||
94 | * Constructs and yields all available frames from the peer. |
||
95 | * |
||
96 | * @return Generator|Frame[] |
||
97 | */ |
||
98 | public function getFrames () { |
||
104 | |||
105 | /** |
||
106 | * @return int |
||
107 | */ |
||
108 | public function getMaxLength (): int { |
||
111 | |||
112 | /** |
||
113 | * @param int $bytes |
||
114 | * @return $this |
||
115 | */ |
||
116 | public function setMaxLength (int $bytes) { |
||
123 | |||
124 | /** |
||
125 | * Validates the current head by not throwing. |
||
126 | * |
||
127 | * @throws WebSocketError |
||
128 | */ |
||
129 | protected function validate (): void { |
||
147 | } |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.