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 $capath; |
||
| 45 | private $cafile; |
||
| 46 | |||
| 47 | private static $NSS_CIPHERS = array( |
||
| 48 | 'rsa_3des_sha', |
||
| 49 | 'rsa_des_sha', |
||
| 50 | 'rsa_null_md5', |
||
| 51 | 'rsa_null_sha', |
||
| 52 | 'rsa_rc2_40_md5', |
||
| 53 | 'rsa_rc4_128_md5', |
||
| 54 | 'rsa_rc4_128_sha', |
||
| 55 | 'rsa_rc4_40_md5', |
||
| 56 | 'fips_des_sha', |
||
| 57 | 'fips_3des_sha', |
||
| 58 | 'rsa_des_56_sha', |
||
| 59 | 'rsa_rc4_56_sha', |
||
| 60 | 'rsa_aes_128_sha', |
||
| 61 | 'rsa_aes_256_sha', |
||
| 62 | 'rsa_aes_128_gcm_sha_256', |
||
| 63 | 'dhe_rsa_aes_128_gcm_sha_256', |
||
| 64 | 'ecdh_ecdsa_null_sha', |
||
| 65 | 'ecdh_ecdsa_rc4_128_sha', |
||
| 66 | 'ecdh_ecdsa_3des_sha', |
||
| 67 | 'ecdh_ecdsa_aes_128_sha', |
||
| 68 | 'ecdh_ecdsa_aes_256_sha', |
||
| 69 | 'ecdhe_ecdsa_null_sha', |
||
| 70 | 'ecdhe_ecdsa_rc4_128_sha', |
||
| 71 | 'ecdhe_ecdsa_3des_sha', |
||
| 72 | 'ecdhe_ecdsa_aes_128_sha', |
||
| 73 | 'ecdhe_ecdsa_aes_256_sha', |
||
| 74 | 'ecdh_rsa_null_sha', |
||
| 75 | 'ecdh_rsa_128_sha', |
||
| 76 | 'ecdh_rsa_3des_sha', |
||
| 77 | 'ecdh_rsa_aes_128_sha', |
||
| 78 | 'ecdh_rsa_aes_256_sha', |
||
| 79 | 'echde_rsa_null', |
||
| 80 | 'ecdhe_rsa_rc4_128_sha', |
||
| 81 | 'ecdhe_rsa_3des_sha', |
||
| 82 | 'ecdhe_rsa_aes_128_sha', |
||
| 83 | 'ecdhe_rsa_aes_256_sha', |
||
| 84 | 'ecdhe_ecdsa_aes_128_gcm_sha_256', |
||
| 85 | 'ecdhe_rsa_aes_128_gcm_sha_256', |
||
| 86 | ); |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @param string $url |
||
| 90 | * @param string $destination |
||
| 91 | * @param bool $useRedirector |
||
| 92 | * @param IO\IOInterface $io |
||
| 93 | * @param Config $config |
||
| 94 | */ |
||
| 95 | 7 | public function __construct($url, $destination, $useRedirector, IO\IOInterface $io, Config $config) |
|
| 105 | |||
| 106 | 6 | public function __destruct() |
|
| 118 | |||
| 119 | /** |
||
| 120 | * @return string |
||
| 121 | */ |
||
| 122 | 4 | public function getURL() |
|
| 136 | |||
| 137 | /** |
||
| 138 | * @return string user/pass/access_token masked url |
||
| 139 | */ |
||
| 140 | 1 | public function getMaskedURL() |
|
| 148 | |||
| 149 | 5 | private static function ifOr($str, $pre = '', $post = '') |
|
| 156 | |||
| 157 | /** |
||
| 158 | * @param string $url |
||
| 159 | */ |
||
| 160 | 7 | public function setURL($url) |
|
| 171 | |||
| 172 | 1 | public function addParam($key, $val) |
|
| 176 | |||
| 177 | 1 | public function addHeader($key, $val) |
|
| 181 | |||
| 182 | 1 | public function makeSuccess() |
|
| 186 | |||
| 187 | /** |
||
| 188 | * @return array |
||
| 189 | */ |
||
| 190 | 2 | public function getCurlOptions() |
|
| 223 | |||
| 224 | /** |
||
| 225 | * @param IO\IOInterface $io |
||
| 226 | * @param bool $useRedirector |
||
| 227 | */ |
||
| 228 | 6 | private function setupAuthentication(IO\IOInterface $io, $useRedirector) |
|
| 267 | |||
| 268 | 2 | private function getProxy($url) |
|
| 296 | |||
| 297 | /** |
||
| 298 | * enable ECC cipher suites in cURL/NSS |
||
| 299 | * @codeCoverageIgnore |
||
| 300 | */ |
||
| 301 | public static function nssCiphers() |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @param string |
||
| 316 | */ |
||
| 317 | 7 | public function setDestination($destination) |
|
| 335 | |||
| 336 | 6 | private function createDir($fileName) |
|
| 347 | } |
||
| 348 |
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..