| Conditions | 9 |
| Paths | 9 |
| Total Lines | 60 |
| Code Lines | 41 |
| 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 |
||
| 78 | public function read(): Generator |
||
| 79 | { |
||
| 80 | $sndbuf = socket_get_option($this->socket, SOL_SOCKET, SO_SNDBUF); |
||
| 81 | $rcvbuf = socket_get_option($this->socket, SOL_SOCKET, SO_RCVBUF); |
||
| 82 | |||
| 83 | socket_set_option($this->socket, SOL_SOCKET, SO_RCVTIMEO, ['sec' => 2, 'usec' => 0]); |
||
| 84 | socket_set_option($this->socket, SOL_SOCKET, SO_RCVBUF, 1024 * 10); |
||
| 85 | socket_set_option($this->socket, SOL_SOCKET, SO_SNDBUF, 1024 * 10); |
||
| 86 | |||
| 87 | $newFrameAwaitRepeat = 0; |
||
| 88 | $maxFrameAwaitRepeats = 10; |
||
| 89 | $maxRepeats = 10; |
||
| 90 | |||
| 91 | while (true) { |
||
| 92 | if (!socket_recv($this->socket, $header, 8, MSG_WAITALL)) { |
||
| 93 | $socket_last_error = socket_last_error($this->socket); |
||
| 94 | $newFrameAwaitRepeat++; |
||
| 95 | if ($newFrameAwaitRepeat === $maxFrameAwaitRepeats) { |
||
| 96 | $newFrameAwaitRepeat = 0; |
||
| 97 | yield [self::TYPE_RELEASE, $socket_last_error, socket_strerror($socket_last_error)]; |
||
| 98 | } |
||
| 99 | if ($socket_last_error === 35) { |
||
| 100 | usleep(self::DEFAULT_TIMEOUT); |
||
| 101 | continue; |
||
| 102 | } |
||
| 103 | $this->close(); |
||
| 104 | yield [self::TYPE_ERROR, $socket_last_error, socket_strerror($socket_last_error)]; |
||
| 105 | continue; |
||
| 106 | } |
||
| 107 | |||
| 108 | $length = unpack('P', $header); |
||
| 109 | $localBuffer = ''; |
||
| 110 | $bytesToRead = $length[1]; |
||
| 111 | $bytesRead = 0; |
||
| 112 | //$value = 2 ** ((int) ($bytesToRead / 2)); |
||
| 113 | //socket_set_option($this->socket, SOL_SOCKET, SO_RCVBUF, $value); |
||
| 114 | $repeat = 0; |
||
| 115 | while ($bytesRead < $bytesToRead) { |
||
| 116 | //$buffer = socket_read($this->socket, $bytesToRead - $bytesRead); |
||
| 117 | //$bufferLength = strlen($buffer); |
||
| 118 | $bufferLength = socket_recv($this->socket, $buffer, min($bytesToRead - $bytesRead, self::DEFAULT_BUFFER_SIZE), MSG_DONTWAIT); |
||
| 119 | if ($bufferLength === false) { |
||
| 120 | if ($repeat === $maxRepeats) { |
||
| 121 | break; |
||
| 122 | } |
||
| 123 | //if ($bufferLength === false) { |
||
| 124 | $socket_last_error = socket_last_error($this->socket); |
||
| 125 | if ($socket_last_error === 35) { |
||
| 126 | $repeat++; |
||
| 127 | usleep(self::DEFAULT_TIMEOUT * 5); |
||
| 128 | continue; |
||
| 129 | } |
||
| 130 | $this->close(); |
||
| 131 | break; |
||
| 132 | } |
||
| 133 | |||
| 134 | $localBuffer .= $buffer; |
||
| 135 | $bytesRead += $bufferLength; |
||
| 136 | } |
||
| 137 | yield [self::TYPE_RESULT, base64_decode($localBuffer)]; |
||
| 138 | } |
||
| 207 |