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 | public function __construct($url, $destination, $useRedirector, IO\IOInterface $io, Config $config) |
||
| 100 | |||
| 101 | public function __destruct() |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @return string |
||
| 116 | */ |
||
| 117 | public function getURL() |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @return string user/pass/access_token masked url |
||
| 134 | */ |
||
| 135 | public function getMaskedURL() |
||
| 143 | |||
| 144 | private static function ifOr($str, $pre = '', $post = '') |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @param string $url |
||
| 154 | */ |
||
| 155 | public function setURL($url) |
||
| 166 | |||
| 167 | public function addParam($key, $val) |
||
| 171 | |||
| 172 | public function addHeader($key, $val) |
||
| 176 | |||
| 177 | public function makeSuccess() |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @return array |
||
| 184 | */ |
||
| 185 | public function getCurlOptions() |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @param IO\IOInterface $io |
||
| 215 | * @param bool $useRedirector |
||
| 216 | */ |
||
| 217 | private function setupAuthentication(IO\IOInterface $io, $useRedirector) |
||
| 256 | |||
| 257 | private function getProxy($url) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * enable ECC cipher suites in cURL/NSS |
||
| 288 | */ |
||
| 289 | public static function nssCiphers() |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @param string |
||
| 304 | */ |
||
| 305 | public function setDestination($destination) |
||
| 323 | |||
| 324 | private function createDir($fileName) |
||
| 335 | } |
||
| 336 |
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..