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 |
||
| 12 | class Curl |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * File to save cookie |
||
| 16 | * |
||
| 17 | * @var string |
||
| 18 | */ |
||
| 19 | protected $cookieFile = ''; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Debug mode, will log more information, like get/post url |
||
| 23 | * |
||
| 24 | * @var boolean |
||
| 25 | */ |
||
| 26 | protected $debug = false; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * cURL handle |
||
| 30 | * |
||
| 31 | * @var resource |
||
| 32 | */ |
||
| 33 | protected $handle; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Result read from web server |
||
| 37 | * |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | protected $html = ''; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * File to save log |
||
| 44 | * |
||
| 45 | * Empty for direct print out(default), or set to a valid file to save, or |
||
| 46 | * set to /dev/null to do nothing. |
||
| 47 | * |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | protected $logFile = null; |
||
| 51 | |||
| 52 | |||
| 53 | /** |
||
| 54 | * Destructor |
||
| 55 | */ |
||
| 56 | public function __destruct() |
||
| 60 | |||
| 61 | |||
| 62 | /** |
||
| 63 | * Http get method |
||
| 64 | * |
||
| 65 | * @param string $url Host address |
||
| 66 | * @param string|array $param Get parameter |
||
| 67 | * @return string |
||
| 68 | */ |
||
| 69 | public function get($url, $param = null) |
||
| 109 | |||
| 110 | |||
| 111 | /** |
||
| 112 | * Get and initialize curl handle |
||
| 113 | * |
||
| 114 | * @return resource |
||
| 115 | */ |
||
| 116 | public function getHandle() |
||
| 125 | |||
| 126 | |||
| 127 | /** |
||
| 128 | * Get server return code of last curl_exec |
||
| 129 | * |
||
| 130 | * 200-ok, 404-missing file, etc |
||
| 131 | * |
||
| 132 | * @return int |
||
| 133 | */ |
||
| 134 | public function getLastCode() |
||
| 142 | |||
| 143 | |||
| 144 | /** |
||
| 145 | * Get server return content type of last curl_exec |
||
| 146 | * |
||
| 147 | * text/html, image/png, etc |
||
| 148 | * |
||
| 149 | * @return string |
||
| 150 | */ |
||
| 151 | public function getLastContentType() |
||
| 159 | |||
| 160 | |||
| 161 | /** |
||
| 162 | * Log curl action |
||
| 163 | * |
||
| 164 | * @param string $msg |
||
| 165 | */ |
||
| 166 | protected function log($msg) |
||
| 182 | |||
| 183 | |||
| 184 | /** |
||
| 185 | * Match content to variables using preg |
||
| 186 | * |
||
| 187 | * Return value maybe string(for single result) or array(for multiple |
||
| 188 | * result), use carefully and remind which value you use it for. |
||
| 189 | * |
||
| 190 | * Regex should surround wih '/', and mark match target with '()'. |
||
| 191 | * |
||
| 192 | * @param string $preg |
||
| 193 | * @param string $html If omitted, use $this->html |
||
| 194 | * @return string|array |
||
| 195 | */ |
||
| 196 | public function match($preg, $html = '') |
||
| 232 | |||
| 233 | |||
| 234 | /** |
||
| 235 | * Http post method |
||
| 236 | * |
||
| 237 | * @param string $url Host address |
||
| 238 | * @param string|array $params Post parameter, prefer array |
||
| 239 | * @return string |
||
| 240 | */ |
||
| 241 | public function post($url, $params = []) |
||
| 267 | |||
| 268 | |||
| 269 | /** |
||
| 270 | * Renew handle |
||
| 271 | * |
||
| 272 | * This is useful when cookie file is used, and want to reload cookies, |
||
| 273 | * eg: after login, reload cookie then they will be used in next operation. |
||
| 274 | * |
||
| 275 | * @return static |
||
| 276 | */ |
||
| 277 | public function renewHandle() |
||
| 295 | |||
| 296 | |||
| 297 | /** |
||
| 298 | * Set cookie option |
||
| 299 | * |
||
| 300 | * If filename is not given, use default, |
||
| 301 | * If file is given, use & set it as default. |
||
| 302 | * |
||
| 303 | * @param string $cookieFile |
||
| 304 | */ |
||
| 305 | public function setCookieFile($cookieFile = '') |
||
| 316 | |||
| 317 | |||
| 318 | /** |
||
| 319 | * Setter of $debug |
||
| 320 | * |
||
| 321 | * @param boolean $debug |
||
| 322 | * @return static |
||
| 323 | */ |
||
| 324 | public function setDebug($debug) |
||
| 330 | |||
| 331 | |||
| 332 | /** |
||
| 333 | * Set default options |
||
| 334 | * |
||
| 335 | * @param resource $handle |
||
| 336 | * @return static |
||
| 337 | */ |
||
| 338 | protected function setDefaultOptions($handle) |
||
| 372 | |||
| 373 | |||
| 374 | /** |
||
| 375 | * Setter of $logFile |
||
| 376 | * |
||
| 377 | * @param string $logFile |
||
| 378 | * @return static |
||
| 379 | */ |
||
| 380 | public function setLogFile($logFile) |
||
| 386 | |||
| 387 | |||
| 388 | /** |
||
| 389 | * Set proxy option |
||
| 390 | * |
||
| 391 | * @param int $type 0-no proxy, 1-http, 2-socks5 |
||
| 392 | * @param string $host |
||
| 393 | * @param int $port |
||
| 394 | * @param string $auth [username]:[password] |
||
| 395 | */ |
||
| 396 | public function setProxy($type, $host, $port, $auth = '') |
||
| 422 | |||
| 423 | |||
| 424 | /** |
||
| 425 | * Set http referrer url |
||
| 426 | * |
||
| 427 | * @param string $url |
||
| 428 | */ |
||
| 429 | public function setReferrer($url = null) |
||
| 437 | |||
| 438 | |||
| 439 | /** |
||
| 440 | * Enable or disable ssl verify function |
||
| 441 | * |
||
| 442 | * Ssl verify is enabled by curl in default. |
||
| 443 | * |
||
| 444 | * @param boolean $enable |
||
| 445 | */ |
||
| 446 | public function setSslVerify($enable = true) |
||
| 453 | |||
| 454 | |||
| 455 | /** |
||
| 456 | * Set browser agent option |
||
| 457 | * |
||
| 458 | * @param string $userAgent |
||
| 459 | */ |
||
| 460 | public function setUserAgent($userAgent = 'curl') |
||
| 466 | } |
||
| 467 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.