From 5cdf2ec3e868d7e000eb9dddc5e7786724919a16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Thu, 20 Aug 2020 23:31:39 +0200 Subject: [PATCH] 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. --- src/Connection.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Connection.php b/src/Connection.php index 60febcba..5e3b00d9 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -138,12 +138,20 @@ public function handleClose() public function getRemoteAddress() { - return $this->parseAddress(@\stream_socket_get_name($this->stream, true)); + if (!\is_resource($this->stream)) { + return null; + } + + return $this->parseAddress(\stream_socket_get_name($this->stream, true)); } public function getLocalAddress() { - return $this->parseAddress(@\stream_socket_get_name($this->stream, false)); + if (!\is_resource($this->stream)) { + return null; + } + + return $this->parseAddress(\stream_socket_get_name($this->stream, false)); } private function parseAddress($address)