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() |
||
87 | |||
88 | 4 | protected function getProxy($url) |
|
114 | |||
115 | /** |
||
116 | * @param $io |
||
117 | * @param bool $useRedirector |
||
118 | * @param $githubDomains |
||
119 | * @param $gitlabDomains |
||
120 | */ |
||
121 | 6 | protected function setupAuthentication(IO\IOInterface $io, $useRedirector, array $githubDomains, array $gitlabDomains) |
|
160 | |||
161 | /** |
||
162 | * @return array |
||
163 | */ |
||
164 | 4 | public function getCurlOptions() |
|
198 | |||
199 | /** |
||
200 | * @return string |
||
201 | */ |
||
202 | 7 | public function getURL() |
|
216 | |||
217 | /** |
||
218 | * @return string user/pass/access_token masked url |
||
219 | */ |
||
220 | 1 | View Code Duplication | public function getMaskedURL() |
228 | |||
229 | /** |
||
230 | * @return string |
||
231 | */ |
||
232 | 2 | View Code Duplication | public function getOriginURL() |
239 | |||
240 | 9 | private static function ifOr($str, $pre = '', $post = '') |
|
247 | |||
248 | /** |
||
249 | * @param string $url |
||
250 | */ |
||
251 | 11 | public function setURL($url) |
|
262 | |||
263 | 1 | public function addParam($key, $val) |
|
267 | |||
268 | 1 | public function addHeader($key, $val) |
|
272 | |||
273 | 7 | public function setCA($path = null, $file = null) |
|
278 | |||
279 | 1 | public function isHTTP() |
|
283 | } |
||
284 |
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: