Skip to content
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

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

Merged
merged 1 commit into from
Sep 8, 2024
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
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);
}
}
}