Skip to content

Commit

Permalink
Merge pull request #153 from jeromegamez/early-returns
Browse files Browse the repository at this point in the history
Replace `elseif`s with early returns in `Connection`
  • Loading branch information
WyriHaximus committed May 23, 2024
2 parents 873ec47 + b974f8c commit 6aeaaec
Show file tree
Hide file tree
Showing 2 changed files with 292 additions and 92 deletions.
52 changes: 37 additions & 15 deletions spec/generate.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,16 +355,20 @@ function amqpTypeToLength($type, $e)
$connectionContent .= " */\n";
$connectionContent .= " private function onFrameReceived(AbstractFrame \$frame)\n";
$connectionContent .= " {\n";
$connectionContent .= " if (\$frame instanceof MethodFrame) {\n";
$connectionContent .= " if (\$frame instanceof MethodConnectionCloseFrame) {\n";
$connectionContent .= " \$this->disconnect(Constants::STATUS_CONNECTION_FORCED, \"Connection closed by server: ({\$frame->replyCode}) \" . \$frame->replyText);\n";
$connectionContent .= " throw new ClientException('Connection closed by server: ' . \$frame->replyText, \$frame->replyCode);\n";
$connectionContent .= " }\n";
$connectionContent .= " } elseif (\$frame instanceof ContentHeaderFrame) {\n";
$connectionContent .= " if (\$frame instanceof MethodConnectionCloseFrame) {\n";
$connectionContent .= " \$this->disconnect(Constants::STATUS_CONNECTION_FORCED, \"Connection closed by server: ({\$frame->replyCode}) \" . \$frame->replyText);\n";
$connectionContent .= " throw new ClientException('Connection closed by server: ' . \$frame->replyText, \$frame->replyCode);\n";
$connectionContent .= " }\n";
$connectionContent .= "\n";
$connectionContent .= " if (\$frame instanceof ContentHeaderFrame) {\n";
$connectionContent .= " \$this->disconnect(Constants::STATUS_UNEXPECTED_FRAME, 'Got header frame on connection channel (#0).');\n";
$connectionContent .= " } elseif (\$frame instanceof ContentBodyFrame) {\n";
$connectionContent .= " }\n";
$connectionContent .= "\n";
$connectionContent .= " if (\$frame instanceof ContentBodyFrame) {\n";
$connectionContent .= " \$this->disconnect(Constants::STATUS_UNEXPECTED_FRAME, 'Got body frame on connection channel (#0).');\n";
$connectionContent .= " } elseif (\$frame instanceof HeartbeatFrame) {\n";
$connectionContent .= " }\n";
$connectionContent .= "\n";
$connectionContent .= " if (\$frame instanceof HeartbeatFrame) {\n";
$connectionContent .= " return;\n";
$connectionContent .= " }\n";
$connectionContent .= "\n";
Expand Down Expand Up @@ -396,10 +400,14 @@ function amqpTypeToLength($type, $e)
$connectionContent .= " 'filter' => function (AbstractFrame \$frame) use (\$channel): bool {\n";
$connectionContent .= " if (\$frame instanceof Protocol\\ContentHeaderFrame && \$frame->channel === \$channel) {\n";
$connectionContent .= " return true;\n";
$connectionContent .= " } elseif (\$frame instanceof Protocol\\MethodChannelCloseFrame && \$frame->channel === \$channel) {\n";
$connectionContent .= " }\n";
$connectionContent .= "\n";
$connectionContent .= " if (\$frame instanceof Protocol\\MethodChannelCloseFrame && \$frame->channel === \$channel) {\n";
$connectionContent .= " \$this->channelCloseOk(\$channel);\n";
$connectionContent .= " throw new ClientException(\$frame->replyText, \$frame->replyCode);\n";
$connectionContent .= " } elseif (\$frame instanceof Protocol\\MethodConnectionCloseFrame) {\n";
$connectionContent .= " }\n";
$connectionContent .= "\n";
$connectionContent .= " if (\$frame instanceof Protocol\\MethodConnectionCloseFrame) {\n";
$connectionContent .= " \$this->connectionCloseOk();\n";
$connectionContent .= " throw new ClientException(\$frame->replyText, \$frame->replyCode);\n";
$connectionContent .= " }\n";
Expand All @@ -419,10 +427,14 @@ function amqpTypeToLength($type, $e)
$connectionContent .= " 'filter' => function (AbstractFrame \$frame) use (\$channel): bool {\n";
$connectionContent .= " if (\$frame instanceof Protocol\\ContentBodyFrame && \$frame->channel === \$channel) {\n";
$connectionContent .= " return true;\n";
$connectionContent .= " } elseif (\$frame instanceof Protocol\\MethodChannelCloseFrame && \$frame->channel === \$channel) {\n";
$connectionContent .= " }\n";
$connectionContent .= "\n";
$connectionContent .= " if (\$frame instanceof Protocol\\MethodChannelCloseFrame && \$frame->channel === \$channel) {\n";
$connectionContent .= " \$this->channelCloseOk(\$channel);\n";
$connectionContent .= " throw new ClientException(\$frame->replyText, \$frame->replyCode);\n";
$connectionContent .= " } elseif (\$frame instanceof Protocol\\MethodConnectionCloseFrame) {\n";
$connectionContent .= " }\n";
$connectionContent .= "\n";
$connectionContent .= " if (\$frame instanceof Protocol\\MethodConnectionCloseFrame) {\n";
$connectionContent .= " \$this->connectionCloseOk();\n";
$connectionContent .= " throw new ClientException(\$frame->replyText, \$frame->replyCode);\n";
$connectionContent .= " }\n";
Expand Down Expand Up @@ -882,19 +894,29 @@ function amqpTypeToLength($type, $e)
$connectionContent .= " 'filter' => function (Protocol\\AbstractFrame \$frame)" . ($class->id !== 10 ? " use (\$channel)" : "") . ": bool {\n";
$connectionContent .= " if (\$frame instanceof Protocol\\{$className}" . ($class->id !== 10 ? " && \$frame->channel === \$channel" : "") . ") {\n";
$connectionContent .= " return true;\n";
$connectionContent .= " }\n";
$connectionContent .= "\n";

