| Conditions | 43 |
| Paths | 45 |
| Total Lines | 186 |
| Code Lines | 113 |
| 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 |
||
| 178 | protected function applyOptions( |
||
| 179 | RequestInterface $request, |
||
| 180 | array $options = [] |
||
| 181 | ) { |
||
| 182 | $config = $request->getConfig(); |
||
| 183 | $emitter = $request->getEmitter(); |
||
| 184 | |||
| 185 | foreach ($options as $key => $value) { |
||
| 186 | |||
| 187 | if (isset(self::$configMap[$key])) { |
||
| 188 | $config[$key] = $value; |
||
| 189 | continue; |
||
| 190 | } |
||
| 191 | |||
| 192 | switch ($key) { |
||
| 193 | |||
| 194 | case 'allow_redirects': |
||
| 195 | |||
| 196 | if ($value === false) { |
||
| 197 | continue; |
||
| 198 | } |
||
| 199 | |||
| 200 | if ($value === true) { |
||
| 201 | $value = self::$defaultRedirect; |
||
| 202 | } elseif (!is_array($value)) { |
||
| 203 | throw new Iae('allow_redirects must be true, false, or array'); |
||
| 204 | } else { |
||
| 205 | // Merge the default settings with the provided settings |
||
| 206 | $value += self::$defaultRedirect; |
||
| 207 | } |
||
| 208 | |||
| 209 | $config['redirect'] = $value; |
||
| 210 | $emitter->attach($this->redirectPlugin); |
||
| 211 | break; |
||
| 212 | |||
| 213 | case 'decode_content': |
||
| 214 | |||
| 215 | if ($value === false) { |
||
| 216 | continue; |
||
| 217 | } |
||
| 218 | |||
| 219 | $config['decode_content'] = true; |
||
| 220 | if ($value !== true) { |
||
| 221 | $request->setHeader('Accept-Encoding', $value); |
||
| 222 | } |
||
| 223 | break; |
||
| 224 | |||
| 225 | case 'headers': |
||
| 226 | |||
| 227 | if (!is_array($value)) { |
||
| 228 | throw new Iae('header value must be an array'); |
||
| 229 | } |
||
| 230 | foreach ($value as $k => $v) { |
||
| 231 | $request->setHeader($k, $v); |
||
| 232 | } |
||
| 233 | break; |
||
| 234 | |||
| 235 | case 'exceptions': |
||
| 236 | |||
| 237 | if ($value === true) { |
||
| 238 | $emitter->attach($this->errorPlugin); |
||
| 239 | } |
||
| 240 | break; |
||
| 241 | |||
| 242 | case 'body': |
||
| 243 | |||
| 244 | if (is_array($value)) { |
||
| 245 | $this->addPostData($request, $value); |
||
| 246 | } elseif ($value !== null) { |
||
| 247 | $request->setBody(Stream::factory($value)); |
||
| 248 | } |
||
| 249 | break; |
||
| 250 | |||
| 251 | case 'auth': |
||
| 252 | |||
| 253 | if (!$value) { |
||
| 254 | continue; |
||
| 255 | } |
||
| 256 | |||
| 257 | if (is_array($value)) { |
||
| 258 | $type = isset($value[2]) ? strtolower($value[2]) : 'basic'; |
||
| 259 | } else { |
||
| 260 | $type = strtolower($value); |
||
| 261 | } |
||
| 262 | |||
| 263 | $config['auth'] = $value; |
||
| 264 | |||
| 265 | if ($type == 'basic') { |
||
| 266 | $request->setHeader( |
||
| 267 | 'Authorization', |
||
| 268 | 'Basic ' . base64_encode("$value[0]:$value[1]") |
||
| 269 | ); |
||
| 270 | } elseif ($type == 'digest') { |
||
| 271 | // @todo: Do not rely on curl |
||
| 272 | $config->setPath('curl/' . CURLOPT_HTTPAUTH, CURLAUTH_DIGEST); |
||
| 273 | $config->setPath('curl/' . CURLOPT_USERPWD, "$value[0]:$value[1]"); |
||
| 274 | } |
||
| 275 | break; |
||
| 276 | |||
| 277 | case 'query': |
||
| 278 | |||
| 279 | if ($value instanceof Query) { |
||
| 280 | $original = $request->getQuery(); |
||
| 281 | // Do not overwrite existing query string variables by |
||
| 282 | // overwriting the object with the query string data passed |
||
| 283 | // in the URL |
||
| 284 | $value->overwriteWith($original->toArray()); |
||
| 285 | $request->setQuery($value); |
||
| 286 | } elseif (is_array($value)) { |
||
| 287 | // Do not overwrite existing query string variables |
||
| 288 | $query = $request->getQuery(); |
||
| 289 | foreach ($value as $k => $v) { |
||
| 290 | if (!isset($query[$k])) { |
||
| 291 | $query[$k] = $v; |
||
| 292 | } |
||
| 293 | } |
||
| 294 | } else { |
||
| 295 | throw new Iae('query must be an array or Query object'); |
||
| 296 | } |
||
| 297 | break; |
||
| 298 | |||
| 299 | case 'cookies': |
||
| 300 | |||
| 301 | if ($value === true) { |
||
| 302 | static $cookie = null; |
||
| 303 | if (!$cookie) { |
||
| 304 | $cookie = new Cookie(); |
||
| 305 | } |
||
| 306 | $emitter->attach($cookie); |
||
| 307 | } elseif (is_array($value)) { |
||
| 308 | $emitter->attach( |
||
| 309 | new Cookie(CookieJar::fromArray($value, $request->getHost())) |
||
| 310 | ); |
||
| 311 | } elseif ($value instanceof CookieJarInterface) { |
||
| 312 | $emitter->attach(new Cookie($value)); |
||
| 313 | } elseif ($value !== false) { |
||
| 314 | throw new Iae('cookies must be an array, true, or CookieJarInterface'); |
||
| 315 | } |
||
| 316 | break; |
||
| 317 | |||
| 318 | case 'events': |
||
| 319 | |||
| 320 | if (!is_array($value)) { |
||
| 321 | throw new Iae('events must be an array'); |
||
| 322 | } |
||
| 323 | |||
| 324 | $this->attachListeners($request, |
||
| 325 | $this->prepareListeners( |
||
| 326 | $value, |
||
| 327 | ['before', 'complete', 'error', 'progress', 'end'] |
||
| 328 | ) |
||
| 329 | ); |
||
| 330 | break; |
||
| 331 | |||
| 332 | case 'subscribers': |
||
| 333 | |||
| 334 | if (!is_array($value)) { |
||
| 335 | throw new Iae('subscribers must be an array'); |
||
| 336 | } |
||
| 337 | |||
| 338 | foreach ($value as $subscribers) { |
||
| 339 | $emitter->attach($subscribers); |
||
| 340 | } |
||
| 341 | break; |
||
| 342 | |||
| 343 | case 'json': |
||
| 344 | |||
| 345 | $request->setBody(Stream::factory(json_encode($value))); |
||
| 346 | if (!$request->hasHeader('Content-Type')) { |
||
| 347 | $request->setHeader('Content-Type', 'application/json'); |
||
| 348 | } |
||
| 349 | break; |
||
| 350 | |||
| 351 | default: |
||
| 352 | |||
| 353 | // Check for custom handler functions. |
||
| 354 | if (isset($this->customOptions[$key])) { |
||
| 355 | $fn = $this->customOptions[$key]; |
||
| 356 | $fn($request, $value); |
||
| 357 | continue; |
||
| 358 | } |
||
| 359 | |||
| 360 | throw new Iae("No method can handle the {$key} config key"); |
||
| 361 | } |
||
| 362 | } |
||
| 363 | } |
||
| 364 | } |
||
| 365 |