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 | * The query string |
||
76 | * |
||
77 | * @var string |
||
78 | */ |
||
79 | private $queryString; |
||
80 | |||
81 | /** |
||
82 | * Create a new http client |
||
83 | * |
||
84 | * @param Url $url |
||
85 | * The url for http request |
||
86 | * @param string $proto |
||
87 | * The protocol to use (default = HTTP/1.1) |
||
88 | * @param integer $timeout |
||
89 | * Optional timeout for request (default = 10 seconds) |
||
90 | */ |
||
91 | 11 | public function __construct(Url $url, $proto = 'HTTP/1.1', $timeout = 10) |
|
102 | |||
103 | /** |
||
104 | * |
||
105 | * {@inheritdoc} |
||
106 | * @see \Generics\Streams\HttpStream::getHeaders() |
||
107 | */ |
||
108 | 2 | public function getHeaders() |
|
112 | |||
113 | /** |
||
114 | * |
||
115 | * {@inheritdoc} |
||
116 | * @see \Generics\Streams\HttpStream::setHeader() |
||
117 | * @return HttpClient |
||
118 | */ |
||
119 | 10 | public function setHeader($headerName, $headerValue) |
|
124 | |||
125 | /** |
||
126 | * Reset the headers |
||
127 | */ |
||
128 | 1 | public function resetHeaders() |
|
132 | |||
133 | /** |
||
134 | * |
||
135 | * {@inheritdoc} |
||
136 | * @see \Generics\Streams\HttpStream::appendPayload() |
||
137 | */ |
||
138 | 1 | public function appendPayload(InputStream $payload) |
|
144 | |||
145 | /** |
||
146 | * |
||
147 | * {@inheritdoc} |
||
148 | * @see \Generics\Streams\HttpStream::getPayload() |
||
149 | */ |
||
150 | 4 | public function getPayload() |
|
154 | |||
155 | /** |
||
156 | * Load headers from remote and return it |
||
157 | * |
||
158 | * @return array |
||
159 | */ |
||
160 | 1 | public function retrieveHeaders() |
|
174 | |||
175 | /** |
||
176 | * Set connection timeout in seconds |
||
177 | * |
||
178 | * @param int $timeout |
||
179 | */ |
||
180 | 5 | public function setTimeout($timeout) |
|
188 | |||
189 | /** |
||
190 | * |
||
191 | * {@inheritdoc} |
||
192 | * @see \Generics\Streams\HttpStream::request() |
||
193 | */ |
||
194 | 11 | public function request($requestType) |
|
222 | |||
223 | /** |
||
224 | * Check the connection availability |
||
225 | * |
||
226 | * @param int $start |
||
227 | * Timestamp when read request attempt starts |
||
228 | * @throws HttpException |
||
229 | * @return boolean |
||
230 | */ |
||
231 | 10 | private function checkConnection($start) |
|
244 | |||
245 | /** |
||
246 | * Adjust number of bytes to read according content length header |
||
247 | * |
||
248 | * @param int $numBytes |
||
249 | * @return int |
||
250 | */ |
||
251 | 9 | private function adjustNumbytes($numBytes) |
|
260 | |||
261 | /** |
||
262 | * Try to parse line as header and add the results to local header list |
||
263 | * |
||
264 | * @param string $line |
||
265 | */ |
||
266 | 9 | private function addParsedHeader($line) |
|
276 | |||
277 | /** |
||
278 | * Check whether the readen bytes amount has reached the |
||
279 | * content length amount |
||
280 | * |
||
281 | * @return boolean |
||
282 | */ |
||
283 | 7 | private function checkContentLengthExceeded() |
|
292 | |||
293 | /** |
||
294 | * Handle a header line |
||
295 | * |
||
296 | * All parameters by reference, which means the the values can be |
||
297 | * modified during execution of this method. |
||
298 | * |
||
299 | * @param boolean $delimiterFound |
||
300 | * Whether the delimiter for end of header section was found |
||
301 | * @param int $numBytes |
||
302 | * The number of bytes to read from remote |
||
303 | * @param string $tmp |
||
304 | * The current readen line |
||
305 | */ |
||
306 | 9 | private function handleHeader(&$delimiterFound, &$numBytes, &$tmp) |
|
320 | |||
321 | /** |
||
322 | * Retrieve and parse the response |
||
323 | * |
||
324 | * @param string $requestType |
||
325 | * @throws \Generics\Client\HttpException |
||
326 | * @throws \Generics\Socket\SocketException |
||
327 | * @throws \Generics\Streams\StreamException |
||
328 | */ |
||
329 | 10 | private function retrieveAndParseResponse($requestType) |
|
397 | |||
398 | /** |
||
399 | * Append the payload buffer to the request buffer |
||
400 | * |
||
401 | * @param MemoryStream $ms |
||
402 | * @return MemoryStream |
||
403 | * @throws \Generics\Streams\StreamException |
||
404 | * @throws \Generics\ResetException |
||
405 | */ |
||
406 | 10 | private function appendPayloadToRequest(MemoryStream $ms) |
|
418 | |||
419 | /** |
||
420 | * Prepare the request buffer |
||
421 | * |
||
422 | * @param string $requestType |
||
423 | * @return \Generics\Streams\MemoryStream |
||
424 | * @throws \Generics\Streams\StreamException |
||
425 | */ |
||
426 | 10 | private function prepareRequest($requestType) |
|
460 | |||
461 | /** |
||
462 | * Depending on request type the connection header is either |
||
463 | * set to keep-alive or close |
||
464 | * |
||
465 | * @param string $requestType |
||
466 | */ |
||
467 | 8 | private function adjustConnectionHeader($requestType) |
|
475 | |||
476 | /** |
||
477 | * Adjust the headers by injecting default values for missing keys. |
||
478 | */ |
||
479 | 10 | private function adjustHeaders($requestType) |
|
508 | |||
509 | /** |
||
510 | * Retrieve the response status code |
||
511 | * |
||
512 | * @return int |
||
513 | */ |
||
514 | 8 | public function getResponseCode() |
|
518 | |||
519 | /** |
||
520 | * |
||
521 | * {@inheritdoc} |
||
522 | * @see \Generics\Streams\Stream::isOpen() |
||
523 | */ |
||
524 | public function isOpen() |
||
528 | |||
529 | /** |
||
530 | * |
||
531 | * {@inheritdoc} |
||
532 | * @see \Generics\Resettable::reset() |
||
533 | */ |
||
534 | public function reset() |
||
538 | } |
||
539 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.