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 |
||
| 7 | class BaseRequest |
||
| 8 | { |
||
| 9 | private $scheme; |
||
| 10 | private $user; |
||
| 11 | private $pass; |
||
| 12 | private $host; |
||
| 13 | private $port; |
||
| 14 | private $path; |
||
| 15 | private $query = array(); |
||
| 16 | |||
| 17 | /** @var [string => string] */ |
||
| 18 | private $headers = array(); |
||
| 19 | |||
| 20 | private $capath; |
||
| 21 | private $cafile; |
||
| 22 | |||
| 23 | protected static $defaultCurlOptions = array(); |
||
| 24 | |||
| 25 | private static $NSS_CIPHERS = array( |
||
| 26 | 'rsa_3des_sha', |
||
| 27 | 'rsa_des_sha', |
||
| 28 | 'rsa_null_md5', |
||
| 29 | 'rsa_null_sha', |
||
| 30 | 'rsa_rc2_40_md5', |
||
| 31 | 'rsa_rc4_128_md5', |
||
| 32 | 'rsa_rc4_128_sha', |
||
| 33 | 'rsa_rc4_40_md5', |
||
| 34 | 'fips_des_sha', |
||
| 35 | 'fips_3des_sha', |
||
| 36 | 'rsa_des_56_sha', |
||
| 37 | 'rsa_rc4_56_sha', |
||
| 38 | 'rsa_aes_128_sha', |
||
| 39 | 'rsa_aes_256_sha', |
||
| 40 | 'rsa_aes_128_gcm_sha_256', |
||
| 41 | 'dhe_rsa_aes_128_gcm_sha_256', |
||
| 42 | 'ecdh_ecdsa_null_sha', |
||
| 43 | 'ecdh_ecdsa_rc4_128_sha', |
||
| 44 | 'ecdh_ecdsa_3des_sha', |
||
| 45 | 'ecdh_ecdsa_aes_128_sha', |
||
| 46 | 'ecdh_ecdsa_aes_256_sha', |
||
| 47 | 'ecdhe_ecdsa_null_sha', |
||
| 48 | 'ecdhe_ecdsa_rc4_128_sha', |
||
| 49 | 'ecdhe_ecdsa_3des_sha', |
||
| 50 | 'ecdhe_ecdsa_aes_128_sha', |
||
| 51 | 'ecdhe_ecdsa_aes_256_sha', |
||
| 52 | 'ecdh_rsa_null_sha', |
||
| 53 | 'ecdh_rsa_128_sha', |
||
| 54 | 'ecdh_rsa_3des_sha', |
||
| 55 | 'ecdh_rsa_aes_128_sha', |
||
| 56 | 'ecdh_rsa_aes_256_sha', |
||
| 57 | 'echde_rsa_null', |
||
| 58 | 'ecdhe_rsa_rc4_128_sha', |
||
| 59 | 'ecdhe_rsa_3des_sha', |
||
| 60 | 'ecdhe_rsa_aes_128_sha', |
||
| 61 | 'ecdhe_rsa_aes_256_sha', |
||
| 62 | 'ecdhe_ecdsa_aes_128_gcm_sha_256', |
||
| 63 | 'ecdhe_rsa_aes_128_gcm_sha_256', |
||
| 64 | ); |
||
| 65 | |||
| 66 | /** |
||
| 67 | * enable ECC cipher suites in cURL/NSS |
||
| 68 | * @codeCoverageIgnore |
||
| 69 | */ |
||
| 70 | public static function nssCiphers() |
||
| 82 | |||
| 83 | protected function getProxy($url) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @param $io |
||
| 114 | * @param bool $useRedirector |
||
| 115 | * @param $githubDomains |
||
| 116 | * @param $gitlabDomains |
||
| 117 | */ |
||
| 118 | protected function setupAuthentication(IO\IOInterface $io, $useRedirector, array $githubDomains, array $gitlabDomains) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @return array |
||
| 160 | */ |
||
| 161 | public function getCurlOptions() |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @return string |
||
| 196 | */ |
||
| 197 | public function getURL() |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @return string user/pass/access_token masked url |
||
| 214 | */ |
||
| 215 | View Code Duplication | public function getMaskedURL() |
|
| 223 | |||
| 224 | /** |
||
| 225 | * @return string |
||
| 226 | */ |
||
| 227 | View Code Duplication | public function getOriginURL() |
|
| 234 | |||
| 235 | private static function ifOr($str, $pre = '', $post = '') |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @param string $url |
||
| 245 | */ |
||
| 246 | public function setURL($url) |
||
| 257 | |||
| 258 | public function addParam($key, $val) |
||
| 262 | |||
| 263 | public function addHeader($key, $val) |
||
| 267 | |||
| 268 | public function setCA($path = null, $file = null) |
||
| 273 | } |
||
| 274 |
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: