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 | private function setCurlHeaders( $extraHeaders = array() ) { |
||
140 | |||
141 | /** |
||
142 | * @param boolean $verifyssl |
||
143 | */ |
||
144 | private function setVerifySSL( $verifyssl = null ) { |
||
157 | |||
158 | /** |
||
159 | * @param string $cookie_file |
||
160 | */ |
||
161 | public function setCookieJar($cookie_file) |
||
170 | |||
171 | public function setUserAgent($user_agent = null) |
||
179 | |||
180 | /** |
||
181 | * @return string|bool Data. False on failure. |
||
182 | * @throws CURLError |
||
183 | */ |
||
184 | private function doCurlExecWithRetrys() { |
||
210 | |||
211 | /** |
||
212 | * Get an url with HTTP GET |
||
213 | * |
||
214 | * @access public |
||
215 | * |
||
216 | * @param string $url URL to get |
||
217 | * @param array|null $data Array of data to pass. Gets transformed into the URL inside the function. Default null. |
||
218 | * @param array $headers Array of headers to pass to curl |
||
219 | * @param bool $verifyssl override for the global verifyssl value |
||
220 | * |
||
221 | * @return bool|string Result |
||
222 | */ |
||
223 | public function get( $url, $data = null, $headers = array(), $verifyssl = null ) { |
||
255 | |||
256 | /** |
||
257 | * Returns the HTTP code of the last request |
||
258 | * |
||
259 | * @access public |
||
260 | * @return int HTTP code |
||
261 | */ |
||
262 | public function get_HTTP_code() |
||
267 | |||
268 | /** |
||
269 | * Sends data via HTTP POST |
||
270 | * |
||
271 | * @access public |
||
272 | * |
||
273 | * @param string $url URL to send |
||
274 | * @param array $data Array of data to pass. |
||
275 | * @param array $headers Array of headers to pass to curl |
||
276 | * |
||
277 | * @param bool|null $verifyssl override for global verifyssl value |
||
278 | * |
||
279 | * @return bool|string Result |
||
280 | */ |
||
281 | public function post($url, $data, $headers = array(), $verifyssl = null) |
||
310 | |||
311 | /** |
||
312 | * Downloads an URL to the local disk |
||
313 | * |
||
314 | * @access public |
||
315 | * |
||
316 | * @param string $url URL to get |
||
317 | * @param string $local Local filename to download to |
||
318 | * @param array $headers Array of headers to pass to curl |
||
319 | * @param bool|null $verifyssl |
||
320 | * |
||
321 | * @return bool |
||
322 | */ |
||
323 | function download( $url, $local, $headers = array(), $verifyssl = null ) { |
||
350 | |||
351 | /** |
||
352 | * Gets the Header for the last request made |
||
353 | * @return null|string |
||
354 | */ |
||
355 | public function getLastHeader() { |
||
358 | |||
359 | /** |
||
360 | * Destructor, deletes cookies and closes cURL class |
||
361 | * |
||
362 | * @access public |
||
363 | * @return void |
||
364 | */ |
||
365 | public function __destruct() |
||
373 | |||
374 | /** |
||
375 | * The below allows us to only have one instance of this class |
||
376 | */ |
||
377 | private static $defaultInstance = null; |
||
378 | private static $defaultInstanceWithEcho = null; |
||
379 | |||
380 | public static function getDefaultInstance($echo = false) |
||
394 | |||
395 | } |
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.