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

Merge pull request #975 from luzrain/code_improvements_1

Refactoring: type definition improvements
walkor 2 жил өмнө
parent
commit
246715ecf7

+ 4 - 4
src/Connection/AsyncUdpConnection.php

@@ -124,7 +124,7 @@ class AsyncUdpConnection extends UdpConnection
     /**
      * Close connection.
      *
-     * @param mixed|null $data
+     * @param mixed $data
      * @param bool $raw
      * @return void
      * @throws Throwable
@@ -153,17 +153,17 @@ class AsyncUdpConnection extends UdpConnection
      *
      * @param mixed $sendBuffer
      * @param bool $raw
-     * @return void|boolean
+     * @return bool|null
      * @throws Throwable
      */
-    public function send(mixed $sendBuffer, bool $raw = false)
+    public function send(mixed $sendBuffer, bool $raw = false): bool|null
     {
         if (false === $raw && $this->protocol) {
             /** @var ProtocolInterface $parser */
             $parser = $this->protocol;
             $sendBuffer = $parser::encode($sendBuffer, $this);
             if ($sendBuffer === '') {
-                return;
+                return null;
             }
         }
         if ($this->connected === false) {

+ 3 - 3
src/Connection/ConnectionInterface.php

@@ -98,9 +98,9 @@ abstract class ConnectionInterface
      *
      * @param mixed $sendBuffer
      * @param bool $raw
-     * @return void|boolean
+     * @return bool|null
      */
-    abstract public function send(mixed $sendBuffer, bool $raw = false);
+    abstract public function send(mixed $sendBuffer, bool $raw = false): bool|null;
 
     /**
      * Get remote IP.
@@ -147,7 +147,7 @@ abstract class ConnectionInterface
     /**
      * Close connection.
      *
-     * @param mixed|null $data
+     * @param mixed $data
      * @param bool $raw
      * @return void
      */

+ 8 - 7
src/Connection/TcpConnection.php

@@ -377,10 +377,10 @@ class TcpConnection extends ConnectionInterface implements JsonSerializable
      *
      * @param mixed $sendBuffer
      * @param bool $raw
-     * @return bool|void
+     * @return bool|null
      * @throws Throwable
      */
-    public function send(mixed $sendBuffer, bool $raw = false)
+    public function send(mixed $sendBuffer, bool $raw = false): bool|null
     {
         if ($this->status === self::STATUS_CLOSING || $this->status === self::STATUS_CLOSED) {
             return false;
@@ -396,7 +396,7 @@ class TcpConnection extends ConnectionInterface implements JsonSerializable
                 $this->error($e);
             }
             if ($sendBuffer === '') {
-                return;
+                return null;
             }
         }
 
@@ -409,7 +409,7 @@ class TcpConnection extends ConnectionInterface implements JsonSerializable
             }
             $this->sendBuffer .= $sendBuffer;
             $this->checkBufferWillFull();
-            return;
+            return null;
         }
 
         // Attempt to send data directly.
@@ -418,7 +418,7 @@ class TcpConnection extends ConnectionInterface implements JsonSerializable
                 $this->eventLoop->onWritable($this->socket, $this->baseWrite(...));
                 $this->sendBuffer = $sendBuffer;
                 $this->checkBufferWillFull();
-                return;
+                return null;
             }
             $len = 0;
             try {
@@ -454,7 +454,7 @@ class TcpConnection extends ConnectionInterface implements JsonSerializable
             $this->eventLoop->onWritable($this->socket, $this->baseWrite(...));
             // Check if send buffer will be full.
             $this->checkBufferWillFull();
-            return;
+            return null;
         }
 
         if ($this->bufferIsFull()) {
@@ -465,6 +465,7 @@ class TcpConnection extends ConnectionInterface implements JsonSerializable
         $this->sendBuffer .= $sendBuffer;
         // Check if send buffer is full.
         $this->checkBufferWillFull();
+        return null;
     }
 
     /**
@@ -871,7 +872,7 @@ class TcpConnection extends ConnectionInterface implements JsonSerializable
     /**
      * Close connection.
      *
-     * @param mixed|null $data
+     * @param mixed $data
      * @param bool $raw
      * @return void
      * @throws Throwable

+ 4 - 4
src/Connection/UdpConnection.php

@@ -76,16 +76,16 @@ class UdpConnection extends ConnectionInterface implements JsonSerializable
      *
      * @param mixed $sendBuffer
      * @param bool $raw
-     * @return void|boolean
+     * @return bool|null
      */
