Skip to content

Commit a8ab087

Browse files
authored
Merge pull request #252 from Kharhamel/socketWrite
FIX: socket_write correctly handle the default value of its third parameter
2 parents dcf6e2b + 0d8fc2d commit a8ab087

File tree

3 files changed

+31
-30
lines changed

3 files changed

+31
-30
lines changed

generated/sockets.php

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -753,36 +753,6 @@ function socket_shutdown($socket, int $how = 2): void
753753
}
754754

755755

756-
/**
757-
* The function socket_write writes to the
758-
* socket from the given
759-
* buffer.
760-
*
761-
* @param resource $socket
762-
* @param string $buffer The buffer to be written.
763-
* @param int $length The optional parameter length can specify an
764-
* alternate length of bytes written to the socket. If this length is
765-
* greater than the buffer length, it is silently truncated to the length
766-
* of the buffer.
767-
* @return int Returns the number of bytes successfully written to the socket.
768-
* The error code can be retrieved with
769-
* socket_last_error. This code may be passed to
770-
* socket_strerror to get a textual explanation of the
771-
* error.
772-
* @throws SocketsException
773-
*
774-
*/
775-
function socket_write($socket, string $buffer, int $length = 0): int
776-
{
777-
error_clear_last();
778-
$result = \socket_write($socket, $buffer, $length);
779-
if ($result === false) {
780-
throw SocketsException::createFromPhpError();
781-
}
782-
return $result;
783-
}
784-
785-
786756
/**
787757
* Exports the WSAPROTOCOL_INFO structure into shared memory and returns
788758
* an identifier to be used with socket_wsaprotocol_info_import. The

generator/config/specialCasesFunctions.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@
1010
'preg_replace',
1111
'openssl_encrypt',
1212
'readdir',
13+
'socket_write',
1314
];

lib/special_cases.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Safe;
99

10+
use Safe\Exceptions\SocketsException;
1011
use const PREG_NO_ERROR;
1112
use Safe\Exceptions\ApcException;
1213
use Safe\Exceptions\ApcuException;
@@ -209,3 +210,32 @@ function openssl_encrypt(string $data, string $method, string $key, int $options
209210
}
210211
return $result;
211212
}
213+
214+
/**
215+
* The function socket_write writes to the
216+
* socket from the given
217+
* buffer.
218+
*
219+
* @param resource $socket
220+
* @param string $buffer The buffer to be written.
221+
* @param int $length The optional parameter length can specify an
222+
* alternate length of bytes written to the socket. If this length is
223+
* greater than the buffer length, it is silently truncated to the length
224+
* of the buffer.
225+
* @return int Returns the number of bytes successfully written to the socket.
226+
* The error code can be retrieved with
227+
* socket_last_error. This code may be passed to
228+
* socket_strerror to get a textual explanation of the
229+
* error.
230+
* @throws SocketsException
231+
*
232+
*/
233+
function socket_write($socket, string $buffer, int $length = 0): int
234+
{
235+
error_clear_last();
236+
$result = $length === 0 ? \socket_write($socket, $buffer) : \socket_write($socket, $buffer, $length);
237+
if ($result === false) {
238+
throw SocketsException::createFromPhpError();
239+
}
240+
return $result;
241+
}

0 commit comments

Comments
 (0)