Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Curl 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 Curl, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class Curl extends Fwolflib { |
||
|
|||
18 | |||
19 | /** |
||
20 | * File to save cookie |
||
21 | * @var string |
||
22 | * @access protected |
||
23 | */ |
||
24 | protected $mCookiefile = '/dev/null'; |
||
25 | |||
26 | /** |
||
27 | * File to save log |
||
28 | * Set to empty string to direct echo out(default), |
||
29 | * Or set to a file to save in, |
||
30 | * Or set to /dev/null to do nothing. |
||
31 | * @var string |
||
32 | * @access public |
||
33 | */ |
||
34 | public $mLogfile = ''; |
||
35 | |||
36 | /** |
||
37 | * Result read from webserver |
||
38 | * @var string |
||
39 | * @access public |
||
40 | */ |
||
41 | public $mRs = ''; |
||
42 | |||
43 | /** |
||
44 | * Curl session resource |
||
45 | * @var object |
||
46 | * @access public |
||
47 | */ |
||
48 | public $mSh; |
||
49 | |||
50 | |||
51 | /** |
||
52 | * Construct function |
||
53 | * @access public |
||
54 | */ |
||
55 | function __construct() { |
||
56 | parent::__construct(); |
||
57 | |||
58 | $this->mSh = curl_init(); |
||
59 | $this->SetoptCommon(); |
||
60 | } // end of func __construct |
||
61 | |||
62 | |||
63 | /** |
||
64 | * Destruct function |
||
65 | * @access public |
||
66 | */ |
||
67 | function __destruct() { |
||
76 | |||
77 | |||
78 | /** |
||
79 | * Http get content from host |
||
80 | * |
||
81 | * @param string $url Host address |
||
82 | * @param mixed $param Get parameter, can be string or array. |
||
83 | * @access public |
||
84 | * @return string |
||
85 | */ |
||
86 | public function Get ($url, $param = '') { |
||
119 | |||
120 | |||
121 | /** |
||
122 | * Get server return code of last curl_exec |
||
123 | * 200-ok, 404-missing file, etc... |
||
124 | * @return int |
||
125 | */ |
||
126 | public function GetLastCode() |
||
131 | |||
132 | |||
133 | /** |
||
134 | * Get server return content type of last curl_exec |
||
135 | * text/html, image/png, etc... |
||
136 | * @return string |
||
137 | */ |
||
138 | public function GetLastContentType() |
||
143 | |||
144 | |||
145 | /** |
||
146 | * Match content to variables using preg |
||
147 | * To read content currectly, content parsing is nesessary |
||
148 | * Return value maybe string or array, use careful and |
||
149 | * remind which value you use it for. |
||
150 | * @param string $preg |
||
151 | * @param string $str If obmitted, use $this->mRs |
||
152 | * @return mixed |
||
153 | * @see $mRs |
||
154 | * @access public |
||
155 | */ |
||
156 | public function Match($preg, $str = '') |
||
185 | |||
186 | |||
187 | /** |
||
188 | * Http post content from host |
||
189 | * |
||
190 | * @param string $url Host address |
||
191 | * @param mixed $param Post parameter, can be string or array. |
||
192 | * @return string |
||
193 | */ |
||
194 | public function Post ($url, $param = '') { |
||
214 | |||
215 | |||
216 | /** |
||
217 | * Set some common options using curl_setopt |
||
218 | * @access public |
||
219 | */ |
||
220 | public function SetoptCommon () { |
||
247 | |||
248 | |||
249 | /** |
||
250 | * Set cookie option |
||
251 | * |
||
252 | * If filename is not given, use default, |
||
253 | * If file is given, use & set it as default. |
||
254 | * |
||
255 | * @param string $cookiefile |
||
256 | * @access public |
||
257 | */ |
||
258 | public function SetoptCookie ($cookiefile = '') { |
||
268 | |||
269 | |||
270 | /** |
||
271 | * Set proxy option |
||
272 | * @param int $ptype 0-no proxy, 1-http, 2-socks5 |
||
273 | * @param string $phost |
||
274 | * @param int $pport |
||
275 | * @param string $pauth [username]:[password] |
||
276 | * @access public |
||
277 | */ |
||
278 | public function SetoptProxy($ptype, $phost, $pport, $pauth = '') |
||
295 | |||
296 | |||
297 | /** |
||
298 | * Set http referer url |
||
299 | * @param string $url |
||
300 | */ |
||
301 | public function SetoptReferer($url) |
||
306 | |||
307 | |||
308 | /** |
||
309 | * Enable or disable ssl verify functin |
||
310 | * Ssl verify is enabled by curl in default |
||
311 | * |
||
312 | * @param boolean $en True to enable, false to disable |
||
313 | */ |
||
314 | public function SetoptSslverify ($en = true) { |
||
320 | |||
321 | |||
322 | /** |
||
323 | * Set browser agent option |
||
324 | * @param string $browser |
||
325 | * @access public |
||
326 | */ |
||
327 | public function SetoptUseragent ($browser) { |
||
335 | |||
336 | |||
337 | } // end of class Curl |
||
338 | ?> |
||
339 |
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.