| Conditions | 18 |
| Paths | 163 |
| Total Lines | 73 |
| Code Lines | 51 |
| 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 |
||
| 156 | public function onRead() { |
||
| 157 | if ($this->state === self::STATE_PREHANDSHAKE) { |
||
| 158 | if (!$this->handshake()) { |
||
| 159 | return; |
||
| 160 | } |
||
| 161 | } |
||
| 162 | if ($this->state === self::STATE_HANDSHAKED) { |
||
| 163 | |||
| 164 | while (($buflen = $this->getInputLength()) >= 2) { |
||
| 165 | $first = ord($this->look(1)); // first byte integer (fin, opcode) |
||
| 166 | $firstBits = decbin($first); |
||
| 167 | $opcode = (int)bindec(substr($firstBits, 4, 4)); |
||
| 168 | if ($opcode === 0x8) { // CLOSE |
||
| 169 | $this->finish(); |
||
| 170 | return; |
||
| 171 | } |
||
| 172 | $opcodeName = isset(static::$opcodes[$opcode]) ? static::$opcodes[$opcode] : false; |
||
| 173 | if (!$opcodeName) { |
||
| 174 | Daemon::log(get_class($this) . ': Undefined opcode ' . $opcode); |
||
| 175 | $this->finish(); |
||
| 176 | return; |
||
| 177 | } |
||
| 178 | $second = ord($this->look(1, 1)); // second byte integer (masked, payload length) |
||
| 179 | $fin = (bool)($first >> 7); |
||
| 180 | $isMasked = (bool)($second >> 7); |
||
| 181 | $dataLength = $second & 0x7f; |
||
| 182 | $p = 2; |
||
| 183 | if ($dataLength === 0x7e) { // 2 bytes-length |
||
| 184 | if ($buflen < $p + 2) { |
||
| 185 | return; // not enough data yet |
||
| 186 | } |
||
| 187 | $dataLength = Binary::bytes2int($this->look(2, $p), false); |
||
| 188 | $p += 2; |
||
| 189 | } elseif ($dataLength === 0x7f) { // 8 bytes-length |
||
| 190 | if ($buflen < $p + 8) { |
||
| 191 | return; // not enough data yet |
||
| 192 | } |
||
| 193 | $dataLength = Binary::bytes2int($this->look(8, $p)); |
||
| 194 | $p += 8; |
||
| 195 | } |
||
| 196 | if ($this->pool->maxAllowedPacket <= $dataLength) { |
||
| 197 | // Too big packet |
||
| 198 | $this>finish(); |
||
| 199 | return; |
||
| 200 | } |
||
| 201 | if ($isMasked) { |
||
| 202 | if ($buflen < $p + 4) { |
||
| 203 | return; // not enough data yet |
||
| 204 | } |
||
| 205 | $mask = $this->look(4, $p); |
||
| 206 | $p += 4; |
||
| 207 | } |
||
| 208 | if ($buflen < $p + $dataLength) { |
||
| 209 | return; // not enough data yet |
||
| 210 | } |
||
| 211 | $this->drain($p); |
||
| 212 | $data = $this->read($dataLength); |
||
| 213 | if ($isMasked) { |
||
| 214 | $data = $this->mask($data, $mask); |
||
| 215 | } |
||
| 216 | //Daemon::log(Debug::dump(array('ext' => $this->extensions, 'rsv1' => $firstBits[1], 'data' => Debug::exportBytes($data)))); |
||
| 217 | /*if ($firstBits[1] && in_array('deflate-frame', $this->extensions)) { // deflate frame |
||
| 218 | $data = gzuncompress($data, $this->pool->maxAllowedPacket); |
||
| 219 | }*/ |
||
| 220 | if (!$fin) { |
||
| 221 | $this->framebuf .= $data; |
||
| 222 | } else { |
||
| 223 | $this->onFrame($this->framebuf . $data, $opcodeName); |
||
| 224 | $this->framebuf = ''; |
||
| 225 | } |
||
| 226 | } |
||
| 227 | } |
||
| 228 | } |
||
| 229 | } |
||
| 230 |
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.