Skip to content

Commit 77720b8

Browse files
committed
Improve PHP 8.4+ support by avoiding implicitly nullable types
1 parent 7c91c49 commit 77720b8

6 files changed

Lines changed: 43 additions & 5 deletions

File tree

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ jobs:
1313
strategy:
1414
matrix:
1515
php:
16+
- 8.4
1617
- 8.3
1718
- 8.2
1819
- 8.1

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
"php": ">=5.3",
1515
"evenement/evenement": "^3.0 || ^2.0 || ^1.0",
1616
"react/event-loop": "^1.2",
17-
"react/promise": "^3.0 || ^2.9 || ^1.1",
18-
"react/socket": "^1.14"
17+
"react/promise": "^3.2 || ^2.9 || ^1.1",
18+
"react/socket": "^1.16"
1919
},
2020
"require-dev": {
2121
"phpunit/phpunit": "^9.6 || ^5.7 || ^4.8.36",
22-
"react/async": "^4.2 || ^3.2 || ^2.2"
22+
"react/async": "^4.3 || ^3.2 || ^2.2"
2323
},
2424
"autoload": {
2525
"psr-4": {

src/Client.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,16 @@ class Client extends EventEmitter
3333

3434
private $actionId = 0;
3535

36-
public function __construct(ConnectionInterface $stream, Parser $parser = null)
36+
/**
37+
* @param ConnectionInterface $stream
38+
* @param ?Parser $parser
39+
*/
40+
public function __construct(ConnectionInterface $stream, $parser = null)
3741
{
42+
if ($parser !== null && !$parser instanceof Parser) { // manual type check to support legacy PHP < 7.1
43+
throw new \InvalidArgumentException('Argument #2 ($parser) expected null|Clue\React\Ami\Protocol\Parser');
44+
}
45+
3846
if ($parser === null) {
3947
$parser = new Parser();
4048
}

src/Factory.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,19 @@ class Factory
4343
{
4444
private $connector;
4545

46-
public function __construct(LoopInterface $loop = null, ConnectorInterface $connector = null)
46+
/**
47+
* @param ?LoopInterface $loop
48+
* @param ?ConnectorInterface $connector
49+
*/
50+
public function __construct($loop = null, $connector = null)
4751
{
52+
if ($loop !== null && !$loop instanceof LoopInterface) { // manual type check to support legacy PHP < 7.1
53+
throw new \InvalidArgumentException('Argument #1 ($loop) expected null|React\EventLoop\LoopInterface');
54+
}
55+
if ($connector !== null && !$connector instanceof ConnectorInterface) { // manual type check to support legacy PHP < 7.1
56+
throw new \InvalidArgumentException('Argument #2 ($connector) expected null|React\Socket\ConnectorInterface');
57+
}
58+
4859
if ($connector === null) {
4960
$connector = new Connector(array(), $loop);
5061
}

tests/ClientTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ public function testUnexpectedResponseEmitsErrorAndClosesClient()
4343
$client->handleMessage(new Response(array('ActionID' => 1)));
4444
}
4545

46+
public function testCtorThrowsForInvalidParser()
47+
{
48+
$this->setExpectedException('InvalidArgumentException', 'Argument #2 ($parser) expected null|Clue\React\Ami\Protocol\Parser');
49+
new Client($this->createStreamMock(), 'parser');
50+
}
51+
4652
private function createStreamMock()
4753
{
4854
if (method_exists('PHPUnit\Framework\MockObject\MockBuilder', 'onlyMethods')) {

tests/FactoryTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ public function testDefaultCtorCreatesConnectorAutomatically()
3131
$this->assertInstanceOf('React\Socket\Connector', $connector);
3232
}
3333

34+
public function testCtorThrowsForInvalidLoop()
35+
{
36+
$this->setExpectedException('InvalidArgumentException', 'Argument #1 ($loop) expected null|React\EventLoop\LoopInterface');
37+
new Factory('loop');
38+
}
39+
40+
public function testCtorThrowsForInvalidConnector()
41+
{
42+
$this->setExpectedException('InvalidArgumentException', 'Argument #2 ($connector) expected null|React\Socket\ConnectorInterface');
43+
new Factory(null, 'connector');
44+
}
45+
3446
public function testCreateClientUsesDefaultPortForTcpConnection()
3547
{
3648
$promise = new Promise(function () { });

0 commit comments

Comments
 (0)