1 | <?php |
||
10 | abstract class AbstractSocket implements SocketInterface { |
||
11 | |||
12 | /** |
||
13 | * The `SOCK_*` type constant for the class. |
||
14 | * |
||
15 | * @return int |
||
16 | */ |
||
17 | abstract public static function getType (): int; |
||
18 | |||
19 | /** |
||
20 | * The underlying PHP resource. |
||
21 | * |
||
22 | * @var resource |
||
23 | */ |
||
24 | protected $resource; |
||
25 | |||
26 | /** |
||
27 | * Creates an instance of the called class. |
||
28 | * |
||
29 | * @link https://php.net/socket_create |
||
30 | * |
||
31 | * @param int $domain `AF_*` constant. |
||
32 | * @param array $extra Variadic constructor arguments. |
||
33 | * @return static |
||
34 | * @throws SocketError |
||
35 | */ |
||
36 | public static function create (int $domain = AF_INET, ...$extra) { |
||
42 | |||
43 | /** |
||
44 | * Validates and sets the underlying socket resource. |
||
45 | * |
||
46 | * The resource must be open, of the correct type, and have no pending errors. |
||
47 | * |
||
48 | * @param resource $resource PHP socket resource. |
||
49 | * @throws InvalidArgumentException Not a socket resource, or the socket is of the wrong type. |
||
50 | * @throws SocketError Slippage of an existing error on the resource. |
||
51 | */ |
||
52 | public function __construct ($resource) { |
||
65 | |||
66 | /** |
||
67 | * Closes the socket if it's open. |
||
68 | * |
||
69 | * @see close() |
||
70 | */ |
||
71 | public function __destruct () { |
||
76 | |||
77 | /** |
||
78 | * Blocks until the socket becomes available on a given channel. |
||
79 | * |
||
80 | * Throws if the underlying resource has a pending error (non-blocking sockets only). |
||
81 | * |
||
82 | * @see isReady() |
||
83 | * |
||
84 | * @param int $channel {@see SocketInterface} channel constant. |
||
85 | * @return $this |
||
86 | * @throws SocketError |
||
87 | */ |
||
88 | public function await (int $channel) { |
||
95 | |||
96 | /** |
||
97 | * @see await() |
||
98 | * |
||
99 | * @return $this |
||
100 | */ |
||
101 | final public function awaitOutOfBand () { |
||
104 | |||
105 | /** |
||
106 | * @see await() |
||
107 | * |
||
108 | * @return $this |
||
109 | */ |
||
110 | final public function awaitReadable () { |
||
113 | |||
114 | /** |
||
115 | * @see await() |
||
116 | * |
||
117 | * @return $this |
||
118 | */ |
||
119 | final public function awaitWritable () { |
||
122 | |||
123 | /** |
||
124 | * Closes the underlying resource. |
||
125 | * |
||
126 | * @link https://php.net/socket_close |
||
127 | * |
||
128 | * @return $this |
||
129 | */ |
||
130 | public function close () { |
||
134 | |||
135 | /** |
||
136 | * The `AF_*` address family constant. |
||
137 | * |
||
138 | * @return int |
||
139 | */ |
||
140 | final public function getDomain (): int { |
||
143 | |||
144 | /** |
||
145 | * @inheritDoc |
||
146 | */ |
||
147 | final public function getId (): int { |
||
150 | |||
151 | /** |
||
152 | * Retrieves an option value. |
||
153 | * |
||
154 | * @link https://php.net/socket_get_option |
||
155 | * |
||
156 | * @param int $option `SO_*` option constant. |
||
157 | * @return mixed The option's value. This is never `false`. |
||
158 | * @throws SocketError |
||
159 | */ |
||
160 | public function getOption (int $option) { |
||
167 | |||
168 | /** |
||
169 | * @inheritDoc |
||
170 | */ |
||
171 | final public function getResource () { |
||
174 | |||
175 | /** |
||
176 | * The local address and port, or Unix file path and port `0`. |
||
177 | * |
||
178 | * @link https://php.net/socket_getsockname |
||
179 | * |
||
180 | * @return array `[ 0 => address, 1 => port ]` |
||
181 | * @throws SocketError |
||
182 | */ |
||
183 | public function getSockName (): array { |
||
189 | |||
190 | /** |
||
191 | * @inheritDoc |
||
192 | */ |
||
193 | public function isOpen (): bool { |
||
196 | |||
197 | /** |
||
198 | * Polls for whether the socket can perform a non-blocking out-of-band read. |
||
199 | * |
||
200 | * @see isReady() |
||
201 | * |
||
202 | * @return bool |
||
203 | */ |
||
204 | final public function isOutOfBand (): bool { |
||
207 | |||
208 | /** |
||
209 | * Polls for whether the socket can perform a non-blocking read. |
||
210 | * |
||
211 | * @see isReady() |
||
212 | * |
||
213 | * @return bool |
||
214 | */ |
||
215 | final public function isReadable (): bool { |
||
218 | |||
219 | /** |
||
220 | * Selects for channel availability. |
||
221 | * |
||
222 | * @see await() |
||
223 | * |
||
224 | * @param int $channel `SocketInterface` channel constant. |
||
225 | * @param float|null $timeout Maximum seconds to block. `NULL` blocks forever. Defaults to a poll. |
||
226 | * @return bool |
||
227 | * @throws SocketError |
||
228 | */ |
||
229 | public function isReady (int $channel, ?float $timeout = 0): bool { |
||
239 | |||
240 | /** |
||
241 | * Polls for whether the socket can perform a non-blocking write. |
||
242 | * |
||
243 | * @see isReady() |
||
244 | * |
||
245 | * @return bool |
||
246 | */ |
||
247 | final public function isWritable (): bool { |
||
250 | |||
251 | /** |
||
252 | * Enables or disables blocking. |
||
253 | * |
||
254 | * **Note: PHP provides no way to retrieve a socket's blocking mode.** |
||
255 | * |
||
256 | * Sockets are always created in blocking mode. |
||
257 | * |
||
258 | * Non-blocking errors are thrown when performing ordinary operations, even if unrelated to those operations. |
||
259 | * This is known as error slippage. |
||
260 | * To test for and clear an existing non-blocking error, use `->getOption(SO_ERROR)`. |
||
261 | * |
||
262 | * @link https://php.net/socket_set_block |
||
263 | * @link https://php.net/socket_set_nonblock |
||
264 | * |
||
265 | * @param bool $blocking Whether the socket should block. |
||
266 | * @return $this |
||
267 | * @throws SocketError |
||
268 | */ |
||
269 | public function setBlocking (bool $blocking) { |
||
275 | |||
276 | /** |
||
277 | * Sets an option on the underlying resource. |
||
278 | * |
||
279 | * @link https://php.net/socket_set_option |
||
280 | * |
||
281 | * @param int $option `SO_*` constant. |
||
282 | * @param mixed $value |
||
283 | * @return $this |
||
284 | * @throws SocketError |
||
285 | */ |
||
286 | public function setOption (int $option, $value) { |
||
292 | |||
293 | /** |
||
294 | * Sets the I/O timeout length in seconds. |
||
295 | * |
||
296 | * @param float $timeout Zero means "no timeout" (block forever). |
||
297 | * @return $this |
||
298 | */ |
||
299 | public function setTimeout (float $timeout) { |
||
308 | |||
309 | /** |
||
310 | * Shuts down I/O for a single channel. |
||
311 | * |
||
312 | * Shutting down is a formal handshake two peers can do before actually closing. |
||
313 | * It's not required, but it can help assert I/O access logic. |
||
314 | * |
||
315 | * If writing is shut down, trying to send will throw `SOCKET_EPIPE`, |
||
316 | * and the remote peer will read an empty string after receiving all pending data. |
||
317 | * |
||
318 | * If reading is shut down, trying to receive will return an empty string, |
||
319 | * and the remote peer will get an `EPIPE` error if they try to send. |
||
320 | * |
||
321 | * Writing should be shut down first between two connected sockets. |
||
322 | * |
||
323 | * Selection always returns positive for a shut down channel, |
||
324 | * and the remote peer will similarly have positive selection for the opposite channel. |
||
325 | * |
||
326 | * @link https://php.net/socket_shutdown |
||
327 | * |
||
328 | * @param int $channel `CH_READ` or `CH_WRITE` |
||
329 | * @return $this |
||
330 | * @throws SocketError |
||
331 | */ |
||
332 | public function shutdown (int $channel) { |
||
338 | |||
339 | } |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: