Conditions | 11 |
Paths | 24 |
Total Lines | 100 |
Code Lines | 57 |
Lines | 0 |
Ratio | 0 % |
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 |
||
76 | public function feed($buffer, &$requiredBytes = 0) |
||
77 | { |
||
78 | $this->buffer .= $buffer; |
||
79 | $availableBytes = \strlen($this->buffer); |
||
80 | |||
81 | // not enough bytes for a frame ... |
||
82 | if ($availableBytes < $this->requiredBytes) { |
||
83 | $requiredBytes = $this->requiredBytes; |
||
84 | |||
85 | return null; |
||
86 | |||
87 | // we're still looking for the header ... |
||
88 | } |
||
89 | if ($this->requiredBytes === self::MINIMUM_FRAME_SIZE) { |
||
90 | // now that we know the payload size we can add that to the number |
||
91 | // of required bytes ... |
||
92 | $this->requiredBytes += \unpack( |
||
93 | 'N', |
||
94 | \substr( |
||
95 | $this->buffer, |
||
96 | self::HEADER_TYPE_SIZE + self::HEADER_CHANNEL_SIZE, |
||
97 | self::HEADER_PAYLOAD_LENGTH_SIZE |
||
98 | ) |
||
99 | )[1]; |
||
100 | |||
101 | // taking the payload into account we still don't have enough bytes |
||
102 | // for the frame ... |
||
103 | if ($availableBytes < $this->requiredBytes) { |
||
104 | $requiredBytes = $this->requiredBytes; |
||
105 | |||
106 | return null; |
||
107 | } |
||
108 | } |
||
109 | |||
110 | // we've got enough bytes, check that the last byte is the end marker ... |
||
111 | if (\ord($this->buffer[$this->requiredBytes - 1]) !== Constants::FRAME_END) { |
||
112 | throw new AMQPProtocolException( |
||
113 | sprintf( |
||
114 | 'Frame end marker (0x%02x) is invalid.', |
||
115 | \ord($this->buffer[$this->requiredBytes - 1]) |
||
116 | ) |
||
117 | ); |
||
118 | } |
||
119 | |||
120 | // read the (t)ype and (c)hannel then discard the header ... |
||
121 | $fields = \unpack('Ct/nc', $this->buffer); |
||
122 | $this->buffer = \substr($this->buffer, self::HEADER_SIZE); |
||
123 | |||
124 | $type = $fields['t']; |
||
125 | |||
126 | // read the frame ... |
||
127 | if ($type === Constants::FRAME_METHOD) { |
||
128 | $frame = $this->parseMethodFrame(); |
||
129 | } elseif ($type === Constants::FRAME_HEADER) { |
||
130 | $frame = $this->parseHeaderFrame(); |
||
131 | } elseif ($type === Constants::FRAME_BODY) { |
||
132 | $length = $this->requiredBytes - self::MINIMUM_FRAME_SIZE; |
||
133 | $frame = new BodyFrame(); |
||
134 | $frame->content = \substr($this->buffer, 0, $length); |
||
135 | $this->buffer = \substr($this->buffer, $length); |
||
136 | } elseif ($type === Constants::FRAME_HEARTBEAT) { |
||
137 | if (self::MINIMUM_FRAME_SIZE !== $this->requiredBytes) { |
||
138 | throw new AMQPProtocolException( |
||
139 | sprintf( |
||
140 | 'Heartbeat frame payload size (%d) is invalid, must be zero.', |
||
141 | $this->requiredBytes - self::MINIMUM_FRAME_SIZE |
||
142 | ) |
||
143 | ); |
||
144 | } |
||
145 | $frame = new HeartbeatFrame(); |
||
146 | } else { |
||
147 | throw new AMQPProtocolException( |
||
148 | sprintf( |
||
149 | 'Frame type (0x%02x) is invalid.', |
||
150 | $type |
||
151 | ) |
||
152 | ); |
||
153 | } |
||
154 | |||
155 | // discard the end marker ... |
||
156 | $this->buffer = \substr($this->buffer, 1); |
||
157 | |||
158 | $consumedBytes = $availableBytes - \strlen($this->buffer); |
||
159 | |||
160 | // the frame lied about its payload size ... |
||
161 | if ($consumedBytes !== $this->requiredBytes) { |
||
162 | throw new AMQPProtocolException( |
||
163 | sprintf( |
||
164 | 'Mismatch between frame size (%s) and consumed bytes (%s).', |
||
165 | $this->requiredBytes, |
||
166 | $consumedBytes |
||
167 | ) |
||
168 | ); |
||
169 | } |
||
170 | |||
171 | $this->requiredBytes = $requiredBytes = self::MINIMUM_FRAME_SIZE; |
||
172 | $frame->frameChannelId = $fields['c']; |
||
173 | |||
174 | return $frame; |
||
175 | } |
||
176 | } |
||
177 |