| Conditions | 66 |
| Paths | 16435 |
| Total Lines | 374 |
| Code Lines | 267 |
| Lines | 181 |
| Ratio | 48.4 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 35 | public function serialize(OutgoingFrame $frame) |
||
| 36 | { |
||
| 37 | |||
| 38 | switch (true) { |
||
| 39 | case $frame instanceof HeartbeatFrame: |
||
| 40 | return "\x08\x00\x00\x00\x00\x00\x00\xce"; |
||
| 41 | |||
| 42 | case $frame instanceof BodyFrame: |
||
| 43 | return "\x03" . \pack('nN', $frame->frameChannelId, \strlen($frame->content)) . $frame->content . "\xce"; |
||
|
|
|||
| 44 | |||
| 45 | case $frame instanceof Connection\ConnectionStartOkFrame: |
||
| 46 | $payload = "\x00\x0a\x00\x0b" |
||
| 47 | . $this->tableSerializer->serialize($frame->clientProperties) |
||
| 48 | . $this->serializeShortString($frame->mechanism) |
||
| 49 | . $this->serializeLongString($frame->response) |
||
| 50 | . $this->serializeShortString($frame->locale); |
||
| 51 | |||
| 52 | return $this->packX01Payload($frame, $payload); |
||
| 53 | |||
| 54 | case $frame instanceof Connection\ConnectionSecureOkFrame: |
||
| 55 | $payload = "\x00\x0a\x00\x15" |
||
| 56 | . $this->serializeLongString($frame->response); |
||
| 57 | return $this->packX01Payload($frame, $payload); |
||
| 58 | |||
| 59 | case $frame instanceof Connection\ConnectionTuneOkFrame: |
||
| 60 | $payload = "\x00\x0a\x00\x1f" |
||
| 61 | . \pack('nNn', $frame->channelMax, $frame->frameMax, $frame->heartbeat); |
||
| 62 | return $this->packX01Payload($frame, $payload); |
||
| 63 | |||
| 64 | case $frame instanceof Connection\ConnectionOpenFrame: |
||
| 65 | $payload = "\x00\x0a\x00\x28" |
||
| 66 | . $this->serializeShortString($frame->virtualHost) |
||
| 67 | . $this->serializeShortString($frame->capabilities) |
||
| 68 | . ($frame->insist ? "\x01" : "\x00"); |
||
| 69 | |||
| 70 | return $this->packX01Payload($frame, $payload); |
||
| 71 | |||
| 72 | View Code Duplication | case $frame instanceof Connection\ConnectionCloseFrame: |
|
| 73 | $payload = "\x00\x0a\x00\x32" |
||
| 74 | . \pack('n', $frame->replyCode) |
||
| 75 | . $this->serializeShortString($frame->replyText) |
||
| 76 | . \pack('nn', $frame->classId, $frame->methodId); |
||
| 77 | |||
| 78 | return $this->packX01Payload($frame, $payload); |
||
| 79 | |||
| 80 | case $frame instanceof Connection\ConnectionCloseOkFrame: |
||
| 81 | return "\x01" . \pack('n', $frame->frameChannelId) . "\x00\x00\x00\x04\x00\x0a\x00\x33\xce"; |
||
| 82 | |||
| 83 | case $frame instanceof Channel\ChannelOpenFrame: |
||
| 84 | $payload = "\x00\x14\x00\x0a" |
||
| 85 | . $this->serializeShortString($frame->outOfBand); |
||
| 86 | |||
| 87 | return $this->packX01Payload($frame, $payload); |
||
| 88 | |||
| 89 | View Code Duplication | case $frame instanceof Channel\ChannelFlowFrame: |
|
| 90 | $payload = "\x00\x14\x00\x14" |
||
| 91 | . ($frame->active ? "\x01" : "\x00"); |
||
| 92 | |||
| 93 | return $this->packX01Payload($frame, $payload); |
||
| 94 | |||
| 95 | View Code Duplication | case $frame instanceof Channel\ChannelFlowOkFrame: |
|
| 96 | $payload = "\x00\x14\x00\x15" |
||
| 97 | . ($frame->active ? "\x01" : "\x00"); |
||
| 98 | |||
| 99 | return $this->packX01Payload($frame, $payload); |
||
| 100 | |||
| 101 | View Code Duplication | case $frame instanceof Channel\ChannelCloseFrame: |
|
| 102 | $payload = "\x00\x14\x00\x28" |
||
| 103 | . \pack('n', $frame->replyCode) |
||
| 104 | . $this->serializeShortString($frame->replyText) |
||
| 105 | . \pack('nn', $frame->classId, $frame->methodId); |
||
| 106 | |||
| 107 | return $this->packX01Payload($frame, $payload); |
||
| 108 | |||
| 109 | case $frame instanceof Channel\ChannelCloseOkFrame: |
||
| 110 | return "\x01" . \pack('n', $frame->frameChannelId) . "\x00\x00\x00\x04\x00\x14\x00\x29\xce"; |
||
| 111 | |||
| 112 | case $frame instanceof Access\AccessRequestFrame: |
||
| 113 | $payload = "\x00\x1e\x00\x0a" |
||
| 114 | . $this->serializeShortString($frame->realm) |
||
| 115 | . \chr( |
||
| 116 | $frame->exclusive |
||
| 117 | | $frame->passive << 1 |
||
| 118 | | $frame->active << 2 |
||
| 119 | | $frame->write << 3 |
||
| 120 | | $frame->read << 4 |
||
| 121 | ); |
||
| 122 | |||
| 123 | return $this->packX01Payload($frame, $payload); |
||
| 124 | |||
| 125 | View Code Duplication | case $frame instanceof Exchange\ExchangeDeclareFrame: |
|
| 126 | $payload = "\x00\x28\x00\x0a" |
||
| 127 | . \pack('n', $frame->reserved1) |
||
| 128 | . $this->serializeShortString($frame->exchange) |
||
| 129 | . $this->serializeShortString($frame->type) |
||
| 130 | . \chr( |
||
| 131 | $frame->passive |
||
| 132 | | $frame->durable << 1 |
||
| 133 | | $frame->autoDelete << 2 |
||
| 134 | | $frame->internal << 3 |
||
| 135 | | $frame->nowait << 4 |
||
| 136 | ) |
||
| 137 | . $this->tableSerializer->serialize($frame->arguments); |
||
| 138 | |||
| 139 | return $this->packX01Payload($frame, $payload); |
||
| 140 | |||
| 141 | View Code Duplication | case $frame instanceof Exchange\ExchangeDeleteFrame: |
|
| 142 | $payload = "\x00\x28\x00\x14" |
||
| 143 | . \pack('n', $frame->reserved1) |
||
| 144 | . $this->serializeShortString($frame->exchange) |
||
| 145 | . \chr( |
||
| 146 | $frame->ifUnused |
||
| 147 | | $frame->nowait << 1 |
||
| 148 | ); |
||
| 149 | |||
| 150 | return $this->packX01Payload($frame, $payload); |
||
| 151 | |||
| 152 | View Code Duplication | case $frame instanceof Exchange\ExchangeBindFrame: |
|
| 153 | $payload = "\x00\x28\x00\x1e" |
||
| 154 | . \pack('n', $frame->reserved1) |
||
| 155 | . $this->serializeShortString($frame->destination) |
||
| 156 | . $this->serializeShortString($frame->source) |
||
| 157 | . $this->serializeShortString($frame->routingKey) |
||
| 158 | . ($frame->nowait ? "\x01" : "\x00") |
||
| 159 | . $this->tableSerializer->serialize($frame->arguments); |
||
| 160 | |||
| 161 | return $this->packX01Payload($frame, $payload); |
||
| 162 | |||
| 163 | View Code Duplication | case $frame instanceof Exchange\ExchangeUnbindFrame: |
|
| 164 | $payload = "\x00\x28\x00\x28" |
||
| 165 | . \pack('n', $frame->reserved1) |
||
| 166 | . $this->serializeShortString($frame->destination) |
||
| 167 | . $this->serializeShortString($frame->source) |
||
| 168 | . $this->serializeShortString($frame->routingKey) |
||
| 169 | . ($frame->nowait ? "\x01" : "\x00") |
||
| 170 | . $this->tableSerializer->serialize($frame->arguments); |
||
| 171 | |||
| 172 | return $this->packX01Payload($frame, $payload); |
||
| 173 | |||
| 174 | View Code Duplication | case $frame instanceof Queue\QueueDeclareFrame: |
|
| 175 | $payload = "\x00\x32\x00\x0a" |
||
| 176 | . \pack('n', $frame->reserved1) |
||
| 177 | . $this->serializeShortString($frame->queue) |
||
| 178 | . \chr( |
||
| 179 | $frame->passive |
||
| 180 | | $frame->durable << 1 |
||
| 181 | | $frame->exclusive << 2 |
||
| 182 | | $frame->autoDelete << 3 |
||
| 183 | | $frame->nowait << 4 |
||
| 184 | ) |
||
| 185 | . $this->tableSerializer->serialize($frame->arguments); |
||
| 186 | |||
| 187 | return $this->packX01Payload($frame, $payload); |
||
| 188 | |||
| 189 | View Code Duplication | case $frame instanceof Queue\QueueBindFrame: |
|
| 190 | $payload = "\x00\x32\x00\x14" |
||
| 191 | . \pack('n', $frame->reserved1) |
||
| 192 | . $this->serializeShortString($frame->queue) |
||
| 193 | . $this->serializeShortString($frame->exchange) |
||
| 194 | . $this->serializeShortString($frame->routingKey) |
||
| 195 | . ($frame->nowait ? "\x01" : "\x00") |
||
| 196 | . $this->tableSerializer->serialize($frame->arguments); |
||
| 197 | |||
| 198 | return $this->packX01Payload($frame, $payload); |
||
| 199 | |||
| 200 | View Code Duplication | case $frame instanceof Queue\QueuePurgeFrame: |
|
| 201 | $payload = "\x00\x32\x00\x1e" |
||
| 202 | . \pack('n', $frame->reserved1) |
||
| 203 | . $this->serializeShortString($frame->queue) |
||
| 204 | . ($frame->nowait ? "\x01" : "\x00"); |
||
| 205 | |||
| 206 | return $this->packX01Payload($frame, $payload); |
||
| 207 | |||
| 208 | View Code Duplication | case $frame instanceof Queue\QueueDeleteFrame: |
|
| 209 | $payload = "\x00\x32\x00\x28" |
||
| 210 | . \pack('n', $frame->reserved1) |
||
| 211 | . $this->serializeShortString($frame->queue) |
||
| 212 | . \chr( |
||
| 213 | $frame->ifUnused |
||
| 214 | | $frame->ifEmpty << 1 |
||
| 215 | | $frame->nowait << 2 |
||
| 216 | ); |
||
| 217 | |||
| 218 | return $this->packX01Payload($frame, $payload); |
||
| 219 | |||
| 220 | View Code Duplication | case $frame instanceof Queue\QueueUnbindFrame: |
|
| 221 | $payload = "\x00\x32\x00\x32" |
||
| 222 | . \pack('n', $frame->reserved1) |
||
| 223 | . $this->serializeShortString($frame->queue) |
||
| 224 | . $this->serializeShortString($frame->exchange) |
||
| 225 | . $this->serializeShortString($frame->routingKey) |
||
| 226 | . $this->tableSerializer->serialize($frame->arguments); |
||
| 227 | |||
| 228 | return $this->packX01Payload($frame, $payload); |
||
| 229 | |||
| 230 | case $frame instanceof Basic\BasicHeaderFrame: |
||
| 231 | $flags = 0; |
||
| 232 | $properties = ''; |
||
| 233 | |||
| 234 | if (null !== $frame->contentType) { |
||
| 235 | $flags |= 32768; |
||
| 236 | $properties .= $this->serializeShortString($frame->contentType); |
||
| 237 | } |
||
| 238 | |||
| 239 | if (null !== $frame->contentEncoding) { |
||
| 240 | $flags |= 16384; |
||
| 241 | $properties .= $this->serializeShortString($frame->contentEncoding); |
||
| 242 | } |
||
| 243 | |||
| 244 | if (null !== $frame->headers) { |
||
| 245 | $flags |= 8192; |
||
| 246 | $properties .= $this->tableSerializer->serialize($frame->headers); |
||
| 247 | } |
||
| 248 | |||
| 249 | View Code Duplication | if (null !== $frame->deliveryMode) { |
|
| 250 | $flags |= 4096; |
||
| 251 | $properties .= \pack('c', $frame->deliveryMode); |
||
| 252 | } |
||
| 253 | |||
| 254 | View Code Duplication | if (null !== $frame->priority) { |
|
| 255 | $flags |= 2048; |
||
| 256 | $properties .= \pack('c', $frame->priority); |
||
| 257 | } |
||
| 258 | |||
| 259 | if (null !== $frame->correlationId) { |
||
| 260 | $flags |= 1024; |
||
| 261 | $properties .= $this->serializeShortString($frame->correlationId); |
||
| 262 | } |
||
| 263 | |||
| 264 | if (null !== $frame->replyTo) { |
||
| 265 | $flags |= 512; |
||
| 266 | $properties .= $this->serializeShortString($frame->replyTo); |
||
| 267 | } |
||
| 268 | |||
| 269 | if (null !== $frame->expiration) { |
||
| 270 | $flags |= 256; |
||
| 271 | $properties .= $this->serializeShortString($frame->expiration); |
||
| 272 | } |
||
| 273 | |||
| 274 | if (null !== $frame->messageId) { |
||
| 275 | $flags |= 128; |
||
| 276 | $properties .= $this->serializeShortString($frame->messageId); |
||
| 277 | } |
||
| 278 | |||
| 279 | if (null !== $frame->timestamp) { |
||
| 280 | $flags |= 64; |
||
| 281 | $properties .= \pack('J', $frame->timestamp); |
||
| 282 | } |
||
| 283 | |||
| 284 | if (null !== $frame->type) { |
||
| 285 | $flags |= 32; |
||
| 286 | $properties .= $this->serializeShortString($frame->type); |
||
| 287 | } |
||
| 288 | |||
| 289 | if (null !== $frame->userId) { |
||
| 290 | $flags |= 16; |
||
| 291 | $properties .= $this->serializeShortString($frame->userId); |
||
| 292 | } |
||
| 293 | |||
| 294 | if (null !== $frame->appId) { |
||
| 295 | $flags |= 8; |
||
| 296 | $properties .= $this->serializeShortString($frame->appId); |
||
| 297 | } |
||
| 298 | |||
| 299 | if (null !== $frame->clusterId) { |
||
| 300 | $flags |= 4; |
||
| 301 | $properties .= $this->serializeShortString($frame->clusterId); |
||
| 302 | } |
||
| 303 | |||
| 304 | $payload = "\x00\x3c\x00\x00" |
||
| 305 | . \pack('Jn', $frame->contentLength, $flags) |
||
| 306 | . $properties; |
||
| 307 | |||
| 308 | return "\x02" . \pack('nN', $frame->frameChannelId, \strlen($payload)) . $payload . "\xce"; |
||
| 309 | |||
| 310 | case $frame instanceof Basic\BasicQosFrame: |
||
| 311 | $payload = "\x00\x3c\x00\x0a" |
||
| 312 | . \pack('Nn', $frame->prefetchSize, $frame->prefetchCount) |
||
| 313 | . ($frame->global ? "\x01" : "\x00"); |
||
| 314 | |||
| 315 | return $this->packX01Payload($frame, $payload); |
||
| 316 | |||
| 317 | View Code Duplication | case $frame instanceof Basic\BasicConsumeFrame: |
|
| 318 | $payload = "\x00\x3c\x00\x14" |
||
| 319 | . \pack('n', $frame->reserved1) |
||
| 320 | . $this->serializeShortString($frame->queue) |
||
| 321 | . $this->serializeShortString($frame->consumerTag) |
||
| 322 | . \chr( |
||
| 323 | $frame->noLocal |
||
| 324 | | $frame->noAck << 1 |
||
| 325 | | $frame->exclusive << 2 |
||
| 326 | | $frame->nowait << 3 |
||
| 327 | ) |
||
| 328 | . $this->tableSerializer->serialize($frame->arguments); |
||
| 329 | |||
| 330 | return $this->packX01Payload($frame, $payload); |
||
| 331 | |||
| 332 | case $frame instanceof Basic\BasicCancelFrame: |
||
| 333 | $payload = "\x00\x3c\x00\x1e" |
||
| 334 | . $this->serializeShortString($frame->consumerTag) |
||
| 335 | . ($frame->nowait ? "\x01" : "\x00"); |
||
| 336 | |||
| 337 | return $this->packX01Payload($frame, $payload); |
||
| 338 | |||
| 339 | View Code Duplication | case $frame instanceof Basic\BasicPublishFrame: |
|
| 340 | $payload = "\x00\x3c\x00\x28" |
||
| 341 | . \pack('n', $frame->reserved1) |
||
| 342 | . $this->serializeShortString($frame->exchange) |
||
| 343 | . $this->serializeShortString($frame->routingKey) |
||
| 344 | . \chr( |
||
| 345 | $frame->mandatory |
||
| 346 | | $frame->immediate << 1 |
||
| 347 | ); |
||
| 348 | |||
| 349 | return $this->packX01Payload($frame, $payload); |
||
| 350 | |||
| 351 | View Code Duplication | case $frame instanceof Basic\BasicGetFrame: |
|
| 352 | $payload = "\x00\x3c\x00\x46" |
||
| 353 | . \pack('n', $frame->reserved1) |
||
| 354 | . $this->serializeShortString($frame->queue) |
||
| 355 | . ($frame->noAck ? "\x01" : "\x00"); |
||
| 356 | |||
| 357 | return $this->packX01Payload($frame, $payload); |
||
| 358 | |||
| 359 | View Code Duplication | case $frame instanceof Basic\BasicAckFrame: |
|
| 360 | $payload = "\x00\x3c\x00\x50" |
||
| 361 | . \pack('J', $frame->deliveryTag) |
||
| 362 | . ($frame->multiple ? "\x01" : "\x00"); |
||
| 363 | |||
| 364 | return $this->packX01Payload($frame, $payload); |
||
| 365 | |||
| 366 | View Code Duplication | case $frame instanceof Basic\BasicRejectFrame: |
|
| 367 | $payload = "\x00\x3c\x00\x5a" |
||
| 368 | . \pack('J', $frame->deliveryTag) |
||
| 369 | . ($frame->requeue ? "\x01" : "\x00"); |
||
| 370 | |||
| 371 | return $this->packX01Payload($frame, $payload); |
||
| 372 | |||
| 373 | case $frame instanceof Basic\BasicRecoverFrame: |
||
| 374 | $payload = "\x00\x3c\x00\x6e" |
||
| 375 | . ($frame->requeue ? "\x01" : "\x00"); |
||
| 376 | |||
| 377 | return $this->packX01Payload($frame, $payload); |
||
| 378 | |||
| 379 | View Code Duplication | case $frame instanceof Basic\BasicNackFrame: |
|
| 380 | $payload = "\x00\x3c\x00\x78" |
||
| 381 | . \pack('J', $frame->deliveryTag) |
||
| 382 | . \chr( |
||
| 383 | $frame->multiple |
||
| 384 | | $frame->requeue << 1 |
||
| 385 | ); |
||
| 386 | |||
| 387 | return $this->packX01Payload($frame, $payload); |
||
| 388 | |||
| 389 | case $frame instanceof Tx\TxSelectFrame: |
||
| 390 | return "\x01" . \pack('n', $frame->frameChannelId) . "\x00\x00\x00\x04\x00\x5a\x00\x0a\xce"; |
||
| 391 | |||
| 392 | case $frame instanceof Tx\TxCommitFrame: |
||
| 393 | return "\x01" . \pack('n', $frame->frameChannelId) . "\x00\x00\x00\x04\x00\x5a\x00\x14\xce"; |
||
| 394 | |||
| 395 | case $frame instanceof Tx\TxRollbackFrame: |
||
| 396 | return "\x01" . \pack('n', $frame->frameChannelId) . "\x00\x00\x00\x04\x00\x5a\x00\x1e\xce"; |
||
| 397 | |||
| 398 | case $frame instanceof Confirm\ConfirmSelectFrame: |
||
| 399 | $payload = "\x00\x55\x00\x0a" |
||
| 400 | . ($frame->nowait ? "\x01" : "\x00"); |
||
| 401 | |||
| 402 | return $this->packX01Payload($frame, $payload); |
||
| 403 | |||
| 404 | } |
||
| 405 | |||
| 406 | throw new AMQPProtocolException( |
||
| 407 | sprintf('Frame %s not implemented yet', get_class($frame))); |
||
| 408 | } |
||
| 409 | |||
| 420 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.