Skip to content

Commit

Permalink
Code cleanup and Psalm fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski committed Dec 16, 2023
1 parent 8fc4d47 commit 88ca6d2
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/Internal/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ function setupTls($socket, array $options, ?Cancellation $cancellation): void
$suspension = EventLoop::getSuspension();

// Watcher is guaranteed to be created, because we throw above if cancellation has already been requested
/** @psalm-suppress PossiblyUndefinedVariable $callbackId is defined below. */
$cancellationId = $cancellation->subscribe(static function ($e) use ($suspension, &$callbackId): void {
EventLoop::cancel($callbackId);

Expand Down
10 changes: 5 additions & 5 deletions src/ResourceServerSocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ final class ResourceServerSocket implements ServerSocket, ResourceStream
/** @var resource|null Stream socket server resource. */
private $socket;

private string $callbackId;
private readonly string $callbackId;

private readonly SocketAddress $address;

private ?Suspension $acceptor = null;

/** @var \Closure(CancelledException) */
private \Closure $cancel;
/** @var \Closure(CancelledException):void */
private readonly \Closure $cancel;

private readonly \Closure $errorHandler;

Expand Down Expand Up @@ -59,11 +59,11 @@ public function __construct(

$acceptor = &$this->acceptor;
$this->callbackId = EventLoop::onReadable($this->socket, static function () use (&$acceptor): void {
$acceptor->resume(true);
$acceptor?->resume(true);
$acceptor = null;
});

$callbackId = &$this->callbackId;
$callbackId = $this->callbackId;
$this->cancel = static function (CancelledException $exception) use (&$acceptor, $callbackId): void {
EventLoop::disable($callbackId);

Expand Down
14 changes: 7 additions & 7 deletions src/ResourceUdpSocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ final class ResourceUdpSocket implements UdpSocket, ResourceStream
/** @var resource|null UDP socket resource. */
private $socket;

private string $callbackId;
private readonly string $callbackId;

private InternetAddress $address;
private readonly InternetAddress $address;

private ?Suspension $reader = null;

/** @var \Closure(CancelledException) */
private \Closure $cancel;
/** @var \Closure(CancelledException):void */
private readonly \Closure $cancel;

private int $limit;

Expand Down Expand Up @@ -98,7 +98,7 @@ public function __construct($socket, int $limit = self::DEFAULT_LIMIT)
$reader = null;
});

$callbackId = &$this->callbackId;
$callbackId = $this->callbackId;
$this->cancel = static function (CancelledException $exception) use (&$reader, $callbackId): void {
EventLoop::disable($callbackId);

Expand Down Expand Up @@ -168,9 +168,9 @@ public function send(InternetAddress $address, string $data): void
throw new SocketException('The datagram socket is not writable');
}

try {
\set_error_handler($errorHandler);
\set_error_handler($errorHandler);

try {
$result = \stream_socket_sendto($this->socket, $data, 0, $address->toString());
/** @psalm-suppress TypeDoesNotContainType */
if ($result < 0 || $result === false) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use function Amp\async;
use function Amp\delay;

class ResourceDatagramSocketTest extends AsyncTestCase
class ResourceUdpSocketTest extends AsyncTestCase
{
public function testBindEndpointInvalidScheme(): void
{
Expand Down

0 comments on commit 88ca6d2

Please sign in to comment.