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 |
||
21 | class HttpClient extends ClientSocket implements HttpStream |
||
22 | { |
||
23 | |||
24 | /** |
||
25 | * Path to file on server (excluding endpoint address) |
||
26 | * |
||
27 | * @var string |
||
28 | */ |
||
29 | private $path; |
||
30 | |||
31 | /** |
||
32 | * Headers |
||
33 | * |
||
34 | * @var array |
||
35 | */ |
||
36 | private $headers; |
||
37 | |||
38 | /** |
||
39 | * The payload |
||
40 | * |
||
41 | * @var MemoryStream |
||
42 | */ |
||
43 | private $payload; |
||
44 | |||
45 | /** |
||
46 | * The HTTP protocol version |
||
47 | * |
||
48 | * @var string |
||
49 | */ |
||
50 | private $protocol; |
||
51 | |||
52 | /** |
||
53 | * Whether to use https instead of http |
||
54 | * |
||
55 | * @var boolean |
||
56 | */ |
||
57 | private $secure; |
||
58 | |||
59 | /** |
||
60 | * The response status code |
||
61 | * |
||
62 | * @var int |
||
63 | */ |
||
64 | private $responseCode; |
||
65 | |||
66 | /** |
||
67 | * When the connection times out (in seconds) |
||
68 | * |
||
69 | * @var int |
||
70 | */ |
||
71 | private $timeout; |
||
72 | |||
73 | /** |
||
74 | * Create a new http client |
||
75 | * |
||
76 | * @param Url $url |
||
77 | * The url for http request |
||
78 | * @param string $proto |
||
79 | * The protocol to use (default = HTTP/1.1) |
||
80 | * @param integer $timeout |
||
81 | * Optional timeout for request (default = 10 seconds) |
||
82 | */ |
||
83 | 4 | public function __construct(Url $url, $proto = 'HTTP/1.1', $timeout = 10) |
|
93 | |||
94 | /** |
||
95 | * {@inheritDoc} |
||
96 | * @see \Generics\Streams\HttpStream::getHeaders() |
||
97 | */ |
||
98 | 1 | public function getHeaders() |
|
102 | |||
103 | /** |
||
104 | * {@inheritDoc} |
||
105 | * @see \Generics\Streams\HttpStream::setHeader() |
||
106 | * @return HttpClient |
||
107 | */ |
||
108 | 3 | public function setHeader($headerName, $headerValue) |
|
113 | |||
114 | /** |
||
115 | * Reset the headers |
||
116 | */ |
||
117 | public function resetHeaders() |
||
118 | { |
||
119 | $this->headers = array(); |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * {@inheritDoc} |
||
124 | * @see \Generics\Streams\HttpStream::appendPayload() |
||
125 | */ |
||
126 | 1 | public function appendPayload(InputStream $payload) |
|
132 | |||
133 | /** |
||
134 | *{@inheritDoc} |
||
135 | * @see \Generics\Streams\HttpStream::getPayload() |
||
136 | */ |
||
137 | public function getPayload() |
||
138 | { |
||
139 | return $this->payload; |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * Load headers from remote and return it |
||
144 | * |
||
145 | * @return array |
||
146 | */ |
||
147 | public function retrieveHeaders() |
||
148 | { |
||
149 | $this->setHeader('Connection', 'close'); |
||
150 | $this->setHeader('Accept', ''); |
||
151 | $this->setHeader('Accept-Language', ''); |
||
152 | $this->setHeader('User-Agent', ''); |
||
153 | |||
154 | $savedProto = $this->protocol; |
||
155 | $this->protocol = 'HTTP/1.0'; |
||
156 | $this->request('HEAD'); |
||
157 | $this->protocol = $savedProto; |
||
158 | |||
159 | return $this->headers; |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * Set connection timeout in seconds |
||
164 | * |
||
165 | * @param int $timeout |
||
166 | */ |
||
167 | 1 | public function setTimeout($timeout) |
|
168 | { |
||
169 | 1 | $timeout = intval($timeout); |
|
170 | 1 | if ($timeout < 1 || $timeout > 60) { |
|
171 | $timeout = 5; |
||
172 | } |
||
173 | 1 | $this->timeout = $timeout; |
|
174 | 1 | } |
|
175 | |||
176 | /** |
||
177 | * {@inheritDoc} |
||
178 | * @see \Generics\Streams\HttpStream::request() |
||
179 | */ |
||
180 | 4 | public function request($requestType) |
|
181 | { |
||
182 | 4 | if ($this->secure) { |
|
183 | 1 | throw new HttpException("Secure connection using HTTPs is not supported yet!"); |
|
184 | } |
||
185 | |||
186 | 3 | if ($requestType == 'HEAD') { |
|
187 | $this->setTimeout(1); // Don't wait too long on simple head |
||
188 | } |
||
189 | |||
190 | 3 | $ms = $this->prepareRequest($requestType); |
|
191 | |||
192 | 3 | $ms = $this->appendPayloadToRequest($ms); |
|
193 | |||
194 | 3 | if (! $this->isConnected()) { |
|
195 | 2 | $this->connect(); |
|
196 | } |
||
197 | |||
198 | 3 | while ($ms->ready()) { |
|
199 | 3 | $this->write($ms->read(1024)); |
|
200 | } |
||
201 | |||
202 | 3 | $this->retrieveAndParseResponse($requestType); |
|
203 | |||
204 | 2 | if ($this->headers['Connection'] == 'close') { |
|
205 | 1 | $this->disconnect(); |
|
206 | } |
||
207 | 2 | } |
|
208 | |||
209 | /** |
||
210 | * Check the connection availability |
||
211 | * |
||
212 | * @param int $start Timestamp when read request attempt starts |
||
213 | * @throws HttpException |
||
214 | * @return boolean |
||
215 | */ |
||
216 | 3 | private function checkConnection($start) |
|
229 | |||
230 | /** |
||
231 | * Adjust number of bytes to read according content length header |
||
232 | * |
||
233 | * @param int $numBytes |
||
234 | * @return int |
||
235 | */ |
||
236 | 2 | private function adjustNumbytes($numBytes) |
|
245 | |||
246 | /** |
||
247 | * Try to parse line as header and add the results to local header list |
||
248 | * |
||
249 | * @param string $line |
||
250 | */ |
||
251 | 2 | private function addParsedHeader($line) |
|
261 | |||
262 | /** |
||
263 | * Check whether the readen bytes amount has reached the |
||
264 | * content length amount |
||
265 | * |
||
266 | * @return boolean |
||
267 | */ |
||
268 | 2 | private function checkContentLengthExceeded() |
|
277 | |||
278 | /** |
||
279 | * Handle a header line |
||
280 | * |
||
281 | * All parameters by reference, which means the the values can be |
||
282 | * modified during execution of this method. |
||
283 | * |
||
284 | * @param boolean $delimiterFound Whether the delimiter for end of header section was found |
||
285 | * @param int $numBytes The number of bytes to read from remote |
||
286 | * @param string $tmp The current readen line |
||
287 | */ |
||
288 | 2 | private function handleHeader(&$delimiterFound, &$numBytes, &$tmp) |
|
302 | |||
303 | /** |
||
304 | * Retrieve and parse the response |
||
305 | * |
||
306 | * @param string $requestType |
||
307 | * @throws \Generics\Client\HttpException |
||
308 | * @throws \Generics\Socket\SocketException |
||
309 | * @throws \Generics\Streams\StreamException |
||
310 | */ |
||
311 | 3 | private function retrieveAndParseResponse($requestType) |
|
312 | { |
||
313 | 3 | $this->payload = new MemoryStream(); |
|
314 | 3 | $this->headers = array(); |
|
315 | |||
316 | 3 | $delimiterFound = false; |
|
317 | |||
318 | 3 | $tmp = ""; |
|
319 | 3 | $numBytes = 1; |
|
320 | 3 | $start = time(); |
|
321 | 3 | while (true) { |
|
322 | 3 | if (!$this->checkConnection($start)) { |
|
323 | 3 | continue; |
|
324 | } |
||
325 | |||
326 | 2 | $c = $this->read($numBytes); |
|
327 | |||
328 | 2 | if ($c == null) { |
|
329 | break; |
||
330 | } |
||
331 | |||
332 | 2 | $start = time(); // we have readen something => adjust timeout start point |
|
333 | 2 | $tmp .= $c; |
|
334 | |||
335 | 2 | if (!$delimiterFound) { |
|
336 | 2 | $this->handleHeader($delimiterFound, $numBytes, $tmp); |
|
337 | } |
||
338 | |||
339 | 2 | if ($delimiterFound) { |
|
340 | 2 | if ($requestType == 'HEAD') { |
|
341 | // Header readen, in type HEAD it is now time to leave |
||
342 | break; |
||
343 | } |
||
344 | |||
345 | // delimiter already found, append to payload |
||
346 | 2 | $this->payload->write($tmp); |
|
347 | 2 | $tmp = ""; |
|
348 | |||
349 | 2 | if ($this->checkContentLengthExceeded()) { |
|
350 | 2 | break; |
|
351 | } |
||
352 | } |
||
353 | } |
||
354 | |||
355 | // Set pointer to start |
||
356 | 2 | $this->payload->reset(); |
|
357 | 2 | } |
|
358 | |||
359 | /** |
||
360 | * Append the payload buffer to the request buffer |
||
361 | * |
||
362 | * @param MemoryStream $ms |
||
363 | * @return MemoryStream |
||
364 | * @throws \Generics\Streams\StreamException |
||
365 | * @throws \Generics\ResetException |
||
366 | */ |
||
367 | 3 | private function appendPayloadToRequest(MemoryStream $ms) |
|
379 | |||
380 | /** |
||
381 | * Prepare the request buffer |
||
382 | * |
||
383 | * @param string $requestType |
||
384 | * @return \Generics\Streams\MemoryStream |
||
385 | * @throws \Generics\Streams\StreamException |
||
386 | */ |
||
387 | 3 | private function prepareRequest($requestType) |
|
420 | |||
421 | /** |
||
422 | * Depending on request type the connection header is either |
||
423 | * set to keep-alive or close |
||
424 | * |
||
425 | * @param string $requestType |
||
426 | */ |
||
427 | 2 | private function adjustConnectionHeader($requestType) |
|
428 | { |
||
429 | 2 | if ($requestType == 'HEAD') { |
|
430 | $this->setHeader('Connection', 'close'); |
||
431 | } else { |
||
432 | 2 | $this->setHeader('Connection', 'keep-alive'); |
|
433 | } |
||
434 | 2 | } |
|
435 | |||
436 | /** |
||
437 | * Adjust the headers by injecting default values for missing keys. |
||
438 | */ |
||
439 | 3 | private function adjustHeaders($requestType) |
|
457 | |||
458 | /** |
||
459 | * Retrieve the response status code |
||
460 | * |
||
461 | * @return int |
||
462 | */ |
||
463 | 1 | public function getResponseCode() |
|
467 | |||
468 | /** |
||
469 | * {@inheritDoc} |
||
470 | * @see \Generics\Streams\Stream::isOpen() |
||
471 | */ |
||
472 | public function isOpen() |
||
476 | |||
477 | /** |
||
478 | * {@inheritDoc} |
||
479 | * @see \Generics\Resettable::reset() |
||
480 | */ |
||
481 | public function reset() |
||
485 | } |
||
486 |