Skip to content

Commit

Permalink
Merge pull request #24 from clue-labs/nullable
Browse files Browse the repository at this point in the history
Improve PHP 8.4+ support by avoiding implicitly nullable types
  • Loading branch information
clue authored Sep 8, 2024
2 parents b9c9509 + 3407574 commit d686670
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
},
"require": {
"php": ">=5.3",
"react/event-loop": "^1.2",
"react/datagram": "~1.0"
"react/datagram": "^1.10",
"react/event-loop": "^1.2"
},
"require-dev": {
"clue/hexdump": "0.2.*",
Expand Down
8 changes: 6 additions & 2 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Factory

/**
* The `Factory` is responsible for creating your [`SocketInterface`](#socketinterface) instances.
*
*
* This class takes an optional `LoopInterface|null $loop` parameter that can be used to
* pass the event loop instance to use for this object. You can use a `null` value
* here in order to use the [default loop](https://github.com/reactphp/event-loop#loop).
Expand All @@ -29,8 +29,12 @@ class Factory
*
* @param LoopInterface $loop
*/
public function __construct(LoopInterface $loop = null)
public function __construct($loop = 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();
}

Expand Down
12 changes: 9 additions & 3 deletions tests/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,21 @@ public function testMultipleReceivers()

$this->loop->run();
}

public function testConstructWithoutLoopAssignsLoopAutomatically()
{
$factory = new Factory();

$ref = new \ReflectionProperty($factory, 'loop');
$ref->setAccessible(true);
$loop = $ref->getValue($factory);

$this->assertInstanceOf('React\EventLoop\LoopInterface', $loop);
}

public function testCtorThrowsForInvalidLoop()
{
$this->setExpectedException('InvalidArgumentException', 'Argument #1 ($loop) expected null|React\EventLoop\LoopInterface');
new Factory('loop');
}
}
17 changes: 17 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,21 @@ protected function createCallableMock()
{
return $this->getMockBuilder('stdClass')->setMethods(array('__invoke'))->getMock();
}

public function setExpectedException($exception, $exceptionMessage = '', $exceptionCode = null)
{
if (method_exists($this, 'expectException')) {
// PHPUnit 5.2+
$this->expectException($exception);
if ($exceptionMessage !== '') {
$this->expectExceptionMessage($exceptionMessage);
}
if ($exceptionCode !== null) {
$this->expectExceptionCode($exceptionCode);
}
} else {
// legacy PHPUnit
parent::setExpectedException($exception, $exceptionMessage, $exceptionCode);
}
}
}

0 comments on commit d686670

Please sign in to comment.