Complex classes like AbstractCurlRequest 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 AbstractCurlRequest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | abstract class AbstractCurlRequest implements CurlRequestInterface, HttpInterface { |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Configuration. |
||
| 35 | * |
||
| 36 | * @var CurlConfiguration |
||
| 37 | */ |
||
| 38 | private $configuration; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Headers. |
||
| 42 | * |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | private $headers; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Method. |
||
| 49 | * |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | private $method; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * POST data. |
||
| 56 | * |
||
| 57 | * @var array |
||
| 58 | */ |
||
| 59 | private $postData; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Query data. |
||
| 63 | * |
||
| 64 | * @var array |
||
| 65 | */ |
||
| 66 | private $queryData; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Resource path. |
||
| 70 | * |
||
| 71 | * @var string |
||
| 72 | */ |
||
| 73 | private $resourcePath; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Constructor. |
||
| 77 | * |
||
| 78 | * @param string $method The Method. |
||
| 79 | * @param CurlConfiguration $configuration The configuration. |
||
| 80 | * @param string|null $resourcePath The resource path. |
||
| 81 | * @throws InvalidArgumentException Throws an invalid argument exception if the method is invalid. |
||
| 82 | */ |
||
| 83 | protected function __construct(string $method, CurlConfiguration $configuration, ?string $resourcePath) { |
||
| 91 | |||
| 92 | /** |
||
| 93 | * {@inheritDoc} |
||
| 94 | */ |
||
| 95 | public function addHeader(string $name, string $value): CurlRequestInterface { |
||
| 99 | |||
| 100 | /** |
||
| 101 | * {@inheritDoc} |
||
| 102 | */ |
||
| 103 | public function addPostData(string $name, string $value): CurlRequestInterface { |
||
| 107 | |||
| 108 | /** |
||
| 109 | * {@inheritDoc} |
||
| 110 | */ |
||
| 111 | public function addQueryData(string $name, string $value): CurlRequestInterface { |
||
| 115 | |||
| 116 | /** |
||
| 117 | * {@inheritDoc} |
||
| 118 | */ |
||
| 119 | public function call(): CurlResponseInterface { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * {@inheritDoc} |
||
| 177 | */ |
||
| 178 | public function clearHeaders(): CurlRequestInterface { |
||
| 181 | |||
| 182 | /** |
||
| 183 | * {@inheritDoc} |
||
| 184 | */ |
||
| 185 | public function clearPostData(): CurlRequestInterface { |
||
| 188 | |||
| 189 | /** |
||
| 190 | * {@inheritDoc} |
||
| 191 | */ |
||
| 192 | public function clearQueryData(): CurlRequestInterface { |
||
| 195 | |||
| 196 | /** |
||
| 197 | * {@inheritDoc} |
||
| 198 | */ |
||
| 199 | public function getConfiguration(): CurlConfiguration { |
||
| 202 | |||
| 203 | /** |
||
| 204 | * {@inheritDoc} |
||
| 205 | */ |
||
| 206 | public function getHeaders(): array { |
||
| 209 | |||
| 210 | /** |
||
| 211 | * {@inheritDoc} |
||
| 212 | */ |
||
| 213 | public function getMethod(): string { |
||
| 216 | |||
| 217 | /** |
||
| 218 | * {@inheritDoc} |
||
| 219 | */ |
||
| 220 | public function getPostData(): array { |
||
| 223 | |||
| 224 | /** |
||
| 225 | * {@inheritDoc} |
||
| 226 | */ |
||
| 227 | public function getQueryData(): array { |
||
| 230 | |||
| 231 | /** |
||
| 232 | * {@inheritDoc} |
||
| 233 | */ |
||
| 234 | public function getResourcePath(): string { |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Merge the headers. |
||
| 240 | * |
||
| 241 | * @return array Returns the merged headers. |
||
| 242 | */ |
||
| 243 | private function mergeHeaders(): array { |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Merge the URL. |
||
| 255 | * |
||
| 256 | * @return string Returns the merged URL. |
||
| 257 | */ |
||
| 258 | private function mergeUrl(): string { |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Parse the raw header. |
||
| 273 | * |
||
| 274 | * @param string $rawHeader The raw header. |
||
| 275 | * @return array Returns the headers. |
||
| 276 | */ |
||
| 277 | private function parseHeader(string $rawHeader): array { |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Prepare a response. |
||
| 308 | * |
||
| 309 | * @param string $requestBody The request body. |
||
| 310 | * @param array $requestHeader The request header. |
||
| 311 | * @param string $requestUri The request URI. |
||
| 312 | * @param string $responseBody The response body. |
||
| 313 | * @param array $responseHeader The response header. |
||
| 314 | * @param array $responseInfo The response info. |
||
| 315 | * @return CurlResponseInterface Returns the response. |
||
| 316 | */ |
||
| 317 | private function prepareResponse(string $requestBody, array $requestHeader, string $requestUri, string $responseBody, array $responseHeader, array $responseInfo): CurlResponseInterface { |
||
| 329 | |||
| 330 | /** |
||
| 331 | * {@inheritDoc} |
||
| 332 | */ |
||
| 333 | public function removeHeader(string $name): CurlRequestInterface { |
||
| 339 | |||
| 340 | /** |
||
| 341 | * {@inheritDoc} |
||
| 342 | */ |
||
| 343 | public function removePostData(string $name): CurlRequestInterface { |
||
| 349 | |||
| 350 | /** |
||
| 351 | * {@inheritDoc} |
||
| 352 | */ |
||
| 353 | public function removeQueryData(string $name): CurlRequestInterface { |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Set the configuration. |
||
| 362 | * |
||
| 363 | * @param CurlConfiguration $configuration The configuration. |
||
| 364 | * @return CurlRequestInterface Returns this cURL request. |
||
| 365 | */ |
||
| 366 | protected function setConfiguration(CurlConfiguration $configuration): CurlRequestInterface { |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Set the headers. |
||
| 373 | * |
||
| 374 | * @param array $headers The headers. |
||
| 375 | * @return CurlRequestInterface Returns this cURL request. |
||
| 376 | */ |
||
| 377 | protected function setHeaders(array $headers): CurlRequestInterface { |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Set the method. |
||
| 384 | * |
||
| 385 | * @param string $method The method. |
||
| 386 | * @return CurlRequestInterface Returns this cURL request. |
||
| 387 | * @throws InvalidArgumentException Throws an invalid argument exception if the method is invalid. |
||
| 388 | */ |
||
| 389 | protected function setMethod(string $method): CurlRequestInterface { |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Set the POST data. |
||
| 410 | * |
||
| 411 | * @param array $postData The POST data. |
||
| 412 | * @return CurlRequestInterface Returns this cURL request. |
||
| 413 | */ |
||
| 414 | protected function setPostData(array $postData): CurlRequestInterface { |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Set the query data. |
||
| 421 | * |
||
| 422 | * @param array $queryData The query data. |
||
| 423 | * @return CurlRequestInterface Returns this cURL request. |
||
| 424 | */ |
||
| 425 | protected function setQueryData(array $queryData): CurlRequestInterface { |
||
| 429 | |||
| 430 | /** |
||
| 431 | * {@inheritDoc} |
||
| 432 | */ |
||
| 433 | public function setResourcePath(?string $resourcePath): CurlRequestInterface { |
||
| 437 | } |
||
| 438 |