Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace elseifs with early returns in Connection #153

Merged
merged 1 commit into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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";
WyriHaximus marked this conversation as resolved.
Show resolved Hide resolved
$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
Loading