From e113d690f4485de9fbfd057fab45a7864741c013 Mon Sep 17 00:00:00 2001 From: paulrotmann Date: Wed, 23 Jul 2025 16:19:15 +0200 Subject: [PATCH] Improve PHP 8.4+ support by avoiding implicitly nullable types --- src/Client.php | 8 +++++++- tests/ClientTest.php | 28 ++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/Client.php b/src/Client.php index 6a9e46a..ff24bff 100644 --- a/src/Client.php +++ b/src/Client.php @@ -28,8 +28,14 @@ class Client * @param ?LoopInterface $loop * @param ?MulticastFactory $multicast */ - public function __construct(LoopInterface $loop = null, MulticastFactory $multicast = null) + public function __construct($loop = null, $multicast = null) { + if ($loop !== null && !$loop instanceof LoopInterface) { // manual type check to support legacy PHP < 7.1 + throw new \InvalidArgumentException('Argument #1 ($loop) expected null|React\EventLoop\LoopInterface'); + } + if ($multicast !== null && !$multicast instanceof MulticastFactory) { // manual type check to support legacy PHP < 7.1 + throw new \InvalidArgumentException('Argument #2 ($multicast) expected null|Clue\React\Multicast\Factory'); + } $this->loop = $loop ?: Loop::get(); $this->multicast = $multicast ?: new MulticastFactory($this->loop); } diff --git a/tests/ClientTest.php b/tests/ClientTest.php index d4829e8..c6aee06 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -71,4 +71,32 @@ public function testSearchTimeout() $promise->then($this->expectCallableOnce(), $this->expectCallableNever(), $this->expectCallableNever()); } + + public function testCtorThrowsForInvalidLoop() + { + if (method_exists($this, 'expectException')) { + // PHPUnit 5.2+ + $this->expectException('InvalidArgumentException'); + $this->expectExceptionMessage('Argument #1 ($loop) expected null|React\EventLoop\LoopInterface'); + } else { + // legacy PHPUnit + $this->setExpectedException('InvalidArgumentException', 'Argument #1 ($loop) expected null|React\EventLoop\LoopInterface'); + } + + new Client('loop'); + } + + public function testCtorThrowsForInvalidMulticast() + { + if (method_exists($this, 'expectException')) { + // PHPUnit 5.2+ + $this->expectException('InvalidArgumentException'); + $this->expectExceptionMessage('Argument #2 ($multicast) expected null|Clue\React\Multicast\Factory'); + } else { + // legacy PHPUnit + $this->setExpectedException('InvalidArgumentException', 'Argument #2 ($multicast) expected null|Clue\React\Multicast\Factory'); + } + + new Client(null, 'multicast'); + } }