if ($class->id === 60 && $method->id === 71) {
$connectionContent .= " } elseif (\$frame instanceof Protocol\\" . str_replace("GetOk", "GetEmpty", $className) . ($class->id !== 10 ? " && \$frame->channel === \$channel" : "") . ") {\n";
$connectionContent .= " if (\$frame instanceof Protocol\\" . str_replace("GetOk", "GetEmpty", $className) . ($class->id !== 10 ? " && \$frame->channel === \$channel" : "") . ") {\n";
$connectionContent .= " return true;\n";
$connectionContent .= " }\n";
$connectionContent .= "\n";
}

if ($class->id !== 10) {
$connectionContent .= " } elseif (\$frame instanceof Protocol\\MethodChannelCloseFrame && \$frame->channel === \$channel) {\n";
$connectionContent .= " if (\$frame instanceof Protocol\\MethodChannelCloseFrame && \$frame->channel === \$channel) {\n";
$connectionContent .= " \$this->channelCloseOk(\$channel);\n";
$connectionContent .= " throw new ClientException(\$frame->replyText, \$frame->replyCode);\n";
$connectionContent .= " }\n";
$connectionContent .= "\n";
}
$connectionContent .= " } elseif (\$frame instanceof Protocol\\MethodConnectionCloseFrame) {\n";

$connectionContent .= " if (\$frame instanceof Protocol\\MethodConnectionCloseFrame) {\n";
$connectionContent .= " \$this->connectionCloseOk();\n";
$connectionContent .= " throw new ClientException(\$frame->replyText, \$frame->replyCode);\n";
$connectionContent .= " }\n";
$connectionContent .= "\n";
$connectionContent .= " return false;\n";
$connectionContent .= " },\n";
$connectionContent .= " 'promise' => \$deferred,\n";
Expand Down
Loading

0 comments on commit 6aeaaec

Please sign in to comment.