Эх сурвалжийг харах

Added missing types and @throws, removed global function imports (pollutes code).

Ako Tulu 2 жил өмнө
parent
commit
f5dd7cc600

+ 6 - 28
src/Connection/AsyncTcpConnection.php

@@ -22,28 +22,6 @@ use stdClass;
 use Throwable;
 use Workerman\Timer;
 use Workerman\Worker;
-use function class_exists;
-use function explode;
-use function function_exists;
-use function is_resource;
-use function method_exists;
-use function microtime;
-use function parse_url;
-use function socket_import_stream;
-use function socket_set_option;
-use function stream_context_create;
-use function stream_set_blocking;
-use function stream_set_read_buffer;
-use function stream_socket_client;
-use function stream_socket_get_name;
-use function ucfirst;
-use const DIRECTORY_SEPARATOR;
-use const PHP_INT_MAX;
-use const SO_KEEPALIVE;
-use const SOL_SOCKET;
-use const SOL_TCP;
-use const STREAM_CLIENT_ASYNC_CONNECT;
-use const TCP_NODELAY;
 
 /**
  * AsyncTcpConnection.
@@ -210,7 +188,7 @@ class AsyncTcpConnection extends TcpConnection
      * @return void
      * @throws Throwable
      */
-    public function reconnect(int $after = 0)
+    public function reconnect(int $after = 0): void
     {
         $this->status = self::STATUS_INITIAL;
         static::$connections[$this->realId] = $this;
@@ -230,7 +208,7 @@ class AsyncTcpConnection extends TcpConnection
      * @return void
      * @throws Throwable
      */
-    public function connect()
+    public function connect(): void
     {
         if ($this->status !== self::STATUS_INITIAL && $this->status !== self::STATUS_CLOSING &&
             $this->status !== self::STATUS_CLOSED) {
@@ -305,7 +283,7 @@ class AsyncTcpConnection extends TcpConnection
      * @return void
      * @throws Throwable
      */
-    protected function emitError(int $code, mixed $msg)
+    protected function emitError(int $code, mixed $msg): void
     {
         $this->status = self::STATUS_CLOSING;
         if ($this->onError) {
@@ -320,7 +298,7 @@ class AsyncTcpConnection extends TcpConnection
     /**
      * CancelReconnect.
      */
-    public function cancelReconnect()
+    public function cancelReconnect(): void
     {
         if ($this->reconnectTimer) {
             Timer::del($this->reconnectTimer);
@@ -349,12 +327,12 @@ class AsyncTcpConnection extends TcpConnection
     }
 
     /**
-     * Check connection is successfully established or faild.
+     * Check connection is successfully established or failed.
      *
      * @return void
      * @throws Throwable
      */
-    public function checkConnection()
+    public function checkConnection(): void
     {
         // Remove EV_EXPECT for windows.
         if (DIRECTORY_SEPARATOR === '\\' && method_exists($this->eventLoop, 'offExcept')) {

+ 4 - 16
src/Connection/AsyncUdpConnection.php

@@ -20,18 +20,6 @@ use Exception;
 use Throwable;
 use Workerman\Protocols\ProtocolInterface;
 use Workerman\Worker;
-use function class_exists;
-use function explode;
-use function fclose;
-use function stream_context_create;
-use function stream_set_blocking;
-use function stream_socket_client;
-use function stream_socket_recvfrom;
-use function stream_socket_sendto;
-use function strlen;
-use function substr;
-use function ucfirst;
-use const STREAM_CLIENT_CONNECT;
 
 /**
  * AsyncUdpConnection.
@@ -99,7 +87,7 @@ class AsyncUdpConnection extends UdpConnection
      * @return void
      * @throws Throwable
      */
-    public function baseRead($socket)
+    public function baseRead($socket): void
     {
         $recvBuffer = stream_socket_recvfrom($socket, static::MAX_UDP_PACKAGE_SIZE, 0, $remoteAddress);
         if (false === $recvBuffer || empty($remoteAddress)) {
@@ -129,7 +117,7 @@ class AsyncUdpConnection extends UdpConnection
      * @return void
      * @throws Throwable
      */
-    public function close(mixed $data = null, bool $raw = false)
+    public function close(mixed $data = null, bool $raw = false): void
     {
         if ($data !== null) {
             $this->send($data, $raw);
@@ -169,7 +157,7 @@ class AsyncUdpConnection extends UdpConnection
         if ($this->connected === false) {
             $this->connect();
         }
-        return strlen($sendBuffer) === stream_socket_sendto($this->socket, $sendBuffer, 0);
+        return strlen($sendBuffer) === stream_socket_sendto($this->socket, $sendBuffer);
     }
 
     /**
@@ -178,7 +166,7 @@ class AsyncUdpConnection extends UdpConnection
      * @return void
      * @throws Throwable
      */
-    public function connect()
+    public function connect(): void
     {
         if ($this->connected === true) {
             return;

+ 3 - 2
src/Connection/ConnectionInterface.php

@@ -147,9 +147,10 @@ abstract class ConnectionInterface
      * Close connection.
      *
      * @param mixed|null $data
+     * @param bool $raw
      * @return void
      */
-    abstract public function close(mixed $data = null, bool $raw = false);
+    abstract public function close(mixed $data = null, bool $raw = false): void;
 
     /**
      * Is ipv4.
@@ -170,7 +171,7 @@ abstract class ConnectionInterface
      * @return void
      * @throws Throwable
      */
-    public function error(Throwable $exception)
+    public function error(Throwable $exception): void
     {
         if (!$this->errorHandler) {
             Worker::stopAll(250, $exception);

+ 10 - 38
src/Connection/TcpConnection.php

@@ -23,34 +23,6 @@ use Workerman\Events\EventInterface;
 use Workerman\Protocols\Http\Request;
 use Workerman\Protocols\ProtocolInterface;
 use Workerman\Worker;
-use function ceil;
-use function count;
-use function fclose;
-use function feof;
-use function fread;
-use function function_exists;
-use function fwrite;
-use function is_object;
-use function is_resource;
-use function key;
-use function method_exists;
-use function posix_getpid;
-use function restore_error_handler;
-use function set_error_handler;
-use function stream_set_blocking;
-use function stream_set_read_buffer;
-use function stream_socket_enable_crypto;
-use function stream_socket_get_name;
-use function strlen;
-use function strrchr;
-use function strrpos;
-use function substr;
-use function var_export;
-use const PHP_INT_MAX;
-use const STREAM_CRYPTO_METHOD_SSLv23_CLIENT;
-use const STREAM_CRYPTO_METHOD_SSLv23_SERVER;
-use const STREAM_CRYPTO_METHOD_SSLv2_CLIENT;
-use const STREAM_CRYPTO_METHOD_SSLv2_SERVER;
 
 /**
  * TcpConnection.
@@ -193,7 +165,7 @@ class TcpConnection extends ConnectionInterface implements JsonSerializable
 
     /**
      * Sets the maximum send buffer size for the current connection.
-     * OnBufferFull callback will be emited When send buffer is full.
+     * OnBufferFull callback will be emitted When send buffer is full.
      *
      * @var int
      */
@@ -567,7 +539,7 @@ class TcpConnection extends ConnectionInterface implements JsonSerializable
      *
      * @return void
      */
-    public function pauseRecv()
+    public function pauseRecv(): void
     {
         $this->eventLoop->offReadable($this->socket);
         $this->isPaused = true;
@@ -579,7 +551,7 @@ class TcpConnection extends ConnectionInterface implements JsonSerializable
      * @return void
      * @throws Throwable
      */
-    public function resumeRecv()
+    public function resumeRecv(): void
     {
         if ($this->isPaused === true) {
             $this->eventLoop->onReadable($this->socket, [$this, 'baseRead']);
@@ -597,7 +569,7 @@ class TcpConnection extends ConnectionInterface implements JsonSerializable
      * @return void
      * @throws Throwable
      */
-    public function baseRead($socket, bool $checkEof = true)
+    public function baseRead($socket, bool $checkEof = true): void
     {
         static $requests = [];
         // SSL handshake.
@@ -737,7 +709,7 @@ class TcpConnection extends ConnectionInterface implements JsonSerializable
      * @return void
      * @throws Throwable
      */
-    public function baseWrite()
+    public function baseWrite(): void
     {
         $len = 0;
         try {
@@ -833,7 +805,7 @@ class TcpConnection extends ConnectionInterface implements JsonSerializable
      * @param self $dest
      * @return void
      */
-    public function pipe(self $dest)
+    public function pipe(self $dest): void
     {
         $source = $this;
         $this->onMessage = function ($source, $data) use ($dest) {
@@ -856,7 +828,7 @@ class TcpConnection extends ConnectionInterface implements JsonSerializable
      * @param int $length
      * @return void
      */
-    public function consumeRecvBuffer(int $length)
+    public function consumeRecvBuffer(int $length): void
     {
         $this->recvBuffer = substr($this->recvBuffer, $length);
     }
@@ -869,7 +841,7 @@ class TcpConnection extends ConnectionInterface implements JsonSerializable
      * @return void
      * @throws Throwable
      */
-    public function close(mixed $data = null, bool $raw = false)
+    public function close(mixed $data = null, bool $raw = false): void
     {
         if ($this->status === self::STATUS_CONNECTING) {
             $this->destroy();
@@ -937,7 +909,7 @@ class TcpConnection extends ConnectionInterface implements JsonSerializable
      * @return void
      * @throws Throwable
      */
-    protected function checkBufferWillFull()
+    protected function checkBufferWillFull(): void
     {
         if ($this->maxSendBufferSize <= strlen($this->sendBuffer)) {
             if ($this->onBufferFull) {
@@ -988,7 +960,7 @@ class TcpConnection extends ConnectionInterface implements JsonSerializable
      * @return void
      * @throws Throwable
      */
-    public function destroy()
+    public function destroy(): void
     {
         // Avoid repeated calls.
         if ($this->status === self::STATUS_CLOSED) {

+ 7 - 9
src/Connection/UdpConnection.php

@@ -20,13 +20,6 @@ use JetBrains\PhpStorm\ArrayShape;
 use JetBrains\PhpStorm\Pure;
 use JsonSerializable;
 use Workerman\Protocols\ProtocolInterface;
-use function stream_socket_get_name;
-use function stream_socket_sendto;
-use function strlen;
-use function strrchr;
-use function strrpos;
-use function substr;
-use function trim;
 
 /**
  * UdpConnection.
@@ -175,9 +168,10 @@ class UdpConnection extends ConnectionInterface implements JsonSerializable
      * Close connection.
      *
      * @param mixed|null $data
+     * @param bool $raw
      * @return void
      */
-    public function close(mixed $data = null, bool $raw = false)
+    public function close(mixed $data = null, bool $raw = false): void
     {
         if ($data !== null) {
             $this->send($data, $raw);
@@ -228,7 +222,11 @@ class UdpConnection extends ConnectionInterface implements JsonSerializable
      *
      * @return array
      */
-    #[ArrayShape(['transport' => "string", 'getRemoteIp' => "string", 'remotePort' => "int", 'getRemoteAddress' => "string", 'getLocalIp' => "string", 'getLocalPort' => "int", 'getLocalAddress' => "string", 'isIpV4' => "bool", 'isIpV6' => "bool"])] public function jsonSerialize(): array
+    #[ArrayShape([
+        'transport' => "string", 'getRemoteIp' => "string", 'remotePort' => "int", 'getRemoteAddress' => "string",
+        'getLocalIp' => "string", 'getLocalPort' => "int", 'getLocalAddress' => "string", 'isIpV4' => "bool", 'isIpV6' => "bool"]
+    )]
+    public function jsonSerialize(): array
     {
         return [
             'transport' => $this->transport,

+ 7 - 8
src/Events/Ev.php

@@ -18,7 +18,6 @@ namespace Workerman\Events;
 use EvIo;
 use EvSignal;
 use EvTimer;
-use function count;
 
 /**
  * Ev eventloop
@@ -115,7 +114,7 @@ class Ev implements EventInterface
     /**
      * {@inheritdoc}
      */
-    public function onReadable($stream, callable $func)
+    public function onReadable($stream, callable $func): void
     {
         $fdKey = (int)$stream;
         $event = new EvIo($stream, \Ev::READ, function () use ($func, $stream) {
@@ -141,7 +140,7 @@ class Ev implements EventInterface
     /**
      * {@inheritdoc}
      */
-    public function onWritable($stream, callable $func)
+    public function onWritable($stream, callable $func): void
     {
         $fdKey = (int)$stream;
         $event = new EvIo($stream, \Ev::WRITE, function () use ($func, $stream) {
@@ -167,7 +166,7 @@ class Ev implements EventInterface
     /**
      * {@inheritdoc}
      */
-    public function onSignal(int $signal, callable $func)
+    public function onSignal(int $signal, callable $func): void
     {
         $event = new EvSignal($signal, function () use ($func, $signal) {
             $func($signal);
@@ -191,7 +190,7 @@ class Ev implements EventInterface
     /**
      * {@inheritdoc}
      */
-    public function deleteAllTimer()
+    public function deleteAllTimer(): void
     {
         foreach ($this->eventTimer as $event) {
             $event->stop();
@@ -202,7 +201,7 @@ class Ev implements EventInterface
     /**
      * {@inheritdoc}
      */
-    public function run()
+    public function run(): void
     {
         \Ev::run();
     }
@@ -210,7 +209,7 @@ class Ev implements EventInterface
     /**
      * {@inheritdoc}
      */
-    public function stop()
+    public function stop(): void
     {
         \Ev::stop();
     }
@@ -226,7 +225,7 @@ class Ev implements EventInterface
     /**
      * {@inheritdoc}
      */
-    public function setErrorHandler(callable $errorHandler)
+    public function setErrorHandler(callable $errorHandler): void
     {
         $this->errorHandler = $errorHandler;
     }

+ 12 - 13
src/Events/Event.php

@@ -17,9 +17,8 @@ declare(strict_types=1);
 namespace Workerman\Events;
 
 use EventBase;
+use RuntimeException;
 use Throwable;
-use function class_exists;
-use function count;
 
 /**
  * libevent eventloop
@@ -30,7 +29,7 @@ class Event implements EventInterface
      * Event base.
      * @var EventBase
      */
-    protected $eventBase;
+    protected EventBase $eventBase;
 
     /**
      * All listeners for read event.
@@ -109,7 +108,7 @@ class Event implements EventInterface
             }
         });
         if (!$event->addTimer($delay)) {
-            throw new \RuntimeException("Event::addTimer($delay) failed");
+            throw new RuntimeException("Event::addTimer($delay) failed");
         }
         $this->eventTimer[$timerId] = $event;
         return $timerId;
@@ -151,7 +150,7 @@ class Event implements EventInterface
             }
         });
         if (!$event->addTimer($interval)) {
-            throw new \RuntimeException("Event::addTimer($interval) failed");
+            throw new RuntimeException("Event::addTimer($interval) failed");
         }
         $this->eventTimer[$timerId] = $event;
         return $timerId;
@@ -160,7 +159,7 @@ class Event implements EventInterface
     /**
      * {@inheritdoc}
      */
-    public function onReadable($stream, callable $func)
+    public function onReadable($stream, callable $func): void
     {
         $className = $this->eventClassName;
         $fdKey = (int)$stream;
@@ -188,7 +187,7 @@ class Event implements EventInterface
     /**
      * {@inheritdoc}
      */
-    public function onWritable($stream, callable $func)
+    public function onWritable($stream, callable $func): void
     {
         $className = $this->eventClassName;
         $fdKey = (int)$stream;
@@ -216,7 +215,7 @@ class Event implements EventInterface
     /**
      * {@inheritdoc}
      */
-    public function onSignal(int $signal, callable $func)
+    public function onSignal(int $signal, callable $func): void
     {
         $className = $this->eventClassName;
         $fdKey = $signal;
@@ -244,7 +243,7 @@ class Event implements EventInterface
     /**
      * {@inheritdoc}
      */
-    public function deleteAllTimer()
+    public function deleteAllTimer(): void
     {
         foreach ($this->eventTimer as $event) {
             $event->del();
@@ -255,7 +254,7 @@ class Event implements EventInterface
     /**
      * {@inheritdoc}
      */
-    public function run()
+    public function run(): void
     {
         $this->eventBase->loop();
     }
@@ -263,7 +262,7 @@ class Event implements EventInterface
     /**
      * {@inheritdoc}
      */
-    public function stop()
+    public function stop(): void
     {
         $this->eventBase->exit();
     }
@@ -279,7 +278,7 @@ class Event implements EventInterface
     /**
      * {@inheritdoc}
      */
-    public function setErrorHandler($errorHandler)
+    public function setErrorHandler(callable $errorHandler): void
     {
         $this->errorHandler = $errorHandler;
     }
@@ -297,7 +296,7 @@ class Event implements EventInterface
      * @return void
      * @throws Throwable
      */
-    public function error(Throwable $e)
+    public function error(Throwable $e): void
     {
         try {
             if (!$this->errorHandler) {

+ 7 - 7
src/Events/EventInterface.php

@@ -55,7 +55,7 @@ interface EventInterface
      * @param callable $func
      * @return void
      */
-    public function onReadable($stream, callable $func);
+    public function onReadable($stream, callable $func): void;
 
     /**
      * Cancel a callback of stream readable.
@@ -70,7 +70,7 @@ interface EventInterface
      * @param callable $func
      * @return void
      */
-    public function onWritable($stream, callable $func);
+    public function onWritable($stream, callable $func): void;
 
     /**
      * Cancel a callback of stream writable.
@@ -86,7 +86,7 @@ interface EventInterface
      * @return void
      * @throws Throwable
      */
-    public function onSignal(int $signal, callable $func);
+    public function onSignal(int $signal, callable $func): void;
 
     /**
      * Cancel a callback of signal.
@@ -99,20 +99,20 @@ interface EventInterface
      * Delete all timer.
      * @return void
      */
-    public function deleteAllTimer();
+    public function deleteAllTimer(): void;
 
     /**
      * Run the event loop.
      * @return void
      * @throws Throwable
      */
-    public function run();
+    public function run(): void;
 
     /**
      * Stop event loop.
      * @return void
      */
-    public function stop();
+    public function stop(): void;
 
     /**
      * Get Timer count.
@@ -125,7 +125,7 @@ interface EventInterface
      * @param callable $errorHandler
      * @return void
      */
-    public function setErrorHandler(callable $errorHandler);
+    public function setErrorHandler(callable $errorHandler): void;
 
     /**
      * Get error handler

+ 7 - 10
src/Events/Revolt.php

@@ -18,9 +18,6 @@ namespace Workerman\Events;
 
 use Revolt\EventLoop;
 use Revolt\EventLoop\Driver;
-use function count;
-use function function_exists;
-use function pcntl_signal;
 
 /**
  * Revolt eventloop
@@ -83,7 +80,7 @@ class Revolt implements EventInterface
     /**
      * {@inheritdoc}
      */
-    public function run()
+    public function run(): void
     {
         $this->driver->run();
     }
@@ -91,7 +88,7 @@ class Revolt implements EventInterface
     /**
      * {@inheritdoc}
      */
-    public function stop()
+    public function stop(): void
     {
         foreach ($this->eventSignal as $cbId) {
             $this->driver->cancel($cbId);
@@ -134,7 +131,7 @@ class Revolt implements EventInterface
     /**
      * {@inheritdoc}
      */
-    public function onReadable($stream, callable $func)
+    public function onReadable($stream, callable $func): void
     {
         $fdKey = (int)$stream;
         if (isset($this->readEvents[$fdKey])) {
@@ -164,7 +161,7 @@ class Revolt implements EventInterface
     /**
      * {@inheritdoc}
      */
-    public function onWritable($stream, callable $func)
+    public function onWritable($stream, callable $func): void
     {
         $fdKey = (int)$stream;
         if (isset($this->writeEvents[$fdKey])) {
@@ -193,7 +190,7 @@ class Revolt implements EventInterface
     /**
      * {@inheritdoc}
      */
-    public function onSignal(int $signal, callable $func)
+    public function onSignal(int $signal, callable $func): void
     {
         $fdKey = $signal;
         if (isset($this->eventSignal[$fdKey])) {
@@ -243,7 +240,7 @@ class Revolt implements EventInterface
     /**
      * {@inheritdoc}
      */
-    public function deleteAllTimer()
+    public function deleteAllTimer(): void
     {
         foreach ($this->eventTimer as $cbId) {
             $this->driver->cancel($cbId);
@@ -262,7 +259,7 @@ class Revolt implements EventInterface
     /**
      * {@inheritdoc}
      */
-    public function setErrorHandler(callable $errorHandler)
+    public function setErrorHandler(callable $errorHandler): void
     {
         $this->driver->setErrorHandler($errorHandler);
     }

+ 15 - 11
src/Events/Select.php

@@ -185,7 +185,7 @@ class Select implements EventInterface
     /**
      * {@inheritdoc}
      */
-    public function onReadable($stream, callable $func)
+    public function onReadable($stream, callable $func): void
     {
         $count = count($this->readFds);
         if ($count >= 1024) {
@@ -214,7 +214,7 @@ class Select implements EventInterface
     /**
      * {@inheritdoc}
      */
-    public function onWritable($stream, callable $func)
+    public function onWritable($stream, callable $func): void
     {
         $count = count($this->writeFds);
         if ($count >= 1024) {
@@ -242,8 +242,10 @@ class Select implements EventInterface
 
     /**
      * On except.
+     * @param resource $stream
+     * @param $func
      */
-    public function onExcept($stream, $func)
+    public function onExcept($stream, $func): void
     {
         $fdKey = (int)$stream;
         $this->exceptEvents[$fdKey] = $func;
@@ -252,6 +254,8 @@ class Select implements EventInterface
 
     /**
      * Off except.
+     * @param resource $stream
+     * @return bool
      */
     public function offExcept($stream): bool
     {
@@ -266,7 +270,7 @@ class Select implements EventInterface
     /**
      * {@inheritdoc}
      */
-    public function onSignal(int $signal, callable $func)
+    public function onSignal(int $signal, callable $func): void
     {
         if (!function_exists('pcntl_signal')) {
             return;
@@ -296,7 +300,7 @@ class Select implements EventInterface
      *
      * @param int $signal
      */
-    public function signalHandler(int $signal)
+    public function signalHandler(int $signal): void
     {
         $this->signalEvents[$signal]($signal);
     }
@@ -307,7 +311,7 @@ class Select implements EventInterface
      * @return void
      * @throws Throwable
      */
-    protected function tick()
+    protected function tick(): void
     {
         $tasksToInsert = [];
         while (!$this->scheduler->isEmpty()) {
@@ -357,7 +361,7 @@ class Select implements EventInterface
     /**
      * {@inheritdoc}
      */
-    public function deleteAllTimer()
+    public function deleteAllTimer(): void
     {
         $this->scheduler = new SplPriorityQueue();
         $this->scheduler->setExtractFlags(SplPriorityQueue::EXTR_BOTH);
@@ -367,7 +371,7 @@ class Select implements EventInterface
     /**
      * {@inheritdoc}
      */
-    public function run()
+    public function run(): void
     {
         while ($this->running) {
             $read = $this->readFds;
@@ -418,7 +422,7 @@ class Select implements EventInterface
     /**
      * {@inheritdoc}
      */
-    public function stop()
+    public function stop(): void
     {
         $this->running = false;
         $this->deleteAllTimer();
@@ -440,7 +444,7 @@ class Select implements EventInterface
     /**
      * {@inheritdoc}
      */
-    public function setErrorHandler(callable $errorHandler)
+    public function setErrorHandler(callable $errorHandler): void
     {
         $this->errorHandler = $errorHandler;
     }
@@ -458,7 +462,7 @@ class Select implements EventInterface
      * @return void
      * @throws Throwable
      */
-    public function error(Throwable $e)
+    public function error(Throwable $e): void
     {
         if (!$this->errorHandler) {
             throw new $e;

+ 11 - 13
src/Events/Swoole.php

@@ -19,10 +19,6 @@ use Swoole\Event;
 use Swoole\Process;
 use Swoole\Timer;
 use Throwable;
-use function count;
-use function posix_kill;
-use const SWOOLE_EVENT_READ;
-use const SWOOLE_EVENT_WRITE;
 
 class Swoole implements EventInterface
 {
@@ -61,6 +57,7 @@ class Swoole implements EventInterface
 
     /**
      * {@inheritdoc}
+     * @throws Throwable
      */
     public function delay(float $delay, callable $func, array $args = []): int
     {
@@ -101,6 +98,7 @@ class Swoole implements EventInterface
 
     /**
      * {@inheritdoc}
+     * @throws Throwable
      */
     public function repeat(float $interval, callable $func, array $args = []): int
     {
@@ -120,7 +118,7 @@ class Swoole implements EventInterface
     /**
      * {@inheritdoc}
      */
-    public function onReadable($stream, callable $func)
+    public function onReadable($stream, callable $func): void
     {
         $fd = (int)$stream;
         if (!isset($this->readEvents[$fd]) && !isset($this->writeEvents[$fd])) {
@@ -156,7 +154,7 @@ class Swoole implements EventInterface
     /**
      * {@inheritdoc}
      */
-    public function onWritable($stream, callable $func)
+    public function onWritable($stream, callable $func): void
     {
         $fd = (int)$stream;
         if (!isset($this->readEvents[$fd]) && !isset($this->writeEvents[$fd])) {
@@ -192,9 +190,9 @@ class Swoole implements EventInterface
     /**
      * {@inheritdoc}
      */
-    public function onSignal(int $signal, callable $func)
+    public function onSignal(int $signal, callable $func): void
     {
-        return Process::signal($signal, $func);
+        Process::signal($signal, $func);
     }
 
     /**
@@ -209,7 +207,7 @@ class Swoole implements EventInterface
     /**
      * {@inheritdoc}
      */
-    public function deleteAllTimer()
+    public function deleteAllTimer(): void
     {
         foreach ($this->eventTimer as $timerId) {
             Timer::clear($timerId);
@@ -219,7 +217,7 @@ class Swoole implements EventInterface
     /**
      * {@inheritdoc}
      */
-    public function run()
+    public function run(): void
     {
         Event::wait();
     }
@@ -229,7 +227,7 @@ class Swoole implements EventInterface
      *
      * @return void
      */
-    public function stop()
+    public function stop(): void
     {
         Event::exit();
         posix_kill(posix_getpid(), SIGINT);
@@ -248,7 +246,7 @@ class Swoole implements EventInterface
     /**
      * {@inheritdoc}
      */
-    public function setErrorHandler(callable $errorHandler)
+    public function setErrorHandler(callable $errorHandler): void
     {
         $this->errorHandler = $errorHandler;
     }
@@ -266,7 +264,7 @@ class Swoole implements EventInterface
      * @return void
      * @throws Throwable
      */
-    public function error(Throwable $e)
+    public function error(Throwable $e): void
     {
         if (!$this->errorHandler) {
             throw new $e;

+ 10 - 18
src/Events/Swow.php

@@ -9,16 +9,6 @@ use Swow\Coroutine;
 use Swow\Signal;
 use Swow\SignalException;
 use Throwable;
-use function count;
-use function is_resource;
-use function max;
-use function msleep;
-use function stream_poll_one;
-use function Swow\Sync\waitAll;
-use const STREAM_POLLHUP;
-use const STREAM_POLLIN;
-use const STREAM_POLLNONE;
-use const STREAM_POLLOUT;
 
 class Swow implements EventInterface
 {
@@ -63,6 +53,7 @@ class Swow implements EventInterface
 
     /**
      * {@inheritdoc}
+     * @throws Throwable
      */
     public function delay(float $delay, callable $func, array $args = []): int
     {
@@ -85,6 +76,7 @@ class Swow implements EventInterface
 
     /**
      * {@inheritdoc}
+     * @throws Throwable
      */
     public function repeat(float $interval, callable $func, array $args = []): int
     {
@@ -133,7 +125,7 @@ class Swow implements EventInterface
     /**
      * {@inheritdoc}
      */
-    public function deleteAllTimer()
+    public function deleteAllTimer(): void
     {
         foreach ($this->eventTimer as $timerId) {
             $this->offDelay($timerId);
@@ -143,7 +135,7 @@ class Swow implements EventInterface
     /**
      * {@inheritdoc}
      */
-    public function onReadable($stream, callable $func)
+    public function onReadable($stream, callable $func): void
     {
         $fd = (int)$stream;
         if (isset($this->readEvents[$fd])) {
@@ -192,7 +184,7 @@ class Swow implements EventInterface
     /**
      * {@inheritdoc}
      */
-    public function onWritable($stream, callable $func)
+    public function onWritable($stream, callable $func): void
     {
         $fd = (int)$stream;
         if (isset($this->writeEvents[$fd])) {
@@ -236,7 +228,7 @@ class Swow implements EventInterface
     /**
      * {@inheritdoc}
      */
-    public function onSignal(int $signal, callable $func)
+    public function onSignal(int $signal, callable $func): void
     {
         Coroutine::run(function () use ($signal, $func): void {
             $this->signalListener[$signal] = Coroutine::getCurrent();
@@ -269,7 +261,7 @@ class Swow implements EventInterface
     /**
      * {@inheritdoc}
      */
-    public function run()
+    public function run(): void
     {
         waitAll();
     }
@@ -279,7 +271,7 @@ class Swow implements EventInterface
      *
      * @return void
      */
-    public function stop()
+    public function stop(): void
     {
         Coroutine::killAll();
     }
@@ -287,7 +279,7 @@ class Swow implements EventInterface
     /**
      * {@inheritdoc}
      */
-    public function setErrorHandler(callable $errorHandler)
+    public function setErrorHandler(callable $errorHandler): void
     {
         $this->errorHandler = $errorHandler;
     }
@@ -305,7 +297,7 @@ class Swow implements EventInterface
      * @return void
      * @throws Throwable
      */
-    public function error(Throwable $e)
+    public function error(Throwable $e): void
     {
         if (!$this->errorHandler) {
             throw new $e;

+ 0 - 5
src/Protocols/Frame.php

@@ -16,11 +16,6 @@ declare(strict_types=1);
 
 namespace Workerman\Protocols;
 
-use function pack;
-use function strlen;
-use function substr;
-use function unpack;
-
 /**
  * Frame Protocol.
  */

+ 2 - 21
src/Protocols/Http.php

@@ -20,25 +20,6 @@ use Throwable;
 use Workerman\Connection\TcpConnection;
 use Workerman\Protocols\Http\Request;
 use Workerman\Protocols\Http\Response;
-use function clearstatcache;
-use function count;
-use function explode;
-use function filesize;
-use function fopen;
-use function fread;
-use function fseek;
-use function ftell;
-use function in_array;
-use function ini_get;
-use function is_array;
-use function is_object;
-use function key;
-use function preg_match;
-use function strlen;
-use function strpos;
-use function strstr;
-use function substr;
-use function sys_get_temp_dir;
 
 /**
  * Class Http.
@@ -271,7 +252,7 @@ class Http
      * @param int $length
      * @throws Throwable
      */
-    protected static function sendStream(TcpConnection $connection, $handler, int $offset = 0, int $length = 0)
+    protected static function sendStream(TcpConnection $connection, $handler, int $offset = 0, int $length = 0): void
     {
         $connection->context->bufferFull = false;
         $connection->context->streamSending = true;
@@ -293,7 +274,7 @@ class Http
                         $connection->onBufferDrain = null;
                         return;
                     }
-                    $size = $remainSize > $size ? $size : $remainSize;
+                    $size = min($remainSize, $size);
                 }
 
                 $buffer = fread($handler, $size);

+ 0 - 3
src/Protocols/Http/Chunk.php

@@ -16,9 +16,6 @@ declare(strict_types=1);
 
 namespace Workerman\Protocols\Http;
 
-use function dechex;
-use function strlen;
-
 /**
  * Class Chunk
  * @package Workerman\Protocols\Http

+ 0 - 24
src/Protocols/Http/Request.php

@@ -20,30 +20,6 @@ use Exception;
 use RuntimeException;
 use Workerman\Connection\TcpConnection;
 use Workerman\Protocols\Http;
-use function array_walk_recursive;
-use function bin2hex;
-use function clearstatcache;
-use function count;
-use function explode;
-use function file_put_contents;
-use function is_file;
-use function json_decode;
-use function ltrim;
-use function microtime;
-use function pack;
-use function parse_str;
-use function parse_url;
-use function preg_match;
-use function preg_replace;
-use function strlen;
-use function strpos;
-use function strstr;
-use function strtolower;
-use function substr;
-use function tempnam;
-use function trim;
-use function unlink;
-use function urlencode;
 
 /**
  * Class Request

+ 0 - 15
src/Protocols/Http/Response.php

@@ -16,21 +16,6 @@ declare(strict_types=1);
 
 namespace Workerman\Protocols\Http;
 
-use function array_merge_recursive;
-use function explode;
-use function file;
-use function filemtime;
-use function gmdate;
-use function is_array;
-use function is_file;
-use function pathinfo;
-use function preg_match;
-use function rawurlencode;
-use function strlen;
-use function substr;
-use const FILE_IGNORE_NEW_LINES;
-use const FILE_SKIP_EMPTY_LINES;
-
 /**
  * Class Response
  * @package Workerman\Protocols\Http

+ 0 - 2
src/Protocols/Http/ServerSentEvents.php

@@ -16,8 +16,6 @@ declare(strict_types=1);
 
 namespace Workerman\Protocols\Http;
 
-use function str_replace;
-
 /**
  * Class ServerSentEvents
  * @package Workerman\Protocols\Http

+ 10 - 19
src/Protocols/Http/Session.php

@@ -21,15 +21,6 @@ use JetBrains\PhpStorm\ArrayShape;
 use RuntimeException;
 use Workerman\Protocols\Http\Session\FileSessionHandler;
 use Workerman\Protocols\Http\Session\SessionHandlerInterface;
-use function array_key_exists;
-use function ini_get;
-use function is_array;
-use function is_scalar;
-use function preg_match;
-use function random_int;
-use function serialize;
-use function session_get_cookie_params;
-use function unserialize;
 
 /**
  * Class Session
@@ -194,7 +185,7 @@ class Session
      * @param string $name
      * @param mixed $value
      */
-    public function set(string $name, mixed $value)
+    public function set(string $name, mixed $value): void
     {
         $this->data[$name] = $value;
         $this->needSave = true;
@@ -205,7 +196,7 @@ class Session
      *
      * @param string $name
      */
-    public function delete(string $name)
+    public function delete(string $name): void
     {
         unset($this->data[$name]);
         $this->needSave = true;
@@ -231,7 +222,7 @@ class Session
      * @param array|string $key
      * @param mixed|null $value
      */
-    public function put(array|string $key, mixed $value = null)
+    public function put(array|string $key, mixed $value = null): void
     {
         if (!is_array($key)) {
             $this->set($key, $value);
@@ -249,7 +240,7 @@ class Session
      *
      * @param array|string $name
      */
-    public function forget(array|string $name)
+    public function forget(array|string $name): void
     {
         if (is_scalar($name)) {
             $this->delete($name);
@@ -278,7 +269,7 @@ class Session
      *
      * @return void
      */
-    public function flush()
+    public function flush(): void
     {
         $this->needSave = true;
         $this->data = [];
@@ -311,7 +302,7 @@ class Session
      *
      * @return void
      */
-    public function save()
+    public function save(): void
     {
         if ($this->needSave) {
             if (empty($this->data)) {
@@ -340,7 +331,7 @@ class Session
      *
      * @return void
      */
-    public static function init()
+    public static function init(): void
     {
         if (($gcProbability = (int)ini_get('session.gc_probability')) && ($gcDivisor = (int)ini_get('session.gc_divisor'))) {
             static::$gcProbability = [$gcProbability, $gcDivisor];
@@ -399,7 +390,7 @@ class Session
      *
      * @return void
      */
-    protected static function initHandler()
+    protected static function initHandler(): void
     {
         if (static::$handlerConfig === null) {
             static::$handler = new static::$handlerClass();
@@ -413,7 +404,7 @@ class Session
      *
      * @return void
      */
-    public function gc()
+    public function gc(): void
     {
         static::$handler->gc(static::$lifetime);
     }
@@ -437,7 +428,7 @@ class Session
      *
      * @param string $sessionId
      */
-    protected static function checkSessionId(string $sessionId)
+    protected static function checkSessionId(string $sessionId): void
     {
         if (!preg_match('/^[a-zA-Z0-9]+$/', $sessionId)) {
             throw new RuntimeException("session_id $sessionId is invalid");

+ 0 - 15
src/Protocols/Http/Session/FileSessionHandler.php

@@ -18,21 +18,6 @@ namespace Workerman\Protocols\Http\Session;
 
 use Exception;
 use Workerman\Protocols\Http\Session;
-use function clearstatcache;
-use function file_get_contents;
-use function file_put_contents;
-use function filemtime;
-use function glob;
-use function is_dir;
-use function is_file;
-use function mkdir;
-use function rename;
-use function session_save_path;
-use function strlen;
-use function sys_get_temp_dir;
-use function time;
-use function touch;
-use function unlink;
 
 /**
  * Class FileSessionHandler

+ 0 - 3
src/Protocols/Text.php

@@ -17,9 +17,6 @@ declare(strict_types=1);
 namespace Workerman\Protocols;
 
 use Workerman\Connection\ConnectionInterface;
-use function rtrim;
-use function strlen;
-use function strpos;
 
 /**
  * Text Protocol.

+ 0 - 15
src/Protocols/Websocket.php

@@ -22,21 +22,6 @@ use Workerman\Connection\ConnectionInterface;
 use Workerman\Connection\TcpConnection;
 use Workerman\Protocols\Http\Request;
 use Workerman\Worker;
-use function base64_encode;
-use function chr;
-use function floor;
-use function gettype;
-use function is_scalar;
-use function ord;
-use function pack;
-use function preg_match;
-use function sha1;
-use function str_repeat;
-use function stripos;
-use function strlen;
-use function strpos;
-use function substr;
-use function unpack;
 
 /**
  * WebSocket protocol.

+ 0 - 16
src/Protocols/Ws.php

@@ -22,22 +22,6 @@ use Workerman\Connection\AsyncTcpConnection;
 use Workerman\Connection\ConnectionInterface;
 use Workerman\Timer;
 use Workerman\Worker;
-use function base64_encode;
-use function bin2hex;
-use function floor;
-use function gettype;
-use function is_array;
-use function is_scalar;
-use function ord;
-use function pack;
-use function preg_match;
-use function sha1;
-use function str_repeat;
-use function strlen;
-use function strpos;
-use function substr;
-use function trim;
-use function unpack;
 
 /**
  * Websocket protocol for client.

+ 2 - 12
src/Timer.php

@@ -16,22 +16,12 @@ declare(strict_types=1);
 
 namespace Workerman;
 
-use Exception;
-use Revolt\EventLoop;
 use RuntimeException;
-use Swoole\Coroutine\System;
 use Throwable;
 use Workerman\Events\EventInterface;
 use Workerman\Events\Revolt;
 use Workerman\Events\Swoole;
 use Workerman\Events\Swow;
-use function function_exists;
-use function is_callable;
-use function pcntl_alarm;
-use function pcntl_signal;
-use function time;
-use const PHP_INT_MAX;
-use const SIGALRM;
 
 /**
  * Timer.
@@ -161,7 +151,7 @@ class Timer
         switch (Worker::$eventLoopClass) {
             // Fiber
             case Revolt::class:
-                $suspension = EventLoop::getSuspension();
+                $suspension = \Revolt\EventLoop::getSuspension();
                 static::add($delay, function () use ($suspension) {
                     $suspension->resume();
                 }, null, false);
@@ -169,7 +159,7 @@ class Timer
                 return;
             // Swoole
             case Swoole::class:
-                System::sleep($delay);
+                \Swoole\Coroutine\System::sleep($delay);
                 return;
             // Swow
             case Swow::class:

Файлын зөрүү хэтэрхий том тул дарагдсан байна
+ 196 - 197
src/Worker.php


Энэ ялгаанд хэт олон файл өөрчлөгдсөн тул зарим файлыг харуулаагүй болно