Skip to content

Commit

Permalink
Merge pull request #46 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 Nov 23, 2024
2 parents 29f9d70 + 6ed90a0 commit 5c22d81
Show file tree
Hide file tree
Showing 3 changed files with 22 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,8 +14,8 @@
"php": ">=5.4",
"evenement/evenement": "^3.0 || ^2.0",
"react/event-loop": "^1.2",
"react/http": "^1.6",
"react/promise": "^3 || ^2.10 || ^1.2.1"
"react/http": "^1.11",
"react/promise": "^3.2 || ^2.10 || ^1.2.1"
},
"require-dev": {
"phpunit/phpunit": "^9.6 || ^5.7 || ^4.8.36"
Expand Down
9 changes: 8 additions & 1 deletion src/EventSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,20 @@ class EventSource extends EventEmitter
* @param ?LoopInterface $loop
* @throws \InvalidArgumentException for invalid URL
*/
public function __construct($url, Browser $browser = null, LoopInterface $loop = null)
public function __construct($url, $browser = null, $loop = null)
{
$parts = parse_url($url);
if (!isset($parts['scheme'], $parts['host']) || !in_array($parts['scheme'], array('http', 'https'))) {
throw new \InvalidArgumentException();
}

if ($browser !== null && !$browser instanceof Browser) { // manual type check to support legacy PHP < 7.1
throw new \InvalidArgumentException('Argument #2 ($browser) expected null|React\Http\Browser');
}
if ($loop !== null && !$loop instanceof LoopInterface) { // manual type check to support legacy PHP < 7.1
throw new \InvalidArgumentException('Argument #3 ($loop) expected null|React\EventLoop\LoopInterface');
}

$this->loop = $loop ?: Loop::get();
if ($browser === null) {
$browser = new Browser(null, $this->loop);
Expand Down
12 changes: 12 additions & 0 deletions tests/EventSourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ public function testConstructorThrowsIfUriArgumentIncludesInvalidScheme()
new EventSource('ftp://example.com');
}

public function testCtorThrowsForInvalidBrowser()
{
$this->setExpectedException('InvalidArgumentException', 'Argument #2 ($browser) expected null|React\Http\Browser');
new EventSource('http://example.com', 'browser');
}

public function testCtorThrowsForInvalidLoop()
{
$this->setExpectedException('InvalidArgumentException', 'Argument #3 ($loop) expected null|React\EventLoop\LoopInterface');
new EventSource('http://example.com', null, 'loop');
}

public function testConstructWithoutBrowserAndLoopAssignsBrowserAndLoopAutomatically()
{
$es = new EventSource('http://example.com');
Expand Down

0 comments on commit 5c22d81

Please sign in to comment.