Skip to content

Improve PHP 8.4+ support by avoiding implicitly nullable types #203

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ will not have to wait for an actual underlying connection.

#### __construct()

The `new MysqlClient(string $uri, ConnectorInterface $connector = null, LoopInterface $loop = null)` constructor can be used to
The `new MysqlClient(string $uri, ?ConnectorInterface $connector = null, ?LoopInterface $loop = null)` constructor can be used to
create a new `MysqlClient` instance.

The `$uri` parameter must contain the database host, optional
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
"php": ">=5.4.0",
"evenement/evenement": "^3.0 || ^2.1 || ^1.1",
"react/event-loop": "^1.2",
"react/promise": "^3 || ^2.7",
"react/promise": "^3.2 || ^2.7",
"react/promise-stream": "^1.6",
"react/promise-timer": "^1.9",
"react/socket": "^1.12"
"react/promise-timer": "^1.11",
"react/socket": "^1.16"
},
"require-dev": {
"phpunit/phpunit": "^9.6 || ^5.7 || ^4.8.36",
"react/async": "^4 || ^3 || ^2"
"react/async": "^4.3 || ^3 || ^2"
},
"autoload": {
"psr-4": {
Expand Down
6 changes: 5 additions & 1 deletion src/Io/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,12 @@ class Factory
* @param ?LoopInterface $loop
* @param ?ConnectorInterface $connector
*/
public function __construct(LoopInterface $loop = null, ConnectorInterface $connector = null)
public function __construct($loop = null, $connector = null)
{
// manual type check to support legacy PHP < 7.1
assert($loop === null || $loop instanceof LoopInterface);
assert($connector === null || $connector instanceof ConnectorInterface);

$this->loop = $loop ?: Loop::get();
$this->connector = $connector ?: new Connector([], $this->loop);
}
Expand Down
16 changes: 14 additions & 2 deletions src/MysqlClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,24 @@ class MysqlClient extends EventEmitter
*/
private $quitting = false;

/**
* @param string $uri
* @param ?ConnectorInterface $connector
* @param ?LoopInterface $loop
*/
public function __construct(
#[\SensitiveParameter]
$uri,
ConnectorInterface $connector = null,
LoopInterface $loop = null
$connector = null,
$loop = null
) {
if ($connector !== null && !$connector instanceof ConnectorInterface) { // manual type check to support legacy PHP < 7.1
throw new \InvalidArgumentException('Argument #2 ($connector) expected null|React\Socket\ConnectorInterface');
}
if ($loop !== null && !$loop instanceof LoopInterface) { // manual type check to support legacy PHP < 7.1
throw new \InvalidArgumentException('Argument #3 ($loop) expected null|React\EventLoop\LoopInterface');
}

$this->factory = new Factory($loop, $connector);
$this->uri = $uri;
}
Expand Down
12 changes: 12 additions & 0 deletions tests/MysqlClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ public function testConstructWithConnectorAndLoopAssignsGivenConnectorAndLoop()
$this->assertSame($loop, $ref->getValue($factory));
}

public function testContructorThrowsExceptionForInvalidConnector()
{
$this->setExpectedException('InvalidArgumentException', 'Argument #2 ($connector) expected null|React\Socket\ConnectorInterface');
new MysqlClient('localhost', 'connector');
}

public function testContructorThrowsExceptionForInvalidLoop()
{
$this->setExpectedException('InvalidArgumentException', 'Argument #3 ($loop) expected null|React\EventLoop\LoopInterface');
new MysqlClient('localhost', null, 'loop');
}

public function testPingWillNotCloseConnectionWhenPendingConnectionFails()
{
$deferred = new Deferred();
Expand Down