Complex classes like RequestHelper often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use RequestHelper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class RequestHelper |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @var array |
||
| 20 | */ |
||
| 21 | protected $options = array(); |
||
| 22 | |||
| 23 | /** |
||
| 24 | * RequestHelper constructor. |
||
| 25 | */ |
||
| 26 | 15 | public function __construct() |
|
| 31 | |||
| 32 | /** |
||
| 33 | * allow_redirects: (bool|array) Controls redirect behavior. Pass false |
||
| 34 | * to disable redirects, pass true to enable redirects, pass an |
||
| 35 | * associative to provide custom redirect settings. Defaults to "false". |
||
| 36 | * This option only works if your handler has the RedirectMiddleware. When |
||
| 37 | * passing an associative array, you can provide the following key value |
||
| 38 | * pairs: |
||
| 39 | * |
||
| 40 | * - max: (int, default=5) maximum number of allowed redirects. |
||
| 41 | * - strict: (bool, default=false) Set to true to use strict redirects |
||
| 42 | * meaning redirect POST requests with POST requests vs. doing what most |
||
| 43 | * browsers do which is redirect POST requests with GET requests |
||
| 44 | * - referer: (bool, default=true) Set to false to disable the Referer |
||
| 45 | * header. |
||
| 46 | * - protocols: (array, default=['http', 'https']) Allowed redirect |
||
| 47 | * protocols. |
||
| 48 | * - on_redirect: (callable) PHP callable that is invoked when a redirect |
||
| 49 | * is encountered. The callable is invoked with the request, the redirect |
||
| 50 | * response that was received, and the effective URI. Any return value |
||
| 51 | * from the on_redirect function is ignored. |
||
| 52 | * @param int $max |
||
| 53 | * @param bool $strict |
||
| 54 | * @param bool $referer |
||
| 55 | * @param array $protocolsArray |
||
| 56 | * @param callable|null $onRedirect |
||
| 57 | * @return $this |
||
| 58 | */ |
||
| 59 | public function setAllowRedirects($max=5,$strict=false,$referer=true,$protocolsArray=['http', 'https'],callable $onRedirect=null) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * auth: Pass HTTP authentication parameters to use |
||
| 90 | * with the request. |
||
| 91 | * |
||
| 92 | * @param string $username |
||
| 93 | * @param string $password |
||
| 94 | * @param string $typeAuthentication |
||
| 95 | * @return $this |
||
| 96 | */ |
||
| 97 | 6 | public function setAuth($username, $password, $typeAuthentication=TypeAuthentication::BASIC) |
|
| 105 | /** |
||
| 106 | * body: (string|null|callable|iterator|object) Body to send in the |
||
| 107 | * request. |
||
| 108 | * @param (string|null|callable|iterator|object) $body |
||
| 109 | * @return $this |
||
| 110 | */ |
||
| 111 | public function setBody($body) |
||
| 121 | /** |
||
| 122 | * cert: (string|array) Set to a string to specify the path to a file |
||
| 123 | * containing a PEM formatted SSL client side certificate. If a password |
||
| 124 | * is required, then set cert to an array containing the path to the PEM |
||
| 125 | * file in the first array element followed by the certificate password |
||
| 126 | * in the second array element. |
||
| 127 | * |
||
| 128 | * @param (string|array) $certificate |
||
| 129 | * @return $this |
||
| 130 | */ |
||
| 131 | public function setCert($certificate) |
||
| 141 | /** |
||
| 142 | * cookies: (bool|GuzzleHttp\Cookie\CookieJarInterface) |
||
| 143 | * Specifies whether or not cookies are used in a request or what cookie |
||
| 144 | * jar to use or what cookies to send. This option only works if your |
||
| 145 | * handler has the `cookie` middleware. Valid values are `false` and |
||
| 146 | * an instance of {@see GuzzleHttp\Cookie\CookieJarInterface}. |
||
| 147 | * |
||
| 148 | * @param (bool|GuzzleHttp\Cookie\CookieJarInterface) $cookies |
||
| 149 | * @return $this |
||
| 150 | */ |
||
| 151 | public function setCookies($cookies) |
||
| 161 | /** |
||
| 162 | * connect_timeout: (float, default=0) Float describing the number of |
||
| 163 | * seconds to wait while trying to connect to a server. Use 0 to wait |
||
| 164 | * indefinitely (the default behavior). |
||
| 165 | * @param int $secondConnectTimeout |
||
| 166 | * @return $this |
||
| 167 | */ |
||
| 168 | public function setConnectTimeout($secondConnectTimeout=0) |
||
| 173 | /** |
||
| 174 | * debug: (bool|resource) Set to true or set to a PHP stream returned by |
||
| 175 | * fopen() enable debug output with the HTTP handler used to send a |
||
| 176 | * request. |
||
| 177 | * @param $debug |
||
| 178 | * @return $this |
||
| 179 | */ |
||
| 180 | public function setDebug($debug) |
||
| 190 | /** |
||
| 191 | * decode_content: (bool, default=true) Specify whether or not |
||
| 192 | * Content-Encoding responses (gzip, deflate, etc.) are automatically |
||
| 193 | * decoded. |
||
| 194 | * @return $this |
||
| 195 | */ |
||
| 196 | public function setDecodeContentTrue() |
||
| 201 | /** |
||
| 202 | * decode_content: (bool, default=true) Specify whether or not |
||
| 203 | * Content-Encoding responses (gzip, deflate, etc.) are automatically |
||
| 204 | * decoded. |
||
| 205 | * @return $this |
||
| 206 | */ |
||
| 207 | public function setDecodeContentFalse() |
||
| 212 | /** |
||
| 213 | * delay: (int) The amount of time to delay before sending in milliseconds. |
||
| 214 | * @param int $millisecondDelay |
||
| 215 | * @return $this |
||
| 216 | */ |
||
| 217 | public function setDelay($millisecondDelay) |
||
| 222 | /** |
||
| 223 | * expect: (bool|integer) Controls the behavior of the |
||
| 224 | * "Expect: 100-Continue" header. |
||
| 225 | * |
||
| 226 | * Set to `true` to enable the "Expect: 100-Continue" header for all |
||
| 227 | * requests that sends a body. Set to `false` to disable the |
||
| 228 | * "Expect: 100-Continue" header for all requests. Set to a number so that |
||
| 229 | * the size of the payload must be greater than the number in order to send |
||
| 230 | * the Expect header. Setting to a number will send the Expect header for |
||
| 231 | * all requests in which the size of the payload cannot be determined or |
||
| 232 | * where the body is not rewindable. |
||
| 233 | * |
||
| 234 | * By default, Guzzle will add the "Expect: 100-Continue" header when the |
||
| 235 | * size of the body of a request is greater than 1 MB and a request is |
||
| 236 | * using HTTP/1.1. |
||
| 237 | * |
||
| 238 | * @param (bool|integer) $expect |
||
| 239 | * @return $this |
||
| 240 | */ |
||
| 241 | public function setExpect($expect) |
||
| 250 | /** |
||
| 251 | * form_params: (array) Associative array of form field names to values |
||
| 252 | * where each value is a string or array of strings. Sets the Content-Type |
||
| 253 | * header to application/x-www-form-urlencoded when no Content-Type header |
||
| 254 | * is already present. |
||
| 255 | * |
||
| 256 | * @param array $form_params |
||
| 257 | * @return $this |
||
| 258 | */ |
||
| 259 | public function setFormParams($form_params) |
||
| 268 | /** |
||
| 269 | * headers: (array) Associative array of HTTP headers. Each value MUST be |
||
| 270 | * a string or array of strings. |
||
| 271 | * |
||
| 272 | * @param array $headers |
||
| 273 | * @return $this |
||
| 274 | */ |
||
| 275 | public function setHeaders($headers) |
||
| 284 | /** |
||
| 285 | * http_errors: (bool, default=true) Set to false to disable exceptions |
||
| 286 | * when a non- successful HTTP response is received. By default, |
||
| 287 | * exceptions will be thrown for 4xx and 5xx responses. This option only |
||
| 288 | * works if your handler has the `httpErrors` middleware. |
||
| 289 | * |
||
| 290 | * @return $this |
||
| 291 | */ |
||
| 292 | 6 | public function setHttpErrorsTrue() |
|
| 297 | /** |
||
| 298 | * http_errors: (bool, default=true) Set to false to disable exceptions |
||
| 299 | * when a non- successful HTTP response is received. By default, |
||
| 300 | * exceptions will be thrown for 4xx and 5xx responses. This option only |
||
| 301 | * works if your handler has the `httpErrors` middleware. |
||
| 302 | * |
||
| 303 | * @return $this |
||
| 304 | */ |
||
| 305 | 15 | public function setHttpErrorsFalse() |
|
| 310 | /** |
||
| 311 | * json: (mixed) Adds JSON data to a request. |
||
| 312 | * The provided value it will be JSON encoded |
||
| 313 | * and a Content-Type header of application/json will be added to |
||
| 314 | * the request if no Content-Type header is already present. |
||
| 315 | * |
||
| 316 | * @param $json |
||
| 317 | * @return $this |
||
| 318 | */ |
||
| 319 | 6 | public function setJson($json) |
|
| 324 | /** |
||
| 325 | * @param Multipart $multipart |
||
| 326 | * @return $this |
||
| 327 | */ |
||
| 328 | public function setMultipart(Multipart $multipart) |
||
| 337 | /** |
||
| 338 | * on_headers: (callable) A callable that is invoked when the HTTP headers |
||
| 339 | * of the response have been received but the body has not yet begun to |
||
| 340 | * download. |
||
| 341 | * |
||
| 342 | * @param callable $on_headers |
||
| 343 | * @return $this |
||
| 344 | */ |
||
| 345 | public function setOnHeaders(callable $on_headers) |
||
| 354 | /** |
||
| 355 | * on_stats: (callable) allows you to get access to transfer statistics of |
||
| 356 | * a request and access the lower level transfer details of the handler |
||
| 357 | * associated with your client. ``on_stats`` is a callable that is invoked |
||
| 358 | * when a handler has finished sending a request. The callback is invoked |
||
| 359 | * with transfer statistics about the request, the response received, or |
||
| 360 | * the error encountered. Included in the data is the total amount of time |
||
| 361 | * taken to send the request. |
||
| 362 | * |
||
| 363 | * @param callable $on_stats |
||
| 364 | * @return $this |
||
| 365 | */ |
||
| 366 | public function setOnStats(callable $on_stats) |
||
| 375 | /** |
||
| 376 | * progress: (callable) Defines a function to invoke when transfer |
||
| 377 | * progress is made. The function accepts the following positional |
||
| 378 | * arguments: the total number of bytes expected to be downloaded, the |
||
| 379 | * number of bytes downloaded so far, the number of bytes expected to be |
||
| 380 | * uploaded, the number of bytes uploaded so far. |
||
| 381 | * |
||
| 382 | * @param callable $progress |
||
| 383 | * @return $this |
||
| 384 | */ |
||
| 385 | public function setProgress(callable $progress) |
||
| 394 | /** |
||
| 395 | * proxy: (string|array) Pass a string to specify an HTTP proxy, or an |
||
| 396 | * array to specify different proxies for different protocols (where the |
||
| 397 | * key is the protocol and the value is a proxy string). |
||
| 398 | * |
||
| 399 | * @param (string|array) $proxy |
||
| 400 | * @return $this |
||
| 401 | */ |
||
| 402 | public function setProxy($proxy) |
||
| 411 | /** |
||
| 412 | * query: (array|string) Associative array of query string values to add |
||
| 413 | * to the request. This option uses PHP's http_build_query() to create |
||
| 414 | * the string representation. Pass a string value if you need more |
||
| 415 | * control than what this method provides |
||
| 416 | * |
||
| 417 | * @param (array|string) $query |
||
| 418 | * @return $this |
||
| 419 | */ |
||
| 420 | public function setQuery($query) |
||
| 429 | /** |
||
| 430 | * sink: (resource|string|StreamInterface) Where the data of the |
||
| 431 | * response is written to. Defaults to a PHP temp stream. Providing a |
||
| 432 | * string will write data to a file by the given name. |
||
| 433 | * |
||
| 434 | * @param (resource|string|StreamInterface) $sink |
||
| 435 | * @return $this |
||
| 436 | */ |
||
| 437 | public function setSink($sink) |
||
| 447 | /** |
||
| 448 | * synchronous: (bool) Set to true to inform HTTP handlers that you intend |
||
| 449 | * on waiting on the response. This can be useful for optimizations. Note |
||
| 450 | * that a promise is still returned if you are using one of the async |
||
| 451 | * client methods. |
||
| 452 | * |
||
| 453 | * @return $this |
||
| 454 | */ |
||
| 455 | public function setSynchronousTrue() |
||
| 460 | /** |
||
| 461 | * synchronous: (bool) Set to true to inform HTTP handlers that you intend |
||
| 462 | * on waiting on the response. This can be useful for optimizations. Note |
||
| 463 | * that a promise is still returned if you are using one of the async |
||
| 464 | * client methods. |
||
| 465 | * |
||
| 466 | * @return $this |
||
| 467 | */ |
||
| 468 | public function setSynchronousFalse() |
||
| 473 | /** |
||
| 474 | * ssl_key: (array|string) Specify the path to a file containing a private |
||
| 475 | * SSL key in PEM format. If a password is required, then set to an array |
||
| 476 | * containing the path to the SSL key in the first array element followed |
||
| 477 | * by the password required for the certificate in the second element. |
||
| 478 | * |
||
| 479 | * @param (array|string) $ssl_key |
||
| 480 | * @return $this |
||
| 481 | */ |
||
| 482 | public function setSslKey($ssl_key) |
||
| 492 | /** |
||
| 493 | * stream: Set to true to attempt to stream a response rather than |
||
| 494 | * download it all up-front. |
||
| 495 | * |
||
| 496 | * @param $stream |
||
| 497 | * @return $this |
||
| 498 | */ |
||
| 499 | public function setStream($stream) |
||
| 504 | /** |
||
| 505 | * verify: (bool|string, default=true) Describes the SSL certificate |
||
| 506 | * verification behavior of a request. Set to true to enable SSL |
||
| 507 | * certificate verification using the system CA bundle when available |
||
| 508 | * (the default). Set to false to disable certificate verification (this |
||
| 509 | * is insecure!). Set to a string to provide the path to a CA bundle on |
||
| 510 | * disk to enable verification using a custom certificate. |
||
| 511 | * |
||
| 512 | * @param (bool|string) $verify |
||
| 513 | * @return $this |
||
| 514 | */ |
||
| 515 | 3 | public function setVerify($verify) |
|
| 525 | /** |
||
| 526 | * timeout: (float, default=0) Float describing the timeout of the |
||
| 527 | * request in seconds. Use 0 to wait indefinitely (the default behavior). |
||
| 528 | * |
||
| 529 | * @param float $secondRequestTimeout |
||
| 530 | * @return $this |
||
| 531 | */ |
||
| 532 | public function setTimeout($secondRequestTimeout=0.0) |
||
| 541 | /** |
||
| 542 | * version: (float, default=1.1) Specifies the HTTP protocol version to attempt to use. |
||
| 543 | * |
||
| 544 | * @param float $version |
||
| 545 | * @return $this |
||
| 546 | */ |
||
| 547 | public function setVersion($version) |
||
| 556 | |||
| 557 | /** |
||
| 558 | * @param $property |
||
| 559 | * @return mixed |
||
| 560 | */ |
||
| 561 | 15 | public function __get($property) |
|
| 569 | |||
| 570 | /** |
||
| 571 | * @param $property |
||
| 572 | * @param $value |
||
| 573 | * @return $this |
||
| 574 | */ |
||
| 575 | public function __set($property, $value) |
||
| 583 | |||
| 584 | /** |
||
| 585 | * Set the default Request headers. |
||
| 586 | */ |
||
| 587 | 15 | public function setDefaults() |
|
| 591 | } |
||
| 592 |