Browse Source

simplified if-else branches

Mark Magyar 2 years ago
parent
commit
9209697d12
4 changed files with 45 additions and 40 deletions
  1. 7 8
      src/Protocols/Http/Request.php
  2. 18 14
      src/Protocols/Websocket.php
  3. 11 9
      src/Protocols/Ws.php
  4. 9 9
      src/Worker.php

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

@@ -611,15 +611,14 @@ class Request
                             $file['type'] = '';
                         }
                         break;
-                    } // Is post field.
-                    else {
-                        // Parse $POST.
-                        if (preg_match('/name="(.*?)"$/', $value, $match)) {
-                            $k = $match[1];
-                            $postEncodeString .= urlencode($k) . "=" . urlencode($boundaryValue) . '&';
-                        }
-                        return $sectionEndOffset + strlen($boundary) + 2;
                     }
+                    // Is post field.
+                    // Parse $POST.
+                    if (preg_match('/name="(.*?)"$/', $value, $match)) {
+                        $k = $match[1];
+                        $postEncodeString .= urlencode($k) . "=" . urlencode($boundaryValue) . '&';
+                    }
+                    return $sectionEndOffset + strlen($boundary) + 2;
                 case "content-type":
                     $file['type'] = trim($value);
                     break;

+ 18 - 14
src/Protocols/Websocket.php

@@ -184,7 +184,9 @@ class Websocket
                         }
                     }
                     return 0;
-                } else if ($opcode === 0xa) {
+                }
+
+                if ($opcode === 0xa) {
                     if ($recvLen >= $currentFrameLength) {
                         $pongData = static::decode(substr($buffer, 0, $currentFrameLength), $connection);
                         $connection->consumeRecvBuffer($currentFrameLength);
@@ -207,9 +209,9 @@ class Websocket
                     return 0;
                 }
                 return $currentFrameLength;
-            } else {
-                $connection->context->websocketCurrentFrameLength = $currentFrameLength;
             }
+
+            $connection->context->websocketCurrentFrameLength = $currentFrameLength;
         }
 
         // Received just a frame length data.
@@ -218,18 +220,20 @@ class Websocket
             $connection->consumeRecvBuffer($connection->context->websocketCurrentFrameLength);
             $connection->context->websocketCurrentFrameLength = 0;
             return 0;
-        } // The length of the received data is greater than the length of a frame.
-        elseif ($connection->context->websocketCurrentFrameLength < $recvLen) {
+        }
+
+        // The length of the received data is greater than the length of a frame.
+        if ($connection->context->websocketCurrentFrameLength < $recvLen) {
             static::decode(substr($buffer, 0, $connection->context->websocketCurrentFrameLength), $connection);
             $connection->consumeRecvBuffer($connection->context->websocketCurrentFrameLength);
             $currentFrameLength = $connection->context->websocketCurrentFrameLength;
             $connection->context->websocketCurrentFrameLength = 0;
             // Continue to read next frame.
             return static::input(substr($buffer, $currentFrameLength), $connection);
-        } // The length of the received data is less than the length of a frame.
-        else {
-            return 0;
         }
+
+        // The length of the received data is less than the length of a frame.
+        return 0;
     }
 
     /**
@@ -325,13 +329,13 @@ class Websocket
         if ($connection->context->websocketCurrentFrameLength) {
             $connection->context->websocketDataBuffer .= $decoded;
             return $connection->context->websocketDataBuffer;
-        } else {
-            if ($connection->context->websocketDataBuffer !== '') {
-                $decoded = $connection->context->websocketDataBuffer . $decoded;
-                $connection->context->websocketDataBuffer = '';
-            }
-            return $decoded;
         }
+
+        if ($connection->context->websocketDataBuffer !== '') {
+            $decoded = $connection->context->websocketDataBuffer . $decoded;
+            $connection->context->websocketDataBuffer = '';
+        }
+        return $decoded;
     }
 
     /**

+ 11 - 9
src/Protocols/Ws.php

@@ -181,7 +181,9 @@ class Ws
                     }
                     return 0;
 
-                } else if ($opcode === 0xa) {
+                }
+
+                if ($opcode === 0xa) {
                     if ($recvLen >= $currentFrameLength) {
                         $pongData = static::decode(substr($buffer, 0, $currentFrameLength), $connection);
                         $connection->consumeRecvBuffer($currentFrameLength);
@@ -203,9 +205,9 @@ class Ws
                     return 0;
                 }
                 return $currentFrameLength;
-            } else {
-                $connection->context->websocketCurrentFrameLength = $currentFrameLength;
             }
+
+            $connection->context->websocketCurrentFrameLength = $currentFrameLength;
         }
         // Received just a frame length data.
         if ($connection->context->websocketCurrentFrameLength === $recvLen) {
@@ -310,13 +312,13 @@ class Ws
         if ($connection->context->websocketCurrentFrameLength) {
             $connection->context->websocketDataBuffer .= $decodedData;
             return $connection->context->websocketDataBuffer;
-        } else {
-            if ($connection->context->websocketDataBuffer !== '') {
-                $decodedData = $connection->context->websocketDataBuffer . $decodedData;
-                $connection->context->websocketDataBuffer = '';
-            }
-            return $decodedData;
         }
+
+        if ($connection->context->websocketDataBuffer !== '') {
+            $decodedData = $connection->context->websocketDataBuffer . $decodedData;
+            $connection->context->websocketDataBuffer = '';
+        }
+        return $decodedData;
     }
 
     /**

+ 9 - 9
src/Worker.php

@@ -1448,15 +1448,15 @@ class Worker
                 exit(250);
             }
             exit(0);
-        } else {
-            static::$globalEvent = new Select();
-            static::$globalEvent->setErrorHandler(function ($exception) {
-                static::stopAll(250, $exception);
-            });
-            Timer::init(static::$globalEvent);
-            foreach ($files as $startFile) {
-                static::forkOneWorkerForWindows($startFile);
-            }
+        }
+
+        static::$globalEvent = new Select();
+        static::$globalEvent->setErrorHandler(function ($exception) {
+            static::stopAll(250, $exception);
+        });
+        Timer::init(static::$globalEvent);
+        foreach ($files as $startFile) {
+            static::forkOneWorkerForWindows($startFile);
         }
     }