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 BaseRequest 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 BaseRequest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class BaseRequest |
||
| 13 | { |
||
| 14 | private $scheme; |
||
| 15 | private $user; |
||
| 16 | private $pass; |
||
| 17 | private $host; |
||
| 18 | private $port; |
||
| 19 | private $path; |
||
| 20 | private $query = array(); |
||
| 21 | |||
| 22 | /** @var [string => string] */ |
||
| 23 | private $headers = array(); |
||
| 24 | |||
| 25 | private $capath; |
||
| 26 | private $cafile; |
||
| 27 | |||
| 28 | protected static $defaultCurlOptions = array(); |
||
| 29 | |||
| 30 | private static $NSS_CIPHERS = array( |
||
| 31 | 'rsa_3des_sha', |
||
| 32 | 'rsa_des_sha', |
||
| 33 | 'rsa_null_md5', |
||
| 34 | 'rsa_null_sha', |
||
| 35 | 'rsa_rc2_40_md5', |
||
| 36 | 'rsa_rc4_128_md5', |
||
| 37 | 'rsa_rc4_128_sha', |
||
| 38 | 'rsa_rc4_40_md5', |
||
| 39 | 'fips_des_sha', |
||
| 40 | 'fips_3des_sha', |
||
| 41 | 'rsa_des_56_sha', |
||
| 42 | 'rsa_rc4_56_sha', |
||
| 43 | 'rsa_aes_128_sha', |
||
| 44 | 'rsa_aes_256_sha', |
||
| 45 | 'rsa_aes_128_gcm_sha_256', |
||
| 46 | 'dhe_rsa_aes_128_gcm_sha_256', |
||
| 47 | 'ecdh_ecdsa_null_sha', |
||
| 48 | 'ecdh_ecdsa_rc4_128_sha', |
||
| 49 | 'ecdh_ecdsa_3des_sha', |
||
| 50 | 'ecdh_ecdsa_aes_128_sha', |
||
| 51 | 'ecdh_ecdsa_aes_256_sha', |
||
| 52 | 'ecdhe_ecdsa_null_sha', |
||
| 53 | 'ecdhe_ecdsa_rc4_128_sha', |
||
| 54 | 'ecdhe_ecdsa_3des_sha', |
||
| 55 | 'ecdhe_ecdsa_aes_128_sha', |
||
| 56 | 'ecdhe_ecdsa_aes_256_sha', |
||
| 57 | 'ecdh_rsa_null_sha', |
||
| 58 | 'ecdh_rsa_128_sha', |
||
| 59 | 'ecdh_rsa_3des_sha', |
||
| 60 | 'ecdh_rsa_aes_128_sha', |
||
| 61 | 'ecdh_rsa_aes_256_sha', |
||
| 62 | 'echde_rsa_null', |
||
| 63 | 'ecdhe_rsa_rc4_128_sha', |
||
| 64 | 'ecdhe_rsa_3des_sha', |
||
| 65 | 'ecdhe_rsa_aes_128_sha', |
||
| 66 | 'ecdhe_rsa_aes_256_sha', |
||
| 67 | 'ecdhe_ecdsa_aes_128_gcm_sha_256', |
||
| 68 | 'ecdhe_rsa_aes_128_gcm_sha_256', |
||
| 69 | ); |
||
| 70 | |||
| 71 | /** |
||
| 72 | * enable ECC cipher suites in cURL/NSS |
||
| 73 | * @codeCoverageIgnore |
||
| 74 | */ |
||
| 75 | public static function nssCiphers() |
||
| 76 | { |
||
| 77 | static $cache; |
||
| 78 | if (isset($cache)) { |
||
| 79 | return $cache; |
||
| 80 | } |
||
| 81 | $ver = curl_version(); |
||
| 82 | if (preg_match('/^NSS.*Basic ECC$/', $ver['ssl_version'])) { |
||
| 83 | return $cache = implode(',', self::$NSS_CIPHERS); |
||
| 84 | } |
||
| 85 | return $cache = false; |
||
| 86 | } |
||
| 87 | |||
| 88 | 4 | protected function getProxy($url) |
|
|
|
|||
| 89 | { |
||
| 90 | 4 | if (isset($_SERVER['no_proxy'])) { |
|
| 91 | 1 | $pattern = new Util\NoProxyPattern($_SERVER['no_proxy']); |
|
| 92 | 1 | if ($pattern->test($url)) { |
|
| 93 | 1 | return null; |
|
| 94 | } |
||
| 95 | 1 | } |
|
| 96 | |||
| 97 | 4 | foreach (array('https', 'http') as $scheme) { |
|
| 98 | 4 | if ($this->scheme === $scheme) { |
|
| 99 | 4 | $label = $scheme . '_proxy'; |
|
| 100 | 4 | foreach (array(strtoupper($label), $label) as $l) { |
|
| 101 | 4 | if (isset($_SERVER[$l])) { |
|
| 102 | 1 | return $_SERVER[$l]; |
|
| 103 | } |
||
| 104 | 3 | } |
|
| 105 | 3 | } |
|
| 106 | 4 | } |
|
| 107 | 3 | return null; |
|
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @param $io |
||
| 112 | * @param bool $useRedirector |
||
| 113 | * @param $githubDomains |
||
| 114 | * @param $gitlabDomains |
||
| 115 | */ |
||
| 116 | 6 | protected function setupAuthentication(IO\IOInterface $io, $useRedirector, array $githubDomains, array $gitlabDomains) |
|
| 117 | { |
||
| 118 | 6 | if (preg_match('/\.github\.com$/', $this->host)) { |
|
| 119 | 1 | $authKey = 'github.com'; |
|
| 120 | 1 | if ($useRedirector) { |
|
| 121 | 1 | if ($this->host === 'api.github.com' && preg_match('%^/repos(/[^/]+/[^/]+/)zipball(.+)$%', $this->path, $_)) { |
|
| 122 | 1 | $this->host = 'codeload.github.com'; |
|
| 123 | 1 | $this->path = $_[1] . 'legacy.zip' . $_[2]; |
|
| 124 | 1 | } |
|
| 125 | 1 | } |
|
| 126 | 1 | } else { |
|
| 127 | 5 | $authKey = $this->host; |
|
| 128 | } |
||
| 129 | 6 | if (!$io->hasAuthentication($authKey)) { |
|
| 130 | 4 | if ($this->user || $this->pass) { |
|
| 131 | 1 | $io->setAuthentication($authKey, $this->user, $this->pass); |
|
| 132 | 1 | } else { |
|
| 133 | 3 | return; |
|
| 134 | } |
||
| 135 | 1 | } |
|
| 136 | |||
| 137 | 3 | $auth = $io->getAuthentication($authKey); |
|
| 138 | |||
| 139 | // is github |
||
| 140 | 3 | if (in_array($authKey, $githubDomains) && 'x-oauth-basic' === $auth['password']) { |
|
| 141 | 1 | $this->addParam('access_token', $auth['username']); |
|
| 142 | 1 | $this->user = $this->pass = null; |
|
| 143 | 1 | return; |
|
| 144 | } |
||
| 145 | // is gitlab |
||
| 146 | 2 | if (in_array($authKey, $gitlabDomains) && 'oauth2' === $auth['password']) { |
|
| 147 | 1 | $this->addHeader('authorization', 'Bearer ' . $auth['username']); |
|
| 148 | 1 | $this->user = $this->pass = null; |
|
| 149 | 1 | return; |
|
| 150 | } |
||
| 151 | // others, includes bitbucket |
||
| 152 | 1 | $this->user = $auth['username']; |
|
| 153 | 1 | $this->pass = $auth['password']; |
|
| 154 | 1 | } |
|
| 155 | |||
| 156 | /** |
||
| 157 | * @return array |
||
| 158 | */ |
||
| 159 | 4 | public function getCurlOptions() |
|
| 160 | { |
||
| 161 | 4 | $headers = array(); |
|
| 162 | 4 | foreach ($this->headers as $key => $val) { |
|
| 163 | 1 | $headers[] = strtr(ucwords(strtr($key, '-', ' ')), ' ', '-') . ': ' . $val; |
|
| 164 | 4 | } |
|
| 165 | |||
| 166 | 4 | $url = $this->getURL(); |
|
| 167 | |||
| 168 | $curlOpts = array( |
||
| 169 | 4 | CURLOPT_URL => $url, |
|
| 170 | 4 | CURLOPT_HTTPHEADER => $headers, |
|
| 171 | 4 | CURLOPT_USERAGENT => ConfigFacade::getUserAgent(), |
|
| 172 | //CURLOPT_VERBOSE => true, //for debug |
||
| 173 | 4 | ); |
|
| 174 | 4 | $curlOpts += static::$defaultCurlOptions; |
|
| 175 | |||
| 176 | // @codeCoverageIgnoreStart |
||
| 177 | if ($ciphers = $this->nssCiphers()) { |
||
| 178 | $curlOpts[CURLOPT_SSL_CIPHER_LIST] = $ciphers; |
||
| 179 | } |
||
| 180 | // @codeCoverageIgnoreEnd |
||
| 181 | 4 | if ($proxy = $this->getProxy($url)) { |
|
| 182 | 1 | $curlOpts[CURLOPT_PROXY] = $proxy; |
|
| 183 | 1 | } |
|
| 184 | 4 | if ($this->capath) { |
|
| 185 | 1 | $curlOpts[CURLOPT_CAPATH] = $this->capath; |
|
| 186 | 1 | } |
|
| 187 | 4 | if ($this->cafile) { |
|
| 188 | 1 | $curlOpts[CURLOPT_CAINFO] = $this->cafile; |
|
| 189 | 1 | } |
|
| 190 | |||
| 191 | 4 | return $curlOpts; |
|
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @return string |
||
| 196 | */ |
||
| 197 | 7 | public function getURL() |
|
| 198 | { |
||
| 199 | 7 | $url = self::ifOr($this->scheme, '', '://'); |
|
| 200 | 7 | if ($this->user) { |
|
| 201 | 1 | $user = $this->user; |
|
| 202 | 1 | $user .= self::ifOr($this->pass, ':'); |
|
| 203 | 1 | $url .= $user . '@'; |
|
| 204 | 1 | } |
|
| 205 | 7 | $url .= self::ifOr($this->host); |
|
| 206 | 7 | $url .= self::ifOr($this->port, ':'); |
|
| 207 | 7 | $url .= self::ifOr($this->path); |
|
| 208 | 7 | $url .= self::ifOr(http_build_query($this->query), '?'); |
|
| 209 | 7 | return $url; |
|
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @return string user/pass/access_token masked url |
||
| 214 | */ |
||
| 215 | 1 | View Code Duplication | public function getMaskedURL() |
| 223 | |||
| 224 | /** |
||
| 225 | * @return string |
||
| 226 | */ |
||
| 227 | 2 | View Code Duplication | public function getOriginURL() |
| 228 | { |
||
| 229 | 2 | $url = self::ifOr($this->scheme, '', '://'); |
|
| 230 | 2 | $url .= self::ifOr($this->host); |
|
| 231 | 2 | $url .= self::ifOr($this->port, ':'); |
|
| 232 | 2 | return $url; |
|
| 233 | } |
||
| 234 | |||
| 235 | 9 | private static function ifOr($str, $pre = '', $post = '') |
|
| 242 | |||
| 243 | /** |
||
| 244 | * @param string $url |
||
| 245 | */ |
||
| 246 | 11 | public function setURL($url) |
|
| 247 | { |
||
| 248 | 11 | $struct = parse_url($url); |
|
| 249 | 11 | foreach ($struct as $key => $val) { |
|
| 250 | 11 | if ($key === 'query') { |
|
| 251 | 4 | parse_str($val, $this->query); |
|
| 252 | 4 | } else { |
|
| 253 | 11 | $this->$key = $val; |
|
| 254 | } |
||
| 255 | 11 | } |
|
| 256 | 11 | } |
|
| 257 | |||
| 258 | 1 | public function addParam($key, $val) |
|
| 262 | |||
| 263 | 1 | public function addHeader($key, $val) |
|
| 267 | |||
| 268 | 7 | public function setCA($path = null, $file = null) |
|
| 273 | |||
| 274 | 1 | public function isHTTP() |
|
| 275 | { |
||
| 276 | 1 | return $this->scheme === 'http' || $this->scheme === 'https'; |
|
| 277 | } |
||
| 278 | } |
||
| 279 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: