Complex classes like CurlRequest 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 CurlRequest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class CurlRequest |
||
14 | { |
||
15 | /** |
||
16 | * If set to true (via self::setReturnHeaderOnly()), headers only are returned (via self::execute()) |
||
17 | * @var bool |
||
18 | */ |
||
19 | protected $headerOnly = false; |
||
20 | |||
21 | /** |
||
22 | * Curl resource handle |
||
23 | * @var resource |
||
24 | */ |
||
25 | public static $ch; |
||
26 | |||
27 | /* |
||
28 | * If set to true (via self::setEncodingGzip($gzip)), self::execute() will try to uncompress cURL output |
||
29 | * @var bool |
||
30 | */ |
||
31 | protected $gzip = false; |
||
32 | |||
33 | /** |
||
34 | * If set to true (via self::setReturnHeader()), self::execute() will extract HTTP header |
||
35 | * from the cURL output and stock it in self::$header wich can be get with self::getHeader() |
||
36 | * @var bool |
||
37 | */ |
||
38 | protected $rHeader = false; |
||
39 | |||
40 | /** |
||
41 | * Contain (after self::execute()) header returned by curl request |
||
42 | * @var string $header |
||
43 | */ |
||
44 | protected $header; |
||
45 | |||
46 | /** |
||
47 | * Constructor |
||
48 | * |
||
49 | * @param string $url The URL to request |
||
50 | * @param bool $usePreviousSession If the query must use the previous session (so using same connexion if it's the same host) |
||
51 | */ |
||
52 | public function __construct($url, $usePreviousSession = false) |
||
62 | |||
63 | /** |
||
64 | * Change the URL to cURL |
||
65 | * |
||
66 | * @param string $url URL to cURL |
||
67 | * @param bool $reset True if you want to remove cURLs params setted before calling this function |
||
68 | * |
||
69 | * @return self |
||
70 | */ |
||
71 | public function setUrl($url, $reset = false) |
||
80 | |||
81 | /** |
||
82 | * Add a cURL's option |
||
83 | * |
||
84 | * @param int $option cURL Predefined Constant |
||
85 | * @param mixed $value |
||
86 | * |
||
87 | * @return self |
||
88 | */ |
||
89 | public function setOpt($option, $value) |
||
95 | |||
96 | /** |
||
97 | * A short way to set some classic options to cURL a web page |
||
98 | * |
||
99 | * @return self |
||
100 | */ |
||
101 | public function setDefaultGetOptions($connectTimeOut = 5, $timeOut = 10, $dnsCacheTimeOut = 600, $followLocation = true, $maxRedirs = 5) |
||
113 | |||
114 | /** |
||
115 | * A short way to set some classic options to cURL a web page quickly (but lossing some data like header, cookie...) |
||
116 | * |
||
117 | * @return self |
||
118 | */ |
||
119 | public function setDefaultSpeedOptions() |
||
129 | |||
130 | /** |
||
131 | * Call it if you want header informations. |
||
132 | * After self::execute(), you would have this informations with getHeader(); |
||
133 | * |
||
134 | * @return self |
||
135 | */ |
||
136 | public function setReturnHeader() |
||
143 | |||
144 | /** |
||
145 | * Call it if you want header informations only. |
||
146 | * After self::execute(), you would have this informations with getHeader(); |
||
147 | * |
||
148 | * @return self |
||
149 | */ |
||
150 | public function setReturnHeaderOnly() |
||
158 | |||
159 | /** |
||
160 | * An self::setOpt()'s alias to add a cookie to your request |
||
161 | * |
||
162 | * @param string $cookie |
||
163 | * |
||
164 | * @return self |
||
165 | */ |
||
166 | public function setCookie($cookie) |
||
172 | |||
173 | /** |
||
174 | * An self::setOpt()'s alias to add a referrer to your request |
||
175 | * |
||
176 | * @param string $referrer |
||
177 | * |
||
178 | * @return self |
||
179 | */ |
||
180 | public function setReferrer($referrer) |
||
186 | |||
187 | /** |
||
188 | * An self::setOpt()'s alias to add an user-agent to your request |
||
189 | * |
||
190 | * @param string $ua |
||
191 | * |
||
192 | * @return self |
||
193 | */ |
||
194 | public function setUserAgent($ua) |
||
200 | |||
201 | /** |
||
202 | * An self::setUserAgent()'s alias to add an user-agent wich correspond to a Destkop PC |
||
203 | * |
||
204 | * @return self |
||
205 | */ |
||
206 | public function setDestkopUserAgent() |
||
212 | |||
213 | /** |
||
214 | * An self::setUserAgent()'s alias to add an user-agent wich correspond to a mobile |
||
215 | * |
||
216 | * @return self |
||
217 | */ |
||
218 | public function setMobileUserAgent() |
||
224 | |||
225 | /** |
||
226 | * An self::setUserAgent()'s alias to add an user-agent wich correspond to a webrowser without javascript |
||
227 | * |
||
228 | * @return self |
||
229 | */ |
||
230 | public function setLessJsUserAgent() |
||
236 | |||
237 | /** |
||
238 | * A short way to set post's options to cURL a web page |
||
239 | * |
||
240 | * @param array $post_array Contain data (key=>vvalue) to post |
||
241 | * |
||
242 | * @return self |
||
243 | */ |
||
244 | public function setPost($post_array) |
||
252 | |||
253 | /** |
||
254 | * If you want to request the URL and hope get the result gzipped. |
||
255 | * The output will be automatically uncompress with execute(); |
||
256 | * |
||
257 | * @return self |
||
258 | */ |
||
259 | public function setEncodingGzip($decode = false) |
||
266 | |||
267 | /** |
||
268 | * If you want to request the URL with a (http|socks...) proxy (public or private) |
||
269 | * |
||
270 | * @param string $proxy [scheme]IP:PORT[:LOGIN:PASSWORD] (Eg. : socks5://98.023.023.02:1098:cUrlRequestProxId:SecretPassword) |
||
271 | * |
||
272 | * @return self |
||
273 | */ |
||
274 | public function setProxy($proxy) |
||
288 | |||
289 | /** |
||
290 | * Return scheme from proxy string and remove Scheme From proxy |
||
291 | * |
||
292 | * @param string $proxy |
||
293 | * |
||
294 | * @return string |
||
295 | */ |
||
296 | protected static function getSchemeFrom(&$proxy) |
||
307 | |||
308 | /** |
||
309 | * Execute the request |
||
310 | * |
||
311 | * @return string wich is the request's result without the header (you can obtain with self::getHeader() now) |
||
312 | */ |
||
313 | public function execute() |
||
331 | |||
332 | /** |
||
333 | * Return header's data return by the request |
||
334 | * |
||
335 | * @param bool $arrayFormatted True to get an array, false to get a string |
||
336 | * |
||
337 | * @return array|string containing header's data |
||
338 | */ |
||
339 | public function getHeader($arrayFormatted = true) |
||
345 | |||
346 | /** |
||
347 | * Return current effective url |
||
348 | * |
||
349 | * @return string |
||
350 | */ |
||
351 | function getEffectiveUrl() |
||
355 | |||
356 | /** |
||
357 | * Return the cookie(s) returned by the request (if there are) |
||
358 | * |
||
359 | * @return null|array containing the cookies |
||
360 | */ |
||
361 | public function getCookies() |
||
370 | |||
371 | /** |
||
372 | * Return the last error number (curl_errno) |
||
373 | * |
||
374 | * @return int the error number or 0 (zero) if no error occurred. |
||
375 | */ |
||
376 | public function hasError() |
||
380 | |||
381 | /** |
||
382 | * Return a string containing the last error for the current session (curl_error) |
||
383 | * |
||
384 | * @return string the error message or '' (the empty string) if no error occurred. |
||
385 | */ |
||
386 | public function getErrors() |
||
390 | |||
391 | /** |
||
392 | * Get information regarding the request |
||
393 | * |
||
394 | * @return bool|array an associative array with the following elements (which correspond to opt), or FALSE on failure |
||
395 | */ |
||
396 | public function getInfo() |
||
400 | |||
401 | /** |
||
402 | * Close the connexion |
||
403 | * Call curl_reset function |
||
404 | */ |
||
405 | public function close() |
||
409 | |||
410 | /** |
||
411 | * Parse HTTP headers (php HTTP functions but generally, this packet isn't installed) |
||
412 | * @source http://www.php.net/manual/en/function.http-parse-headers.php#112917 |
||
413 | * |
||
414 | * @param string $raw_headers Contain HTTP headers |
||
415 | * |
||
416 | * @return bool|array an array on success or FALSE on failure. |
||
417 | */ |
||
418 | public static function http_parse_headers($raw_headers) |
||
452 | |||
453 | /** |
||
454 | * Decode a string |
||
455 | * |
||
456 | * @param string $str String to decode |
||
457 | */ |
||
458 | public static function gzdecode($str) |
||
462 | } |
||
463 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.