Complex classes like ConsoleRequest 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 ConsoleRequest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class ConsoleRequest |
||
| 13 | { |
||
| 14 | /** @var ApiHandlerInterface */ |
||
| 15 | private $handler; |
||
| 16 | |||
| 17 | 3 | public function __construct(ApiHandlerInterface $handler) |
|
| 21 | |||
| 22 | 3 | public function makeRequest(string $url, string $method, array $values, array $additionalValues = [], ?string $token = null): ConsoleResponse |
|
| 23 | { |
||
| 24 | 3 | list($postFields, $getFields, $cookieFields, $rawPost, $putFields) = $this->processValues($values); |
|
| 25 | |||
| 26 | 3 | $postFields = array_merge($postFields, $additionalValues['postFields'] ?? []); |
|
| 27 | 3 | $getFields = array_merge($getFields, $additionalValues['getFields'] ?? []); |
|
| 28 | 3 | $cookieFields = array_merge($cookieFields, $additionalValues['cookieFields'] ?? []); |
|
| 29 | 3 | $putFields = array_merge($putFields, $additionalValues['putFields'] ?? []); |
|
| 30 | |||
| 31 | 3 | $postFields = $this->normalizeValues($postFields); |
|
| 32 | 3 | $getFields = $this->normalizeValues($getFields); |
|
| 33 | 3 | $putFields = $this->normalizeValues($putFields); |
|
| 34 | |||
| 35 | 3 | if (count($getFields)) { |
|
| 36 | 3 | $parts = []; |
|
| 37 | 3 | foreach ($getFields as $key => $value) { |
|
| 38 | 3 | $parts[] = "$key=$value"; |
|
| 39 | } |
||
| 40 | 3 | $url = $url . '?' . implode('&', $parts); |
|
| 41 | } |
||
| 42 | |||
| 43 | 3 | $putRawPost = null; |
|
| 44 | 3 | if (count($putFields)) { |
|
| 45 | $parts = []; |
||
| 46 | foreach ($putFields as $key => $value) { |
||
| 47 | $parts[] = "$key=$value"; |
||
| 48 | } |
||
| 49 | $putRawPost = implode('&', $parts); |
||
| 50 | } |
||
| 51 | |||
| 52 | 3 | $startTime = microtime(true); |
|
| 53 | |||
| 54 | 3 | $curl = curl_init(); |
|
| 55 | 3 | curl_setopt($curl, CURLOPT_URL, $url); |
|
| 56 | 3 | curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method); |
|
| 57 | 3 | curl_setopt($curl, CURLOPT_NOBODY, false); |
|
| 58 | 3 | curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); |
|
| 59 | 3 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
|
| 60 | 3 | curl_setopt($curl, CURLOPT_VERBOSE, false); |
|
| 61 | 3 | curl_setopt($curl, CURLOPT_TIMEOUT, $additionalValues['timeout'] ?? 30); |
|
| 62 | 3 | curl_setopt($curl, CURLOPT_HEADER, true); |
|
| 63 | |||
| 64 | 3 | if (count($postFields) || $rawPost || $putRawPost !== null) { |
|
| 65 | curl_setopt($curl, CURLOPT_POST, true); |
||
| 66 | curl_setopt($curl, CURLOPT_POSTFIELDS, count($postFields) ? $postFields : ($rawPost ?: $putRawPost)); |
||
| 67 | } |
||
| 68 | |||
| 69 | 3 | $headers = $additionalValues['headers'] ?? []; |
|
| 70 | 3 | if (count($cookieFields)) { |
|
| 71 | $parts = []; |
||
| 72 | foreach ($cookieFields as $key => $value) { |
||
| 73 | $parts[] = "$key=$value"; |
||
| 74 | } |
||
| 75 | $headers[] = "Cookie: " . implode('&', $parts); |
||
| 76 | } |
||
| 77 | 3 | if ($token !== null && $token !== false) { |
|
| 78 | $headers[] = 'Authorization: Bearer ' . $token; |
||
| 79 | } |
||
| 80 | 3 | if (count($headers)) { |
|
| 81 | curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); |
||
| 82 | } |
||
| 83 | |||
| 84 | 3 | $consoleResponse = new ConsoleResponse( |
|
| 85 | 3 | $url, |
|
| 86 | 2 | $method, |
|
| 87 | 2 | $postFields, |
|
| 88 | 2 | $getFields, |
|
| 89 | 2 | $cookieFields, |
|
| 90 | 2 | $headers, |
|
| 91 | 2 | $rawPost |
|
| 92 | ); |
||
| 93 | |||
| 94 | 3 | $response = curl_exec($curl); |
|
| 95 | 3 | $elapsed = intval((microtime(true) - $startTime) * 1000); |
|
| 96 | |||
| 97 | 3 | if ($response === false) { |
|
| 98 | 3 | $response = ''; |
|
| 99 | } |
||
| 100 | |||
| 101 | 3 | $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE); |
|
| 102 | 3 | $responseHeaders = substr($response, 0, $headerSize); |
|
| 103 | 3 | $responseBody = substr($response, $headerSize); |
|
| 104 | |||
| 105 | 3 | $curlErrorNumber = curl_errno($curl); |
|
| 106 | 3 | $curlError = curl_error($curl); |
|
| 107 | 3 | if ($curlErrorNumber > 0) { |
|
| 108 | 3 | $consoleResponse->logError($curlErrorNumber, $curlError, $elapsed); |
|
| 109 | } else { |
||
| 110 | $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); |
||
| 111 | $consoleResponse->logRequest($httpCode, $responseBody, $responseHeaders, $elapsed); |
||
| 112 | } |
||
| 113 | |||
| 114 | 3 | return $consoleResponse; |
|
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Process given values to POST and GET fields |
||
| 119 | * |
||
| 120 | * @param array $values |
||
| 121 | * |
||
| 122 | * @return array |
||
| 123 | */ |
||
| 124 | 3 | private function processValues(array $values): array |
|
| 172 | |||
| 173 | /** |
||
| 174 | * Process one param and returns value |
||
| 175 | * |
||
| 176 | * @param ParamInterface $param input param |
||
| 177 | * @param string $key param key |
||
| 178 | * @param mixed $value actual value from request |
||
| 179 | * |
||
| 180 | * @return mixed |
||
| 181 | */ |
||
| 182 | 3 | private function processParam(ParamInterface $param, string $key, $value) |
|
| 203 | |||
| 204 | 3 | private function normalizeValues(array $values): array |
|
| 221 | } |
||
| 222 |