Skip to content

Commit d686670

Browse files
authored
Merge pull request #24 from clue-labs/nullable
Improve PHP 8.4+ support by avoiding implicitly nullable types
2 parents b9c9509 + 3407574 commit d686670

File tree

4 files changed

+34
-7
lines changed

4 files changed

+34
-7
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
},
2323
"require": {
2424
"php": ">=5.3",
25-
"react/event-loop": "^1.2",
26-
"react/datagram": "~1.0"
25+
"react/datagram": "^1.10",
26+
"react/event-loop": "^1.2"
2727
},
2828
"require-dev": {
2929
"clue/hexdump": "0.2.*",

src/Factory.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Factory
1515

1616
/**
1717
* The `Factory` is responsible for creating your [`SocketInterface`](#socketinterface) instances.
18-
*
18+
*
1919
* This class takes an optional `LoopInterface|null $loop` parameter that can be used to
2020
* pass the event loop instance to use for this object. You can use a `null` value
2121
* here in order to use the [default loop](https://github.com/reactphp/event-loop#loop).
@@ -29,8 +29,12 @@ class Factory
2929
*
3030
* @param LoopInterface $loop
3131
*/
32-
public function __construct(LoopInterface $loop = null)
32+
public function __construct($loop = null)
3333
{
34+
if ($loop !== null && !$loop instanceof LoopInterface) { // manual type check to support legacy PHP < 7.1
35+
throw new \InvalidArgumentException('Argument #1 ($loop) expected null|React\EventLoop\LoopInterface');
36+
}
37+
3438
$this->loop = $loop ?: Loop::get();
3539
}
3640

tests/FunctionalTest.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,21 @@ public function testMultipleReceivers()
5757

5858
$this->loop->run();
5959
}
60-
60+
6161
public function testConstructWithoutLoopAssignsLoopAutomatically()
6262
{
6363
$factory = new Factory();
64-
64+
6565
$ref = new \ReflectionProperty($factory, 'loop');
6666
$ref->setAccessible(true);
6767
$loop = $ref->getValue($factory);
68-
68+
6969
$this->assertInstanceOf('React\EventLoop\LoopInterface', $loop);
7070
}
71+
72+
public function testCtorThrowsForInvalidLoop()
73+
{
74+
$this->setExpectedException('InvalidArgumentException', 'Argument #1 ($loop) expected null|React\EventLoop\LoopInterface');
75+
new Factory('loop');
76+
}
7177
}

tests/TestCase.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,21 @@ protected function createCallableMock()
4141
{
4242
return $this->getMockBuilder('stdClass')->setMethods(array('__invoke'))->getMock();
4343
}
44+
45+
public function setExpectedException($exception, $exceptionMessage = '', $exceptionCode = null)
46+
{
47+
if (method_exists($this, 'expectException')) {
48+
// PHPUnit 5.2+
49+
$this->expectException($exception);
50+
if ($exceptionMessage !== '') {
51+
$this->expectExceptionMessage($exceptionMessage);
52+
}
53+
if ($exceptionCode !== null) {
54+
$this->expectExceptionCode($exceptionCode);
55+
}
56+
} else {
57+
// legacy PHPUnit
58+
parent::setExpectedException($exception, $exceptionMessage, $exceptionCode);
59+
}
60+
}
4461
}

0 commit comments

Comments
 (0)