Skip to content

Commit dbf58dc

Browse files
committed
Improve PHP 8.4+ support by avoiding implicitly nullable types
1 parent 216d3ae commit dbf58dc

23 files changed

+161
-23
lines changed

composer.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@
2828
"require": {
2929
"php": ">=5.3.0",
3030
"evenement/evenement": "^3.0 || ^2.0 || ^1.0",
31-
"react/dns": "^1.11",
31+
"react/dns": "^1.13",
3232
"react/event-loop": "^1.2",
33-
"react/promise": "^3 || ^2.6 || ^1.2.1",
34-
"react/stream": "^1.2"
33+
"react/promise": "^3.2 || ^2.6 || ^1.2.1",
34+
"react/stream": "^1.4"
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.3 || ^2",
3939
"react/promise-stream": "^1.4",
40-
"react/promise-timer": "^1.10"
40+
"react/promise-timer": "^1.11"
4141
},
4242
"autoload": {
4343
"psr-4": {

src/FdServer.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ final class FdServer extends EventEmitter implements ServerInterface
7575
* @throws \InvalidArgumentException if the listening address is invalid
7676
* @throws \RuntimeException if listening on this address fails (already in use etc.)
7777
*/
78-
public function __construct($fd, LoopInterface $loop = null)
78+
public function __construct($fd, $loop = null)
7979
{
8080
if (\preg_match('#^php://fd/(\d+)$#', $fd, $m)) {
8181
$fd = (int) $m[1];
@@ -87,6 +87,10 @@ public function __construct($fd, LoopInterface $loop = null)
8787
);
8888
}
8989

90+
if ($loop !== null && !$loop instanceof LoopInterface) { // manual type check to support legacy PHP < 7.1
91+
throw new \InvalidArgumentException('Argument #2 ($loop) expected null|React\EventLoop\LoopInterface');
92+
}
93+
9094
$this->loop = $loop ?: Loop::get();
9195

9296
$errno = 0;

src/HappyEyeBallsConnector.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,26 @@ final class HappyEyeBallsConnector implements ConnectorInterface
1313
private $connector;
1414
private $resolver;
1515

16-
public function __construct(LoopInterface $loop = null, ConnectorInterface $connector = null, ResolverInterface $resolver = null)
16+
/**
17+
* @param ?LoopInterface $loop
18+
* @param ConnectorInterface $connector
19+
* @param ResolverInterface $resolver
20+
*/
21+
public function __construct($loop = null, $connector = null, $resolver = null)
1722
{
1823
// $connector and $resolver arguments are actually required, marked
1924
// optional for technical reasons only. Nullable $loop without default
2025
// requires PHP 7.1, null default is also supported in legacy PHP
2126
// versions, but required parameters are not allowed after arguments
2227
// with null default. Mark all parameters optional and check accordingly.
23-
if ($connector === null || $resolver === null) {
24-
throw new \InvalidArgumentException('Missing required $connector or $resolver argument');
28+
if ($loop !== null && !$loop instanceof LoopInterface) { // manual type check to support legacy PHP < 7.1
29+
throw new \InvalidArgumentException('Argument #1 ($loop) expected null|React\EventLoop\LoopInterface');
30+
}
31+
if (!$connector instanceof ConnectorInterface) { // manual type check to support legacy PHP < 7.1
32+
throw new \InvalidArgumentException('Argument #2 ($connector) expected React\Socket\ConnectorInterface');
33+
}
34+
if (!$resolver instanceof ResolverInterface) { // manual type check to support legacy PHP < 7.1
35+
throw new \InvalidArgumentException('Argument #3 ($resolver) expected React\Dns\Resolver\ResolverInterface');
2536
}
2637

2738
$this->loop = $loop ?: Loop::get();

src/SecureConnector.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,17 @@ final class SecureConnector implements ConnectorInterface
1515
private $streamEncryption;
1616
private $context;
1717

18-
public function __construct(ConnectorInterface $connector, LoopInterface $loop = null, array $context = array())
18+
/**
19+
* @param ConnectorInterface $connector
20+
* @param ?LoopInterface $loop
21+
* @param array $context
22+
*/
23+
public function __construct(ConnectorInterface $connector, $loop = null, array $context = array())
1924
{
25+
if ($loop !== null && !$loop instanceof LoopInterface) { // manual type check to support legacy PHP < 7.1
26+
throw new \InvalidArgumentException('Argument #2 ($loop) expected null|React\EventLoop\LoopInterface');
27+
}
28+
2029
$this->connector = $connector;
2130
$this->streamEncryption = new StreamEncryption($loop ?: Loop::get(), false);
2231
$this->context = $context;

src/SecureServer.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,12 @@ final class SecureServer extends EventEmitter implements ServerInterface
122122
* @see TcpServer
123123
* @link https://www.php.net/manual/en/context.ssl.php for TLS context options
124124
*/
125-
public function __construct(ServerInterface $tcp, LoopInterface $loop = null, array $context = array())
125+
public function __construct(ServerInterface $tcp, $loop = null, array $context = array())
126126
{
127+
if ($loop !== null && !$loop instanceof LoopInterface) { // manual type check to support legacy PHP < 7.1
128+
throw new \InvalidArgumentException('Argument #2 ($loop) expected null|React\EventLoop\LoopInterface');
129+
}
130+
127131
if (!\function_exists('stream_socket_enable_crypto')) {
128132
throw new \BadMethodCallException('Encryption not supported on your platform (HHVM < 3.8?)'); // @codeCoverageIgnore
129133
}

src/Server.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,18 @@ final class Server extends EventEmitter implements ServerInterface
4343
* For BC reasons, you can also pass the TCP socket context options as a simple
4444
* array without wrapping this in another array under the `tcp` key.
4545
*
46-
* @param string|int $uri
47-
* @param LoopInterface $loop
48-
* @param array $context
46+
* @param string|int $uri
47+
* @param ?LoopInterface $loop
48+
* @param array $context
4949
* @deprecated 1.9.0 See `SocketServer` instead
5050
* @see SocketServer
5151
*/
52-
public function __construct($uri, LoopInterface $loop = null, array $context = array())
52+
public function __construct($uri, $loop = null, array $context = array())
5353
{
54+
if ($loop !== null && !$loop instanceof LoopInterface) { // manual type check to support legacy PHP < 7.1
55+
throw new \InvalidArgumentException('Argument #2 ($loop) expected null|React\EventLoop\LoopInterface');
56+
}
57+
5458
$loop = $loop ?: Loop::get();
5559

5660
// sanitize TCP context options if not properly wrapped

src/SocketServer.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,12 @@ final class SocketServer extends EventEmitter implements ServerInterface
3131
* @throws \InvalidArgumentException if the listening address is invalid
3232
* @throws \RuntimeException if listening on this address fails (already in use etc.)
3333
*/
34-
public function __construct($uri, array $context = array(), LoopInterface $loop = null)
34+
public function __construct($uri, array $context = array(), $loop = null)
3535
{
36+
if ($loop !== null && !$loop instanceof LoopInterface) { // manual type check to support legacy PHP < 7.1
37+
throw new \InvalidArgumentException('Argument #3 ($loop) expected null|React\EventLoop\LoopInterface');
38+
}
39+
3640
// apply default options if not explicitly given
3741
$context += array(
3842
'tcp' => array(),

src/TcpConnector.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,16 @@ final class TcpConnector implements ConnectorInterface
1313
private $loop;
1414
private $context;
1515

16-
public function __construct(LoopInterface $loop = null, array $context = array())
16+
/**
17+
* @param ?LoopInterface $loop
18+
* @param array $context
19+
*/
20+
public function __construct($loop = null, array $context = array())
1721
{
22+
if ($loop !== null && !$loop instanceof LoopInterface) { // manual type check to support legacy PHP < 7.1
23+
throw new \InvalidArgumentException('Argument #1 ($loop) expected null|React\EventLoop\LoopInterface');
24+
}
25+
1826
$this->loop = $loop ?: Loop::get();
1927
$this->context = $context;
2028
}

src/TcpServer.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,12 @@ final class TcpServer extends EventEmitter implements ServerInterface
128128
* @throws InvalidArgumentException if the listening address is invalid
129129
* @throws RuntimeException if listening on this address fails (already in use etc.)
130130
*/
131-
public function __construct($uri, LoopInterface $loop = null, array $context = array())
131+
public function __construct($uri, $loop = null, array $context = array())
132132
{
133+
if ($loop !== null && !$loop instanceof LoopInterface) { // manual type check to support legacy PHP < 7.1
134+
throw new \InvalidArgumentException('Argument #2 ($loop) expected null|React\EventLoop\LoopInterface');
135+
}
136+
133137
$this->loop = $loop ?: Loop::get();
134138

135139
// a single port has been given => assume localhost

src/TimeoutConnector.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,17 @@ final class TimeoutConnector implements ConnectorInterface
1212
private $timeout;
1313
private $loop;
1414

15-
public function __construct(ConnectorInterface $connector, $timeout, LoopInterface $loop = null)
15+
/**
16+
* @param ConnectorInterface $connector
17+
* @param float $timeout
18+
* @param ?LoopInterface $loop
19+
*/
20+
public function __construct(ConnectorInterface $connector, $timeout, $loop = null)
1621
{
22+
if ($loop !== null && !$loop instanceof LoopInterface) { // manual type check to support legacy PHP < 7.1
23+
throw new \InvalidArgumentException('Argument #3 ($loop) expected null|React\EventLoop\LoopInterface');
24+
}
25+
1726
$this->connector = $connector;
1827
$this->timeout = $timeout;
1928
$this->loop = $loop ?: Loop::get();

0 commit comments

Comments
 (0)