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_MAXREDIRS, 10 ); |
||
114 | $this->setCurlHeaders(); |
||
115 | curl_setopt( $this->curl_instance, CURLOPT_ENCODING, 'gzip' ); |
||
116 | curl_setopt( $this->curl_instance, CURLOPT_RETURNTRANSFER, 1 ); |
||
117 | curl_setopt( $this->curl_instance, CURLOPT_HEADER, 1 ); |
||
118 | curl_setopt( $this->curl_instance, CURLOPT_TIMEOUT, 10 ); |
||
119 | curl_setopt( $this->curl_instance, CURLOPT_CONNECTTIMEOUT, 10 ); |
||
120 | |||
121 | global $pgProxy; |
||
122 | if( isset( $pgProxy ) && count( $pgProxy ) ) { |
||
123 | curl_setopt( $this->curl_instance, CURLOPT_PROXY, $pgProxy['addr'] ); |
||
124 | if( isset( $pgProxy['type'] ) ) { |
||
125 | curl_setopt( $this->curl_instance, CURLOPT_PROXYTYPE, $pgProxy['type'] ); |
||
126 | } |
||
127 | if( isset( $pgProxy['userpass'] ) ) { |
||
128 | curl_setopt( $this->curl_instance, CURLOPT_PROXYUSERPWD, $pgProxy['userpass'] ); |
||
129 | } |
||
130 | if( isset( $pgProxy['port'] ) ) { |
||
131 | curl_setopt( $this->curl_instance, CURLOPT_PROXYPORT, $pgProxy['port'] ); |
||
132 | } |
||
133 | } |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * @param array $extraHeaders |
||
138 | */ |
||
139 | private function setCurlHeaders( $extraHeaders = array() ) { |
||
142 | |||
143 | /** |
||
144 | * @param boolean $verifyssl |
||
145 | */ |
||
146 | private function setVerifySSL( $verifyssl = null ) { |
||
159 | |||
160 | /** |
||
161 | * @param string $cookie_file |
||
162 | */ |
||
163 | public function setCookieJar($cookie_file) |
||
172 | |||
173 | /** |
||
174 | * @param null $user_agent |
||
175 | * @throws BadEntryError |
||
176 | * @throws HookError |
||
177 | */ |
||
178 | public function setUserAgent($user_agent = null) |
||
186 | |||
187 | /** |
||
188 | * @return string|bool Data. False on failure. |
||
189 | * @throws CURLError |
||
190 | */ |
||
191 | private function doCurlExecWithRetrys() { |
||
217 | |||
218 | /** |
||
219 | * Get an url with HTTP GET |
||
220 | * |
||
221 | * @access public |
||
222 | * |
||
223 | * @param string $url URL to get |
||
224 | * @param array|null $data Array of data to pass. Gets transformed into the URL inside the function. Default null. |
||
225 | * @param array $headers Array of headers to pass to curl |
||
226 | * @param bool $verifyssl override for the global verifyssl value |
||
227 | * |
||
228 | * @return bool|string Result |
||
229 | */ |
||
230 | public function get( $url, $data = null, $headers = array(), $verifyssl = null ) { |
||
262 | |||
263 | /** |
||
264 | * Returns the HTTP code of the last request |
||
265 | * |
||
266 | * @access public |
||
267 | * @return int HTTP code |
||
268 | */ |
||
269 | public function get_HTTP_code() |
||
274 | |||
275 | /** |
||
276 | * Sends data via HTTP POST |
||
277 | * |
||
278 | * @access public |
||
279 | * |
||
280 | * @param string $url URL to send |
||
281 | * @param array $data Array of data to pass. |
||
282 | * @param array $headers Array of headers to pass to curl |
||
283 | * |
||
284 | * @param bool|null $verifyssl override for global verifyssl value |
||
285 | * |
||
286 | * @return bool|string Result |
||
287 | */ |
||
288 | public function post($url, $data, $headers = array(), $verifyssl = null) |
||
317 | |||
318 | /** |
||
319 | * Downloads an URL to the local disk |
||
320 | * |
||
321 | * @access public |
||
322 | * |
||
323 | * @param string $url URL to get |
||
324 | * @param string $local Local filename to download to |
||
325 | * @param array $headers Array of headers to pass to curl |
||
326 | * @param bool|null $verifyssl |
||
327 | * |
||
328 | * @return bool |
||
329 | */ |
||
330 | function download( $url, $local, $headers = array(), $verifyssl = null ) { |
||
357 | |||
358 | /** |
||
359 | * Gets the Header for the last request made |
||
360 | * @return null|string |
||
361 | */ |
||
362 | public function getLastHeader() { |
||
365 | |||
366 | /** |
||
367 | * Destructor, deletes cookies and closes cURL class |
||
368 | * |
||
369 | * @access public |
||
370 | * @return void |
||
371 | */ |
||
372 | public function __destruct() |
||
380 | |||
381 | /** |
||
382 | * The below allows us to only have one instance of this class |
||
383 | */ |
||
384 | private static $defaultInstance = null; |
||
385 | private static $defaultInstanceWithEcho = null; |
||
386 | |||
387 | /** |
||
388 | * @param bool|false $echo |
||
389 | * @return HTTP|null |
||
390 | */ |
||
391 | public static function getDefaultInstance($echo = false) |
||
405 | |||
406 | } |
Adding a
@return
annotation 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.