| Conditions | 18 |
| Paths | 460 |
| Total Lines | 89 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 94 | public function push(): array |
||
| 95 | { |
||
| 96 | $responseCollection = []; |
||
| 97 | |||
| 98 | if (!$this->curlMultiHandle) { |
||
| 99 | $this->curlMultiHandle = curl_multi_init(); |
||
| 100 | |||
| 101 | if (!defined('CURLPIPE_MULTIPLEX')) { |
||
| 102 | define('CURLPIPE_MULTIPLEX', 2); |
||
| 103 | } |
||
| 104 | |||
| 105 | curl_multi_setopt($this->curlMultiHandle, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX); |
||
| 106 | if (defined('CURLMOPT_MAX_HOST_CONNECTIONS')) { |
||
| 107 | curl_multi_setopt($this->curlMultiHandle, CURLMOPT_MAX_HOST_CONNECTIONS, $this->maxConcurrentConnections); |
||
| 108 | } |
||
| 109 | } |
||
| 110 | |||
| 111 | $mh = $this->curlMultiHandle; |
||
| 112 | |||
| 113 | $i = 0; |
||
| 114 | while (!empty($this->notifications) && $i++ < $this->nbConcurrentRequests) { |
||
| 115 | $notification = array_pop($this->notifications); |
||
| 116 | curl_multi_add_handle($mh, $this->prepareHandle($notification)); |
||
| 117 | } |
||
| 118 | |||
| 119 | // Clear out curl handle buffer |
||
| 120 | do { |
||
| 121 | $execrun = curl_multi_exec($mh, $running); |
||
| 122 | } while ($execrun === CURLM_CALL_MULTI_PERFORM); |
||
| 123 | |||
| 124 | // Continue processing while we have active curl handles |
||
| 125 | while ($running > 0 && $execrun === CURLM_OK) { |
||
| 126 | // Block until data is available |
||
| 127 | $select_fd = curl_multi_select($mh); |
||
| 128 | // If select returns -1 while running, wait 250 microseconds before continuing |
||
| 129 | // Using curl_multi_timeout would be better but it isn't available in PHP yet |
||
| 130 | // https://php.net/manual/en/function.curl-multi-select.php#115381 |
||
| 131 | if ($running && $select_fd === -1) { |
||
| 132 | usleep(250); |
||
| 133 | } |
||
| 134 | |||
| 135 | // Continue to wait for more data if needed |
||
| 136 | do { |
||
| 137 | $execrun = curl_multi_exec($mh, $running); |
||
| 138 | } while ($execrun === CURLM_CALL_MULTI_PERFORM); |
||
| 139 | |||
| 140 | // Start reading results |
||
| 141 | while ($done = curl_multi_info_read($mh)) { |
||
| 142 | $handle = $done['handle']; |
||
| 143 | |||
| 144 | $result = curl_multi_getcontent($handle); |
||
| 145 | |||
| 146 | // find out which token the response is about |
||
| 147 | $token = curl_getinfo($handle, CURLINFO_PRIVATE); |
||
| 148 | |||
| 149 | $responseParts = explode("\r\n\r\n", $result, 2); |
||
| 150 | $headers = ''; |
||
| 151 | $body = ''; |
||
| 152 | if (isset($responseParts[0])) { |
||
| 153 | $headers = $responseParts[0]; |
||
| 154 | } |
||
| 155 | if (isset($responseParts[1])) { |
||
| 156 | $body = $responseParts[1]; |
||
| 157 | } |
||
| 158 | |||
| 159 | $statusCode = curl_getinfo($handle, CURLINFO_HTTP_CODE); |
||
| 160 | if ($statusCode === 0) { |
||
| 161 | throw new \Exception(curl_error($handle)); |
||
| 162 | } |
||
| 163 | |||
| 164 | $responseCollection[] = new Response($statusCode, $headers, (string)$body, $token); |
||
| 165 | curl_multi_remove_handle($mh, $handle); |
||
| 166 | curl_close($handle); |
||
| 167 | |||
| 168 | if (!empty($this->notifications)) { |
||
| 169 | $notification = array_pop($this->notifications); |
||
| 170 | curl_multi_add_handle($mh, $this->prepareHandle($notification)); |
||
| 171 | $running++; |
||
| 172 | } |
||
| 173 | } |
||
| 174 | } |
||
| 175 | |||
| 176 | if ($this->autoCloseConnections) { |
||
| 177 | curl_multi_close($mh); |
||
| 178 | $this->curlMultiHandle = null; |
||
| 179 | } |
||
| 180 | |||
| 181 | return $responseCollection; |
||
| 182 | } |
||
| 183 | |||
| 286 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..