Complex classes like HttpClient 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 HttpClient, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class HttpClient extends ClientSocket implements HttpStream |
||
23 | { |
||
24 | |||
25 | /** |
||
26 | * Path to file on server (excluding endpoint address) |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | private $path; |
||
31 | |||
32 | /** |
||
33 | * Headers |
||
34 | * |
||
35 | * @var array |
||
36 | */ |
||
37 | private $headers; |
||
38 | |||
39 | /** |
||
40 | * The payload |
||
41 | * |
||
42 | * @var MemoryStream |
||
43 | */ |
||
44 | private $payload; |
||
45 | |||
46 | /** |
||
47 | * The HTTP protocol version |
||
48 | * |
||
49 | * @var string |
||
50 | */ |
||
51 | private $protocol; |
||
52 | |||
53 | /** |
||
54 | * Whether to use https instead of http |
||
55 | * |
||
56 | * @var boolean |
||
57 | */ |
||
58 | private $secure; |
||
59 | |||
60 | /** |
||
61 | * The response status code |
||
62 | * |
||
63 | * @var int |
||
64 | */ |
||
65 | private $responseCode; |
||
66 | |||
67 | /** |
||
68 | * When the connection times out (in seconds) |
||
69 | * |
||
70 | * @var int |
||
71 | */ |
||
72 | private $timeout; |
||
73 | |||
74 | /** |
||
75 | * Create a new http client |
||
76 | * |
||
77 | * @param Url $url |
||
78 | * The url for http request |
||
79 | * @param string $proto |
||
80 | * The protocol to use (default = HTTP/1.1) |
||
81 | * @param integer $timeout |
||
82 | * Optional timeout for request (default = 10 seconds) |
||
83 | */ |
||
84 | 8 | public function __construct(Url $url, $proto = 'HTTP/1.1', $timeout = 10) |
|
94 | |||
95 | /** |
||
96 | * {@inheritDoc} |
||
97 | * @see \Generics\Streams\HttpStream::getHeaders() |
||
98 | */ |
||
99 | public function getHeaders() |
||
103 | |||
104 | /** |
||
105 | * {@inheritDoc} |
||
106 | * @see \Generics\Streams\HttpStream::setHeader() |
||
107 | * @return HttpClient |
||
108 | */ |
||
109 | public function setHeader($headerName, $headerValue) |
||
114 | 7 | ||
115 | /** |
||
116 | * Reset the headers |
||
117 | */ |
||
118 | public function resetHeaders() |
||
122 | 1 | ||
123 | 1 | /** |
|
124 | * {@inheritDoc} |
||
125 | * @see \Generics\Streams\HttpStream::appendPayload() |
||
126 | */ |
||
127 | public function appendPayload(InputStream $payload) |
||
133 | 1 | ||
134 | 1 | /** |
|
135 | 1 | *{@inheritDoc} |
|
136 | * @see \Generics\Streams\HttpStream::getPayload() |
||
137 | */ |
||
138 | public function getPayload() |
||
142 | 1 | ||
143 | /** |
||
144 | 1 | * Load headers from remote and return it |
|
145 | * |
||
146 | * @return array |
||
147 | */ |
||
148 | public function retrieveHeaders() |
||
162 | 1 | ||
163 | /** |
||
164 | 1 | * Set connection timeout in seconds |
|
165 | * |
||
166 | * @param int $timeout |
||
167 | */ |
||
168 | public function setTimeout($timeout) |
||
176 | 1 | ||
177 | 1 | /** |
|
178 | 5 | * {@inheritDoc} |
|
179 | 5 | * @see \Generics\Streams\HttpStream::request() |
|
180 | */ |
||
181 | public function request($requestType) |
||
209 | |||
210 | 6 | /** |
|
211 | 3 | * Check the connection availability |
|
212 | 3 | * |
|
213 | 6 | * @param int $start Timestamp when read request attempt starts |
|
214 | * @throws HttpException |
||
215 | * @return boolean |
||
216 | */ |
||
217 | private function checkConnection($start) |
||
230 | 7 | ||
231 | /** |
||
232 | * Adjust number of bytes to read according content length header |
||
233 | 6 | * |
|
234 | * @param int $numBytes |
||
235 | * @return int |
||
236 | */ |
||
237 | private function adjustNumbytes($numBytes) |
||
246 | 6 | ||
247 | 6 | /** |
|
248 | * Try to parse line as header and add the results to local header list |
||
249 | 6 | * |
|
250 | * @param string $line |
||
251 | */ |
||
252 | private function addParsedHeader($line) |
||
262 | 6 | ||
263 | 6 | /** |
|
264 | 6 | * Check whether the readen bytes amount has reached the |
|
265 | * content length amount |
||
266 | 6 | * |
|
267 | * @return boolean |
||
268 | */ |
||
269 | private function checkContentLengthExceeded() |
||
278 | 4 | ||
279 | /** |
||
280 | 4 | * Handle a header line |
|
281 | 4 | * |
|
282 | * All parameters by reference, which means the the values can be |
||
283 | * modified during execution of this method. |
||
284 | * |
||
285 | * @param boolean $delimiterFound Whether the delimiter for end of header section was found |
||
286 | * @param int $numBytes The number of bytes to read from remote |
||
287 | * @param string $tmp The current readen line |
||
288 | */ |
||
289 | private function handleHeader(&$delimiterFound, &$numBytes, &$tmp) |
||
303 | 6 | ||
304 | 6 | /** |
|
305 | 6 | * Retrieve and parse the response |
|
306 | 6 | * |
|
307 | 6 | * @param string $requestType |
|
308 | * @throws \Generics\Client\HttpException |
||
309 | * @throws \Generics\Socket\SocketException |
||
310 | * @throws \Generics\Streams\StreamException |
||
311 | */ |
||
312 | private function retrieveAndParseResponse($requestType) |
||
359 | 6 | ||
360 | /** |
||
361 | * Append the payload buffer to the request buffer |
||
362 | 6 | * |
|
363 | 6 | * @param MemoryStream $ms |
|
364 | * @return MemoryStream |
||
365 | * @throws \Generics\Streams\StreamException |
||
366 | * @throws \Generics\ResetException |
||
367 | */ |
||
368 | private function appendPayloadToRequest(MemoryStream $ms) |
||
380 | |||
381 | 7 | /** |
|
382 | * Prepare the request buffer |
||
383 | 7 | * |
|
384 | * @param string $requestType |
||
385 | * @return \Generics\Streams\MemoryStream |
||
386 | * @throws \Generics\Streams\StreamException |
||
387 | */ |
||
388 | private function prepareRequest($requestType) |
||
421 | |||
422 | 7 | /** |
|
423 | * Depending on request type the connection header is either |
||
424 | 7 | * set to keep-alive or close |
|
425 | * |
||
426 | * @param string $requestType |
||
427 | */ |
||
428 | private function adjustConnectionHeader($requestType) |
||
436 | 1 | ||
437 | 1 | /** |
|
438 | 4 | * Adjust the headers by injecting default values for missing keys. |
|
439 | */ |
||
440 | 5 | private function adjustHeaders($requestType) |
|
458 | |||
459 | 7 | /** |
|
460 | 5 | * Retrieve the response status code |
|
461 | 5 | * |
|
462 | 7 | * @return int |
|
463 | */ |
||
464 | public function getResponseCode() |
||
468 | |||
469 | 5 | /** |
|
470 | * {@inheritDoc} |
||
471 | 5 | * @see \Generics\Streams\Stream::isOpen() |
|
472 | */ |
||
473 | public function isOpen() |
||
477 | } |
||
478 |