Skip to content

Commit 558b1d0

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

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,33 @@ public function testSearchTimeout()
7171

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

0 commit comments

Comments
 (0)