diff --git a/src/Internal/functions.php b/src/Internal/functions.php index b4a1c12..b0311d5 100644 --- a/src/Internal/functions.php +++ b/src/Internal/functions.php @@ -114,6 +114,7 @@ function setupTls($socket, array $options, ?Cancellation $cancellation): void $suspension = EventLoop::getSuspension(); // Watcher is guaranteed to be created, because we throw above if cancellation has already been requested + /** @psalm-suppress PossiblyUndefinedVariable $callbackId is defined below. */ $cancellationId = $cancellation->subscribe(static function ($e) use ($suspension, &$callbackId): void { EventLoop::cancel($callbackId); diff --git a/src/ResourceServerSocket.php b/src/ResourceServerSocket.php index ce096c5..20f60e0 100644 --- a/src/ResourceServerSocket.php +++ b/src/ResourceServerSocket.php @@ -19,14 +19,14 @@ final class ResourceServerSocket implements ServerSocket, ResourceStream /** @var resource|null Stream socket server resource. */ private $socket; - private string $callbackId; + private readonly string $callbackId; private readonly SocketAddress $address; private ?Suspension $acceptor = null; - /** @var \Closure(CancelledException) */ - private \Closure $cancel; + /** @var \Closure(CancelledException):void */ + private readonly \Closure $cancel; private readonly \Closure $errorHandler; @@ -59,11 +59,11 @@ public function __construct( $acceptor = &$this->acceptor; $this->callbackId = EventLoop::onReadable($this->socket, static function () use (&$acceptor): void { - $acceptor->resume(true); + $acceptor?->resume(true); $acceptor = null; }); - $callbackId = &$this->callbackId; + $callbackId = $this->callbackId; $this->cancel = static function (CancelledException $exception) use (&$acceptor, $callbackId): void { EventLoop::disable($callbackId); diff --git a/src/ResourceUdpSocket.php b/src/ResourceUdpSocket.php index cb7b1ca..4189ac9 100644 --- a/src/ResourceUdpSocket.php +++ b/src/ResourceUdpSocket.php @@ -21,14 +21,14 @@ final class ResourceUdpSocket implements UdpSocket, ResourceStream /** @var resource|null UDP socket resource. */ private $socket; - private string $callbackId; + private readonly string $callbackId; - private InternetAddress $address; + private readonly InternetAddress $address; private ?Suspension $reader = null; - /** @var \Closure(CancelledException) */ - private \Closure $cancel; + /** @var \Closure(CancelledException):void */ + private readonly \Closure $cancel; private int $limit; @@ -98,7 +98,7 @@ public function __construct($socket, int $limit = self::DEFAULT_LIMIT) $reader = null; }); - $callbackId = &$this->callbackId; + $callbackId = $this->callbackId; $this->cancel = static function (CancelledException $exception) use (&$reader, $callbackId): void { EventLoop::disable($callbackId); @@ -168,9 +168,9 @@ public function send(InternetAddress $address, string $data): void throw new SocketException('The datagram socket is not writable'); } - try { - \set_error_handler($errorHandler); + \set_error_handler($errorHandler); + try { $result = \stream_socket_sendto($this->socket, $data, 0, $address->toString()); /** @psalm-suppress TypeDoesNotContainType */ if ($result < 0 || $result === false) { diff --git a/test/ResourceDatagramSocketTest.php b/test/ResourceUdpSocketTest.php similarity index 99% rename from test/ResourceDatagramSocketTest.php rename to test/ResourceUdpSocketTest.php index 237641d..a54d766 100644 --- a/test/ResourceDatagramSocketTest.php +++ b/test/ResourceUdpSocketTest.php @@ -11,7 +11,7 @@ use function Amp\async; use function Amp\delay; -class ResourceDatagramSocketTest extends AsyncTestCase +class ResourceUdpSocketTest extends AsyncTestCase { public function testBindEndpointInvalidScheme(): void {