Skip to content

Commit fe512aa

Browse files
committed
Improve PHP 8.4+ support by avoiding implicitly nullable types
1 parent d518c59 commit fe512aa

File tree

6 files changed

+37
-7
lines changed

6 files changed

+37
-7
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ on:
77
jobs:
88
PHPUnit:
99
name: PHPUnit (PHP ${{ matrix.php }})
10-
runs-on: ubuntu-22.04
10+
runs-on: ubuntu-24.04
1111
strategy:
1212
matrix:
1313
php:
14+
- 8.4
1415
- 8.3
1516
- 8.2
1617
- 8.1
@@ -35,7 +36,7 @@ jobs:
3536

3637
PHPUnit-hhvm:
3738
name: PHPUnit (HHVM)
38-
runs-on: ubuntu-22.04
39+
runs-on: ubuntu-24.04
3940
continue-on-error: true
4041
steps:
4142
- uses: actions/checkout@v4

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
],
1313
"require": {
1414
"php": ">=5.3",
15-
"react/promise": "^3 || ^2.1 || ^1.2",
16-
"react/socket": "^1.12"
15+
"react/promise": "^3.2 || ^2.1 || ^1.2",
16+
"react/socket": "^1.16"
1717
},
1818
"require-dev": {
1919
"clue/block-react": "^1.5",

src/Client.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,12 @@ final class Client implements ConnectorInterface
3434
public function __construct(
3535
#[\SensitiveParameter]
3636
$socksUri,
37-
ConnectorInterface $connector = null
37+
$connector = null
3838
) {
39+
if ($connector !== null && !$connector instanceof ConnectorInterface) { // manual type check to support legacy PHP < 7.1
40+
throw new InvalidArgumentException('Argument #2 ($connector) expected null|React\Socket\ConnectorInterface');
41+
}
42+
3943
// support `sockss://` scheme for SOCKS over TLS
4044
// support `socks+unix://` scheme for Unix domain socket (UDS) paths
4145
if (preg_match('/^(socks(?:5|4)?)(s|\+unix):\/\/(.*?@)?(.+?)$/', $socksUri, $match)) {

src/Server.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,18 @@ final class Server
5858
* @param null|array|callable $auth
5959
*/
6060
public function __construct(
61-
LoopInterface $loop = null,
62-
ConnectorInterface $connector = null,
61+
$loop = null,
62+
$connector = null,
6363
#[\SensitiveParameter]
6464
$auth = null
6565
) {
66+
if ($loop !== null && !$loop instanceof LoopInterface) { // manual type check to support legacy PHP < 7.1
67+
throw new \InvalidArgumentException('Argument #1 ($loop) expected null|React\EventLoop\LoopInterface');
68+
}
69+
if ($connector !== null && !$connector instanceof ConnectorInterface) { // manual type check to support legacy PHP < 7.1
70+
throw new \InvalidArgumentException('Argument #2 ($connector) expected null|React\Socket\ConnectorInterface');
71+
}
72+
6673
if (\is_array($auth)) {
6774
// wrap authentication array in authentication callback
6875
$this->auth = function (

tests/ClientTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ public function testCtorAcceptsUriWithSocks5UnixScheme()
8383
$this->assertTrue(true);
8484
}
8585

86+
public function testCtorThrowsForInvalidConnector()
87+
{
88+
$this->setExpectedException('InvalidArgumentException', 'Argument #2 ($connector) expected null|React\Socket\ConnectorInterface');
89+
new Client('127.0.0.1:1080', 'connector');
90+
}
91+
8692
public function testCtorThrowsForInvalidUri()
8793
{
8894
$this->setExpectedException("InvalidArgumentException");

tests/ServerTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,18 @@ public function testConstructorWithStaticAuthArray()
6868
));
6969
}
7070

71+
public function testConstructorThrowsForInvalidLoop()
72+
{
73+
$this->setExpectedException('InvalidArgumentException', 'Argument #1 ($loop) expected null|React\EventLoop\LoopInterface');
74+
new Server('loop');
75+
}
76+
77+
public function testConstructorThrowsForInvalidConnector()
78+
{
79+
$this->setExpectedException('InvalidArgumentException', 'Argument #2 ($connector) expected null|React\Socket\ConnectorInterface');
80+
new Server(null, 'connector');
81+
}
82+
7183
public function testConstructorWithInvalidAuthenticatorThrows()
7284
{
7385
$this->setExpectedException("InvalidArgumentException");

0 commit comments

Comments
 (0)