Complex classes like HTTP 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 HTTP, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | class HTTP { |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Curl object |
||
| 33 | * |
||
| 34 | * @var resource a cURL handle |
||
| 35 | * @access private |
||
| 36 | */ |
||
| 37 | private $curl_instance; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Hash to use for cookies |
||
| 41 | * |
||
| 42 | * @var string |
||
| 43 | * @access private |
||
| 44 | */ |
||
| 45 | private $cookie_hash; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Whether or not to enable GET:, POST:, and DLOAD: messages being sent to the terminal. |
||
| 49 | * |
||
| 50 | * @var bool |
||
| 51 | * @access private |
||
| 52 | */ |
||
| 53 | private $echo; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Useragent |
||
| 57 | * |
||
| 58 | * @var mixed |
||
| 59 | * @access private |
||
| 60 | */ |
||
| 61 | private $user_agent; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Temporary file where cookies are stored |
||
| 65 | * |
||
| 66 | * @var mixed |
||
| 67 | * @access private |
||
| 68 | */ |
||
| 69 | private $cookie_jar; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var string|null |
||
| 73 | */ |
||
| 74 | private $lastHeader = null; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Construction method for the HTTP class |
||
| 78 | * |
||
| 79 | * @access public |
||
| 80 | * |
||
| 81 | * @param bool $echo Whether or not to enable GET:, POST:, and DLOAD: messages being sent to the terminal. Default false; |
||
| 82 | * |
||
| 83 | * @note please consider using HTTP::getDefaultInstance() instead |
||
| 84 | * |
||
| 85 | * @throws RuntimeException |
||
| 86 | * @throws DependencyError |
||
| 87 | * |
||
| 88 | * @return HTTP |
||
|
|
|||
| 89 | */ |
||
| 90 | public function __construct($echo = false) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @param array $extraHeaders |
||
| 139 | */ |
||
| 140 | private function setCurlHeaders( $extraHeaders = array() ) { |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @param boolean $verifyssl |
||
| 146 | */ |
||
| 147 | private function setVerifySSL( $verifyssl = null ) { |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @param string $cookie_file |
||
| 163 | */ |
||
| 164 | public function setCookieJar($cookie_file) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @param null $user_agent |
||
| 176 | * @throws BadEntryError |
||
| 177 | * @throws HookError |
||
| 178 | */ |
||
| 179 | public function setUserAgent($user_agent = null) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @return string|bool Data. False on failure. |
||
| 190 | * @throws CURLError |
||
| 191 | */ |
||
| 192 | private function doCurlExecWithRetrys() { |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Get an url with HTTP GET |
||
| 221 | * |
||
| 222 | * @access public |
||
| 223 | * |
||
| 224 | * @param string $url URL to get |
||
| 225 | * @param array|null $data Array of data to pass. Gets transformed into the URL inside the function. Default null. |
||
| 226 | * @param array $headers Array of headers to pass to curl |
||
| 227 | * @param bool $verifyssl override for the global verifyssl value |
||
| 228 | * |
||
| 229 | * @return bool|string Result |
||
| 230 | */ |
||
| 231 | public function get( $url, $data = null, $headers = array(), $verifyssl = null ) { |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Returns the HTTP code of the last request |
||
| 266 | * |
||
| 267 | * @access public |
||
| 268 | * @return int HTTP code |
||
| 269 | */ |
||
| 270 | public function get_HTTP_code() |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Sends data via HTTP POST |
||
| 278 | * |
||
| 279 | * @access public |
||
| 280 | * |
||
| 281 | * @param string $url URL to send |
||
| 282 | * @param array $data Array of data to pass. |
||
| 283 | * @param array $headers Array of headers to pass to curl |
||
| 284 | * |
||
| 285 | * @param bool|null $verifyssl override for global verifyssl value |
||
| 286 | * |
||
| 287 | * @return bool|string Result |
||
| 288 | */ |
||
| 289 | public function post($url, $data, $headers = array(), $verifyssl = null) |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Downloads an URL to the local disk |
||
| 321 | * |
||
| 322 | * @access public |
||
| 323 | * |
||
| 324 | * @param string $url URL to get |
||
| 325 | * @param string $local Local filename to download to |
||
| 326 | * @param array $headers Array of headers to pass to curl |
||
| 327 | * @param bool|null $verifyssl |
||
| 328 | * |
||
| 329 | * @return bool |
||
| 330 | */ |
||
| 331 | function download( $url, $local, $headers = array(), $verifyssl = null ) { |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Gets the Header for the last request made |
||
| 361 | * @return null|string |
||
| 362 | */ |
||
| 363 | public function getLastHeader() { |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Destructor, deletes cookies and closes cURL class |
||
| 369 | * |
||
| 370 | * @access public |
||
| 371 | * @return void |
||
| 372 | */ |
||
| 373 | public function __destruct() |
||
| 381 | |||
| 382 | /** |
||
| 383 | * The below allows us to only have one instance of this class |
||
| 384 | */ |
||
| 385 | private static $defaultInstance = null; |
||
| 386 | private static $defaultInstanceWithEcho = null; |
||
| 387 | |||
| 388 | /** |
||
| 389 | * @param bool|false $echo |
||
| 390 | * @return HTTP|null |
||
| 391 | */ |
||
| 392 | public static function getDefaultInstance($echo = false) |
||
| 406 | |||
| 407 | } |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.