diff --git a/composer.json b/composer.json index 5ff904e..e6d17e1 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,7 @@ "react/dns": "^1.13", "react/event-loop": "^1.2", "react/promise": "^3.2 || ^2.6 || ^1.2.1", - "react/stream": "^1.4" + "react/stream": "3.x-dev as 1.666.666" }, "require-dev": { "phpunit/phpunit": "^9.6 || ^7.5", diff --git a/src/Connection.php b/src/Connection.php index 6bc8deb..7707e8e 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -78,42 +78,42 @@ public function __construct($resource, LoopInterface $loop) $this->input->on('close', [$this, 'close']); } - public function isReadable() + public function isReadable(): bool { return $this->input->isReadable(); } - public function isWritable() + public function isWritable(): bool { return $this->input->isWritable(); } - public function pause() + public function pause(): void { $this->input->pause(); } - public function resume() + public function resume(): void { $this->input->resume(); } - public function pipe(WritableStreamInterface $dest, array $options = []) + public function pipe(WritableStreamInterface $dest, array $options = []): WritableStreamInterface { return $this->input->pipe($dest, $options); } - public function write($data) + public function write($data): bool { return $this->input->write($data); } - public function end($data = null) + public function end($data = null): void { $this->input->end($data); } - public function close() + public function close(): void { $this->input->close(); $this->handleClose(); @@ -132,7 +132,7 @@ public function handleClose() @\stream_socket_shutdown($this->stream, \STREAM_SHUT_RDWR); } - public function getRemoteAddress() + public function getRemoteAddress(): ?string { if (!\is_resource($this->stream)) { return null; @@ -141,7 +141,7 @@ public function getRemoteAddress() return $this->parseAddress(\stream_socket_get_name($this->stream, true)); } - public function getLocalAddress() + public function getLocalAddress(): ?string { if (!\is_resource($this->stream)) { return null; @@ -150,7 +150,7 @@ public function getLocalAddress() return $this->parseAddress(\stream_socket_get_name($this->stream, false)); } - private function parseAddress($address) + private function parseAddress($address): ?string { if ($address === false) { return null; diff --git a/src/ConnectionInterface.php b/src/ConnectionInterface.php index 64613b5..04e842e 100644 --- a/src/ConnectionInterface.php +++ b/src/ConnectionInterface.php @@ -82,7 +82,7 @@ interface ConnectionInterface extends DuplexStreamInterface * * @return ?string remote address (URI) or null if unknown */ - public function getRemoteAddress(); + public function getRemoteAddress(): ?string; /** * Returns the full local address (full URI with scheme, IP and port) where this connection has been established with @@ -115,5 +115,5 @@ public function getRemoteAddress(); * @return ?string local address (URI) or null if unknown * @see self::getRemoteAddress() */ - public function getLocalAddress(); + public function getLocalAddress(): ?string; } diff --git a/src/Connector.php b/src/Connector.php index 8a5e994..4c7b97a 100644 --- a/src/Connector.php +++ b/src/Connector.php @@ -6,6 +6,7 @@ use React\Dns\Resolver\Factory as DnsFactory; use React\Dns\Resolver\ResolverInterface; use React\EventLoop\LoopInterface; +use React\Promise\PromiseInterface; use function React\Promise\reject; /** @@ -146,7 +147,7 @@ public function __construct(array $context = [], ?LoopInterface $loop = null) } } - public function connect($uri) + public function connect($uri): PromiseInterface { $scheme = 'tcp'; if (\strpos($uri, '://') !== false) { @@ -173,7 +174,7 @@ public function connect($uri) * @return string * @internal */ - public static function uri(array $parts, $host, $ip) + public static function uri(array $parts, $host, $ip): string { $uri = ''; diff --git a/src/ConnectorInterface.php b/src/ConnectorInterface.php index 1f07b75..687d43c 100644 --- a/src/ConnectorInterface.php +++ b/src/ConnectorInterface.php @@ -2,6 +2,8 @@ namespace React\Socket; +use React\Promise\PromiseInterface; + /** * The `ConnectorInterface` is responsible for providing an interface for * establishing streaming connections, such as a normal TCP/IP connection. @@ -55,5 +57,5 @@ interface ConnectorInterface * Resolves with a `ConnectionInterface` on success or rejects with an `Exception` on error. * @see ConnectionInterface */ - public function connect($uri); + public function connect($uri): PromiseInterface; } diff --git a/src/DnsConnector.php b/src/DnsConnector.php index 4a8d1a0..aa883ce 100644 --- a/src/DnsConnector.php +++ b/src/DnsConnector.php @@ -18,7 +18,7 @@ public function __construct(ConnectorInterface $connector, ResolverInterface $re $this->resolver = $resolver; } - public function connect($uri) + public function connect($uri): PromiseInterface { $original = $uri; if (\strpos($uri, '://') === false) { diff --git a/src/FdServer.php b/src/FdServer.php index b00681c..cada924 100644 --- a/src/FdServer.php +++ b/src/FdServer.php @@ -146,7 +146,7 @@ public function __construct($fd, ?LoopInterface $loop = null) $this->resume(); } - public function getAddress() + public function getAddress(): ?string { if (!\is_resource($this->master)) { return null; @@ -167,7 +167,7 @@ public function getAddress() return 'tcp://' . $address; } - public function pause() + public function pause(): void { if (!$this->listening) { return; @@ -177,7 +177,7 @@ public function pause() $this->listening = false; } - public function resume() + public function resume(): void { if ($this->listening || !\is_resource($this->master)) { return; @@ -195,7 +195,7 @@ public function resume() $this->listening = true; } - public function close() + public function close(): void { if (!\is_resource($this->master)) { return; @@ -207,7 +207,7 @@ public function close() } /** @internal */ - public function handleConnection($socket) + public function handleConnection($socket): void { $connection = new Connection($socket, $this->loop); $connection->unix = $this->unix; diff --git a/src/FixedUriConnector.php b/src/FixedUriConnector.php index f83241d..7d4f998 100644 --- a/src/FixedUriConnector.php +++ b/src/FixedUriConnector.php @@ -2,6 +2,8 @@ namespace React\Socket; +use React\Promise\PromiseInterface; + /** * Decorates an existing Connector to always use a fixed, preconfigured URI * @@ -34,7 +36,7 @@ public function __construct($uri, ConnectorInterface $connector) $this->connector = $connector; } - public function connect($_) + public function connect($_): PromiseInterface { return $this->connector->connect($this->uri); } diff --git a/src/HappyEyeBallsConnectionBuilder.php b/src/HappyEyeBallsConnectionBuilder.php index 57a94aa..beefd4f 100644 --- a/src/HappyEyeBallsConnectionBuilder.php +++ b/src/HappyEyeBallsConnectionBuilder.php @@ -54,7 +54,7 @@ final class HappyEyeBallsConnectionBuilder public $lastError6; public $lastError4; - public function __construct(LoopInterface $loop, ConnectorInterface $connector, ResolverInterface $resolver, $uri, $host, $parts) + public function __construct(LoopInterface $loop, ConnectorInterface $connector, ResolverInterface $resolver, string $uri, string $host, array $parts) { $this->loop = $loop; $this->connector = $connector; @@ -64,7 +64,7 @@ public function __construct(LoopInterface $loop, ConnectorInterface $connector, $this->parts = $parts; } - public function connect() + public function connect(): PromiseInterface { return new Promise(function ($resolve, $reject) { $lookupResolve = function ($type) use ($resolve, $reject) { @@ -123,7 +123,7 @@ public function connect() * always resolves with a list of IP addresses on success or an empty * list on error. */ - public function resolve($type, $reject) + public function resolve(int $type, callable $reject): PromiseInterface { return $this->resolver->resolveAll($this->host, $type)->then(null, function (\Exception $e) use ($type, $reject) { unset($this->resolverPromises[$type]); @@ -159,7 +159,7 @@ public function resolve($type, $reject) /** * @internal */ - public function check($resolve, $reject) + public function check(callable $resolve, callable $reject) { $ip = \array_shift($this->connectQueue); @@ -168,13 +168,13 @@ public function check($resolve, $reject) \end($this->connectionPromises); $index = \key($this->connectionPromises); - $this->connectionPromises[$index]->then(function ($connection) use ($index, $resolve) { + $this->connectionPromises[$index]->then(function ($connection) use ($index, $resolve): void { unset($this->connectionPromises[$index]); $this->cleanUp(); $resolve($connection); - }, function (\Exception $e) use ($index, $ip, $resolve, $reject) { + }, function (\Exception $e) use ($index, $ip, $resolve, $reject): void { unset($this->connectionPromises[$index]); $this->failureCount++; @@ -228,8 +228,9 @@ public function check($resolve, $reject) /** * @internal + * @return PromiseInterface */ - public function attemptConnection($ip) + public function attemptConnection($ip): PromiseInterface { $uri = Connector::uri($this->parts, $this->host, $ip); @@ -239,7 +240,7 @@ public function attemptConnection($ip) /** * @internal */ - public function cleanUp() + public function cleanUp(): void { // clear list of outstanding IPs to avoid creating new connections $this->connectQueue = []; @@ -267,7 +268,7 @@ public function cleanUp() /** * @internal */ - public function hasBeenResolved() + public function hasBeenResolved(): bool { foreach ($this->resolved as $typeHasBeenResolved) { if ($typeHasBeenResolved === false) { @@ -286,6 +287,8 @@ public function hasBeenResolved() * @link https://tools.ietf.org/html/rfc8305#section-4 * * @internal + * + * @param array $ips */ public function mixIpsIntoConnectQueue(array $ips) { @@ -307,7 +310,7 @@ public function mixIpsIntoConnectQueue(array $ips) * @internal * @return string */ - public function error() + public function error(): string { if ($this->lastError4 === $this->lastError6) { $message = $this->lastError6; diff --git a/src/HappyEyeBallsConnector.php b/src/HappyEyeBallsConnector.php index 89ec203..777f6e7 100644 --- a/src/HappyEyeBallsConnector.php +++ b/src/HappyEyeBallsConnector.php @@ -5,6 +5,7 @@ use React\Dns\Resolver\ResolverInterface; use React\EventLoop\Loop; use React\EventLoop\LoopInterface; +use React\Promise\PromiseInterface; use function React\Promise\reject; final class HappyEyeBallsConnector implements ConnectorInterface @@ -20,7 +21,7 @@ public function __construct(?LoopInterface $loop, ConnectorInterface $connector, $this->resolver = $resolver; } - public function connect($uri) + public function connect($uri): PromiseInterface { $original = $uri; if (\strpos($uri, '://') === false) { diff --git a/src/LimitingServer.php b/src/LimitingServer.php index 4742e25..de1922a 100644 --- a/src/LimitingServer.php +++ b/src/LimitingServer.php @@ -113,17 +113,17 @@ public function __construct(ServerInterface $server, $connectionLimit, $pauseOnL * * @return ConnectionInterface[] */ - public function getConnections() + public function getConnections(): array { return $this->connections; } - public function getAddress() + public function getAddress(): ?string { return $this->server->getAddress(); } - public function pause() + public function pause(): void { if (!$this->manuPaused) { $this->manuPaused = true; @@ -134,7 +134,7 @@ public function pause() } } - public function resume() + public function resume(): void { if ($this->manuPaused) { $this->manuPaused = false; @@ -145,13 +145,13 @@ public function resume() } } - public function close() + public function close(): void { $this->server->close(); } /** @internal */ - public function handleConnection(ConnectionInterface $connection) + public function handleConnection(ConnectionInterface $connection): void { // close connection if limit exceeded if ($this->limit !== null && \count($this->connections) >= $this->limit) { @@ -178,7 +178,7 @@ public function handleConnection(ConnectionInterface $connection) } /** @internal */ - public function handleDisconnection(ConnectionInterface $connection) + public function handleDisconnection(ConnectionInterface $connection): void { unset($this->connections[\array_search($connection, $this->connections)]); @@ -193,7 +193,7 @@ public function handleDisconnection(ConnectionInterface $connection) } /** @internal */ - public function handleError(\Exception $error) + public function handleError(\Exception $error): void { $this->emit('error', [$error]); } diff --git a/src/SecureConnector.php b/src/SecureConnector.php index 7626b0a..9574f1b 100644 --- a/src/SecureConnector.php +++ b/src/SecureConnector.php @@ -5,6 +5,7 @@ use React\EventLoop\Loop; use React\EventLoop\LoopInterface; use React\Promise\Promise; +use React\Promise\PromiseInterface; use function React\Promise\reject; final class SecureConnector implements ConnectorInterface @@ -20,7 +21,7 @@ public function __construct(ConnectorInterface $connector, ?LoopInterface $loop $this->context = $context; } - public function connect($uri) + public function connect($uri): PromiseInterface { if (\strpos($uri, '://') === false) { $uri = 'tls://' . $uri; diff --git a/src/SecureServer.php b/src/SecureServer.php index 7ef5d94..04f7f0f 100644 --- a/src/SecureServer.php +++ b/src/SecureServer.php @@ -138,7 +138,7 @@ public function __construct(ServerInterface $tcp, ?LoopInterface $loop = null, a }); } - public function getAddress() + public function getAddress(): ?string { $address = $this->tcp->getAddress(); if ($address === null) { @@ -148,23 +148,23 @@ public function getAddress() return \str_replace('tcp://' , 'tls://', $address); } - public function pause() + public function pause(): void { $this->tcp->pause(); } - public function resume() + public function resume(): void { $this->tcp->resume(); } - public function close() + public function close(): void { - return $this->tcp->close(); + $this->tcp->close(); } /** @internal */ - public function handleConnection(ConnectionInterface $connection) + public function handleConnection(ConnectionInterface $connection): void { if (!$connection instanceof Connection) { $this->emit('error', [new \UnexpectedValueException('Base server does not use internal Connection class exposing stream resource')]); diff --git a/src/ServerInterface.php b/src/ServerInterface.php index aa79fa1..56511c2 100644 --- a/src/ServerInterface.php +++ b/src/ServerInterface.php @@ -75,7 +75,7 @@ interface ServerInterface extends EventEmitterInterface * * @return ?string the full listening address (URI) or NULL if it is unknown (not applicable to this server socket or already closed) */ - public function getAddress(); + public function getAddress(): ?string; /** * Pauses accepting new incoming connections. @@ -114,7 +114,7 @@ public function getAddress(); * @see self::resume() * @return void */ - public function pause(); + public function pause(): void; /** * Resumes accepting new incoming connections. @@ -136,7 +136,7 @@ public function pause(); * @see self::pause() * @return void */ - public function resume(); + public function resume(): void; /** * Shuts down this listening socket @@ -147,5 +147,5 @@ public function resume(); * * @return void */ - public function close(); + public function close(): void; } diff --git a/src/SocketServer.php b/src/SocketServer.php index 2106ff3..7d9720f 100644 --- a/src/SocketServer.php +++ b/src/SocketServer.php @@ -75,22 +75,22 @@ public function __construct($uri, array $context = [], ?LoopInterface $loop = nu }); } - public function getAddress() + public function getAddress(): ?string { return $this->server->getAddress(); } - public function pause() + public function pause(): void { $this->server->pause(); } - public function resume() + public function resume(): void { $this->server->resume(); } - public function close() + public function close(): void { $this->server->close(); } @@ -144,7 +144,7 @@ public static function accept($socket) * @copyright Copyright (c) 2023 Christian Lück, taken from https://github.com/clue/errno with permission * @codeCoverageIgnore */ - public static function errno($errstr) + public static function errno($errstr): int { // PHP defines the required `strerror()` function through either `ext-sockets`, `ext-posix` or `ext-pcntl` $strerror = \function_exists('socket_strerror') ? 'socket_strerror' : (\function_exists('posix_strerror') ? 'posix_strerror' : (\function_exists('pcntl_strerror') ? 'pcntl_strerror' : null)); @@ -193,7 +193,7 @@ public static function errno($errstr) * @copyright Copyright (c) 2023 Christian Lück, taken from https://github.com/clue/errno with permission * @codeCoverageIgnore */ - public static function errconst($errno) + public static function errconst($errno): string { // PHP defines most useful errno constants like `ECONNREFUSED` through constants in `ext-sockets` like `SOCKET_ECONNREFUSED` // PHP also defines a hand full of errno constants like `EMFILE` through constants in `ext-pcntl` like `PCNTL_EMFILE` diff --git a/src/StreamEncryption.php b/src/StreamEncryption.php index b03b79b..e3d3c79 100644 --- a/src/StreamEncryption.php +++ b/src/StreamEncryption.php @@ -4,6 +4,7 @@ use React\EventLoop\LoopInterface; use React\Promise\Deferred; +use React\Promise\PromiseInterface; /** * This class is considered internal and its API should not be relied upon @@ -45,7 +46,7 @@ public function __construct(LoopInterface $loop, $server = true) * @param Connection $stream * @return \React\Promise\PromiseInterface */ - public function enable(Connection $stream) + public function enable(Connection $stream): PromiseInterface { return $this->toggle($stream, true); } @@ -55,7 +56,7 @@ public function enable(Connection $stream) * @param bool $toggle * @return \React\Promise\PromiseInterface */ - public function toggle(Connection $stream, $toggle) + public function toggle(Connection $stream, bool $toggle): PromiseInterface { // pause actual stream instance to continue operation on raw stream socket $stream->pause(); @@ -106,10 +107,10 @@ public function toggle(Connection $stream, $toggle) * @param int $method * @return void */ - public function toggleCrypto($socket, Deferred $deferred, $toggle, $method) + public function toggleCrypto($socket, Deferred $deferred, $toggle, $method): void { $error = null; - \set_error_handler(function ($_, $errstr) use (&$error) { + \set_error_handler(function ($_, string $errstr) use (&$error) { $error = \str_replace(["\r", "\n"], ' ', $errstr); // remove useless function name from error message diff --git a/src/TcpConnector.php b/src/TcpConnector.php index 0949184..59f4e26 100644 --- a/src/TcpConnector.php +++ b/src/TcpConnector.php @@ -5,6 +5,7 @@ use React\EventLoop\Loop; use React\EventLoop\LoopInterface; use React\Promise\Promise; +use React\Promise\PromiseInterface; use function React\Promise\reject; final class TcpConnector implements ConnectorInterface @@ -18,7 +19,7 @@ public function __construct(?LoopInterface $loop = null, array $context = []) $this->context = $context; } - public function connect($uri) + public function connect($uri): PromiseInterface { if (\strpos($uri, '://') === false) { $uri = 'tcp://' . $uri; diff --git a/src/TcpServer.php b/src/TcpServer.php index a49ca9d..a7c91c0 100644 --- a/src/TcpServer.php +++ b/src/TcpServer.php @@ -189,7 +189,7 @@ public function __construct($uri, ?LoopInterface $loop = null, array $context = $this->resume(); } - public function getAddress() + public function getAddress(): ?string { if (!\is_resource($this->master)) { return null; @@ -206,7 +206,7 @@ public function getAddress() return 'tcp://' . $address; } - public function pause() + public function pause(): void { if (!$this->listening) { return; @@ -216,7 +216,7 @@ public function pause() $this->listening = false; } - public function resume() + public function resume(): void { if ($this->listening || !\is_resource($this->master)) { return; @@ -234,7 +234,7 @@ public function resume() $this->listening = true; } - public function close() + public function close(): void { if (!\is_resource($this->master)) { return; @@ -246,7 +246,7 @@ public function close() } /** @internal */ - public function handleConnection($socket) + public function handleConnection($socket): void { $this->emit('connection', [ new Connection($socket, $this->loop) diff --git a/src/TimeoutConnector.php b/src/TimeoutConnector.php index 5031a0b..4ee2c55 100644 --- a/src/TimeoutConnector.php +++ b/src/TimeoutConnector.php @@ -5,6 +5,7 @@ use React\EventLoop\Loop; use React\EventLoop\LoopInterface; use React\Promise\Promise; +use React\Promise\PromiseInterface; final class TimeoutConnector implements ConnectorInterface { @@ -19,7 +20,7 @@ public function __construct(ConnectorInterface $connector, $timeout, ?LoopInterf $this->loop = $loop ?? Loop::get(); } - public function connect($uri) + public function connect($uri): PromiseInterface { $promise = $this->connector->connect($uri); diff --git a/src/UnixConnector.php b/src/UnixConnector.php index ecc6262..4fec34e 100644 --- a/src/UnixConnector.php +++ b/src/UnixConnector.php @@ -4,6 +4,7 @@ use React\EventLoop\Loop; use React\EventLoop\LoopInterface; +use React\Promise\PromiseInterface; use function React\Promise\reject; use function React\Promise\resolve; @@ -22,7 +23,7 @@ public function __construct(?LoopInterface $loop = null) $this->loop = $loop ?? Loop::get(); } - public function connect($path) + public function connect($path): PromiseInterface { if (\strpos($path, '://') === false) { $path = 'unix://' . $path; diff --git a/src/UnixServer.php b/src/UnixServer.php index 8b4e416..ce2d054 100644 --- a/src/UnixServer.php +++ b/src/UnixServer.php @@ -93,7 +93,7 @@ public function __construct($path, ?LoopInterface $loop = null, array $context = $this->resume(); } - public function getAddress() + public function getAddress(): ?string { if (!\is_resource($this->master)) { return null; @@ -102,7 +102,7 @@ public function getAddress() return 'unix://' . \stream_socket_get_name($this->master, false); } - public function pause() + public function pause(): void { if (!$this->listening) { return; @@ -112,7 +112,7 @@ public function pause() $this->listening = false; } - public function resume() + public function resume(): void { if ($this->listening || !is_resource($this->master)) { return; @@ -130,7 +130,7 @@ public function resume() $this->listening = true; } - public function close() + public function close(): void { if (!\is_resource($this->master)) { return; @@ -142,7 +142,7 @@ public function close() } /** @internal */ - public function handleConnection($socket) + public function handleConnection($socket): void { $connection = new Connection($socket, $this->loop); $connection->unix = true;