Conditions | 18 |
Paths | 241 |
Total Lines | 78 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
65 | public function retrieveResponse( |
||
66 | UriInterface $endpoint, |
||
67 | $requestBody, |
||
68 | array $extraHeaders = [], |
||
69 | $method = 'POST' |
||
70 | ) { |
||
71 | // Normalize method name |
||
72 | $method = strtoupper($method); |
||
73 | |||
74 | $this->normalizeHeaders($extraHeaders); |
||
75 | |||
76 | if ($method === 'GET' && !empty($requestBody)) { |
||
77 | throw new InvalidArgumentException('No body expected for "GET" request.'); |
||
78 | } |
||
79 | |||
80 | if (!isset($extraHeaders['Content-Type']) && $method === 'POST' && is_array($requestBody)) { |
||
81 | $extraHeaders['Content-Type'] = 'Content-Type: application/x-www-form-urlencoded'; |
||
82 | } |
||
83 | |||
84 | $extraHeaders['Host'] = 'Host: ' . $endpoint->getHost(); |
||
85 | $extraHeaders['Connection'] = 'Connection: close'; |
||
86 | |||
87 | $ch = curl_init(); |
||
88 | |||
89 | curl_setopt($ch, CURLOPT_URL, $endpoint->getAbsoluteUri()); |
||
90 | |||
91 | if ($method === 'POST' || $method === 'PUT' || $method === 'PATCH') { |
||
92 | if ($requestBody && is_array($requestBody)) { |
||
93 | $requestBody = http_build_query($requestBody, '', '&'); |
||
94 | } |
||
95 | |||
96 | if ($method === 'PUT' || $method === 'PATCH') { |
||
97 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); |
||
98 | } else { |
||
99 | curl_setopt($ch, CURLOPT_POST, true); |
||
100 | } |
||
101 | |||
102 | curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody); |
||
103 | } else { |
||
104 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); |
||
105 | } |
||
106 | |||
107 | if ($this->maxRedirects > 0) { |
||
108 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
||
109 | curl_setopt($ch, CURLOPT_MAXREDIRS, $this->maxRedirects); |
||
110 | } |
||
111 | |||
112 | curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout); |
||
113 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
||
114 | curl_setopt($ch, CURLOPT_HEADER, false); |
||
115 | curl_setopt($ch, CURLOPT_HTTPHEADER, $extraHeaders); |
||
116 | curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent); |
||
117 | |||
118 | foreach ($this->parameters as $key => $value) { |
||
119 | curl_setopt($ch, $key, $value); |
||
120 | } |
||
121 | |||
122 | if ($this->forceSSL3) { |
||
123 | curl_setopt($ch, CURLOPT_SSLVERSION, 3); |
||
124 | } |
||
125 | |||
126 | $response = curl_exec($ch); |
||
127 | $responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
||
128 | |||
129 | if (false === $response) { |
||
130 | $errNo = curl_errno($ch); |
||
131 | $errStr = curl_error($ch); |
||
132 | curl_close($ch); |
||
133 | if (empty($errStr)) { |
||
134 | throw new TokenResponseException('Failed to request resource.', $responseCode); |
||
135 | } |
||
136 | throw new TokenResponseException('cURL Error # ' . $errNo . ': ' . $errStr, $responseCode); |
||
137 | } |
||
138 | |||
139 | curl_close($ch); |
||
140 | |||
141 | return $response; |
||
142 | } |
||
143 | } |
||
144 |