-    public function send(mixed $sendBuffer, bool $raw = false)
+    public function send(mixed $sendBuffer, bool $raw = false): bool|null
     {
         if (false === $raw && $this->protocol) {
             /** @var ProtocolInterface $parser */
             $parser = $this->protocol;
             $sendBuffer = $parser::encode($sendBuffer, $this);
             if ($sendBuffer === '') {
-                return;
+                return null;
             }
         }
         return strlen($sendBuffer) === stream_socket_sendto($this->socket, $sendBuffer, 0, $this->isIpV6() ? '[' . $this->getRemoteIp() . ']:' . $this->getRemotePort() : $this->remoteAddress);
@@ -172,7 +172,7 @@ class UdpConnection extends ConnectionInterface implements JsonSerializable
     /**
      * Close connection.
      *
-     * @param mixed|null $data
+     * @param mixed $data
      * @param bool $raw
      * @return void
      */

+ 3 - 2
src/Protocols/Http.php

@@ -70,12 +70,12 @@ class Http
     /**
      * Get or set the request class name.
      *
-     * @param string|null $className
+     * @param class-string|null $className
      * @return string
      */
     public static function requestClass(string $className = null): string
     {
-        if ($className) {
+        if ($className !== null) {
             static::$requestClass = $className;
         }
         return static::$requestClass;
@@ -202,6 +202,7 @@ class Http
             $request = $connection->request;
             $request->session = $request->connection = $connection->request = null;
         }
+
         if (!is_object($response)) {
             $extHeader = '';
             if ($connection->headers) {

+ 19 - 20
src/Protocols/Http/Request.php

@@ -97,7 +97,7 @@ class Request implements Stringable
      *
      * @var bool
      */
-    protected $isSafe = true;
+    protected bool $isSafe = true;
 
     /**
      * Enable cache.
@@ -109,7 +109,7 @@ class Request implements Stringable
     /**
      * Session id.
      *
-     * @var mixed|string
+     * @var mixed
      */
     protected mixed $sid;
 
@@ -127,7 +127,7 @@ class Request implements Stringable
      * Get query.
      *
      * @param string|null $name
-     * @param mixed|null $default
+     * @param mixed $default
      * @return mixed
      */
     public function get(string $name = null, mixed $default = null): mixed
@@ -145,7 +145,7 @@ class Request implements Stringable
      * Get post.
      *
      * @param string|null $name
-     * @param mixed|null $default
+     * @param mixed $default
      * @return mixed
      */
     public function post(string $name = null, mixed $default = null): mixed
@@ -163,7 +163,7 @@ class Request implements Stringable
      * Get header item by name.
      *
      * @param string|null $name
-     * @param mixed|null $default
+     * @param mixed $default
      * @return mixed
      */
     public function header(string $name = null, mixed $default = null): mixed
@@ -182,7 +182,7 @@ class Request implements Stringable
      * Get cookie item by name.
      *
      * @param string|null $name
-     * @param mixed|null $default
+     * @param mixed $default
      * @return mixed
      */
     public function cookie(string $name = null, mixed $default = null): mixed
@@ -203,7 +203,7 @@ class Request implements Stringable
      * @param string|null $name
      * @return array|null
      */
-    public function file(string $name = null)
+    public function file(string $name = null): array|null
     {
         if (!isset($this->data['files'])) {
             $this->parsePost();
@@ -569,14 +569,14 @@ class Request implements Stringable
     /**
      * Parse upload file.
      *
-     * @param $boundary
-     * @param $sectionStartOffset
-     * @param $postEncodeString
-     * @param $filesEncodeStr
-     * @param $files
+     * @param string $boundary
+     * @param int $sectionStartOffset
+     * @param string $postEncodeString
+     * @param string $filesEncodeStr
+     * @param array $files
      * @return int
      */
-    protected function parseUploadFile($boundary, $sectionStartOffset, &$postEncodeString, &$filesEncodeStr, &$files): int
+    protected function parseUploadFile(string $boundary, int $sectionStartOffset, string &$postEncodeString, string &$filesEncodeStr, array &$files): int
     {
         $file = [];
         $boundary = "\r\n$boundary";
@@ -700,7 +700,7 @@ class Request implements Stringable
      * @param mixed $value
      * @return void
      */
-    public function __set(string $name, mixed $value)
+    public function __set(string $name, mixed $value): void
     {
         $this->properties[$name] = $value;
     }
@@ -709,9 +709,9 @@ class Request implements Stringable
      * Getter.
      *
      * @param string $name
-     * @return mixed|null
+     * @return mixed
      */
-    public function __get(string $name)
+    public function __get(string $name): mixed
     {
         return $this->properties[$name] ?? null;
     }
@@ -722,7 +722,7 @@ class Request implements Stringable
      * @param string $name
      * @return bool
      */
-    public function __isset(string $name)
+    public function __isset(string $name): bool
     {
         return isset($this->properties[$name]);
     }
@@ -733,18 +733,17 @@ class Request implements Stringable
      * @param string $name
      * @return void
      */
-    public function __unset(string $name)
+    public function __unset(string $name): void
     {
         unset($this->properties[$name]);
     }
 
-
     /**
      * __wakeup.
      *
      * @return void
      */
-    public function __wakeup()
+    public function __wakeup(): void
     {
         $this->isSafe = false;
     }

+ 5 - 5
src/Protocols/Http/Session.php

@@ -186,7 +186,7 @@ class Session
      * Get session.
      *
      * @param string $name
-     * @param mixed|null $default
+     * @param mixed $default
      * @return mixed
      */
     public function get(string $name, mixed $default = null): mixed
@@ -221,7 +221,7 @@ class Session
      * Retrieve and delete an item from the session.
      *
      * @param string $name
-     * @param mixed|null $default
+     * @param mixed $default
      * @return mixed
      */
     public function pull(string $name, mixed $default = null): mixed
@@ -235,7 +235,7 @@ class Session
      * Store data in the session.
      *
      * @param array|string $key
-     * @param mixed|null $value
+     * @param mixed $value
      */
     public function put(array|string $key, mixed $value = null): void
     {
@@ -367,8 +367,8 @@ class Session
     /**
      * Set session handler class.
      *
-     * @param mixed|null $className
-     * @param mixed|null $config
+     * @param mixed $className
+     * @param mixed $config
      * @return string
      */
     public static function handlerClass(mixed $className = null, mixed $config = null): string

+ 1 - 1
src/Worker.php

@@ -1992,7 +1992,7 @@ class Worker
             }
 
             file_put_contents(static::$statisticsFile, serialize($allWorkerInfo) . "\n", FILE_APPEND);
-            $loadavg = function_exists('sys_getloadavg') ? array_map('round', sys_getloadavg(), [2, 2, 2]) : ['-', '-', '-'];
+            $loadavg = function_exists('sys_getloadavg') ? array_map(round(...), sys_getloadavg(), [2, 2, 2]) : ['-', '-', '-'];
             file_put_contents(static::$statisticsFile,
                 "----------------------------------------------GLOBAL STATUS----------------------------------------------------\n", FILE_APPEND);
             file_put_contents(static::$statisticsFile,