From c86fc33fe01cdfa8064e6495bfa06137465aabb4 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Wed, 19 Oct 2022 19:05:44 +0200 Subject: [PATCH] Use Null Coalesce Operator --- src/Connection/AsyncTcpConnection.php | 2 +- src/Protocols/Http/Request.php | 14 +++++++------- src/Protocols/Http/Response.php | 10 ++++------ src/Protocols/Http/Session.php | 2 +- src/Protocols/Websocket.php | 4 ++-- src/Protocols/Ws.php | 3 +-- 6 files changed, 16 insertions(+), 19 deletions(-) diff --git a/src/Connection/AsyncTcpConnection.php b/src/Connection/AsyncTcpConnection.php index d8f42e512..98c5ea4d0 100644 --- a/src/Connection/AsyncTcpConnection.php +++ b/src/Connection/AsyncTcpConnection.php @@ -136,7 +136,7 @@ public function __construct($remote_address, array $context_option = []) $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; diff --git a/src/Protocols/Http/Request.php b/src/Protocols/Http/Request.php index 9509b5f07..fa67753b3 100644 --- a/src/Protocols/Http/Request.php +++ b/src/Protocols/Http/Request.php @@ -98,7 +98,7 @@ public function get($name = null, $default = null) 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 @@ public function post($name = null, $default = null) 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 @@ public function header($name = null, $default = null) 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 @@ public function cookie($name = null, $default = null) 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 @@ public function file($name = null) 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 @@ protected function parseHeadFirstLine() $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 @@ public function __set($name, $value) */ public function __get($name) { - return isset($this->properties[$name]) ? $this->properties[$name] : null; + return $this->properties[$name] ?? null; } /** diff --git a/src/Protocols/Http/Response.php b/src/Protocols/Http/Response.php index 69686cc4a..f0e549b90 100644 --- a/src/Protocols/Http/Response.php +++ b/src/Protocols/Http/Response.php @@ -219,10 +219,8 @@ public function withoutHeader($name) */ public function getHeader($name) { - if (!isset($this->_header[$name])) { - return null; - } - return $this->_header[$name]; + + return $this->_header[$name] ?? null; } /** @@ -375,8 +373,8 @@ protected function createHeadForFile($file_info) } $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"; diff --git a/src/Protocols/Http/Session.php b/src/Protocols/Http/Session.php index 113e78ce8..24ca56a59 100644 --- a/src/Protocols/Http/Session.php +++ b/src/Protocols/Http/Session.php @@ -171,7 +171,7 @@ public function getId() */ public function get($name, $default = null) { - return isset($this->_data[$name]) ? $this->_data[$name] : $default; + return $this->_data[$name] ?? $default; } /** diff --git a/src/Protocols/Websocket.php b/src/Protocols/Websocket.php index 4e3402c8e..3e33d9d0d 100644 --- a/src/Protocols/Websocket.php +++ b/src/Protocols/Websocket.php @@ -150,7 +150,7 @@ public static function input($buffer, ConnectionInterface $connection) 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 @@ public static function input($buffer, ConnectionInterface $connection) 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; diff --git a/src/Protocols/Ws.php b/src/Protocols/Ws.php index 158765769..d1be0b884 100644 --- a/src/Protocols/Ws.php +++ b/src/Protocols/Ws.php @@ -347,8 +347,7 @@ public static function sendHandshake(TcpConnection $connection) $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)) {