| Conditions | 15 |
| Paths | 41 |
| Total Lines | 84 |
| Code Lines | 61 |
| 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 |
||
| 92 | public function isAvailable(string $host, int $port, string $protocol = 'http', ?int $timeout = null): bool |
||
| 93 | { |
||
| 94 | if (!in_array($protocol, $this->supportedProtocols, true)) { |
||
| 95 | throw new \InvalidArgumentException("Unsupported protocol '$protocol'"); |
||
| 96 | } |
||
| 97 | |||
| 98 | if ($timeout === null) { |
||
| 99 | $timeout = $this->options['timeout']; |
||
| 100 | } |
||
| 101 | |||
| 102 | $available = false; |
||
| 103 | $sock = null; |
||
| 104 | |||
| 105 | $backend = $this->options['backend']; |
||
| 106 | switch ($backend) { |
||
| 107 | case self::BACKEND_PFSOCKOPEN: |
||
| 108 | $sock = @pfsockopen("$protocol://$host", $port, $errno, $errstr, $timeout); |
||
| 109 | if ($sock === false) { |
||
| 110 | $available = true; |
||
| 111 | } else { |
||
| 112 | fclose($sock); |
||
| 113 | } |
||
| 114 | break; |
||
| 115 | |||
| 116 | case self::BACKEND_SOCKET_CREATE: |
||
| 117 | $timeout = 0; |
||
| 118 | $protocolMap = ['tcp' => SOL_TCP, 'udp' => SOL_UDP]; |
||
| 119 | if (!array_key_exists($protocol, $protocolMap)) { |
||
| 120 | throw new \RuntimeException("Backedn socket_create does not support protocol $protocol"); |
||
| 121 | } |
||
| 122 | $proto = $protocolMap[$protocol]; |
||
| 123 | |||
| 124 | $sock = socket_create(AF_INET, SOCK_STREAM, $proto); |
||
| 125 | socket_set_option($sock, SOL_SOCKET, SO_RCVTIMEO, ['sec' => $timeout, 'usec' => 0]); |
||
| 126 | if (!@socket_connect($sock, $host, $port)) { |
||
| 127 | $available = true; |
||
| 128 | } else { |
||
| 129 | socket_close($sock); |
||
| 130 | } |
||
| 131 | break; |
||
| 132 | |||
| 133 | case self::BACKEND_STREAM_SOCKET: |
||
| 134 | $flags = STREAM_CLIENT_CONNECT; // & ~STREAM_CLIENT_PERSISTENT |
||
| 135 | $sock = @stream_socket_client("$protocol://$host:$port", $errno, $errstr, $timeout, $flags); |
||
| 136 | if (!$sock) { |
||
|
|
|||
| 137 | $available = true; |
||
| 138 | } else { |
||
| 139 | if (!stream_socket_shutdown($sock, STREAM_SHUT_RDWR)) { |
||
| 140 | throw new \RuntimeException('Cannot properly close socket stream.'); |
||
| 141 | } |
||
| 142 | fclose($sock); |
||
| 143 | } |
||
| 144 | break; |
||
| 145 | |||
| 146 | case 'curl': |
||
| 147 | if (!$this->isCurlAvailable()) { |
||
| 148 | throw new \RuntimeException('Curl not available'); |
||
| 149 | } |
||
| 150 | $curl_options = [ |
||
| 151 | CURLOPT_URL => "http://$host:$port", |
||
| 152 | CURLOPT_TIMEOUT => $timeout, |
||
| 153 | CURLOPT_RETURNTRANSFER => true, |
||
| 154 | CURLOPT_FAILONERROR => true, |
||
| 155 | CURLOPT_PORT => $port, |
||
| 156 | ]; |
||
| 157 | |||
| 158 | $curl_handle = curl_init(); |
||
| 159 | curl_setopt_array($curl_handle, $curl_options); |
||
| 160 | curl_exec($curl_handle); |
||
| 161 | $errno = curl_errno($curl_handle); |
||
| 162 | if ($errno != 0) { |
||
| 163 | $available = true; |
||
| 164 | } |
||
| 165 | curl_close($curl_handle); |
||
| 166 | break; |
||
| 167 | default: |
||
| 168 | throw new \InvalidArgumentException("Unsupported backend: '$backend'."); |
||
| 169 | } |
||
| 170 | unset($sock); |
||
| 171 | if ($this->options['close_timeout_ms'] > 0) { |
||
| 172 | usleep($this->options['close_timeout_ms'] * 1000); |
||
| 173 | } |
||
| 174 | |||
| 175 | return $available; |
||
| 176 | } |
||
| 183 |