Skip to content

Commit 5cdf2ec

Browse files
committed
Socket address of closed socket should be null (support PHP 8)
The previous code works just fine on PHP 7 and older. PHP 8 adds stricter type checks for closed socket resources, so the underlying function now throws a `TypeError`. This can be avoided by first checking if the socket resource is still valid (not closed). This works across all PHP versions and also helps with avoiding some uneeded error suppression operators.
1 parent 3fab905 commit 5cdf2ec

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/Connection.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,20 @@ public function handleClose()
138138

139139
public function getRemoteAddress()
140140
{
141-
return $this->parseAddress(@\stream_socket_get_name($this->stream, true));
141+
if (!\is_resource($this->stream)) {
142+
return null;
143+
}
144+
145+
return $this->parseAddress(\stream_socket_get_name($this->stream, true));
142146
}
143147

144148
public function getLocalAddress()
145149
{
146-
return $this->parseAddress(@\stream_socket_get_name($this->stream, false));
150+
if (!\is_resource($this->stream)) {
151+
return null;
152+
}
153+
154+
return $this->parseAddress(\stream_socket_get_name($this->stream, false));
147155
}
148156

149157
private function parseAddress($address)

0 commit comments

Comments
 (0)