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 | * Content-Type "application/x-www-form-urlencoded". |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | const CONTENT_TYPE_X_WWW_FORM_URLENCODED = "Content-Type: application/x-www-form-urlencoded"; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Configuration. |
||
| 41 | * |
||
| 42 | * @var CurlConfiguration |
||
| 43 | */ |
||
| 44 | private $configuration; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Headers. |
||
| 48 | * |
||
| 49 | * @var array |
||
| 50 | */ |
||
| 51 | private $headers = []; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Method. |
||
| 55 | * |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | private $method = self::HTTP_METHOD_GET; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * POST data. |
||
| 62 | * |
||
| 63 | * @var array |
||
| 64 | */ |
||
| 65 | private $postData = []; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Query data. |
||
| 69 | * |
||
| 70 | * @var array |
||
| 71 | */ |
||
| 72 | private $queryData = []; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Resource path. |
||
| 76 | * |
||
| 77 | * @var string |
||
| 78 | */ |
||
| 79 | private $resourcePath; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Constructor. |
||
| 83 | * |
||
| 84 | * @param string $method The Method. |
||
| 85 | * @param CurlConfiguration $configuration The configuration. |
||
| 86 | * @param string $resourcePath The resource path. |
||
| 87 | * @throws InvalidArgumentException Throws an invalid argument exception if the method is invalid. |
||
| 88 | */ |
||
| 89 | protected function __construct($method, CurlConfiguration $configuration, $resourcePath) { |
||
| 94 | |||
| 95 | /** |
||
| 96 | * {@inheritdoc} |
||
| 97 | */ |
||
| 98 | public function addHeader($name, $value) { |
||
| 103 | |||
| 104 | /** |
||
| 105 | * {@inheritdoc} |
||
| 106 | */ |
||
| 107 | public function addPostData($name, $value) { |
||
| 112 | |||
| 113 | /** |
||
| 114 | * {@inheritdoc} |
||
| 115 | */ |
||
| 116 | public function addQueryData($name, $value) { |
||
| 121 | |||
| 122 | /** |
||
| 123 | * {@inheritdoc} |
||
| 124 | */ |
||
| 125 | public function call() { |
||
| 186 | |||
| 187 | /** |
||
| 188 | * {@inheritdoc} |
||
| 189 | */ |
||
| 190 | public function clearHeaders() { |
||
| 193 | |||
| 194 | /** |
||
| 195 | * {@inheritdoc} |
||
| 196 | */ |
||
| 197 | public function clearPostData() { |
||
| 200 | |||
| 201 | /** |
||
| 202 | * {@inheritdoc} |
||
| 203 | */ |
||
| 204 | public function clearQueryData() { |
||
| 207 | |||
| 208 | /** |
||
| 209 | * {@inheritdoc} |
||
| 210 | */ |
||
| 211 | public function getConfiguration() { |
||
| 214 | |||
| 215 | /** |
||
| 216 | * {@inheritdoc} |
||
| 217 | */ |
||
| 218 | public function getHeaders() { |
||
| 221 | |||
| 222 | /** |
||
| 223 | * {@inheritdoc} |
||
| 224 | */ |
||
| 225 | public function getMethod() { |
||
| 228 | |||
| 229 | /** |
||
| 230 | * {@inheritdoc} |
||
| 231 | */ |
||
| 232 | public function getPostData() { |
||
| 235 | |||
| 236 | /** |
||
| 237 | * {@inheritdoc} |
||
| 238 | */ |
||
| 239 | public function getQueryData() { |
||
| 242 | |||
| 243 | /** |
||
| 244 | * {@inheritdoc} |
||
| 245 | */ |
||
| 246 | public function getResourcePath() { |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Merge the headers. |
||
| 252 | * |
||
| 253 | * @return array Returns the merged headers. |
||
| 254 | */ |
||
| 255 | private function mergeHeaders() { |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Merge the URL. |
||
| 267 | * |
||
| 268 | * @return string Returns the merged URL. |
||
| 269 | */ |
||
| 270 | private function mergeUrl() { |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Parse the raw header. |
||
| 285 | * |
||
| 286 | * @param string $rawHeader The raw header. |
||
| 287 | * @return array Returns the headers. |
||
| 288 | */ |
||
| 289 | private function parseHeader($rawHeader) { |
||
| 317 | |||
| 318 | /** |
||
| 319 | * {@inheritdoc} |
||
| 320 | */ |
||
| 321 | public function removeHeader($name) { |
||
| 327 | |||
| 328 | /** |
||
| 329 | * {@inheritdoc} |
||
| 330 | */ |
||
| 331 | public function removePostData($name) { |
||
| 337 | |||
| 338 | /** |
||
| 339 | * {@inheritdoc} |
||
| 340 | */ |
||
| 341 | public function removeQueryData($name) { |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Set the configuration. |
||
| 350 | * |
||
| 351 | * @param CurlConfiguration $configuration The configuration. |
||
| 352 | * @return CurlRequestInterface Returns this cURL request. |
||
| 353 | */ |
||
| 354 | protected function setConfiguration(CurlConfiguration $configuration) { |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Set the headers. |
||
| 361 | * |
||
| 362 | * @param array $headers The headers. |
||
| 363 | * @return CurlRequestInterface Returns this cURL request. |
||
| 364 | */ |
||
| 365 | protected function setHeaders(array $headers) { |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Set the method. |
||
| 372 | * |
||
| 373 | * @param string $method The method. |
||
| 374 | * @return CurlRequestInterface Returns this cURL request. |
||
| 375 | * @throws InvalidArgumentException Throws an invalid argument exception if the method is invalid. |
||
| 376 | */ |
||
| 377 | protected function setMethod($method) { |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Set the POST data. |
||
| 398 | * |
||
| 399 | * @param array $postData The POST data. |
||
| 400 | * @return CurlRequestInterface Returns this cURL request. |
||
| 401 | */ |
||
| 402 | protected function setPostData(array $postData) { |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Set the query data. |
||
| 409 | * |
||
| 410 | * @param array $queryData The query data. |
||
| 411 | * @return CurlRequestInterface Returns this cURL request. |
||
| 412 | */ |
||
| 413 | protected function setQueryData(array $queryData) { |
||
| 417 | |||
| 418 | /** |
||
| 419 | * {@inheritdoc} |
||
| 420 | */ |
||
| 421 | public function setResourcePath($resourcePath) { |
||
| 425 | } |
||
| 426 |