Skip to content

Commit 420d882

Browse files
committed
PHP 8.2 fix
1 parent e5a1ec0 commit 420d882

6 files changed

+74
-53
lines changed

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"require": {
2525
"php": "^7.4 | ^8.0",
2626
"phrity/net-uri": "^1.0",
27+
"phrity/util-errorhandler": "^1.0",
2728
"psr/log": "^1.0 | ^2.0 | ^3.0",
2829
"psr/http-message": "^1.0"
2930
},

lib/Client.php

+26-25
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99

1010
namespace WebSocket;
1111

12+
use ErrorException;
1213
use InvalidArgumentException;
1314
use Phrity\Net\Uri;
15+
use Phrity\Util\ErrorHandler;
1416
use Psr\Http\Message\UriInterface;
1517
use Psr\Log\{
1618
LoggerAwareInterface,
@@ -330,36 +332,35 @@ protected function connect(): void
330332
$persistent = $this->options['persistent'] === true;
331333
$flags = STREAM_CLIENT_CONNECT;
332334
$flags = $persistent ? $flags | STREAM_CLIENT_PERSISTENT : $flags;
333-
334-
$error = $errno = $errstr = null;
335-
set_error_handler(function (int $severity, string $message, string $file, int $line) use (&$error) {
336-
$this->logger->warning($message, ['severity' => $severity]);
337-
$error = $message;
338-
}, E_ALL);
339-
340-
// Open the socket.
341-
$socket = stream_socket_client(
342-
$host_uri,
343-
$errno,
344-
$errstr,
345-
$this->options['timeout'],
346-
$flags,
347-
$context
348-
);
349-
350-
restore_error_handler();
351-
352-
if (!$socket) {
353-
$error = "Could not open socket to \"{$host_uri->getAuthority()}\": {$errstr} ({$errno}) {$error}.";
354-
$this->logger->error($error);
355-
throw new ConnectionException($error);
335+
$socket = null;
336+
337+
try {
338+
$handler = new ErrorHandler();
339+
$socket = $handler->with(function () use ($host_uri, $flags, $context) {
340+
$error = $errno = $errstr = null;
341+
// Open the socket.
342+
return stream_socket_client(
343+
$host_uri,
344+
$errno,
345+
$errstr,
346+
$this->options['timeout'],
347+
$flags,
348+
$context
349+
);
350+
});
351+
if (!$socket) {
352+
throw new ErrorException('No socket');
353+
}
354+
} catch (ErrorException $e) {
355+
$error = "Could not open socket to \"{$host_uri->getAuthority()}\": {$e->getMessage()} ({$e->getCode()}).";
356+
$this->logger->error($error, ['severity' => $e->getSeverity()]);
357+
throw new ConnectionException($error, 0, [], $e);
356358
}
357359

358360
$this->connection = new Connection($socket, $this->options);
359361
$this->connection->setLogger($this->logger);
360-
361362
if (!$this->isConnected()) {
362-
$error = "Invalid stream on \"{$host_uri->getAuthority()}\": {$errstr} ({$errno}) {$error}.";
363+
$error = "Invalid stream on \"{$host_uri->getAuthority()}\".";
363364
$this->logger->error($error);
364365
throw new ConnectionException($error);
365366
}

lib/Server.php

+19-16
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
namespace WebSocket;
1111

1212
use Closure;
13+
use ErrorException;
14+
use Phrity\Util\ErrorHandler;
1315
use Psr\Log\{
1416
LoggerAwareInterface,
1517
LoggerAwareTrait,
@@ -389,22 +391,23 @@ public function getPier(): ?string
389391
// Connect when read/write operation is performed.
390392
private function connect(): void
391393
{
392-
$error = null;
393-
set_error_handler(function (int $severity, string $message, string $file, int $line) use (&$error) {
394-
$this->logger->warning($message, ['severity' => $severity]);
395-
$error = $message;
396-
}, E_ALL);
397-
398-
if (isset($this->options['timeout'])) {
399-
$socket = stream_socket_accept($this->listening, $this->options['timeout']);
400-
} else {
401-
$socket = stream_socket_accept($this->listening);
402-
}
403-
404-
restore_error_handler();
405-
406-
if (!$socket) {
407-
throw new ConnectionException("Server failed to connect. {$error}");
394+
try {
395+
$handler = new ErrorHandler();
396+
$socket = $handler->with(function () {
397+
if (isset($this->options['timeout'])) {
398+
$socket = stream_socket_accept($this->listening, $this->options['timeout']);
399+
} else {
400+
$socket = stream_socket_accept($this->listening);
401+
}
402+
if (!$socket) {
403+
throw new ErrorException('No socket');
404+
}
405+
return $socket;
406+
});
407+
} catch (ErrorException $e) {
408+
$error = "Server failed to connect. {$e->getMessage()}";
409+
$this->logger->error($error, ['severity' => $e->getSeverity()]);
410+
throw new ConnectionException($error, 0, [], $e);
408411
}
409412

410413
$connection = new Connection($socket, $this->options);

phpunit.xml.dist

+1-10
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
bootstrap="tests/bootstrap.php"
5-
colors="true"
6-
convertDeprecationsToExceptions="true"
7-
convertErrorsToExceptions="true"
8-
convertNoticesToExceptions="true"
9-
convertWarningsToExceptions="true"
10-
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
11-
>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="tests/bootstrap.php" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
123
<coverage>
134
<include>
145
<directory suffix=".php">lib/</directory>

tests/ClientTest.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99

1010
namespace WebSocket;
1111

12+
use ErrorException;
1213
use Phrity\Net\Uri;
14+
use Phrity\Util\ErrorHandler;
1315
use PHPUnit\Framework\TestCase;
1416

1517
class ClientTest extends TestCase
@@ -516,7 +518,13 @@ public function testUnconnectedClient(): void
516518
public function testDeprecated(): void
517519
{
518520
$client = new Client('ws://localhost:8000/my/mock/path');
519-
$this->expectDeprecation();
520-
$this->assertNull($client->getPier());
521+
(new ErrorHandler())->with(function () use ($client) {
522+
$this->assertNull($client->getPier());
523+
}, function (ErrorException $e) {
524+
$this->assertEquals(
525+
'getPier() is deprecated and will be removed in future version. Use getRemoteName() instead.',
526+
$e->getMessage()
527+
);
528+
}, E_USER_DEPRECATED);
521529
}
522530
}

tests/ServerTest.php

+17
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
namespace WebSocket;
1111

12+
use ErrorException;
13+
use Phrity\Util\ErrorHandler;
1214
use PHPUnit\Framework\TestCase;
1315

1416
class ServerTest extends TestCase
@@ -491,4 +493,19 @@ public function testServerDisconnect(): void
491493
$this->assertFalse($server->isConnected());
492494
$this->assertTrue(MockSocket::isEmpty());
493495
}
496+
497+
public function testDeprecated(): void
498+
{
499+
MockSocket::initialize('server.construct', $this);
500+
$server = new Server();
501+
$this->assertTrue(MockSocket::isEmpty());
502+
(new ErrorHandler())->with(function () use ($server) {
503+
$this->assertNull($server->getPier());
504+
}, function (ErrorException $e) {
505+
$this->assertEquals(
506+
'getPier() is deprecated and will be removed in future version. Use getRemoteName() instead.',
507+
$e->getMessage()
508+
);
509+
}, E_USER_DEPRECATED);
510+
}
494511
}

0 commit comments

Comments
 (0)