Browse Source

isset conditions replaced by null coalescing assignments

Anton 2 years ago
parent
commit
96cc3ad405

+ 2 - 6
src/Connection/AsyncTcpConnection.php

@@ -168,12 +168,8 @@ class AsyncTcpConnection extends TcpConnection
                 throw new RuntimeException('Bad remoteAddress');
             }
         } else {
-            if (!isset($addressInfo['port'])) {
-                $addressInfo['port'] = 0;
-            }
-            if (!isset($addressInfo['path'])) {
-                $addressInfo['path'] = '/';
-            }
+            $addressInfo['port'] ??= 0;
+            $addressInfo['path'] ??= '/';
             if (!isset($addressInfo['query'])) {
                 $addressInfo['query'] = '';
             } else {

+ 1 - 3
src/Connection/TcpConnection.php

@@ -1081,9 +1081,7 @@ class TcpConnection extends ConnectionInterface implements JsonSerializable
         static $mod;
         self::$statistics['connection_count']--;
         if (Worker::getGracefulStop()) {
-            if (!isset($mod)) {
-                $mod = ceil((self::$statistics['connection_count'] + 1) / 3);
-            }
+            $mod ??= ceil((self::$statistics['connection_count'] + 1) / 3);
 
             if (0 === self::$statistics['connection_count'] % $mod) {
                 $pid = function_exists('posix_getpid') ? posix_getpid() : 0;

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

@@ -275,10 +275,7 @@ class Request implements Stringable
      */
     public function path(): string
     {
-        if (!isset($this->data['path'])) {
-            $this->data['path'] = (string)parse_url($this->uri(), PHP_URL_PATH);
-        }
-        return $this->data['path'];
+        return $this->data['path'] ??= (string)parse_url($this->uri(), PHP_URL_PATH);
     }
 
     /**
@@ -288,10 +285,7 @@ class Request implements Stringable
      */
     public function queryString(): string
     {
-        if (!isset($this->data['query_string'])) {
-            $this->data['query_string'] = (string)parse_url($this->uri(), PHP_URL_QUERY);
-        }
-        return $this->data['query_string'];
+        return $this->data['query_string'] ??= (string)parse_url($this->uri(), PHP_URL_QUERY);
     }
 
     /**
@@ -302,10 +296,7 @@ class Request implements Stringable
      */
     public function session(): Session
     {
-        if ($this->session === null) {
-            $this->session = new Session($this->sessionId());
-        }
-        return $this->session;
+        return $this->session ??= new Session($this->sessionId());
     }
 
     /**
@@ -366,10 +357,7 @@ class Request implements Stringable
      */
     public function rawHead(): string
     {
-        if (!isset($this->data['head'])) {
-            $this->data['head'] = strstr($this->buffer, "\r\n\r\n", true);
-        }
-        return $this->data['head'];
+        return $this->data['head'] ??= strstr($this->buffer, "\r\n\r\n", true);
     }
 
     /**
@@ -623,9 +611,7 @@ class Request implements Stringable
                         $uploadKey = $fileName;
                         // Parse upload files.
                         $file = [...$file, 'name' => $match[2], 'tmp_name' => $tmpFile, 'size' => $size, 'error' => $error, 'full_path' => $match[2]];
-                        if (!isset($file['type'])) {
-                            $file['type'] = '';
-                        }
+                        $file['type'] ??= '';
                         break;
                     }
                     // Is post field.

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

@@ -59,12 +59,8 @@ class RedisSessionHandler implements SessionHandlerInterface
             throw new RuntimeException('Please install redis extension.');
         }
 
-        if (!isset($config['timeout'])) {
-            $config['timeout'] = 2;
-        }
-
+        $config['timeout'] ??= 2;
         $this->config = $config;
-
         $this->connect();
 
         Timer::add($config['ping'] ?? 55, function () {

+ 4 - 8
src/Worker.php

@@ -1738,10 +1738,8 @@ class Worker
                         }
 
                         // For Statistics.
-                        if (!isset(static::$globalStatistics['worker_exit_info'][$workerId][$status])) {
-                            static::$globalStatistics['worker_exit_info'][$workerId][$status] = 0;
-                        }
-                        ++static::$globalStatistics['worker_exit_info'][$workerId][$status];
+                        static::$globalStatistics['worker_exit_info'][$workerId][$status] ??= 0;
+                        static::$globalStatistics['worker_exit_info'][$workerId][$status]++;
 
                         // Clear process data.
                         unset(static::$pidMap[$workerId][$pid]);
@@ -2260,9 +2258,7 @@ class Worker
         // Context for socket.
         if ($socketName) {
             $this->socketName = $socketName;
-            if (!isset($socketContext['socket']['backlog'])) {
-                $socketContext['socket']['backlog'] = static::DEFAULT_BACKLOG;
-            }
+            $socketContext['socket']['backlog'] ??= static::DEFAULT_BACKLOG;
             $this->socketContext = stream_context_create($socketContext);
         }
 
@@ -2589,7 +2585,7 @@ class Worker
                 } else {
                     $messageCallback($connection, $recvBuffer);
                 }
-                ++ConnectionInterface::$statistics['total_request'];
+                ConnectionInterface::$statistics['total_request']++;
             } catch (Throwable $e) {
                 static::stopAll(250, $e);
             }