Skip to content

Commit 841e15f

Browse files
committed
Improve PHP 8.4+ support by avoiding implicitly nullable types
1 parent da2e173 commit 841e15f

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/Client.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,14 @@ class Client
2828
* @param ?LoopInterface $loop
2929
* @param ?MulticastFactory $multicast
3030
*/
31-
public function __construct(LoopInterface $loop = null, MulticastFactory $multicast = null)
31+
public function __construct($loop = null, $multicast = null)
3232
{
33+
if ($loop !== null && !$loop instanceof LoopInterface) { // manual type check to support legacy PHP < 7.1
34+
throw new \InvalidArgumentException('Argument #1 ($loop) expected null|React\EventLoop\LoopInterface');
35+
}
36+
if ($multicast !== null && !$multicast instanceof MulticastFactory) { // manual type check to support legacy PHP < 7.1
37+
throw new \InvalidArgumentException('Argument #2 ($multicast) expected null|Clue\React\Multicast\Factory');
38+
}
3339
$this->loop = $loop ?: Loop::get();
3440
$this->multicast = $multicast ?: new MulticastFactory($this->loop);
3541
}

tests/ClientTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,32 @@ public function testSearchTimeout()
7171

7272
$promise->then($this->expectCallableOnce(), $this->expectCallableNever(), $this->expectCallableNever());
7373
}
74+
75+
public function testCtorThrowsForInvalidLoop()
76+
{
77+
if (method_exists($this, 'expectException')) {
78+
// PHPUnit 5.2+
79+
$this->expectException('InvalidArgumentException');
80+
$this->expectExceptionMessage('Argument #1 ($loop) expected null|React\EventLoop\LoopInterface');
81+
} else {
82+
// legacy PHPUnit
83+
$this->setExpectedException('InvalidArgumentException', 'Argument #1 ($loop) expected null|React\EventLoop\LoopInterface');
84+
}
85+
86+
new Client('loop');
87+
}
88+
89+
public function testCtorThrowsForInvalidMulticast()
90+
{
91+
if (method_exists($this, 'expectException')) {
92+
// PHPUnit 5.2+
93+
$this->expectException('InvalidArgumentException');
94+
$this->expectExceptionMessage('Argument #2 ($multicast) expected null|Clue\React\Multicast\Factory');
95+
} else {
96+
// legacy PHPUnit
97+
$this->setExpectedException('InvalidArgumentException', 'Argument #2 ($multicast) expected null|Clue\React\Multicast\Factory');
98+
}
99+
100+
new Client(null, 'multicast');
101+
}
74102
}

0 commit comments

Comments
 (0)