Conditions | 12 |
Paths | 128 |
Total Lines | 62 |
Code Lines | 41 |
Lines | 0 |
Ratio | 0 % |
Tests | 0 |
CRAP Score | 156 |
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 |
||
139 | public function sendRequest(RequestInterface $request) { |
||
140 | $response = $this->getClient()->createResponse(); |
||
141 | $this->getCurl()->reset(); |
||
142 | $curlopts = array( |
||
143 | CURLOPT_URL => $request->getUrl(), |
||
144 | ); |
||
145 | |||
146 | $method = $request->getMethod(); |
||
147 | if ($method !== RequestInterface::METHOD_GET |
||
148 | && $method !== RequestInterface::METHOD_POST) { |
||
149 | $curlopts[CURLOPT_CUSTOMREQUEST] = $method; |
||
150 | } |
||
151 | |||
152 | $curlopts = $this->getOpts()->getArrayCopy() + $curlopts; |
||
153 | |||
154 | if ($request->getHeaders()->count()) { |
||
155 | $headers = array(); |
||
156 | foreach ($request->getHeaders() as $header => $value) { |
||
157 | $headers[] = "{$header}: {$value}"; |
||
158 | } |
||
159 | $curlopts[CURLOPT_HTTPHEADER] = $headers; |
||
160 | } |
||
161 | |||
162 | $postfields = array(); |
||
163 | if ($method === RequestInterface::METHOD_POST |
||
164 | && $request->getFileParams()->count() |
||
165 | ) { |
||
166 | $postfields = array_merge( |
||
167 | $postfields, |
||
168 | array_map( |
||
169 | array($this->getCurl(), 'preparePostFileField'), |
||
170 | $request->getFileParams()->getArrayCopy())); |
||
171 | } |
||
172 | if ($method !== RequestInterface::METHOD_GET |
||
173 | && $request->getBodyParams()->count()) { |
||
174 | $postfields |
||
175 | = array_merge($postfields, $request->getBodyParams()->export()); |
||
176 | } |
||
177 | |||
178 | if (!empty($postfields)) { |
||
179 | $curlopts[CURLOPT_POSTFIELDS] = $postfields; |
||
180 | } |
||
181 | |||
182 | $this->getCurl()->setoptArray($curlopts); |
||
183 | $raw_response = $this->getCurl()->exec(); |
||
184 | |||
185 | $status_code = $this->getCurl()->getInfo(CURLINFO_HTTP_CODE); |
||
186 | $curl_errno = $this->getCurl()->errno(); |
||
187 | $curl_error = $curl_errno ? $this->getCurl()->error() : null; |
||
188 | |||
189 | $response_parts = $this->extractResponseHeadersAndBody($raw_response); |
||
190 | |||
191 | $response->setStatusCode($status_code); |
||
192 | $this->parseHeaders($response->getHeaders(), $response_parts[0]); |
||
193 | $response->setBody($response_parts[1]); |
||
194 | |||
195 | if ($curl_errno) { |
||
196 | throw new Exception($curl_error, $curl_errno); |
||
197 | } |
||
198 | |||
199 | return $response; |
||
200 | } |
||
201 | } |
||
202 |