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 Nov 23, 2024
1 parent 29f9d70 commit 6ed90a0
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 6ed90a0

Please sign in to comment.