Conditions | 18 |
Paths | 163 |
Total Lines | 73 |
Code Lines | 51 |
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 |
||
181 | public function onRead() |
||
182 | { |
||
183 | if ($this->state === self::STATE_PREHANDSHAKE) { |
||
184 | if (!$this->handshake()) { |
||
185 | return; |
||
186 | } |
||
187 | } |
||
188 | if ($this->state === self::STATE_HANDSHAKED) { |
||
189 | while (($buflen = $this->getInputLength()) >= 2) { |
||
190 | $first = ord($this->look(1)); // first byte integer (fin, opcode) |
||
191 | $firstBits = decbin($first); |
||
192 | $opcode = (int)bindec(substr($firstBits, 4, 4)); |
||
193 | if ($opcode === 0x8) { // CLOSE |
||
194 | $this->finish(); |
||
195 | return; |
||
196 | } |
||
197 | $opcodeName = isset(static::$opcodes[$opcode]) ? static::$opcodes[$opcode] : false; |
||
198 | if (!$opcodeName) { |
||
199 | Daemon::log(get_class($this) . ': Undefined opcode ' . $opcode); |
||
200 | $this->finish(); |
||
201 | return; |
||
202 | } |
||
203 | $second = ord($this->look(1, 1)); // second byte integer (masked, payload length) |
||
204 | $fin = (bool)($first >> 7); |
||
205 | $isMasked = (bool)($second >> 7); |
||
206 | $dataLength = $second & 0x7f; |
||
207 | $p = 2; |
||
208 | if ($dataLength === 0x7e) { // 2 bytes-length |
||
209 | if ($buflen < $p + 2) { |
||
210 | return; // not enough data yet |
||
211 | } |
||
212 | $dataLength = Binary::bytes2int($this->look(2, $p), false); |
||
213 | $p += 2; |
||
214 | } elseif ($dataLength === 0x7f) { // 8 bytes-length |
||
215 | if ($buflen < $p + 8) { |
||
216 | return; // not enough data yet |
||
217 | } |
||
218 | $dataLength = Binary::bytes2int($this->look(8, $p)); |
||
219 | $p += 8; |
||
220 | } |
||
221 | if ($this->pool->maxAllowedPacket <= $dataLength) { |
||
222 | // Too big packet |
||
223 | $this->finish(); |
||
224 | return; |
||
225 | } |
||
226 | if ($isMasked) { |
||
227 | if ($buflen < $p + 4) { |
||
228 | return; // not enough data yet |
||
229 | } |
||
230 | $mask = $this->look(4, $p); |
||
231 | $p += 4; |
||
232 | } |
||
233 | if ($buflen < $p + $dataLength) { |
||
234 | return; // not enough data yet |
||
235 | } |
||
236 | $this->drain($p); |
||
237 | $data = $this->read($dataLength); |
||
238 | if ($isMasked) { |
||
239 | $data = $this->mask($data, $mask); |
||
240 | } |
||
241 | //Daemon::log(Debug::dump(array('ext' => $this->extensions, 'rsv1' => $firstBits[1], 'data' => Debug::exportBytes($data)))); |
||
242 | /*if ($firstBits[1] && in_array('deflate-frame', $this->extensions)) { // deflate frame |
||
243 | $data = gzuncompress($data, $this->pool->maxAllowedPacket); |
||
244 | }*/ |
||
245 | if (!$fin) { |
||
246 | $this->framebuf .= $data; |
||
247 | } else { |
||
248 | $this->onFrame($this->framebuf . $data, $opcodeName); |
||
249 | $this->framebuf = ''; |
||
250 | } |
||
251 | } |
||
252 | } |
||
253 | } |
||
254 | } |
||
255 |
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.