From 86cfede0910d65896ee316228703ba9dfe14d384 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Tue, 24 Dec 2024 14:34:16 +0100 Subject: [PATCH] Improve PHP 8.4+ support by avoiding implicitly nullable types --- composer.json | 4 ++-- src/Factory.php | 6 +++++- tests/FactoryTest.php | 14 ++++++++++++++ tests/Io/BlockingDatabaseTest.php | 2 ++ 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index eddd853..e2d2f2c 100644 --- a/composer.json +++ b/composer.json @@ -14,9 +14,9 @@ "php": ">=5.4", "ext-sqlite3": "*", "clue/ndjson-react": "^1.0", - "react/child-process": "^0.6", + "react/child-process": "^0.6.6", "react/event-loop": "^1.2", - "react/promise": "^3 || ^2.7 || ^1.2.1" + "react/promise": "^3.2 || ^2.7 || ^1.2.1" }, "require-dev": { "phpunit/phpunit": "^9.6 || ^5.7 || ^4.8.36" diff --git a/src/Factory.php b/src/Factory.php index 8cf5284..acfd155 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -62,8 +62,12 @@ class Factory * @param ?LoopInterface $loop * @param ?string $binary */ - public function __construct(LoopInterface $loop = null, $binary = null) + public function __construct($loop = null, $binary = null) { + if ($loop !== null && !$loop instanceof LoopInterface) { // manual type check to support legacy PHP < 7.1 + throw new \InvalidArgumentException('Argument #1 ($loop) expected null|React\EventLoop\LoopInterface'); + } + $this->loop = $loop ?: Loop::get(); $this->bin = $binary === null ? $this->php() : $binary; diff --git a/tests/FactoryTest.php b/tests/FactoryTest.php index 03dae31..bf71c80 100644 --- a/tests/FactoryTest.php +++ b/tests/FactoryTest.php @@ -32,6 +32,20 @@ public function testConstructWitLoopAndBinaryAssignsBothVariables() $this->assertSame('php6.0', $ref->getValue($factory)); } + public function testCtorThrowsForInvalidLoop() + { + if (method_exists($this, 'expectException')) { + // PHPUnit 5.2+ + $this->expectException('InvalidArgumentException'); + $this->expectExceptionMessage('Argument #1 ($loop) expected null|React\EventLoop\LoopInterface'); + } else { + // legacy PHPUnit + $this->setExpectedException('InvalidArgumentException', 'Argument #1 ($loop) expected null|React\EventLoop\LoopInterface'); + } + + new Factory('loop'); + } + public function testLoadLazyReturnsDatabaseImmediately() { $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); diff --git a/tests/Io/BlockingDatabaseTest.php b/tests/Io/BlockingDatabaseTest.php index 5c80d7c..ac8f50c 100644 --- a/tests/Io/BlockingDatabaseTest.php +++ b/tests/Io/BlockingDatabaseTest.php @@ -9,8 +9,10 @@ class BlockingDatabaseTest extends TestCase public function testCtorThrowsForInvalidPath() { if (method_exists($this, 'expectException')) { + // PHPUnit 5.2+ $this->expectException('Exception'); } else { + // legacy PHPUnit $this->setExpectedException('Exception'); } new BlockingDatabase('/dev/foobar');