| Conditions | 16 |
| Paths | 16385 |
| Total Lines | 113 |
| Code Lines | 62 |
| Lines | 62 |
| Ratio | 54.87 % |
| 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 |
||
| 21 | private function parseHeaderFrame() |
||
| 22 | { |
||
| 23 | $fields = \unpack('na/nb/Jc/nd', $this->buffer); |
||
|
|
|||
| 24 | $this->buffer = \substr($this->buffer, 14); |
||
| 25 | |||
| 26 | $class = $fields['a']; |
||
| 27 | $flags = $fields['d']; |
||
| 28 | |||
| 29 | // class "basic" |
||
| 30 | if ($class === 60) { |
||
| 31 | $frame = new Basic\BasicHeaderFrame(); |
||
| 32 | $frame->contentLength = $fields['c']; |
||
| 33 | |||
| 34 | // consume "content-type" (shortstr) |
||
| 35 | View Code Duplication | if ($flags & 32768) { |
|
| 36 | $length = \ord($this->buffer); |
||
| 37 | $frame->contentType = \substr($this->buffer, 1, $length); |
||
| 38 | $this->buffer = \substr($this->buffer, 1 + $length); |
||
| 39 | } |
||
| 40 | |||
| 41 | // consume "content-encoding" (shortstr) |
||
| 42 | View Code Duplication | if ($flags & 16384) { |
|
| 43 | $length = \ord($this->buffer); |
||
| 44 | $frame->contentEncoding = \substr($this->buffer, 1, $length); |
||
| 45 | $this->buffer = \substr($this->buffer, 1 + $length); |
||
| 46 | } |
||
| 47 | |||
| 48 | // consume "headers" (table)e |
||
| 49 | if ($flags & 8192) { |
||
| 50 | $frame->headers = $this->tableParser->parse($this->buffer); |
||
| 51 | } |
||
| 52 | |||
| 53 | // consume "delivery-mode" (octet) |
||
| 54 | View Code Duplication | if ($flags & 4096) { |
|
| 55 | list(, $frame->deliveryMode) = \unpack('c', $this->buffer); |
||
| 56 | $this->buffer = \substr($this->buffer, 1); |
||
| 57 | } |
||
| 58 | |||
| 59 | // consume "priority" (octet) |
||
| 60 | View Code Duplication | if ($flags & 2048) { |
|
| 61 | list(, $frame->priority) = \unpack('c', $this->buffer); |
||
| 62 | $this->buffer = \substr($this->buffer, 1); |
||
| 63 | } |
||
| 64 | |||
| 65 | // consume "correlation-id" (shortstr) |
||
| 66 | View Code Duplication | if ($flags & 1024) { |
|
| 67 | $length = \ord($this->buffer); |
||
| 68 | $frame->correlationId = \substr($this->buffer, 1, $length); |
||
| 69 | $this->buffer = \substr($this->buffer, 1 + $length); |
||
| 70 | } |
||
| 71 | |||
| 72 | // consume "reply-to" (shortstr) |
||
| 73 | View Code Duplication | if ($flags & 512) { |
|
| 74 | $length = \ord($this->buffer); |
||
| 75 | $frame->replyTo = \substr($this->buffer, 1, $length); |
||
| 76 | $this->buffer = \substr($this->buffer, 1 + $length); |
||
| 77 | } |
||
| 78 | |||
| 79 | // consume "expiration" (shortstr) |
||
| 80 | View Code Duplication | if ($flags & 256) { |
|
| 81 | $length = \ord($this->buffer); |
||
| 82 | $frame->expiration = \substr($this->buffer, 1, $length); |
||
| 83 | $this->buffer = \substr($this->buffer, 1 + $length); |
||
| 84 | } |
||
| 85 | |||
| 86 | // consume "message-id" (shortstr) |
||
| 87 | View Code Duplication | if ($flags & 128) { |
|
| 88 | $length = \ord($this->buffer); |
||
| 89 | $frame->messageId = \substr($this->buffer, 1, $length); |
||
| 90 | $this->buffer = \substr($this->buffer, 1 + $length); |
||
| 91 | } |
||
| 92 | |||
| 93 | // consume "timestamp" (timestamp) |
||
| 94 | View Code Duplication | if ($flags & 64) { |
|
| 95 | list(, $frame->timestamp) = \unpack('J', $this->buffer); |
||
| 96 | $this->buffer = \substr($this->buffer, 8); |
||
| 97 | } |
||
| 98 | |||
| 99 | // consume "type" (shortstr) |
||
| 100 | View Code Duplication | if ($flags & 32) { |
|
| 101 | $length = \ord($this->buffer); |
||
| 102 | $frame->type = \substr($this->buffer, 1, $length); |
||
| 103 | $this->buffer = \substr($this->buffer, 1 + $length); |
||
| 104 | } |
||
| 105 | |||
| 106 | // consume "user-id" (shortstr) |
||
| 107 | View Code Duplication | if ($flags & 16) { |
|
| 108 | $length = \ord($this->buffer); |
||
| 109 | $frame->userId = \substr($this->buffer, 1, $length); |
||
| 110 | $this->buffer = \substr($this->buffer, 1 + $length); |
||
| 111 | } |
||
| 112 | |||
| 113 | // consume "app-id" (shortstr) |
||
| 114 | View Code Duplication | if ($flags & 8) { |
|
| 115 | $length = \ord($this->buffer); |
||
| 116 | $frame->appId = \substr($this->buffer, 1, $length); |
||
| 117 | $this->buffer = \substr($this->buffer, 1 + $length); |
||
| 118 | } |
||
| 119 | |||
| 120 | // consume "cluster-id" (shortstr) |
||
| 121 | View Code Duplication | if ($flags & 4) { |
|
| 122 | $length = \ord($this->buffer); |
||
| 123 | $frame->clusterId = \substr($this->buffer, 1, $length); |
||
| 124 | $this->buffer = \substr($this->buffer, 1 + $length); |
||
| 125 | } |
||
| 126 | |||
| 127 | return $frame; |
||
| 128 | } |
||
| 129 | |||
| 130 | throw new AMQPProtocolException( |
||
| 131 | 'Frame class (' . $class . ') is invalid or does not support content frames.' |
||
| 132 | ); |
||
| 133 | } |
||
| 134 | } |
||
| 135 |
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: