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 |
||
| 8 | trait HttpClientTrait |
||
| 9 | { |
||
| 10 | use HttpHeadersTrait; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * The query string |
||
| 14 | * |
||
| 15 | * @var string |
||
| 16 | */ |
||
| 17 | private $queryString; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * The payload |
||
| 21 | * |
||
| 22 | * @var MemoryStream |
||
| 23 | */ |
||
| 24 | private $payload; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * The HTTP protocol version |
||
| 28 | * |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | private $protocol; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Path to file on server (excluding endpoint address) |
||
| 35 | * |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | private $path; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * When the connection times out (in seconds) |
||
| 42 | * |
||
| 43 | * @var int |
||
| 44 | */ |
||
| 45 | private $timeout; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Load headers from remote and return it |
||
| 49 | * |
||
| 50 | * @return array |
||
| 51 | */ |
||
| 52 | 2 | public function retrieveHeaders(): array |
|
| 66 | |||
| 67 | /** |
||
| 68 | * |
||
| 69 | * {@inheritdoc} |
||
| 70 | * @see \Generics\Streams\HttpStream::appendPayload() |
||
| 71 | */ |
||
| 72 | 2 | public function appendPayload(InputStream $payload) |
|
| 78 | |||
| 79 | /** |
||
| 80 | * |
||
| 81 | * {@inheritdoc} |
||
| 82 | * @see \Generics\Streams\HttpStream::getPayload() |
||
| 83 | */ |
||
| 84 | 8 | public function getPayload(): InputOutputStream |
|
| 88 | |||
| 89 | /** |
||
| 90 | * Set connection timeout in seconds |
||
| 91 | * |
||
| 92 | * @param int $timeout |
||
| 93 | */ |
||
| 94 | 23 | public function setTimeout($timeout) |
|
| 102 | |||
| 103 | /** |
||
| 104 | * |
||
| 105 | * {@inheritdoc} |
||
| 106 | * @see \Generics\Resettable::reset() |
||
| 107 | */ |
||
| 108 | 23 | public function reset() |
|
| 115 | |||
| 116 | /** |
||
| 117 | * Prepare the request buffer |
||
| 118 | * |
||
| 119 | * @param string $requestType |
||
| 120 | * @return \Generics\Streams\MemoryStream |
||
| 121 | * @throws \Generics\Streams\StreamException |
||
| 122 | */ |
||
| 123 | 22 | private function prepareRequest($requestType): MemoryStream |
|
| 157 | |||
| 158 | /** |
||
| 159 | * Set the query string |
||
| 160 | * |
||
| 161 | * @param string $queryString |
||
| 162 | */ |
||
| 163 | 23 | public function setQueryString(string $queryString) |
|
| 167 | |||
| 168 | /** |
||
| 169 | * Retrieve and parse the response |
||
| 170 | * |
||
| 171 | * @param string $requestType |
||
| 172 | * @throws \Generics\Client\HttpException |
||
| 173 | * @throws \Generics\Socket\SocketException |
||
| 174 | * @throws \Generics\Streams\StreamException |
||
| 175 | */ |
||
| 176 | 20 | private function retrieveAndParseResponse($requestType) |
|
| 243 | |||
| 244 | /** |
||
| 245 | * Append the payload buffer to the request buffer |
||
| 246 | * |
||
| 247 | * @param MemoryStream $ms |
||
| 248 | * @return MemoryStream |
||
| 249 | * @throws \Generics\Streams\StreamException |
||
| 250 | * @throws \Generics\ResetException |
||
| 251 | */ |
||
| 252 | 22 | private function appendPayloadToRequest(MemoryStream $ms): MemoryStream |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Handle a header line |
||
| 267 | * |
||
| 268 | * All parameters by reference, which means the the values can be |
||
| 269 | * modified during execution of this method. |
||
| 270 | * |
||
| 271 | * @param boolean $delimiterFound |
||
| 272 | * Whether the delimiter for end of header section was found |
||
| 273 | * @param int $numBytes |
||
| 274 | * The number of bytes to read from remote |
||
| 275 | * @param string $tmp |
||
| 276 | * The current readen line |
||
| 277 | */ |
||
| 278 | 18 | private function handleHeader(&$delimiterFound, &$numBytes, &$tmp) |
|
| 292 | |||
| 293 | /** |
||
| 294 | * Perform the request |
||
| 295 | * |
||
| 296 | * @param string $requestType |
||
| 297 | */ |
||
| 298 | 22 | private function requestImpl(string $requestType) |
|
| 322 | |||
| 323 | /** |
||
| 324 | * Check the connection availability |
||
| 325 | * |
||
| 326 | * @param int $start |
||
| 327 | * Timestamp when read request attempt starts |
||
| 328 | * @throws HttpException |
||
| 329 | * @return bool |
||
| 330 | */ |
||
| 331 | 20 | private function checkConnection($start): bool |
|
| 344 | |||
| 345 | /** |
||
| 346 | * Check whether the readen bytes amount has reached the |
||
| 347 | * content length amount |
||
| 348 | * |
||
| 349 | * @return bool |
||
| 350 | */ |
||
| 351 | 14 | private function checkContentLengthExceeded(): bool |
|
| 360 | |||
| 361 | /** |
||
| 362 | * Set the used protocol |
||
| 363 | * |
||
| 364 | * @param string $protocol |
||
| 365 | */ |
||
| 366 | 23 | private function setProtocol(string $protocol) |
|
| 370 | |||
| 371 | /** |
||
| 372 | * Set the path on remote server |
||
| 373 | * |
||
| 374 | * @param string $path |
||
| 375 | */ |
||
| 376 | 23 | private function setPath(string $path) |
|
| 380 | } |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.