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 |
||
32 | abstract class AbstractCurlRequest implements CurlRequestInterface, HttpInterface { |
||
33 | |||
34 | /** |
||
35 | * Content-Type "application/x-www-form-urlencoded". |
||
36 | * @var string |
||
37 | */ |
||
38 | const CONTENT_TYPE_X_WWW_FORM_URLENCODED = "Content-Type: application/x-www-form-urlencoded"; |
||
39 | |||
40 | /** |
||
41 | * Configuration. |
||
42 | * |
||
43 | * @var CurlConfiguration |
||
44 | */ |
||
45 | private $configuration; |
||
46 | |||
47 | /** |
||
48 | * Headers. |
||
49 | * |
||
50 | * @var array |
||
51 | */ |
||
52 | private $headers = []; |
||
53 | |||
54 | /** |
||
55 | * Method. |
||
56 | * |
||
57 | * @var string |
||
58 | */ |
||
59 | private $method = self::HTTP_METHOD_GET; |
||
60 | |||
61 | /** |
||
62 | * POST data. |
||
63 | * |
||
64 | * @var array |
||
65 | */ |
||
66 | private $postData = []; |
||
67 | |||
68 | /** |
||
69 | * Query data. |
||
70 | * |
||
71 | * @var array |
||
72 | */ |
||
73 | private $queryData = []; |
||
74 | |||
75 | /** |
||
76 | * Resource path. |
||
77 | * |
||
78 | * @var string |
||
79 | */ |
||
80 | private $resourcePath; |
||
81 | |||
82 | /** |
||
83 | * Constructor. |
||
84 | * |
||
85 | * @param string $method The Method. |
||
86 | * @param CurlConfiguration $configuration The configuration. |
||
87 | * @param string $resourcePath The resource path. |
||
88 | * @throws InvalidArgumentException Throws an invalid argument exception if the method is invalid. |
||
89 | */ |
||
90 | protected function __construct($method, CurlConfiguration $configuration, $resourcePath) { |
||
95 | |||
96 | /** |
||
97 | * {@inheritdoc} |
||
98 | */ |
||
99 | public function addHeader($name, $value) { |
||
104 | |||
105 | /** |
||
106 | * {@inheritdoc} |
||
107 | */ |
||
108 | public function addPostData($name, $value) { |
||
113 | |||
114 | /** |
||
115 | * {@inheritdoc} |
||
116 | */ |
||
117 | public function addQueryData($name, $value) { |
||
122 | |||
123 | /** |
||
124 | * {@inheritdoc} |
||
125 | */ |
||
126 | public function call() { |
||
181 | |||
182 | /** |
||
183 | * {@inheritdoc} |
||
184 | */ |
||
185 | public function clearHeaders() { |
||
188 | |||
189 | /** |
||
190 | * {@inheritdoc} |
||
191 | */ |
||
192 | public function clearPostData() { |
||
195 | |||
196 | /** |
||
197 | * {@inheritdoc} |
||
198 | */ |
||
199 | public function clearQueryData() { |
||
202 | |||
203 | /** |
||
204 | * {@inheritdoc} |
||
205 | */ |
||
206 | public function getConfiguration() { |
||
209 | |||
210 | /** |
||
211 | * {@inheritdoc} |
||
212 | */ |
||
213 | public function getHeaders() { |
||
216 | |||
217 | /** |
||
218 | * {@inheritdoc} |
||
219 | */ |
||
220 | public function getMethod() { |
||
223 | |||
224 | /** |
||
225 | * {@inheritdoc} |
||
226 | */ |
||
227 | public function getPostData() { |
||
230 | |||
231 | /** |
||
232 | * {@inheritdoc} |
||
233 | */ |
||
234 | public function getQueryData() { |
||
237 | |||
238 | /** |
||
239 | * {@inheritdoc} |
||
240 | */ |
||
241 | public function getResourcePath() { |
||
244 | |||
245 | /** |
||
246 | * Merge the headers. |
||
247 | * |
||
248 | * @return array Returns the merged headers. |
||
249 | */ |
||
250 | private function mergeHeaders() { |
||
259 | |||
260 | /** |
||
261 | * Merge the URL. |
||
262 | * |
||
263 | * @return string Returns the merged URL. |
||
264 | */ |
||
265 | private function mergeUrl() { |
||
277 | |||
278 | /** |
||
279 | * Parse the raw header. |
||
280 | * |
||
281 | * @param string $rawHeader The raw header. |
||
282 | * @return array Returns the headers. |
||
283 | */ |
||
284 | private function parseHeader($rawHeader) { |
||
312 | |||
313 | /** |
||
314 | * Prepare a response. |
||
315 | * |
||
316 | * @param string $requestBody The request body. |
||
317 | * @param array $requestHeader The request header. |
||
318 | * @param string $requestUri The request URI. |
||
319 | * @param string $responseBody The response body. |
||
320 | * @param array $responseHeader The response header. |
||
321 | * @param array $responseInfo The response info. |
||
322 | * @return CurlResponseInterface Returns the response. |
||
323 | */ |
||
324 | private function prepareResponse($requestBody, array $requestHeader, $requestUri, $responseBody, array $responseHeader, array $responseInfo) { |
||
336 | |||
337 | /** |
||
338 | * {@inheritdoc} |
||
339 | */ |
||
340 | public function removeHeader($name) { |
||
346 | |||
347 | /** |
||
348 | * {@inheritdoc} |
||
349 | */ |
||
350 | public function removePostData($name) { |
||
356 | |||
357 | /** |
||
358 | * {@inheritdoc} |
||
359 | */ |
||
360 | public function removeQueryData($name) { |
||
366 | |||
367 | /** |
||
368 | * Set the configuration. |
||
369 | * |
||
370 | * @param CurlConfiguration $configuration The configuration. |
||
371 | * @return CurlRequestInterface Returns this cURL request. |
||
372 | */ |
||
373 | protected function setConfiguration(CurlConfiguration $configuration) { |
||
377 | |||
378 | /** |
||
379 | * Set the headers. |
||
380 | * |
||
381 | * @param array $headers The headers. |
||
382 | * @return CurlRequestInterface Returns this cURL request. |
||
383 | */ |
||
384 | protected function setHeaders(array $headers) { |
||
388 | |||
389 | /** |
||
390 | * Set the method. |
||
391 | * |
||
392 | * @param string $method The method. |
||
393 | * @return CurlRequestInterface Returns this cURL request. |
||
394 | * @throws InvalidArgumentException Throws an invalid argument exception if the method is invalid. |
||
395 | */ |
||
396 | protected function setMethod($method) { |
||
414 | |||
415 | /** |
||
416 | * Set the POST data. |
||
417 | * |
||
418 | * @param array $postData The POST data. |
||
419 | * @return CurlRequestInterface Returns this cURL request. |
||
420 | */ |
||
421 | protected function setPostData(array $postData) { |
||
425 | |||
426 | /** |
||
427 | * Set the query data. |
||
428 | * |
||
429 | * @param array $queryData The query data. |
||
430 | * @return CurlRequestInterface Returns this cURL request. |
||
431 | */ |
||
432 | protected function setQueryData(array $queryData) { |
||
436 | |||
437 | /** |
||
438 | * {@inheritdoc} |
||
439 | */ |
||
440 | public function setResourcePath($resourcePath) { |
||
444 | } |
||
445 |