Skip to content

Commit

Permalink
Improve PHP 8.4+ support by avoiding implicitly nullable types
Browse files Browse the repository at this point in the history
  • Loading branch information
clue committed Jan 1, 2025
1 parent 9f54bcd commit 86cfede
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 5 additions & 1 deletion src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
14 changes: 14 additions & 0 deletions tests/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions tests/Io/BlockingDatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 86cfede

Please sign in to comment.