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 CopyRequest 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 CopyRequest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class CopyRequest |
||
| 14 | { |
||
| 15 | private $scheme; |
||
| 16 | private $user; |
||
| 17 | private $pass; |
||
| 18 | private $host; |
||
| 19 | private $port; |
||
| 20 | private $path; |
||
| 21 | private $query = array(); |
||
| 22 | |||
| 23 | /** @var [string => string] */ |
||
| 24 | private $headers = array(); |
||
| 25 | |||
| 26 | /** @var string */ |
||
| 27 | private $destination; |
||
| 28 | |||
| 29 | /** @var resource<stream<plainfile>> */ |
||
| 30 | private $fp; |
||
| 31 | |||
| 32 | private $success = false; |
||
| 33 | |||
| 34 | private static $defaultCurlOptions = array( |
||
| 35 | CURLOPT_HTTPGET => true, |
||
| 36 | CURLOPT_FOLLOWLOCATION => true, |
||
| 37 | CURLOPT_MAXREDIRS => 20, |
||
| 38 | CURLOPT_ENCODING => '', |
||
| 39 | ); |
||
| 40 | |||
| 41 | private $githubDomains = array(); |
||
| 42 | private $gitlabDomains = array(); |
||
| 43 | |||
| 44 | private static $NSS_CIPHERS = array( |
||
| 45 | 'rsa_3des_sha', |
||
| 46 | 'rsa_des_sha', |
||
| 47 | 'rsa_null_md5', |
||
| 48 | 'rsa_null_sha', |
||
| 49 | 'rsa_rc2_40_md5', |
||
| 50 | 'rsa_rc4_128_md5', |
||
| 51 | 'rsa_rc4_128_sha', |
||
| 52 | 'rsa_rc4_40_md5', |
||
| 53 | 'fips_des_sha', |
||
| 54 | 'fips_3des_sha', |
||
| 55 | 'rsa_des_56_sha', |
||
| 56 | 'rsa_rc4_56_sha', |
||
| 57 | 'rsa_aes_128_sha', |
||
| 58 | 'rsa_aes_256_sha', |
||
| 59 | 'rsa_aes_128_gcm_sha_256', |
||
| 60 | 'dhe_rsa_aes_128_gcm_sha_256', |
||
| 61 | 'ecdh_ecdsa_null_sha', |
||
| 62 | 'ecdh_ecdsa_rc4_128_sha', |
||
| 63 | 'ecdh_ecdsa_3des_sha', |
||
| 64 | 'ecdh_ecdsa_aes_128_sha', |
||
| 65 | 'ecdh_ecdsa_aes_256_sha', |
||
| 66 | 'ecdhe_ecdsa_null_sha', |
||
| 67 | 'ecdhe_ecdsa_rc4_128_sha', |
||
| 68 | 'ecdhe_ecdsa_3des_sha', |
||
| 69 | 'ecdhe_ecdsa_aes_128_sha', |
||
| 70 | 'ecdhe_ecdsa_aes_256_sha', |
||
| 71 | 'ecdh_rsa_null_sha', |
||
| 72 | 'ecdh_rsa_128_sha', |
||
| 73 | 'ecdh_rsa_3des_sha', |
||
| 74 | 'ecdh_rsa_aes_128_sha', |
||
| 75 | 'ecdh_rsa_aes_256_sha', |
||
| 76 | 'echde_rsa_null', |
||
| 77 | 'ecdhe_rsa_rc4_128_sha', |
||
| 78 | 'ecdhe_rsa_3des_sha', |
||
| 79 | 'ecdhe_rsa_aes_128_sha', |
||
| 80 | 'ecdhe_rsa_aes_256_sha', |
||
| 81 | 'ecdhe_ecdsa_aes_128_gcm_sha_256', |
||
| 82 | 'ecdhe_rsa_aes_128_gcm_sha_256', |
||
| 83 | ); |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @param string $url |
||
| 87 | * @param string $destination |
||
| 88 | * @param bool $useRedirector |
||
| 89 | * @param IO\IOInterface $io |
||
| 90 | * @param Config $config |
||
| 91 | */ |
||
| 92 | 7 | public function __construct($url, $destination, $useRedirector, IO\IOInterface $io, Config $config) |
|
| 100 | |||
| 101 | 6 | public function __destruct() |
|
| 102 | { |
||
| 103 | 6 | if ($this->fp) { |
|
| 104 | 6 | fclose($this->fp); |
|
| 105 | 6 | } |
|
| 106 | |||
| 107 | 6 | if (!$this->success) { |
|
| 108 | 6 | if (file_exists($this->destination)) { |
|
| 109 | 6 | unlink($this->destination); |
|
| 110 | 6 | } |
|
| 111 | 6 | } |
|
| 112 | 6 | } |
|
| 113 | |||
| 114 | /** |
||
| 115 | * @return string |
||
| 116 | */ |
||
| 117 | 4 | public function getURL() |
|
| 118 | { |
||
| 119 | 4 | $url = self::ifOr($this->scheme, '', '://'); |
|
| 120 | 4 | if ($this->user) { |
|
| 121 | 1 | $user = $this->user; |
|
| 122 | 1 | $user .= self::ifOr($this->pass, ':'); |
|
| 123 | 1 | $url .= $user . '@'; |
|
| 124 | 1 | } |
|
| 125 | 4 | $url .= self::ifOr($this->host); |
|
| 126 | 4 | $url .= self::ifOr($this->port, ':'); |
|
| 127 | 4 | $url .= self::ifOr($this->path); |
|
| 128 | 4 | $url .= self::ifOr(http_build_query($this->query), '?'); |
|
| 129 | 4 | return $url; |
|
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @return string user/pass/access_token masked url |
||
| 134 | */ |
||
| 135 | 1 | public function getMaskedURL() |
|
| 143 | |||
| 144 | 5 | private static function ifOr($str, $pre = '', $post = '') |
|
| 151 | |||
| 152 | /** |
||
| 153 | * @param string $url |
||
| 154 | */ |
||
| 155 | 7 | public function setURL($url) |
|
| 156 | { |
||
| 157 | 7 | $struct = parse_url($url); |
|
| 158 | 7 | foreach ($struct as $key => $val) { |
|
| 159 | 7 | if ($key === 'query') { |
|
| 160 | 3 | parse_str($val, $this->query); |
|
| 161 | 3 | } else { |
|
| 162 | 7 | $this->$key = $val; |
|
| 163 | } |
||
| 164 | 7 | } |
|
| 165 | 7 | } |
|
| 166 | |||
| 167 | 1 | public function addParam($key, $val) |
|
| 171 | |||
| 172 | 1 | public function addHeader($key, $val) |
|
| 176 | |||
| 177 | 1 | public function makeSuccess() |
|
| 181 | |||
| 182 | /** |
||
| 183 | * @return array |
||
| 184 | */ |
||
| 185 | 2 | public function getCurlOptions() |
|
| 212 | |||
| 213 | /** |
||
| 214 | * @param IO\IOInterface $io |
||
| 215 | * @param bool $useRedirector |
||
| 216 | */ |
||
| 217 | 6 | private function setupAuthentication(IO\IOInterface $io, $useRedirector) |
|
| 256 | |||
| 257 | 2 | private function getProxy($url) |
|
| 285 | |||
| 286 | /** |
||
| 287 | * enable ECC cipher suites in cURL/NSS |
||
| 288 | * @codeCoverageIgnore |
||
| 289 | */ |
||
| 290 | public static function nssCiphers() |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @param string |
||
| 305 | */ |
||
| 306 | 7 | public function setDestination($destination) |
|
| 324 | |||
| 325 | 6 | private function createDir($fileName) |
|
| 336 | } |
||
| 337 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..