Complex classes like HttpGetRequest 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 HttpGetRequest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class HttpGetRequest |
||
19 | { |
||
20 | public $origin; |
||
21 | public $scheme = 'http'; |
||
22 | public $host = 'example.com'; |
||
23 | public $port = 80; |
||
24 | public $path = '/'; |
||
25 | |||
26 | public $query = array(); |
||
27 | public $headers = array(); |
||
28 | |||
29 | public $curlOpts = array(); |
||
30 | |||
31 | public $username = null; |
||
32 | public $password = null; |
||
33 | |||
34 | public $maybePublic = false; |
||
35 | public $verbose = false; |
||
36 | |||
37 | /** @var CConfig */ |
||
38 | protected $config; |
||
39 | |||
40 | /** @internal */ |
||
41 | const TOKEN_LABEL = 'access_token'; |
||
42 | |||
43 | /** |
||
44 | * normalize url and authentication info |
||
45 | * @param string $origin domain text |
||
46 | * @param string $url |
||
47 | * @param IO\IOInterface $io |
||
48 | */ |
||
49 | 16 | public function __construct($origin, $url, IO\IOInterface $io) |
|
63 | |||
64 | 16 | private function setupProxy() |
|
90 | |||
91 | 16 | private static function issetOr(array $arr, $key1, $key2) |
|
101 | |||
102 | /** |
||
103 | * @param string $url |
||
104 | */ |
||
105 | 16 | public function importURL($url) |
|
125 | |||
126 | |||
127 | /** |
||
128 | * @param array $struct |
||
129 | * @param string $key |
||
130 | * @param string $default |
||
131 | * @return mixed |
||
132 | */ |
||
133 | 16 | private static function setOr(array $struct, $key, $default = null) |
|
141 | |||
142 | /** |
||
143 | * process option for RemortFileSystem |
||
144 | * @param array $options |
||
145 | * @return void |
||
146 | */ |
||
147 | 2 | public function processRFSOption(array $options) |
|
153 | |||
154 | /** |
||
155 | * @return array |
||
156 | */ |
||
157 | 4 | public function getCurlOpts() |
|
187 | |||
188 | /** |
||
189 | * enable ECC cipher suites in cURL/NSS |
||
190 | */ |
||
191 | 4 | public function nssCiphers() |
|
210 | |||
211 | 7 | public function getURL() |
|
231 | |||
232 | 1 | public function setConfig(CConfig $config) |
|
236 | |||
237 | /** |
||
238 | * @return string |
||
239 | */ |
||
240 | 4 | public static function genUA() |
|
256 | } |
||
257 |
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: