| Conditions | 23 |
| Paths | 8064 |
| Total Lines | 130 |
| 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 |
||
| 100 | public function init(){ |
||
| 101 | |||
| 102 | $v = $this->request->getProtocolVersion(); |
||
| 103 | |||
| 104 | switch($v){ |
||
| 105 | case '1.0': $v = CURL_HTTP_VERSION_1_0; break; |
||
| 106 | case '1.1': $v = CURL_HTTP_VERSION_1_1; break; |
||
| 107 | case '2.0': $v = CURL_HTTP_VERSION_2_0; break; |
||
| 108 | default: $v = CURL_HTTP_VERSION_NONE; |
||
| 109 | } |
||
| 110 | |||
| 111 | $options = [ |
||
| 112 | CURLOPT_HEADER => false, |
||
| 113 | CURLOPT_RETURNTRANSFER => false, |
||
| 114 | CURLOPT_FOLLOWLOCATION => false, |
||
| 115 | CURLOPT_URL => (string)$this->request->getUri()->withFragment(''), |
||
| 116 | CURLOPT_HTTP_VERSION => $v, |
||
| 117 | CURLOPT_USERAGENT => $this->options->user_agent, |
||
| 118 | CURLOPT_PROTOCOLS => CURLPROTO_HTTP | CURLPROTO_HTTPS, |
||
| 119 | CURLOPT_SSL_VERIFYPEER => true, |
||
| 120 | CURLOPT_SSL_VERIFYHOST => 2, |
||
| 121 | CURLOPT_CAINFO => is_file($this->options->ca_info) ? $this->options->ca_info : null, |
||
| 122 | CURLOPT_TIMEOUT => (int)$this->options->timeout, |
||
| 123 | CURLOPT_CONNECTTIMEOUT => 30, |
||
| 124 | CURLOPT_WRITEFUNCTION => [$this, 'writefunction'], |
||
| 125 | CURLOPT_HEADERFUNCTION => [$this, 'headerfunction'], |
||
| 126 | ]; |
||
| 127 | |||
| 128 | $userinfo = $this->request->getUri()->getUserInfo(); |
||
| 129 | |||
| 130 | if(!empty($userinfo)){ |
||
| 131 | $options[CURLOPT_USERPWD] = $userinfo; |
||
| 132 | } |
||
| 133 | |||
| 134 | /* |
||
| 135 | * Some HTTP methods cannot have payload: |
||
| 136 | * |
||
| 137 | * - GET — cURL will automatically change method to PUT or POST |
||
| 138 | * if we set CURLOPT_UPLOAD or CURLOPT_POSTFIELDS. |
||
| 139 | * - HEAD — cURL treats HEAD as GET request with a same restrictions. |
||
| 140 | * - TRACE — According to RFC7231: a client MUST NOT send a message body in a TRACE request. |
||
| 141 | */ |
||
| 142 | $method = $this->request->getMethod(); |
||
| 143 | $body = $this->request->getBody(); |
||
| 144 | $bodySize = $body->getSize(); |
||
| 145 | |||
| 146 | if(in_array($method, ['DELETE', 'PATCH', 'POST', 'PUT'], true) && $bodySize !== 0){ |
||
| 147 | |||
| 148 | if($body->isSeekable()){ |
||
| 149 | $body->rewind(); |
||
| 150 | } |
||
| 151 | |||
| 152 | // Message has non empty body. |
||
| 153 | if($bodySize === null || $bodySize > 1024 * 1024){ |
||
| 154 | // Avoid full loading large or unknown size body into memory |
||
| 155 | $options[CURLOPT_UPLOAD] = true; |
||
| 156 | |||
| 157 | if($bodySize !== null){ |
||
| 158 | $options[CURLOPT_INFILESIZE] = $bodySize; |
||
| 159 | } |
||
| 160 | |||
| 161 | $options[CURLOPT_READFUNCTION] = [$this, 'readfunction']; |
||
| 162 | } |
||
| 163 | // Small body can be loaded into memory |
||
| 164 | else{ |
||
| 165 | $options[CURLOPT_POSTFIELDS] = (string)$body; |
||
| 166 | } |
||
| 167 | |||
| 168 | } |
||
| 169 | |||
| 170 | // This will set HTTP method to "HEAD". |
||
| 171 | if($method === 'HEAD'){ |
||
| 172 | $options[CURLOPT_NOBODY] = true; |
||
| 173 | } |
||
| 174 | // GET is a default method. Other methods should be specified explicitly. |
||
| 175 | elseif($method !== 'GET'){ |
||
| 176 | $options[CURLOPT_CUSTOMREQUEST] = $method; |
||
| 177 | } |
||
| 178 | |||
| 179 | $curlHeaders = []; |
||
| 180 | |||
| 181 | foreach($this->request->getHeaders() as $name => $values){ |
||
| 182 | $header = strtolower($name); |
||
| 183 | |||
| 184 | // curl-client does not support "Expect-Continue", so dropping "expect" headers |
||
| 185 | if($header === 'expect'){ |
||
| 186 | continue; |
||
| 187 | } |
||
| 188 | |||
| 189 | if($header === 'content-length'){ |
||
| 190 | |||
| 191 | // Small body content length can be calculated here. |
||
| 192 | if(array_key_exists(CURLOPT_POSTFIELDS, $options)){ |
||
| 193 | $values = [strlen($options[CURLOPT_POSTFIELDS])]; |
||
| 194 | } |
||
| 195 | // Else if there is no body, forcing "Content-length" to 0 |
||
| 196 | elseif(!array_key_exists(CURLOPT_READFUNCTION, $options)){ |
||
| 197 | $values = ['0']; |
||
| 198 | } |
||
| 199 | |||
| 200 | } |
||
| 201 | |||
| 202 | foreach($values as $value){ |
||
| 203 | $value = (string)$value; |
||
| 204 | |||
| 205 | // cURL requires a special format for empty headers. |
||
| 206 | // See https://github.com/guzzle/guzzle/issues/1882 for more details. |
||
| 207 | $curlHeaders[] = $value === '' |
||
| 208 | ? $name.';' |
||
| 209 | : $name.': '.$value; |
||
| 210 | } |
||
| 211 | |||
| 212 | } |
||
| 213 | |||
| 214 | $options[CURLOPT_HTTPHEADER] = $curlHeaders; |
||
| 215 | |||
| 216 | // If the Expect header is not present, prevent curl from adding it |
||
| 217 | if (!$this->request->hasHeader('Expect')) { |
||
| 218 | $options[CURLOPT_HTTPHEADER][] = 'Expect:'; |
||
| 219 | } |
||
| 220 | |||
| 221 | // cURL sometimes adds a content-type by default. Prevent this. |
||
| 222 | if (!$this->request->hasHeader('Content-Type')) { |
||
| 223 | $options[CURLOPT_HTTPHEADER][] = 'Content-Type:'; |
||
| 224 | } |
||
| 225 | |||
| 226 | curl_setopt_array($this->ch, $this->options->curl_options + $options); |
||
| 227 | |||
| 228 | return $this->ch; |
||
| 229 | } |
||
| 230 | |||
| 280 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.