Conditions | 12 |
Paths | 144 |
Total Lines | 40 |
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 |
||
155 | function request($url, $method, array $queryData, $postData, $contentType = null, array $arrHeaders = null) { |
||
156 | $this->_header = null; |
||
157 | $header = $arrHeaders ?: array(); |
||
158 | $header[] = sprintf('Host: %s', parse_url($url, PHP_URL_HOST)); |
||
159 | if ($this->_accept && !isset($header['Accept'])) |
||
160 | $header[] = 'Accept: '.$this->_accept; |
||
161 | if ($queryData) { |
||
162 | $url = rtrim($url, '?'); |
||
163 | $url .= '?'.http_build_query($queryData); |
||
164 | } |
||
165 | if ($contentType) { |
||
166 | $header[] = 'Content-Type: '.$contentType; |
||
167 | } |
||
168 | if ($postData) { |
||
169 | if (is_array($postData)) { |
||
170 | $postData = http_build_query($postData); |
||
171 | } else if (is_object($postData)) { |
||
172 | $postData = urlencode(json_encode($postData)); |
||
173 | } else if (!$contentType) { |
||
174 | $postData = urlencode((string)$postData); |
||
175 | } else { |
||
176 | $postData = (string)$postData; |
||
177 | } |
||
178 | curl_setopt($this->_curl, CURLOPT_POSTFIELDS, $postData); |
||
179 | $header[] = 'Content-Length: '.strlen($postData); |
||
180 | $header[] = 'Content-Type: '.($contentType ?: ContentType::FORM_URLENCODED); |
||
181 | } |
||
182 | curl_setopt($this->_curl, CURLOPT_URL, $url); |
||
183 | curl_setopt($this->_curl, CURLOPT_CUSTOMREQUEST, $method); |
||
184 | curl_setopt($this->_curl, CURLOPT_HEADER, 1); |
||
185 | curl_setopt($this->_curl, CURLOPT_HTTPHEADER, $header); |
||
186 | curl_setopt($this->_curl, CURLOPT_CONNECTTIMEOUT_MS, $this->_timeout_ms ?: 10000); |
||
187 | $response = curl_exec($this->_curl); |
||
188 | $header_size = curl_getinfo($this->_curl, CURLINFO_HEADER_SIZE); |
||
189 | $this->_header = substr($response, 0, $header_size); |
||
190 | $body = substr($response, $header_size); |
||
191 | $this->_statusCode = curl_getinfo($this->_curl, CURLINFO_HTTP_CODE); |
||
192 | $this->_errorText = curl_error($this->_curl); |
||
193 | return $body; |
||
194 | } |
||
195 | |||
197 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.