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