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) |
||
| 91 | { |
||
| 92 | if( !function_exists( 'curl_init' ) ) { |
||
| 93 | throw new DependencyError( "cURL", "http://us2.php.net/manual/en/curl.requirements.php" ); |
||
| 94 | } |
||
| 95 | |||
| 96 | $this->echo = $echo; |
||
| 97 | $this->curl_instance = curl_init(); |
||
| 98 | if( $this->curl_instance === false ) { |
||
| 99 | throw new RuntimeException( 'Failed to initialize curl' ); |
||
| 100 | } |
||
| 101 | $this->cookie_hash = md5( time() . '-' . rand( 0, 999 ) ); |
||
| 102 | $this->cookie_jar = sys_get_temp_dir() . 'peachy.cookies.' . $this->cookie_hash . '.dat'; |
||
| 103 | |||
| 104 | $userAgent = 'Peachy MediaWiki Bot API'; |
||
| 105 | if( defined( 'PEACHYVERSION' ) ) $userAgent .= ' Version ' . PEACHYVERSION; |
||
| 106 | $this->setUserAgent( $userAgent ); |
||
| 107 | |||
| 108 | Hooks::runHook( 'HTTPNewCURLInstance', array( &$this, &$echo ) ); |
||
| 109 | |||
| 110 | $this->setCookieJar( $this->cookie_jar ); |
||
| 111 | |||
| 112 | curl_setopt( $this->curl_instance, CURLOPT_MAXCONNECTS, 100 ); |
||
| 113 | curl_setopt( $this->curl_instance, CURLOPT_CLOSEPOLICY, CURLCLOSEPOLICY_LEAST_RECENTLY_USED ); |
||
| 114 | curl_setopt( $this->curl_instance, CURLOPT_MAXREDIRS, 10 ); |
||
| 115 | $this->setCurlHeaders(); |
||
| 116 | curl_setopt( $this->curl_instance, CURLOPT_ENCODING, 'gzip' ); |
||
| 117 | curl_setopt( $this->curl_instance, CURLOPT_RETURNTRANSFER, 1 ); |
||
| 118 | curl_setopt( $this->curl_instance, CURLOPT_HEADER, 1 ); |
||
| 119 | curl_setopt( $this->curl_instance, CURLOPT_TIMEOUT, 10 ); |
||
| 120 | curl_setopt( $this->curl_instance, CURLOPT_CONNECTTIMEOUT, 10 ); |
||
| 121 | |||
| 122 | global $pgProxy; |
||
| 123 | if( isset( $pgProxy ) && count( $pgProxy ) ) { |
||
| 124 | curl_setopt( $this->curl_instance, CURLOPT_PROXY, $pgProxy['addr'] ); |
||
| 125 | if( isset( $pgProxy['type'] ) ) { |
||
| 126 | curl_setopt( $this->curl_instance, CURLOPT_PROXYTYPE, $pgProxy['type'] ); |
||
| 127 | } |
||
| 128 | if( isset( $pgProxy['userpass'] ) ) { |
||
| 129 | curl_setopt( $this->curl_instance, CURLOPT_PROXYUSERPWD, $pgProxy['userpass'] ); |
||
| 130 | } |
||
| 131 | if( isset( $pgProxy['port'] ) ) { |
||
| 132 | curl_setopt( $this->curl_instance, CURLOPT_PROXYPORT, $pgProxy['port'] ); |
||
| 133 | } |
||
| 134 | } |
||
| 135 | } |
||
| 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 ) { |
||
| 232 | global $argv, $displayGetOutData; |
||
| 233 | |||
| 234 | if( is_string( $headers ) ) curl_setopt( $this->curl_instance, CURLOPT_HTTPHEADER, array( $headers ) ); |
||
| 235 | else $this->setCurlHeaders( $headers ); |
||
| 236 | $this->setVerifySSL( $verifyssl ); |
||
| 237 | |||
| 238 | curl_setopt( $this->curl_instance, CURLOPT_FOLLOWLOCATION, 1 ); |
||
| 239 | curl_setopt( $this->curl_instance, CURLOPT_HTTPGET, 1 ); |
||
| 240 | curl_setopt( $this->curl_instance, CURLOPT_POST, 0 ); |
||
| 241 | |||
| 242 | /*if( !is_null( $this->use_cookie ) ) { |
||
| 243 | curl_setopt($this->curl_instance,CURLOPT_COOKIE, $this->use_cookie); |
||
| 244 | }*/ |
||
| 245 | |||
| 246 | if( !is_null( $data ) && is_array( $data ) && !empty( $data ) ) { |
||
| 247 | $url .= '?' . http_build_query( $data ); |
||
| 248 | } |
||
| 249 | |||
| 250 | curl_setopt( $this->curl_instance, CURLOPT_URL, $url ); |
||
| 251 | |||
| 252 | if( ( !is_null( $argv ) && in_array( 'peachyecho', $argv ) ) || $this->echo ) { |
||
| 253 | if( $displayGetOutData ) { |
||
| 254 | pecho( "GET: $url\n", PECHO_NORMAL ); |
||
| 255 | } |
||
| 256 | } |
||
| 257 | |||
| 258 | Hooks::runHook( 'HTTPGet', array( &$this, &$url, &$data ) ); |
||
| 259 | |||
| 260 | return $this->doCurlExecWithRetrys(); |
||
| 261 | |||
| 262 | } |
||
| 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) |
||
| 290 | { |
||
| 291 | global $argv, $displayPostOutData; |
||
| 292 | |||
| 293 | if( is_string( $headers ) ) curl_setopt( $this->curl_instance, CURLOPT_HTTPHEADER, array( $headers ) ); |
||
| 294 | else $this->setCurlHeaders( $headers ); |
||
| 295 | $this->setVerifySSL( $verifyssl ); |
||
| 296 | |||
| 297 | curl_setopt( $this->curl_instance, CURLOPT_FOLLOWLOCATION, 0 ); |
||
| 298 | curl_setopt( $this->curl_instance, CURLOPT_HTTPGET, 0 ); |
||
| 299 | curl_setopt( $this->curl_instance, CURLOPT_POST, 1 ); |
||
| 300 | curl_setopt( $this->curl_instance, CURLOPT_POSTFIELDS, $data ); |
||
| 301 | |||
| 302 | /*if( !is_null( $this->use_cookie ) ) { |
||
| 303 | curl_setopt($this->curl_instance,CURLOPT_COOKIE, $this->use_cookie); |
||
| 304 | }*/ |
||
| 305 | |||
| 306 | curl_setopt( $this->curl_instance, CURLOPT_URL, $url ); |
||
| 307 | |||
| 308 | if( ( !is_null( $argv ) && in_array( 'peachyecho', $argv ) ) || $this->echo ) { |
||
| 309 | if( $displayPostOutData ) { |
||
| 310 | pecho( "POST: $url\n", PECHO_NORMAL ); |
||
| 311 | } |
||
| 312 | } |
||
| 313 | |||
| 314 | Hooks::runHook( 'HTTPPost', array( &$this, &$url, &$data ) ); |
||
| 315 | |||
| 316 | return $this->doCurlExecWithRetrys(); |
||
| 317 | } |
||
| 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 ) { |
||
| 332 | global $argv; |
||
| 333 | |||
| 334 | $out = fopen( $local, 'wb' ); |
||
| 335 | |||
| 336 | if( is_string( $headers ) ) curl_setopt( $this->curl_instance, CURLOPT_HTTPHEADER, array( $headers ) ); |
||
| 337 | else $this->setCurlHeaders( $headers ); |
||
| 338 | $this->setVerifySSL( $verifyssl ); |
||
| 339 | |||
| 340 | // curl_setopt($this->curl_instance, CURLOPT_FILE, $out); |
||
| 341 | curl_setopt( $this->curl_instance, CURLOPT_HTTPGET, 1 ); |
||
| 342 | curl_setopt( $this->curl_instance, CURLOPT_POST, 0 ); |
||
| 343 | curl_setopt( $this->curl_instance, CURLOPT_URL, $url ); |
||
| 344 | curl_setopt( $this->curl_instance, CURLOPT_HEADER, 0 ); |
||
| 345 | |||
| 346 | if( ( !is_null( $argv ) && in_array( 'peachyecho', $argv ) ) || $this->echo ) { |
||
| 347 | pecho( "DLOAD: $url\n", PECHO_NORMAL ); |
||
| 348 | } |
||
| 349 | |||
| 350 | Hooks::runHook( 'HTTPDownload', array( &$this, &$url, &$local ) ); |
||
| 351 | |||
| 352 | fwrite( $out, $this->doCurlExecWithRetrys() ); |
||
| 353 | fclose( $out ); |
||
| 354 | |||
| 355 | return true; |
||
| 356 | |||
| 357 | } |
||
| 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.