Complex classes like HttpClientTrait 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 HttpClientTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | trait HttpClientTrait |
||
| 21 | { |
||
| 22 | use HttpHeadersTrait; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * The query string |
||
| 26 | * |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | private $queryString; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * The payload |
||
| 33 | * |
||
| 34 | * @var MemoryStream |
||
| 35 | */ |
||
| 36 | private $payload; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * The HTTP protocol version |
||
| 40 | * |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | private $protocol; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Path to file on server (excluding endpoint address) |
||
| 47 | * |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | private $path; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * When the connection times out (in seconds) |
||
| 54 | * |
||
| 55 | * @var int |
||
| 56 | */ |
||
| 57 | private $timeout; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Load headers from remote and return it |
||
| 61 | * |
||
| 62 | * @return array |
||
| 63 | */ |
||
| 64 | 2 | public function retrieveHeaders(): array |
|
| 78 | |||
| 79 | /** |
||
| 80 | * |
||
| 81 | * {@inheritdoc} |
||
| 82 | * @see \Generics\Streams\HttpStream::appendPayload() |
||
| 83 | */ |
||
| 84 | 2 | public function appendPayload(InputStream $payload) |
|
| 90 | |||
| 91 | /** |
||
| 92 | * |
||
| 93 | * {@inheritdoc} |
||
| 94 | * @see \Generics\Streams\HttpStream::getPayload() |
||
| 95 | */ |
||
| 96 | 8 | public function getPayload(): InputOutputStream |
|
| 100 | |||
| 101 | /** |
||
| 102 | * Set connection timeout in seconds |
||
| 103 | * |
||
| 104 | * @param int $timeout |
||
| 105 | */ |
||
| 106 | 25 | public function setTimeout($timeout) |
|
| 114 | |||
| 115 | /** |
||
| 116 | * |
||
| 117 | * {@inheritdoc} |
||
| 118 | * @see \Generics\Resettable::reset() |
||
| 119 | */ |
||
| 120 | 25 | public function reset() |
|
| 127 | |||
| 128 | /** |
||
| 129 | * Prepare the request buffer |
||
| 130 | * |
||
| 131 | * @param string $requestType |
||
| 132 | * @return \Generics\Streams\MemoryStream |
||
| 133 | * @throws \Generics\Streams\StreamException |
||
| 134 | */ |
||
| 135 | 22 | private function prepareRequest($requestType): MemoryStream |
|
| 169 | |||
| 170 | /** |
||
| 171 | * Set the query string |
||
| 172 | * |
||
| 173 | * @param string $queryString |
||
| 174 | */ |
||
| 175 | 25 | public function setQueryString(string $queryString) |
|
| 179 | |||
| 180 | /** |
||
| 181 | * Retrieve and parse the response |
||
| 182 | * |
||
| 183 | * @param string $requestType |
||
| 184 | * @throws \Generics\Client\HttpException |
||
| 185 | * @throws \Generics\Socket\SocketException |
||
| 186 | * @throws \Generics\Streams\StreamException |
||
| 187 | */ |
||
| 188 | 20 | private function retrieveAndParseResponse($requestType) |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Append the payload buffer to the request buffer |
||
| 262 | * |
||
| 263 | * @param MemoryStream $ms |
||
| 264 | * @return MemoryStream |
||
| 265 | * @throws \Generics\Streams\StreamException |
||
| 266 | * @throws \Generics\ResetException |
||
| 267 | */ |
||
| 268 | 22 | private function appendPayloadToRequest(MemoryStream $ms): MemoryStream |
|
| 280 | |||
| 281 | /** |
||
| 282 | * Handle a header line |
||
| 283 | * |
||
| 284 | * All parameters by reference, which means the the values can be |
||
| 285 | * modified during execution of this method. |
||
| 286 | * |
||
| 287 | * @param boolean $delimiterFound |
||
| 288 | * Whether the delimiter for end of header section was found |
||
| 289 | * @param int $numBytes |
||
| 290 | * The number of bytes to read from remote |
||
| 291 | * @param string $tmp |
||
| 292 | * The current readen line |
||
| 293 | */ |
||
| 294 | 18 | private function handleHeader(&$delimiterFound, &$numBytes, &$tmp) |
|
| 308 | |||
| 309 | /** |
||
| 310 | * Perform the request |
||
| 311 | * |
||
| 312 | * @param string $requestType |
||
| 313 | */ |
||
| 314 | 22 | private function requestImpl(string $requestType) |
|
| 338 | |||
| 339 | /** |
||
| 340 | * Check the connection availability |
||
| 341 | * |
||
| 342 | * @param int $start |
||
| 343 | * Timestamp when read request attempt starts |
||
| 344 | * @throws HttpException |
||
| 345 | * @return bool |
||
| 346 | */ |
||
| 347 | 20 | private function checkConnection($start): bool |
|
| 360 | |||
| 361 | /** |
||
| 362 | * Check whether the readen bytes amount has reached the |
||
| 363 | * content length amount |
||
| 364 | * |
||
| 365 | * @return bool |
||
| 366 | */ |
||
| 367 | 14 | private function checkContentLengthExceeded(): bool |
|
| 376 | |||
| 377 | /** |
||
| 378 | * Set the used protocol |
||
| 379 | * |
||
| 380 | * @param string $protocol |
||
| 381 | */ |
||
| 382 | 25 | private function setProtocol(string $protocol) |
|
| 386 | |||
| 387 | /** |
||
| 388 | * Set the path on remote server |
||
| 389 | * |
||
| 390 | * @param string $path |
||
| 391 | */ |
||
| 392 | 25 | private function setPath(string $path) |
|
| 396 | |||
| 397 | /** |
||
| 398 | * |
||
| 399 | * {@inheritdoc} |
||
| 400 | * @see \Generics\Streams\HttpStream::request() |
||
| 401 | */ |
||
| 402 | abstract public function request(string $requestType); |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Get the socket endpoint |
||
| 406 | * |
||
| 407 | * @return \Generics\Socket\Endpoint |
||
| 408 | */ |
||
| 409 | abstract public function getEndpoint(): Endpoint; |
||
| 410 | |||
| 411 | /** |
||
| 412 | * |
||
| 413 | * {@inheritdoc} |
||
| 414 | * @see \Generics\Streams\InputStream::read() |
||
| 415 | */ |
||
| 416 | abstract public function read($length = 1, $offset = null): string; |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Whether the client is connected |
||
| 420 | * |
||
| 421 | * @return bool |
||
| 422 | */ |
||
| 423 | abstract public function isConnected(): bool; |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Connect to remote endpoint |
||
| 427 | * |
||
| 428 | * @throws SocketException |
||
| 429 | */ |
||
| 430 | abstract public function connect(); |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Disconnects the socket |
||
| 434 | * |
||
| 435 | * @throws SocketException |
||
| 436 | */ |
||
| 437 | abstract public function disconnect(); |
||
| 438 | |||
| 439 | /** |
||
| 440 | * |
||
| 441 | * {@inheritdoc} |
||
| 442 | * @see \Generics\Streams\OutputStream::write() |
||
| 443 | */ |
||
| 444 | abstract public function write($buffer); |
||
| 445 | |||
| 446 | /** |
||
| 447 | * |
||
| 448 | * {@inheritdoc} |
||
| 449 | * @see \Generics\Streams\Stream::ready() |
||
| 450 | */ |
||
| 451 | abstract public function ready(): bool; |
||
| 452 | } |