Conditions | 46 |
Paths | 46 |
Total Lines | 505 |
Lines | 183 |
Ratio | 36.24 % |
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 |
||
30 | private function parseMethodFrame() |
||
31 | { |
||
32 | list(, $class, $method) = \unpack('n2', $this->buffer); |
||
|
|||
33 | $this->buffer = mb_orig_substr($this->buffer, 4); |
||
34 | |||
35 | // class "connection" |
||
36 | if ($class === 10) { |
||
37 | // method "connection.start" |
||
38 | if ($method === 10) { |
||
39 | $frame = new Connection\ConnectionStartFrame(); |
||
40 | |||
41 | // consume (a) "version-major" (octet) |
||
42 | // consume (b) "version-minor" (octet) |
||
43 | $fields = \unpack('ca/cb', $this->buffer); |
||
44 | $this->buffer = mb_orig_substr($this->buffer, 2); |
||
45 | $frame->versionMajor = $fields['a']; |
||
46 | $frame->versionMinor = $fields['b']; |
||
47 | |||
48 | // consume "server-properties" (table) |
||
49 | $frame->serverProperties = $this->tableParser->parse($this->buffer); |
||
50 | |||
51 | // consume "mechanisms" (longstr) |
||
52 | list(, $length) = \unpack('N', $this->buffer); |
||
53 | $frame->mechanisms = mb_orig_substr($this->buffer, 4, $length); |
||
54 | $this->buffer = mb_orig_substr($this->buffer, 4 + $length); |
||
55 | |||
56 | // consume "locales" (longstr) |
||
57 | list(, $length) = \unpack('N', $this->buffer); |
||
58 | $frame->locales = mb_orig_substr($this->buffer, 4, $length); |
||
59 | $this->buffer = mb_orig_substr($this->buffer, 4 + $length); |
||
60 | |||
61 | return $frame; |
||
62 | |||
63 | // method "connection.secure" |
||
64 | } |
||
65 | View Code Duplication | if ($method === 20) { |
|
66 | $frame = new Connection\ConnectionSecureFrame(); |
||
67 | |||
68 | // consume "challenge" (longstr) |
||
69 | list(, $length) = \unpack('N', $this->buffer); |
||
70 | $frame->challenge = mb_orig_substr($this->buffer, 4, $length); |
||
71 | $this->buffer = mb_orig_substr($this->buffer, 4 + $length); |
||
72 | |||
73 | return $frame; |
||
74 | |||
75 | // method "connection.tune" |
||
76 | } |
||
77 | if ($method === 30) { |
||
78 | $frame = new Connection\ConnectionTuneFrame(); |
||
79 | |||
80 | // consume (a) "channel-max" (short) |
||
81 | // consume (b) "frame-max" (long) |
||
82 | // consume (c) "heartbeat" (short) |
||
83 | $fields = \unpack('na/Nb/nc', $this->buffer); |
||
84 | $this->buffer = mb_orig_substr($this->buffer, 8); |
||
85 | $frame->channelMax = $fields['a']; |
||
86 | $frame->frameMax = $fields['b']; |
||
87 | $frame->heartbeat = $fields['c']; |
||
88 | |||
89 | return $frame; |
||
90 | |||
91 | // method "connection.open-ok" |
||
92 | } |
||
93 | View Code Duplication | if ($method === 41) { |
|
94 | $frame = new Connection\ConnectionOpenOkFrame(); |
||
95 | |||
96 | // consume "known-hosts" (shortstr) |
||
97 | $length = \ord($this->buffer); |
||
98 | $frame->knownHosts = mb_orig_substr($this->buffer, 1, $length); |
||
99 | $this->buffer = mb_orig_substr($this->buffer, 1 + $length); |
||
100 | |||
101 | return $frame; |
||
102 | |||
103 | // method "connection.close" |
||
104 | } |
||
105 | View Code Duplication | if ($method === 50) { |
|
106 | $frame = new Connection\ConnectionCloseFrame(); |
||
107 | |||
108 | // consume "replyCode" (short) |
||
109 | list(, $frame->replyCode) = \unpack('n', $this->buffer); |
||
110 | $this->buffer = mb_orig_substr($this->buffer, 2); |
||
111 | |||
112 | // consume "reply-text" (shortstr) |
||
113 | $length = \ord($this->buffer); |
||
114 | $frame->replyText = mb_orig_substr($this->buffer, 1, $length); |
||
115 | $this->buffer = mb_orig_substr($this->buffer, 1 + $length); |
||
116 | |||
117 | // consume (a) "class-id" (short) |
||
118 | // consume (b) "method-id" (short) |
||
119 | $fields = \unpack('na/nb', $this->buffer); |
||
120 | $this->buffer = mb_orig_substr($this->buffer, 4); |
||
121 | $frame->classId = $fields['a']; |
||
122 | $frame->methodId = $fields['b']; |
||
123 | |||
124 | return $frame; |
||
125 | |||
126 | // method "connection.close-ok" |
||
127 | } |
||
128 | if ($method === 51) { |
||
129 | return new Connection\ConnectionCloseOkFrame(); |
||
130 | |||
131 | // method "connection.blocked" |
||
132 | } |
||
133 | View Code Duplication | if ($method === 60) { |
|
134 | $frame = new Connection\ConnectionBlockedFrame(); |
||
135 | |||
136 | // consume "reason" (shortstr) |
||
137 | $length = \ord($this->buffer); |
||
138 | $frame->reason = mb_orig_substr($this->buffer, 1, $length); |
||
139 | $this->buffer = mb_orig_substr($this->buffer, 1 + $length); |
||
140 | |||
141 | return $frame; |
||
142 | |||
143 | // method "connection.unblocked" |
||
144 | } |
||
145 | if ($method === 61) { |
||
146 | return new Connection\ConnectionUnblockedFrame(); |
||
147 | } |
||
148 | |||
149 | throw new AMQPProtocolException( |
||
150 | 'Frame method (' . $method . ') is invalid for class "connection".' |
||
151 | ); |
||
152 | |||
153 | // class "channel" |
||
154 | } |
||
155 | if ($class === 20) { |
||
156 | // method "channel.open-ok" |
||
157 | View Code Duplication | if ($method === 11) { |
|
158 | $frame = new Channel\ChannelOpenOkFrame(); |
||
159 | |||
160 | // consume "channel-id" (longstr) |
||
161 | list(, $length) = \unpack('N', $this->buffer); |
||
162 | $frame->channelId = mb_orig_substr($this->buffer, 4, $length); |
||
163 | $this->buffer = mb_orig_substr($this->buffer, 4 + $length); |
||
164 | |||
165 | return $frame; |
||
166 | |||
167 | // method "channel.flow" |
||
168 | } |
||
169 | View Code Duplication | if ($method === 20) { |
|
170 | $frame = new Channel\ChannelFlowFrame(); |
||
171 | |||
172 | // consume "active" (bit) |
||
173 | $frame->active = \ord($this->buffer) !== 0; |
||
174 | $this->buffer = mb_orig_substr($this->buffer, 1); |
||
175 | |||
176 | return $frame; |
||
177 | |||
178 | // method "channel.flow-ok" |
||
179 | } |
||
180 | View Code Duplication | if ($method === 21) { |
|
181 | $frame = new Channel\ChannelFlowOkFrame(); |
||
182 | |||
183 | // consume "active" (bit) |
||
184 | $frame->active = \ord($this->buffer) !== 0; |
||
185 | $this->buffer = mb_orig_substr($this->buffer, 1); |
||
186 | |||
187 | return $frame; |
||
188 | |||
189 | // method "channel.close" |
||
190 | } |
||
191 | View Code Duplication | if ($method === 40) { |
|
192 | $frame = new Channel\ChannelCloseFrame(); |
||
193 | |||
194 | // consume "replyCode" (short) |
||
195 | list(, $frame->replyCode) = \unpack('n', $this->buffer); |
||
196 | $this->buffer = mb_orig_substr($this->buffer, 2); |
||
197 | |||
198 | // consume "reply-text" (shortstr) |
||
199 | $length = \ord($this->buffer); |
||
200 | $frame->replyText = mb_orig_substr($this->buffer, 1, $length); |
||
201 | $this->buffer = mb_orig_substr($this->buffer, 1 + $length); |
||
202 | |||
203 | // consume (a) "class-id" (short) |
||
204 | // consume (b) "method-id" (short) |
||
205 | $fields = \unpack('na/nb', $this->buffer); |
||
206 | $this->buffer = mb_orig_substr($this->buffer, 4); |
||
207 | $frame->classId = $fields['a']; |
||
208 | $frame->methodId = $fields['b']; |
||
209 | |||
210 | return $frame; |
||
211 | |||
212 | // method "channel.close-ok" |
||
213 | } |
||
214 | if ($method === 41) { |
||
215 | return new Channel\ChannelCloseOkFrame(); |
||
216 | } |
||
217 | |||
218 | throw new AMQPProtocolException( |
||
219 | 'Frame method (' . $method . ') is invalid for class "channel".' |
||
220 | ); |
||
221 | |||
222 | // class "access" |
||
223 | } |
||
224 | if ($class === 30) { |
||
225 | // method "access.request-ok" |
||
226 | View Code Duplication | if ($method === 11) { |
|
227 | $frame = new Access\AccessRequestOkFrame(); |
||
228 | |||
229 | // consume "reserved1" (short) |
||
230 | list(, $frame->reserved1) = \unpack('n', $this->buffer); |
||
231 | $this->buffer = mb_orig_substr($this->buffer, 2); |
||
232 | |||
233 | return $frame; |
||
234 | } |
||
235 | |||
236 | throw new AMQPProtocolException( |
||
237 | 'Frame method (' . $method . ') is invalid for class "access".' |
||
238 | ); |
||
239 | |||
240 | // class "exchange" |
||
241 | } |
||
242 | if ($class === 40) { |
||
243 | // method "exchange.declare-ok" |
||
244 | if ($method === 11) { |
||
245 | return new Exchange\ExchangeDeclareOkFrame(); |
||
246 | |||
247 | // method "exchange.delete-ok" |
||
248 | } |
||
249 | if ($method === 21) { |
||
250 | return new Exchange\ExchangeDeleteOkFrame(); |
||
251 | |||
252 | // method "exchange.bind-ok" |
||
253 | } |
||
254 | if ($method === 31) { |
||
255 | return new Exchange\ExchangeBindOkFrame(); |
||
256 | |||
257 | // method "exchange.unbind-ok" |
||
258 | } |
||
259 | if ($method === 51) { |
||
260 | return new Exchange\ExchangeUnbindOkFrame(); |
||
261 | } |
||
262 | |||
263 | throw new AMQPProtocolException( |
||
264 | 'Frame method (' . $method . ') is invalid for class "exchange".' |
||
265 | ); |
||
266 | |||
267 | // class "queue" |
||
268 | } |
||
269 | if ($class === 50) { |
||
270 | // method "queue.declare-ok" |
||
271 | if ($method === 11) { |
||
272 | $frame = new Queue\QueueDeclareOkFrame(); |
||
273 | |||
274 | // consume "queue" (shortstr) |
||
275 | $length = \ord($this->buffer); |
||
276 | $frame->queue = mb_orig_substr($this->buffer, 1, $length); |
||
277 | $this->buffer = mb_orig_substr($this->buffer, 1 + $length); |
||
278 | |||
279 | // consume (a) "message-count" (long) |
||
280 | // consume (b) "consumer-count" (long) |
||
281 | $fields = \unpack('Na/Nb', $this->buffer); |
||
282 | $this->buffer = mb_orig_substr($this->buffer, 8); |
||
283 | $frame->messageCount = $fields['a']; |
||
284 | $frame->consumerCount = $fields['b']; |
||
285 | |||
286 | return $frame; |
||
287 | |||
288 | // method "queue.bind-ok" |
||
289 | } |
||
290 | if ($method === 21) { |
||
291 | return new Queue\QueueBindOkFrame(); |
||
292 | |||
293 | // method "queue.purge-ok" |
||
294 | } |
||
295 | View Code Duplication | if ($method === 31) { |
|
296 | $frame = new Queue\QueuePurgeOkFrame(); |
||
297 | |||
298 | // consume "messageCount" (long) |
||
299 | list(, $frame->messageCount) = \unpack('N', $this->buffer); |
||
300 | $this->buffer = mb_orig_substr($this->buffer, 4); |
||
301 | |||
302 | return $frame; |
||
303 | |||
304 | // method "queue.delete-ok" |
||
305 | } |
||
306 | View Code Duplication | if ($method === 41) { |
|
307 | $frame = new Queue\QueueDeleteOkFrame(); |
||
308 | |||
309 | // consume "messageCount" (long) |
||
310 | list(, $frame->messageCount) = \unpack('N', $this->buffer); |
||
311 | $this->buffer = mb_orig_substr($this->buffer, 4); |
||
312 | |||
313 | return $frame; |
||
314 | |||
315 | // method "queue.unbind-ok" |
||
316 | } |
||
317 | if ($method === 51) { |
||
318 | return new Queue\QueueUnbindOkFrame(); |
||
319 | } |
||
320 | |||
321 | throw new AMQPProtocolException( |
||
322 | 'Frame method (' . $method . ') is invalid for class "queue".' |
||
323 | ); |
||
324 | |||
325 | // class "basic" |
||
326 | } |
||
327 | if ($class === 60) { |
||
328 | // method "basic.qos-ok" |
||
329 | if ($method === 11) { |
||
330 | return new Basic\BasicQosOkFrame(); |
||
331 | |||
332 | // method "basic.consume-ok" |
||
333 | } |
||
334 | View Code Duplication | if ($method === 21) { |
|
335 | $frame = new Basic\BasicConsumeOkFrame(); |
||
336 | |||
337 | // consume "consumer-tag" (shortstr) |
||
338 | $length = \ord($this->buffer); |
||
339 | $frame->consumerTag = mb_orig_substr($this->buffer, 1, $length); |
||
340 | $this->buffer = mb_orig_substr($this->buffer, 1 + $length); |
||
341 | |||
342 | return $frame; |
||
343 | |||
344 | // method "basic.cancel-ok" |
||
345 | } |
||
346 | View Code Duplication | if ($method === 31) { |
|
347 | $frame = new Basic\BasicCancelOkFrame(); |
||
348 | |||
349 | // consume "consumer-tag" (shortstr) |
||
350 | $length = \ord($this->buffer); |
||
351 | $frame->consumerTag = mb_orig_substr($this->buffer, 1, $length); |
||
352 | $this->buffer = mb_orig_substr($this->buffer, 1 + $length); |
||
353 | |||
354 | return $frame; |
||
355 | |||
356 | // method "basic.return" |
||
357 | } |
||
358 | if ($method === 50) { |
||
359 | $frame = new Basic\BasicReturnFrame(); |
||
360 | |||
361 | // consume "replyCode" (short) |
||
362 | list(, $frame->replyCode) = \unpack('n', $this->buffer); |
||
363 | $this->buffer = mb_orig_substr($this->buffer, 2); |
||
364 | |||
365 | // consume "reply-text" (shortstr) |
||
366 | $length = \ord($this->buffer); |
||
367 | $frame->replyText = mb_orig_substr($this->buffer, 1, $length); |
||
368 | $this->buffer = mb_orig_substr($this->buffer, 1 + $length); |
||
369 | |||
370 | // consume "exchange" (shortstr) |
||
371 | $length = \ord($this->buffer); |
||
372 | $frame->exchange = mb_orig_substr($this->buffer, 1, $length); |
||
373 | $this->buffer = mb_orig_substr($this->buffer, 1 + $length); |
||
374 | |||
375 | // consume "routing-key" (shortstr) |
||
376 | $length = \ord($this->buffer); |
||
377 | $frame->routingKey = mb_orig_substr($this->buffer, 1, $length); |
||
378 | $this->buffer = mb_orig_substr($this->buffer, 1 + $length); |
||
379 | |||
380 | return $frame; |
||
381 | |||
382 | // method "basic.deliver" |
||
383 | } |
||
384 | if ($method === 60) { |
||
385 | $frame = new Basic\BasicDeliverFrame(); |
||
386 | |||
387 | // consume "consumer-tag" (shortstr) |
||
388 | $length = \ord($this->buffer); |
||
389 | $frame->consumerTag = mb_orig_substr($this->buffer, 1, $length); |
||
390 | $this->buffer = mb_orig_substr($this->buffer, 1 + $length); |
||
391 | |||
392 | // consume "deliveryTag" (longlong) |
||
393 | list(, $frame->deliveryTag) = \unpack('J', $this->buffer); |
||
394 | $this->buffer = mb_orig_substr($this->buffer, 8); |
||
395 | |||
396 | // consume "redelivered" (bit) |
||
397 | $frame->redelivered = \ord($this->buffer) !== 0; |
||
398 | $this->buffer = mb_orig_substr($this->buffer, 1); |
||
399 | |||
400 | // consume "exchange" (shortstr) |
||
401 | $length = \ord($this->buffer); |
||
402 | $frame->exchange = mb_orig_substr($this->buffer, 1, $length); |
||
403 | $this->buffer = mb_orig_substr($this->buffer, 1 + $length); |
||
404 | |||
405 | // consume "routing-key" (shortstr) |
||
406 | $length = \ord($this->buffer); |
||
407 | $frame->routingKey = mb_orig_substr($this->buffer, 1, $length); |
||
408 | $this->buffer = mb_orig_substr($this->buffer, 1 + $length); |
||
409 | |||
410 | return $frame; |
||
411 | |||
412 | // method "basic.get-ok" |
||
413 | } |
||
414 | if ($method === 71) { |
||
415 | $frame = new Basic\BasicGetOkFrame(); |
||
416 | |||
417 | // consume "deliveryTag" (longlong) |
||
418 | list(, $frame->deliveryTag) = \unpack('J', $this->buffer); |
||
419 | $this->buffer = mb_orig_substr($this->buffer, 8); |
||
420 | |||
421 | // consume "redelivered" (bit) |
||
422 | $frame->redelivered = \ord($this->buffer) !== 0; |
||
423 | $this->buffer = mb_orig_substr($this->buffer, 1); |
||
424 | |||
425 | // consume "exchange" (shortstr) |
||
426 | $length = \ord($this->buffer); |
||
427 | $frame->exchange = mb_orig_substr($this->buffer, 1, $length); |
||
428 | $this->buffer = mb_orig_substr($this->buffer, 1 + $length); |
||
429 | |||
430 | // consume "routing-key" (shortstr) |
||
431 | $length = \ord($this->buffer); |
||
432 | $frame->routingKey = mb_orig_substr($this->buffer, 1, $length); |
||
433 | $this->buffer = mb_orig_substr($this->buffer, 1 + $length); |
||
434 | |||
435 | // consume "messageCount" (long) |
||
436 | list(, $frame->messageCount) = \unpack('N', $this->buffer); |
||
437 | $this->buffer = mb_orig_substr($this->buffer, 4); |
||
438 | |||
439 | return $frame; |
||
440 | |||
441 | // method "basic.get-empty" |
||
442 | } |
||
443 | View Code Duplication | if ($method === 72) { |
|
444 | $frame = new Basic\BasicGetEmptyFrame(); |
||
445 | |||
446 | // consume "cluster-id" (shortstr) |
||
447 | $length = \ord($this->buffer); |
||
448 | $frame->clusterId = mb_orig_substr($this->buffer, 1, $length); |
||
449 | $this->buffer = mb_orig_substr($this->buffer, 1 + $length); |
||
450 | |||
451 | return $frame; |
||
452 | |||
453 | // method "basic.ack" |
||
454 | } |
||
455 | if ($method === 80) { |
||
456 | $frame = new Basic\BasicAckFrame(); |
||
457 | |||
458 | // consume "deliveryTag" (longlong) |
||
459 | list(, $frame->deliveryTag) = \unpack('J', $this->buffer); |
||
460 | $this->buffer = mb_orig_substr($this->buffer, 8); |
||
461 | |||
462 | // consume "multiple" (bit) |
||
463 | $frame->multiple = \ord($this->buffer) !== 0; |
||
464 | $this->buffer = mb_orig_substr($this->buffer, 1); |
||
465 | |||
466 | return $frame; |
||
467 | |||
468 | // method "basic.recover-ok" |
||
469 | } |
||
470 | if ($method === 111) { |
||
471 | return new Basic\BasicRecoverOkFrame(); |
||
472 | |||
473 | // method "basic.nack" |
||
474 | } |
||
475 | if ($method === 120) { |
||
476 | $frame = new Basic\BasicNackFrame(); |
||
477 | |||
478 | // consume "deliveryTag" (longlong) |
||
479 | list(, $frame->deliveryTag) = \unpack('J', $this->buffer); |
||
480 | $this->buffer = mb_orig_substr($this->buffer, 8); |
||
481 | |||
482 | // consume "multiple" (bit) |
||
483 | // consume "requeue" (bit) |
||
484 | $octet = \ord($this->buffer); |
||
485 | $this->buffer = mb_orig_substr($this->buffer, 1); |
||
486 | $frame->multiple = $octet & 1 !== 0; |
||
487 | $frame->requeue = $octet & 2 !== 0; |
||
488 | |||
489 | return $frame; |
||
490 | } |
||
491 | |||
492 | throw new AMQPProtocolException( |
||
493 | 'Frame method (' . $method . ') is invalid for class "basic".' |
||
494 | ); |
||
495 | |||
496 | // class "tx" |
||
497 | } |
||
498 | if ($class === 90) { |
||
499 | |||
500 | // method "tx.select-ok" |
||
501 | if ($method === 11) { |
||
502 | return new Tx\TxSelectOkFrame(); |
||
503 | |||
504 | // method "tx.commit-ok" |
||
505 | } |
||
506 | if ($method === 21) { |
||
507 | return new Tx\TxCommitOkFrame(); |
||
508 | |||
509 | // method "tx.rollback-ok" |
||
510 | } |
||
511 | if ($method === 31) { |
||
512 | return new Tx\TxRollbackOkFrame(); |
||
513 | } |
||
514 | |||
515 | throw new AMQPProtocolException( |
||
516 | 'Frame method (' . $method . ') is invalid for class "tx".' |
||
517 | ); |
||
518 | |||
519 | // class "confirm" |
||
520 | } |
||
521 | if ($class === 85) { |
||
522 | |||
523 | // method "confirm.select-ok" |
||
524 | if ($method === 11) { |
||
525 | return new Confirm\ConfirmSelectOkFrame(); |
||
526 | } |
||
527 | |||
528 | throw new AMQPProtocolException( |
||
529 | 'Frame method (' . $method . ') is invalid for class "confirm".' |
||
530 | ); |
||
531 | } |
||
532 | |||
533 | throw new AMQPProtocolException('Frame class (' . $class . ') is invalid.'); |
||
534 | } |
||
535 | } |
||
536 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: