| Conditions | 22 |
| Paths | 0 |
| Total Lines | 92 |
| Code Lines | 70 |
| Lines | 5 |
| Ratio | 5.43 % |
| Changes | 3 | ||
| 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 |
||
| 86 | public function onRead() |
||
| 87 | { |
||
| 88 | start: |
||
| 89 | if ($this->state === static::STATE_HEADER) { |
||
| 90 | $l = $this->getInputLength(); |
||
| 91 | if ($l < 2) { |
||
| 92 | return; |
||
| 93 | } |
||
| 94 | $hdr = $this->look(2); |
||
| 95 | $fb = Binary::getbitmap(ord($hdr)); |
||
| 96 | $fin = (bool)$fb{0}; |
||
| 97 | $opCode = bindec(substr($fb, 4, 4)); |
||
| 98 | |||
| 99 | if (isset($this->opCodes[$opCode])) { |
||
| 100 | $this->type = $this->opCodes[$opCode]; |
||
| 101 | } else { |
||
| 102 | $this->log('opCode: ' . $opCode . ': unknown frame type'); |
||
| 103 | $this->finish(); |
||
| 104 | return; |
||
| 105 | } |
||
| 106 | $sb = ord(mb_orig_substr($hdr, 1)); |
||
| 107 | $sbm = Binary::getbitmap($sb); |
||
| 108 | $this->isMasked = (bool)$sbm{0}; |
||
| 109 | $payloadLength = $sb & 127; |
||
| 110 | |||
| 111 | if ($payloadLength <= 125) { |
||
| 112 | $this->drain(2); |
||
| 113 | $this->pctLength = $payloadLength; |
||
| 114 | } elseif ($payloadLength === 126) { |
||
| 115 | if ($l < 4) { |
||
| 116 | return; |
||
| 117 | } |
||
| 118 | $this->drain(2); |
||
| 119 | $this->pctLength = Binary::b2i($this->read(2)); |
||
| 120 | } elseif ($payloadLength === 127) { |
||
| 121 | if ($l < 10) { |
||
| 122 | return; |
||
| 123 | } |
||
| 124 | $this->drain(2); |
||
| 125 | $this->pctLength = Binary::b2i($this->read(8)); |
||
| 126 | } |
||
| 127 | |||
| 128 | View Code Duplication | if ($this->pool->maxAllowedPacket < $this->pctLength) { |
|
| 129 | Daemon::$process->log('max-allowed-packet (' . $this->pool->config->maxallowedpacket->getHumanValue() . ') exceed, aborting connection'); |
||
| 130 | $this->finish(); |
||
| 131 | return; |
||
| 132 | } |
||
| 133 | $this->setWatermark($this->pctLength + ($this->isMasked ? 4 : 0)); |
||
| 134 | $this->state = static::STATE_DATA; |
||
| 135 | } |
||
| 136 | |||
| 137 | if ($this->state === static::STATE_DATA) { |
||
| 138 | if ($this->getInputLength() < $this->pctLength + ($this->isMasked ? 4 : 0)) { |
||
| 139 | return; |
||
| 140 | } |
||
| 141 | $this->state = static::STATE_HEADER; |
||
| 142 | $this->setWatermark(2); |
||
| 143 | if ($this->isMasked) { |
||
| 144 | $this->trigger('frame', static::mask($this->read(4), $this->read($this->pctLength))); |
||
| 145 | } else { |
||
| 146 | $this->trigger('frame', $this->read($this->pctLength)); |
||
| 147 | } |
||
| 148 | } |
||
| 149 | if ($this->state === static::STATE_STANDBY) { |
||
| 150 | while (($line = $this->readLine()) !== null) { |
||
| 151 | $line = trim($line); |
||
| 152 | if ($line === '') { |
||
| 153 | $expectedKey = base64_encode(pack('H*', sha1($this->key . static::GUID))); |
||
| 154 | if (isset($this->headers['HTTP_SEC_WEBSOCKET_ACCEPT']) && $expectedKey === $this->headers['HTTP_SEC_WEBSOCKET_ACCEPT']) { |
||
| 155 | $this->state = static::STATE_HEADER; |
||
| 156 | if ($this->onConnected) { |
||
| 157 | $this->connected = true; |
||
| 158 | $this->onConnected->executeAll($this); |
||
| 159 | $this->onConnected = null; |
||
| 160 | } |
||
| 161 | $this->trigger('connected'); |
||
| 162 | goto start; |
||
| 163 | } else { |
||
| 164 | Daemon::$process->log(__METHOD__ . ': Handshake failed. Connection to ' . $this->url . ' failed.'); |
||
| 165 | $this->finish(); |
||
| 166 | } |
||
| 167 | } else { |
||
| 168 | $e = explode(': ', $line); |
||
| 169 | if (isset($e[1])) { |
||
| 170 | $this->headers['HTTP_' . strtoupper(strtr($e[0], ['-' => '_']))] = $e[1]; |
||
| 171 | } |
||
| 172 | } |
||
| 173 | } |
||
| 174 | return; |
||
| 175 | } |
||
| 176 | goto start; |
||
| 177 | } |
||
| 178 | |||
| 268 |
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.