Complex classes like Client 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 Client, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 48 | class Client |
||
| 49 | { |
||
| 50 | /** |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | const SCHEME_SSL = "https://"; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | const SCHEME_PLAIN = "http://"; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var SerializerInterface |
||
| 62 | */ |
||
| 63 | protected $serializer; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var resource |
||
| 67 | */ |
||
| 68 | protected $handle; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var array |
||
| 72 | */ |
||
| 73 | protected $httpOptions = array(); |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var array |
||
| 77 | */ |
||
| 78 | protected $httpHeaders = array(); |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var string |
||
| 82 | */ |
||
| 83 | protected $host; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var integer |
||
| 87 | */ |
||
| 88 | protected $port; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var string |
||
| 92 | */ |
||
| 93 | protected $scheme; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * new class instance |
||
| 97 | * |
||
| 98 | * @param string $host the host |
||
| 99 | * @param mixed $port (optional) the port |
||
| 100 | * |
||
| 101 | * @throws ConfigurationException |
||
| 102 | */ |
||
| 103 | 19 | public function __construct($host, $port) |
|
| 108 | |||
| 109 | /** |
||
| 110 | * resets all attributes |
||
| 111 | * |
||
| 112 | * @return void |
||
| 113 | */ |
||
| 114 | public function reset() |
||
| 118 | |||
| 119 | /** |
||
| 120 | * initialize default values |
||
| 121 | * |
||
| 122 | * @return void |
||
| 123 | * @throws Exceptions\ServerErrorException |
||
| 124 | */ |
||
| 125 | 18 | protected function init() |
|
| 137 | |||
| 138 | /** |
||
| 139 | * Sets a serializer instance |
||
| 140 | * |
||
| 141 | * @param SerializerInterface $serializer the serializer instance |
||
| 142 | * |
||
| 143 | * @return void |
||
| 144 | */ |
||
| 145 | 18 | public function setSerializer(SerializerInterface $serializer) |
|
| 149 | |||
| 150 | /** |
||
| 151 | * Returns serializer instance |
||
| 152 | * |
||
| 153 | * @return SerializerInterface |
||
| 154 | */ |
||
| 155 | 10 | public function getSerializer() |
|
| 159 | |||
| 160 | /** |
||
| 161 | * Returns curl resource |
||
| 162 | * |
||
| 163 | * @return resource |
||
| 164 | */ |
||
| 165 | 9 | protected function getHandle() |
|
| 169 | |||
| 170 | /** |
||
| 171 | * Sets username and password for basic authentication |
||
| 172 | * |
||
| 173 | * @param string $username the username |
||
| 174 | * @param string $password the password |
||
| 175 | * |
||
| 176 | * @return void |
||
| 177 | */ |
||
| 178 | 1 | public function setAuthentication($username, $password) |
|
| 182 | |||
| 183 | /** |
||
| 184 | * Sets hostname and optional port |
||
| 185 | * |
||
| 186 | * @param string $host the hostname |
||
| 187 | * @param mixed $port the port |
||
| 188 | * |
||
| 189 | * @return void |
||
| 190 | * @throws \Puzzle\Exceptions\ConfigurationException |
||
| 191 | */ |
||
| 192 | 19 | protected function setHost($host, $port = null) |
|
| 203 | |||
| 204 | /** |
||
| 205 | * closes curl resource |
||
| 206 | * |
||
| 207 | * @return void |
||
| 208 | */ |
||
| 209 | protected function close() |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Return's http status response code |
||
| 216 | * |
||
| 217 | * @return mixed |
||
| 218 | */ |
||
| 219 | public function getStatusCode() |
||
| 223 | |||
| 224 | /** |
||
| 225 | * performs the request |
||
| 226 | * |
||
| 227 | * @param string $method request method |
||
| 228 | * @param string $uri uri string |
||
| 229 | * @param mixed $params optional query string parameters |
||
| 230 | * @param mixed $body "post" body |
||
| 231 | * |
||
| 232 | * @return mixed |
||
| 233 | * @throws \Puzzle\Exceptions\ServerErrorResponseException |
||
| 234 | * @throws \Puzzle\Exceptions\ClientErrorResponseException |
||
| 235 | */ |
||
| 236 | 10 | public function performRequest($method, $uri, $params = null, $body = null) |
|
| 256 | |||
| 257 | /** |
||
| 258 | * perform a get request |
||
| 259 | * |
||
| 260 | * @param string $uri uri string |
||
| 261 | * @param mixed $params optional query string parameters |
||
| 262 | * @param mixed $body the "post" body |
||
| 263 | * |
||
| 264 | * @return mixed |
||
| 265 | * @throws ClientErrorResponseException |
||
| 266 | * @throws ServerErrorResponseException |
||
| 267 | * @throws \Exception |
||
| 268 | */ |
||
| 269 | 1 | public function get($uri, $params = null, $body = null) |
|
| 273 | |||
| 274 | /** |
||
| 275 | * perform a put request |
||
| 276 | * |
||
| 277 | * @param string $uri uri string |
||
| 278 | * @param mixed $params optional query string parameters |
||
| 279 | * @param mixed $body the "post" body |
||
| 280 | * |
||
| 281 | * @return mixed |
||
| 282 | * @throws ClientErrorResponseException |
||
| 283 | * @throws ServerErrorResponseException |
||
| 284 | * @throws \Exception |
||
| 285 | */ |
||
| 286 | 1 | public function put($uri, $params = null, $body = null) |
|
| 290 | |||
| 291 | /** |
||
| 292 | * perform a post request |
||
| 293 | * |
||
| 294 | * @param string $uri uri string |
||
| 295 | * @param mixed $params optional query string parameters |
||
| 296 | * @param mixed $body the "post" body |
||
| 297 | * |
||
| 298 | * @return mixed |
||
| 299 | * @throws ClientErrorResponseException |
||
| 300 | * @throws ServerErrorResponseException |
||
| 301 | * @throws \Exception |
||
| 302 | */ |
||
| 303 | 1 | public function post($uri, $params = null, $body = null) |
|
| 307 | |||
| 308 | /** |
||
| 309 | * perform a patch |
||
| 310 | * |
||
| 311 | * @param string $uri uri string |
||
| 312 | * @param mixed $params optional query string parameters |
||
| 313 | * @param mixed $body the "post" body |
||
| 314 | * |
||
| 315 | * @return mixed |
||
| 316 | * @throws ClientErrorResponseException |
||
| 317 | * @throws ServerErrorResponseException |
||
| 318 | * @throws \Exception |
||
| 319 | */ |
||
| 320 | 1 | public function patch($uri, $params = null, $body = null) |
|
| 324 | |||
| 325 | /** |
||
| 326 | * perform a head request |
||
| 327 | * |
||
| 328 | * @param string $uri uri string |
||
| 329 | * @param mixed $params optional query string parameters |
||
| 330 | * @param mixed $body the "post" body |
||
| 331 | * |
||
| 332 | * @return mixed |
||
| 333 | * @throws ClientErrorResponseException |
||
| 334 | * @throws ServerErrorResponseException |
||
| 335 | * @throws \Exception |
||
| 336 | */ |
||
| 337 | 1 | public function head($uri, $params = null, $body = null) |
|
| 341 | |||
| 342 | /** |
||
| 343 | * perform a delete request |
||
| 344 | * |
||
| 345 | * @param string $uri uri string |
||
| 346 | * @param mixed $params optional query string parameters |
||
| 347 | * @param mixed $body the "post" body |
||
| 348 | * |
||
| 349 | * @return mixed |
||
| 350 | * @throws ClientErrorResponseException |
||
| 351 | * @throws ServerErrorResponseException |
||
| 352 | * @throws \Exception |
||
| 353 | */ |
||
| 354 | 1 | public function delete($uri, $params = null, $body = null) |
|
| 358 | |||
| 359 | /** |
||
| 360 | * precess the request |
||
| 361 | * |
||
| 362 | * @param string $method the request method |
||
| 363 | * @param string $uri the uri string |
||
| 364 | * @param mixed $params the optional query string parameters |
||
| 365 | * @param string $body the (post) body |
||
| 366 | * |
||
| 367 | * @return mixed |
||
| 368 | * @throws Exceptions\InvalidRequestMethodException |
||
| 369 | */ |
||
| 370 | 10 | protected function processRequest($method, $uri, $params = null, $body = null) |
|
| 380 | |||
| 381 | /** |
||
| 382 | * returns the request method |
||
| 383 | * |
||
| 384 | * @param string $method the request method |
||
| 385 | * |
||
| 386 | * @return string |
||
| 387 | */ |
||
| 388 | 10 | protected function getMethod($method) |
|
| 398 | |||
| 399 | /** |
||
| 400 | * get request implementation |
||
| 401 | * |
||
| 402 | * @param string $method the request method |
||
| 403 | * @param string $uri the uri |
||
| 404 | * @param mixed $params optional query string parameters |
||
| 405 | * @param string $body body/post parameters |
||
| 406 | * |
||
| 407 | * @return string |
||
| 408 | * @throws \Puzzle\Exceptions\InvalidRequestException |
||
| 409 | */ |
||
| 410 | 1 | protected function getRequest($method, $uri, $params, $body) |
|
| 416 | |||
| 417 | /** |
||
| 418 | * delete request implementation |
||
| 419 | * |
||
| 420 | * @param string $method the request method |
||
| 421 | * @param string $uri the uri |
||
| 422 | * @param mixed $params optional query string parameters |
||
| 423 | * @param string $body body/post parameters |
||
| 424 | * |
||
| 425 | * @return string |
||
| 426 | * @throws \Puzzle\Exceptions\InvalidRequestException |
||
| 427 | */ |
||
| 428 | 1 | protected function deleteRequest($method, $uri, $params, $body) |
|
| 432 | |||
| 433 | /** |
||
| 434 | * head request implementation |
||
| 435 | * |
||
| 436 | * @param string $method the request method |
||
| 437 | * @param string $uri the uri |
||
| 438 | * @param mixed $params optional query string parameters |
||
| 439 | * @param string $body body/post parameters |
||
| 440 | * |
||
| 441 | * @return string |
||
| 442 | * @throws \Puzzle\Exceptions\InvalidRequestException |
||
| 443 | */ |
||
| 444 | 1 | protected function headRequest($method, $uri, $params, $body) |
|
| 450 | |||
| 451 | /** |
||
| 452 | * post request implementation |
||
| 453 | * |
||
| 454 | * @param string $method the request method |
||
| 455 | * @param string $uri the uri |
||
| 456 | * @param mixed $params optional query string parameters |
||
| 457 | * @param string $body body/post parameters |
||
| 458 | * |
||
| 459 | * @return string |
||
| 460 | * @throws \Puzzle\Exceptions\InvalidRequestException |
||
| 461 | */ |
||
| 462 | 4 | protected function postRequest($method, $uri, $params, $body) |
|
| 468 | |||
| 469 | /** |
||
| 470 | * put request implementation |
||
| 471 | * |
||
| 472 | * @param string $method the request method |
||
| 473 | * @param string $uri the uri |
||
| 474 | * @param mixed $params optional query string parameters |
||
| 475 | * @param string $body body/post parameters |
||
| 476 | * |
||
| 477 | * @return string |
||
| 478 | * @throws \Puzzle\Exceptions\InvalidRequestException |
||
| 479 | */ |
||
| 480 | 2 | protected function putRequest($method, $uri, $params, $body) |
|
| 489 | |||
| 490 | /** |
||
| 491 | * checks if body is not null |
||
| 492 | * |
||
| 493 | * @param mixed $body the body or null |
||
| 494 | * @param string $method the request method |
||
| 495 | * |
||
| 496 | * @return void |
||
| 497 | * @throws InvalidRequestException |
||
| 498 | */ |
||
| 499 | 4 | protected function checkBody($body, $method) |
|
| 505 | |||
| 506 | /** |
||
| 507 | * Executes the curl request |
||
| 508 | * |
||
| 509 | * @param string $method the request method |
||
| 510 | * @param string $uri the uri |
||
| 511 | * @param mixed $params optional query string parameters |
||
| 512 | * @param string $body body/post parameters |
||
| 513 | * |
||
| 514 | * @return mixed |
||
| 515 | * @throws Exceptions\ClientErrorException |
||
| 516 | * @throws Exceptions\ServerErrorException |
||
| 517 | * @throws Exceptions\TransportException |
||
| 518 | */ |
||
| 519 | 9 | protected function execute($method, $uri, $params, $body) |
|
| 539 | |||
| 540 | /** |
||
| 541 | * prepares the response |
||
| 542 | * |
||
| 543 | * @param string $result the curl result |
||
| 544 | * |
||
| 545 | * @return array |
||
| 546 | * @throws ClientErrorException |
||
| 547 | * @throws ConnectionException |
||
| 548 | * @throws ServerErrorException |
||
| 549 | */ |
||
| 550 | 9 | protected function prepareResponse($result) |
|
| 573 | |||
| 574 | /** |
||
| 575 | * executes curl_exec |
||
| 576 | * |
||
| 577 | * @return mixed |
||
| 578 | */ |
||
| 579 | protected function curlExec() |
||
| 583 | |||
| 584 | /** |
||
| 585 | * checks if a curl error occurred |
||
| 586 | * |
||
| 587 | * @return void |
||
| 588 | * @throws ConnectionException |
||
| 589 | */ |
||
| 590 | protected function checkForCurlErrors() |
||
| 597 | |||
| 598 | /** |
||
| 599 | * Enables ssl for the connection |
||
| 600 | * |
||
| 601 | * @param bool $strict optional value if ssl should be strict (check server certificate) |
||
| 602 | * |
||
| 603 | * @return void |
||
| 604 | */ |
||
| 605 | 1 | public function enableSSL($strict = false) |
|
| 616 | |||
| 617 | /** |
||
| 618 | * Disables ssl for the connection |
||
| 619 | * |
||
| 620 | * @return void |
||
| 621 | */ |
||
| 622 | 1 | public function disableSSL() |
|
| 626 | |||
| 627 | /** |
||
| 628 | * Sets the http/https scheme string |
||
| 629 | * |
||
| 630 | * @param string $scheme http/http scheme string |
||
| 631 | * |
||
| 632 | * @return void |
||
| 633 | */ |
||
| 634 | 1 | protected function setScheme($scheme) |
|
| 638 | |||
| 639 | /** |
||
| 640 | * Sets a http option (e.g. use strict ssl) |
||
| 641 | * |
||
| 642 | * @param integer $key the curl option key |
||
| 643 | * @param integer $value the value to set |
||
| 644 | * |
||
| 645 | * @return void |
||
| 646 | */ |
||
| 647 | 1 | protected function setOption($key, $value) |
|
| 651 | |||
| 652 | /** |
||
| 653 | * Returns all set http options as array |
||
| 654 | * |
||
| 655 | * @return array |
||
| 656 | */ |
||
| 657 | 10 | protected function getOptions() |
|
| 661 | |||
| 662 | /** |
||
| 663 | * build complete url |
||
| 664 | * |
||
| 665 | * @param string $uri uri string |
||
| 666 | * @param mixed $params query string parameters |
||
| 667 | * |
||
| 668 | * @return string |
||
| 669 | */ |
||
| 670 | 10 | protected function buildUrl($uri, $params) |
|
| 689 | |||
| 690 | /** |
||
| 691 | * build the query string |
||
| 692 | * |
||
| 693 | * @param array $params query string parameters |
||
| 694 | * |
||
| 695 | * @return string |
||
| 696 | */ |
||
| 697 | 11 | protected function buildQueryString(array $params) |
|
| 710 | |||
| 711 | /** |
||
| 712 | * build host string (scheme - hostname/ip - (optional) port |
||
| 713 | * |
||
| 714 | * @return string |
||
| 715 | * |
||
| 716 | * @throws Exceptions\ConfigurationException |
||
| 717 | */ |
||
| 718 | 9 | protected function buildHostString() |
|
| 732 | |||
| 733 | /** |
||
| 734 | * Adds given scheme to hostname |
||
| 735 | * |
||
| 736 | * @param string $scheme the http scheme |
||
| 737 | * @param string $host the hostname |
||
| 738 | * |
||
| 739 | * @return string |
||
| 740 | * @throws Exceptions\ConfigurationException |
||
| 741 | */ |
||
| 742 | 9 | protected function prepareHost($scheme, $host) |
|
| 752 | |||
| 753 | /** |
||
| 754 | * strip http/https scheme from hostname |
||
| 755 | * |
||
| 756 | * @param string $host the hostname |
||
| 757 | * |
||
| 758 | * @return string |
||
| 759 | */ |
||
| 760 | 10 | protected function stripScheme($host) |
|
| 764 | |||
| 765 | /** |
||
| 766 | * Returns scheme string |
||
| 767 | * |
||
| 768 | * @return string |
||
| 769 | */ |
||
| 770 | 11 | protected function getScheme() |
|
| 774 | |||
| 775 | /** |
||
| 776 | * Returns hostname |
||
| 777 | * |
||
| 778 | * @return string |
||
| 779 | */ |
||
| 780 | 9 | protected function getHost() |
|
| 784 | |||
| 785 | /** |
||
| 786 | * Returns port |
||
| 787 | * |
||
| 788 | * @return int |
||
| 789 | */ |
||
| 790 | 9 | protected function getPort() |
|
| 794 | |||
| 795 | /** |
||
| 796 | * Sets given url as curl "url" parameter |
||
| 797 | * |
||
| 798 | * @param string $url complete url to query |
||
| 799 | * |
||
| 800 | * @return void |
||
| 801 | */ |
||
| 802 | 9 | protected function setUrl($url) |
|
| 806 | |||
| 807 | /** |
||
| 808 | * Sets given request method as curl "request" parameter |
||
| 809 | * |
||
| 810 | * @param string $value the request method |
||
| 811 | * |
||
| 812 | * @return void |
||
| 813 | */ |
||
| 814 | 9 | protected function setMethod($value) |
|
| 818 | |||
| 819 | /** |
||
| 820 | * Sets given string as curl post body |
||
| 821 | * |
||
| 822 | * @param string $body the "post" body |
||
| 823 | * |
||
| 824 | * @return void |
||
| 825 | */ |
||
| 826 | 9 | protected function setBody($body) |
|
| 830 | |||
| 831 | /** |
||
| 832 | * Adds a new http header to header list |
||
| 833 | * |
||
| 834 | * @param string $header http header to set |
||
| 835 | * |
||
| 836 | * @return void |
||
| 837 | */ |
||
| 838 | 3 | public function setHttpHeader($header) |
|
| 845 | |||
| 846 | /** |
||
| 847 | * Returns http headers as array |
||
| 848 | * |
||
| 849 | * @return array |
||
| 850 | */ |
||
| 851 | 10 | protected function getHttpHeaders() |
|
| 855 | |||
| 856 | /** |
||
| 857 | * Saving given array of http headers to attributes |
||
| 858 | * |
||
| 859 | * @param array $headers http headers as array |
||
| 860 | * |
||
| 861 | * @return void |
||
| 862 | */ |
||
| 863 | 3 | protected function setHttpHeaders(array $headers) |
|
| 867 | } |
||
| 868 |