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

Merge pull request #830 from joanhey/test

Use Null Coalesce Operator
walkor 3 жил өмнө
parent
commit
0c7aa6e745

+ 1 - 1
src/Connection/AsyncTcpConnection.php

@@ -136,7 +136,7 @@ class AsyncTcpConnection extends TcpConnection
             $this->_remoteHost = $address_info['host'];
             $this->_remotePort = $address_info['port'];
             $this->_remoteURI = "{$address_info['path']}{$address_info['query']}";
-            $scheme = isset($address_info['scheme']) ? $address_info['scheme'] : 'tcp';
+            $scheme = $address_info['scheme'] ?? 'tcp';
             $this->_remoteAddress = 'unix' === strtolower($scheme)
                 ? substr($remote_address, strpos($remote_address, '/') + 2)
                 : $this->_remoteHost . ':' . $this->_remotePort;

+ 7 - 7
src/Protocols/Http/Request.php

@@ -98,7 +98,7 @@ class Request
         if (null === $name) {
             return $this->_data['get'];
         }
-        return isset($this->_data['get'][$name]) ? $this->_data['get'][$name] : $default;
+        return $this->_data['get'][$name] ?? $default;
     }
 
     /**
@@ -116,7 +116,7 @@ class Request
         if (null === $name) {
             return $this->_data['post'];
         }
-        return isset($this->_data['post'][$name]) ? $this->_data['post'][$name] : $default;
+        return $this->_data['post'][$name] ?? $default;
     }
 
     /**
@@ -135,7 +135,7 @@ class Request
             return $this->_data['headers'];
         }
         $name = \strtolower($name);
-        return isset($this->_data['headers'][$name]) ? $this->_data['headers'][$name] : $default;
+        return $this->_data['headers'][$name] ?? $default;
     }
 
     /**
@@ -154,7 +154,7 @@ class Request
         if ($name === null) {
             return $this->_data['cookie'];
         }
-        return isset($this->_data['cookie'][$name]) ? $this->_data['cookie'][$name] : $default;
+        return $this->_data['cookie'][$name] ?? $default;
     }
 
     /**
@@ -171,7 +171,7 @@ class Request
         if (null === $name) {
             return $this->_data['files'];
         }
-        return isset($this->_data['files'][$name]) ? $this->_data['files'][$name] : null;
+        return $this->_data['files'][$name] ?? null;
     }
 
     /**
@@ -372,7 +372,7 @@ class Request
         $first_line = \strstr($this->_buffer, "\r\n", true);
         $tmp = \explode(' ', $first_line, 3);
         $this->_data['method'] = $tmp[0];
-        $this->_data['uri'] = isset($tmp[1]) ? $tmp[1] : '/';
+        $this->_data['uri'] = $tmp[1] ?? '/';
     }
 
     /**
@@ -637,7 +637,7 @@ class Request
      */
     public function __get($name)
     {
-        return isset($this->properties[$name]) ? $this->properties[$name] : null;
+        return $this->properties[$name] ?? null;
     }
 
     /**

+ 4 - 6
src/Protocols/Http/Response.php

@@ -230,10 +230,8 @@ class Response
      */
     public function getHeader($name)
     {
-        if (!isset($this->_header[$name])) {
-            return null;
-        }
-        return $this->_header[$name];
+
+        return $this->_header[$name] ?? null;
     }
 
     /**
@@ -386,8 +384,8 @@ class Response
         }
 
         $file_info = \pathinfo($file);
-        $extension = isset($file_info['extension']) ? $file_info['extension'] : '';
-        $base_name = isset($file_info['basename']) ? $file_info['basename'] : 'unknown';
+        $extension = $file_info['extension'] ?? '';
+        $base_name = $file_info['basename'] ?? 'unknown';
         if (!isset($headers['Content-Type'])) {
             if (isset(self::$_mimeTypeMap[$extension])) {
                 $head .= "Content-Type: " . self::$_mimeTypeMap[$extension] . "\r\n";

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

@@ -171,7 +171,7 @@ class Session
      */
     public function get($name, $default = null)
     {
-        return isset($this->_data[$name]) ? $this->_data[$name] : $default;
+        return $this->_data[$name] ?? $default;
     }
 
     /**

+ 2 - 2
src/Protocols/Websocket.php

@@ -150,7 +150,7 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
                     if ($recv_len >= $current_frame_length) {
                         $ping_data = static::decode(\substr($buffer, 0, $current_frame_length), $connection);
                         $connection->consumeRecvBuffer($current_frame_length);
-                        $tmp_connection_type = isset($connection->websocketType) ? $connection->websocketType : static::BINARY_TYPE_BLOB;
+                        $tmp_connection_type = $connection->websocketType ?? static::BINARY_TYPE_BLOB;
                         $connection->websocketType = "\x8a";
                         $ping_cb = $connection->onWebSocketPing ?? $connection->worker->onWebSocketPing ?? false;
                         if ($ping_cb) {
@@ -172,7 +172,7 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
                     if ($recv_len >= $current_frame_length) {
                         $pong_data = static::decode(\substr($buffer, 0, $current_frame_length), $connection);
                         $connection->consumeRecvBuffer($current_frame_length);
-                        $tmp_connection_type = isset($connection->websocketType) ? $connection->websocketType : static::BINARY_TYPE_BLOB;
+                        $tmp_connection_type = $connection->websocketType ?? static::BINARY_TYPE_BLOB;
                         $connection->websocketType = "\x8a";
                         // Try to emit onWebSocketPong callback.
                         $pong_cb = $connection->onWebSocketPong ?? $connection->worker->onWebSocketPong ?? false;

+ 1 - 2
src/Protocols/Ws.php

@@ -347,8 +347,7 @@ class Ws
         $host = $port === 80 ? $connection->getRemoteHost() : $connection->getRemoteHost() . ':' . $port;
         // Handshake header.
         $connection->websocketSecKey = \base64_encode(random_bytes(16));
-        $user_header = isset($connection->headers) ? $connection->headers :
-            (isset($connection->wsHttpHeader) ? $connection->wsHttpHeader : null);
+        $user_header = $connection->headers ?? $connection->wsHttpHeader ?? null;
         $user_header_str = '';
         if (!empty($user_header)) {
             if (\is_array($user_header)) {