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:
| 1 | <?php |
||
| 12 | class FetchRequest extends BaseRequest |
||
| 13 | { |
||
| 14 | protected static $defaultCurlOptions = array( |
||
| 15 | CURLOPT_HTTPGET => true, |
||
| 16 | CURLOPT_FOLLOWLOCATION => true, |
||
| 17 | CURLOPT_MAXREDIRS => 20, |
||
| 18 | CURLOPT_ENCODING => '', |
||
| 19 | CURLOPT_RETURNTRANSFER => true, |
||
| 20 | ); |
||
| 21 | |||
| 22 | private $headers = array(); |
||
|
|
|||
| 23 | private $errno; |
||
| 24 | private $error; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @param string $url |
||
| 28 | * @param IO\IOInterface $io |
||
| 29 | * @param Config $config |
||
| 30 | */ |
||
| 31 | 1 | View Code Duplication | public function __construct($url, IO\IOInterface $io, Config $config) |
| 32 | { |
||
| 33 | 1 | $this->setURL($url); |
|
| 34 | 1 | $this->setCA($config->get('capath'), $config->get('cafile')); |
|
| 35 | 1 | $this->setupAuthentication( |
|
| 36 | 1 | $io, |
|
| 37 | 1 | false, |
|
| 38 | 1 | $config->get('github-domains') ?: array(), |
|
| 39 | 1 | $config->get('gitlab-domains') ?: array() |
|
| 40 | ); |
||
| 41 | 1 | } |
|
| 42 | |||
| 43 | 1 | public function getCurlOptions() |
|
| 50 | |||
| 51 | 1 | private static function getCurl($key) |
|
| 64 | |||
| 65 | /** |
||
| 66 | * @return string|false |
||
| 67 | */ |
||
| 68 | 1 | public function fetch() |
|
| 69 | { |
||
| 70 | 1 | $url = $this->getOriginURL(); |
|
| 71 | 1 | $ch = self::getCurl($url); |
|
| 72 | 1 | curl_setopt_array($ch, $this->getCurlOptions()); |
|
| 73 | |||
| 74 | // feature detect http2 support in the php client/curl version. |
||
| 75 | 1 | if (0 === stripos($url, 'https://') && defined('CURL_VERSION_HTTP2') && defined('CURL_HTTP_VERSION_2_0')) { |
|
| 76 | 1 | $curlVersion = curl_version(); |
|
| 77 | 1 | if ($curlVersion["features"] & CURL_VERSION_HTTP2 !== 0) { |
|
| 78 | 1 | curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0); |
|
| 79 | } |
||
| 80 | } |
||
| 81 | |||
| 82 | 1 | $result = curl_exec($ch); |
|
| 83 | |||
| 84 | 1 | $this->errno = $errno = curl_errno($ch); |
|
| 85 | 1 | $this->error = curl_error($ch); |
|
| 86 | 1 | $info = curl_getinfo($ch); |
|
| 87 | |||
| 88 | 1 | if (!$this->isHTTP()) { |
|
| 89 | return $result; |
||
| 90 | 1 | } elseif ($errno === CURLE_OK && in_array($info['http_code'], array(200, 304))) { |
|
| 91 | return $result; |
||
| 92 | } else { |
||
| 93 | 1 | return false; |
|
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | public function getLastError() |
||
| 105 | |||
| 106 | 1 | public function getLastHeaders() |
|
| 110 | |||
| 111 | public function headerCallback($ch, $headerString) |
||
| 117 | } |
||
| 118 |