Skip to content

Commit 98dedf6

Browse files
authored
Merge pull request #59 from clue-labs/nullable
Improve PHP 8.4+ support by avoiding implicitly nullable types
2 parents e474997 + ac93944 commit 98dedf6

File tree

5 files changed

+42
-5
lines changed

5 files changed

+42
-5
lines changed

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@
2929
"require": {
3030
"php": ">=5.3",
3131
"evenement/evenement": "^3.0 || ^2.0 || ^1.0",
32-
"react/dns": "^1.7",
32+
"react/dns": "^1.13",
3333
"react/event-loop": "^1.2",
34-
"react/promise": "^3 || ^2.1 || ^1.2"
34+
"react/promise": "^3.2 || ^2.1 || ^1.2"
3535
},
3636
"require-dev": {
3737
"phpunit/phpunit": "^9.6 || ^5.7 || ^4.8.36",
38-
"react/async": "^4 || ^3 || ^2"
38+
"react/async": "^4.3 || ^3 || ^2"
3939
},
4040
"autoload": {
4141
"psr-4": {

src/Factory.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,15 @@ class Factory
2929
* try to load the system default DNS config or fall back to using
3030
* Google's public DNS 8.8.8.8
3131
*/
32-
public function __construct(LoopInterface $loop = null, ResolverInterface $resolver = null)
32+
public function __construct($loop = null, $resolver = null)
3333
{
34+
if ($loop !== null && !$loop instanceof LoopInterface) { // manual type check to support legacy PHP < 7.1
35+
throw new \InvalidArgumentException('Argument #1 ($loop) expected null|React\EventLoop\LoopInterface');
36+
}
37+
if ($resolver !== null && !$resolver instanceof ResolverInterface) { // manual type check to support legacy PHP < 7.1
38+
throw new \InvalidArgumentException('Argument #2 ($resolver) expected null|React\Dns\Resolver\ResolverInterface');
39+
}
40+
3441
$loop = $loop ?: Loop::get();
3542
if ($resolver === null) {
3643
// try to load nameservers from system config or default to Google's public DNS

src/Socket.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,17 @@ class Socket extends EventEmitter implements SocketInterface
1515

1616
public $bufferSize = 65536;
1717

18-
public function __construct(LoopInterface $loop, $socket, Buffer $buffer = null)
18+
/**
19+
* @param LoopInterface $loop
20+
* @param resource $socket
21+
* @param ?Buffer $buffer
22+
*/
23+
public function __construct(LoopInterface $loop, $socket, $buffer = null)
1924
{
25+
if ($buffer !== null && !$buffer instanceof Buffer) { // manual type check to support legacy PHP < 7.1
26+
throw new \InvalidArgumentException('Argument #3 ($buffer) expected null|React\Datagram\Buffer');
27+
}
28+
2029
$this->loop = $loop;
2130
$this->socket = $socket;
2231

tests/FactoryTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ public function testConstructWithoutLoopAssignsLoopAutomatically()
3333
$this->assertInstanceOf('React\EventLoop\LoopInterface', $loop);
3434
}
3535

36+
public function testCtorThrowsForInvalidLoop()
37+
{
38+
$this->setExpectedException('InvalidArgumentException', 'Argument #1 ($loop) expected null|React\EventLoop\LoopInterface');
39+
new Factory('loop');
40+
}
41+
42+
public function testCtorThrowsForInvalidResolver()
43+
{
44+
$this->setExpectedException('InvalidArgumentException', 'Argument #2 ($resolver) expected null|React\Dns\Resolver\ResolverInterface');
45+
new Factory(null, 'resolver');
46+
}
47+
3648
public function testCreateClient()
3749
{
3850
$this->resolver->expects($this->never())->method('resolve');

tests/SocketTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ public function setUpFactory()
1818
$this->factory = new \React\Datagram\Factory($this->loop, $this->createResolverMock());
1919
}
2020

21+
public function testCtorThrowsForInvalidBuffer()
22+
{
23+
$socket = stream_socket_server('udp://127.0.0.1:0', $errno, $errstr, STREAM_SERVER_BIND);
24+
assert(is_resource($socket));
25+
26+
$this->setExpectedException('InvalidArgumentException', 'Argument #3 ($buffer) expected null|React\Datagram\Buffer');
27+
new Socket($this->loop, $socket, 'buffer');
28+
}
29+
2130
/**
2231
* @doesNotPerformAssertions
2332
*/

0 commit comments

Comments
 (0)