Conditions | 31 |
Paths | 0 |
Total Lines | 147 |
Code Lines | 120 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 1 | Features | 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 |
||
125 | protected function onRead() |
||
126 | { |
||
127 | start: |
||
128 | if ($this->state === static::STATE_STANDBY) { |
||
129 | if (($hdr = $this->readExact(2)) === false) { |
||
130 | return; // not enough data |
||
131 | } |
||
132 | |||
133 | $u = unpack('S', $hdr); |
||
134 | $this->responseCode = $u[1]; |
||
135 | $this->state = static::STATE_PACKET_HDR; |
||
136 | } |
||
137 | if ($this->state === static::STATE_PACKET_HDR) { |
||
138 | if ($this->responseCode === static::REPL_KVAL) { |
||
139 | $this->result = []; |
||
140 | if (($hdr = $this->readExact(9)) === false) { |
||
141 | return; // not enough data |
||
142 | } |
||
143 | $this->encoding = Binary::getByte($hdr); |
||
144 | $this->responseLength = Binary::getDword($hdr, true) - 4; |
||
145 | $this->totalNum = Binary::getDword($hdr, true); |
||
146 | $this->readedNum = 0; |
||
147 | $this->state = static::STATE_PACKET_DATA; |
||
148 | } else { |
||
149 | if (($hdr = $this->lookExact(5)) === false) { |
||
150 | return; // not enough data |
||
151 | } |
||
152 | $this->encoding = Binary::getByte($hdr); |
||
153 | $pl = Binary::getDword($hdr, true); |
||
154 | if ($this->getInputLength() < 5 + $pl) { |
||
155 | return; // not enough data |
||
156 | } |
||
157 | $this->drain(5); |
||
158 | $this->responseLength = $pl; |
||
159 | if ($this->responseLength > $this->pool->maxAllowedPacket) { |
||
160 | $this->log('max-allowed-packet (' . $this->pool->config->maxallowedpacket->getHumanValue() . ') exceed, aborting connection'); |
||
161 | $this->finish(); |
||
162 | return; |
||
163 | } |
||
164 | if ($this->responseCode === static::REPL_ERR_NOT_FOUND) { |
||
165 | $this->drain($this->responseLength); |
||
166 | $this->result = null; |
||
167 | $this->isFinal = true; |
||
168 | $this->totalNum = 0; |
||
169 | $this->readedNum = 0; |
||
170 | $this->executeCb(); |
||
171 | } elseif ($this->responseCode === static::REPL_OK) { |
||
172 | $this->drain($this->responseLength); |
||
173 | $this->result = true; |
||
174 | $this->isFinal = true; |
||
175 | $this->totalNum = 0; |
||
176 | $this->readedNum = 0; |
||
177 | $this->executeCb(); |
||
178 | } elseif (($this->responseCode === static::REPL_ERR_MEM) || |
||
179 | ($this->responseCode === static::REPL_ERR_NAN) || |
||
180 | ($this->responseCode === static::REPL_ERR_LOCKED) |
||
181 | ) { |
||
182 | $this->drain($this->responseLength); |
||
183 | $this->result = false; |
||
184 | $this->isFinal = true; |
||
185 | $this->totalNum = 0; |
||
186 | $this->readedNum = 0; |
||
187 | $this->executeCb(); |
||
188 | } else { |
||
189 | if ($this->responseCode === static::REPL_KVAL && $this->totalNum <= 0) { |
||
190 | $this->drain($this->responseLength); |
||
191 | $this->isFinal = true; |
||
192 | $this->totalNum = 0; |
||
193 | $this->readedNum = 0; |
||
194 | $this->result = []; |
||
195 | $this->executeCb(); |
||
196 | } else { |
||
197 | $this->state = static::STATE_PACKET_DATA; |
||
198 | } |
||
199 | } |
||
200 | } |
||
201 | } |
||
202 | if ($this->state === static::STATE_PACKET_DATA) { |
||
203 | if ($this->responseCode === static::REPL_KVAL) { |
||
204 | $keyAdded = false; |
||
205 | nextElement: |
||
206 | $l = $this->getInputLength(); |
||
207 | if ($l < 9) { |
||
208 | goto cursorCall; |
||
209 | } |
||
210 | if (($hdr = $this->lookExact($o = 4)) === false) { |
||
211 | goto cursorCall; |
||
212 | } |
||
213 | $keyLen = Binary::getDword($hdr, true); |
||
214 | if (($key = $this->lookExact($keyLen, $o)) === false) { |
||
215 | goto cursorCall; |
||
216 | } |
||
217 | $o += $keyLen; |
||
218 | if (($encoding = $this->lookExact(1, $o)) === false) { |
||
219 | goto cursorCall; |
||
220 | } |
||
221 | $encoding = ord($encoding); |
||
222 | ++$o; |
||
223 | if (($hdr = $this->lookExact(4, $o)) === false) { |
||
224 | goto cursorCall; |
||
225 | } |
||
226 | $o += 4; |
||
227 | $valLen = Binary::getDword($hdr, true); |
||
228 | if ($o + $valLen > $l) { |
||
229 | goto cursorCall; |
||
230 | } |
||
231 | $this->drain($o); |
||
232 | if ($encoding === static::GB_ENC_NUMBER) { |
||
233 | $val = $this->read($valLen); |
||
234 | $this->result[$key] = $valLen === 8 |
||
235 | ? Binary::getQword($val, true) |
||
236 | : Binary::getDword($val, true); |
||
237 | } else { |
||
238 | $this->result[$key] = $this->read($valLen); |
||
239 | } |
||
240 | $keyAdded = true; |
||
241 | if (++$this->readedNum >= $this->totalNum) { |
||
242 | $this->isFinal = true; |
||
243 | $this->executeCb(); |
||
244 | goto start; |
||
245 | } else { |
||
246 | goto nextElement; |
||
247 | } |
||
248 | cursorCall: |
||
249 | if ($keyAdded) { |
||
250 | $this->onResponse->executeAndKeepOne($this); |
||
251 | } |
||
252 | return; |
||
253 | } else { |
||
254 | if (($this->result = $this->readExact($this->responseLength)) === false) { |
||
255 | $this->setWatermark($this->responseLength); |
||
256 | return; |
||
257 | } |
||
258 | $this->setWatermark(2, $this->pool->maxAllowedPacket); |
||
259 | if ($this->encoding === static::GB_ENC_NUMBER) { |
||
260 | $this->result = $this->responseLength === 8 |
||
261 | ? Binary::getQword($this->result, true) |
||
262 | : Binary::getDword($this->result, true); |
||
263 | } |
||
264 | $this->isFinal = true; |
||
265 | $this->totalNum = 1; |
||
266 | $this->readedNum = 1; |
||
267 | $this->executeCb(); |
||
268 | } |
||
269 | } |
||
270 | goto start; |
||
271 | } |
||
272 | |||
290 |
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.