Conditions | 23 |
Paths | > 20000 |
Total Lines | 151 |
Code Lines | 58 |
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 |
||
74 | public function request($method, UriInterface $uri, $data = null, array $headers = null, $timeout = null, $userAgent = null) |
||
75 | { |
||
76 | // Setup the cURL handle. |
||
77 | $ch = curl_init(); |
||
78 | |||
79 | $options = array(); |
||
80 | |||
81 | // Set the request method. |
||
82 | switch (strtoupper($method)) |
||
83 | { |
||
84 | case 'GET': |
||
85 | $options[CURLOPT_HTTPGET] = true; |
||
86 | break; |
||
87 | |||
88 | case 'POST': |
||
89 | $options[CURLOPT_POST] = true; |
||
90 | break; |
||
91 | |||
92 | default: |
||
93 | $options[CURLOPT_CUSTOMREQUEST] = strtoupper($method); |
||
94 | break; |
||
95 | } |
||
96 | |||
97 | // Don't wait for body when $method is HEAD |
||
98 | $options[CURLOPT_NOBODY] = ($method === 'HEAD'); |
||
99 | |||
100 | // Initialize the certificate store |
||
101 | $options[CURLOPT_CAINFO] = isset($this->options['curl.certpath']) ? $this->options['curl.certpath'] : CaBundle::getSystemCaRootBundlePath(); |
||
102 | |||
103 | // If data exists let's encode it and make sure our Content-type header is set. |
||
104 | if (isset($data)) |
||
105 | { |
||
106 | // If the data is a scalar value simply add it to the cURL post fields. |
||
107 | if (is_scalar($data) || (isset($headers['Content-Type']) && strpos($headers['Content-Type'], 'multipart/form-data') === 0)) |
||
108 | { |
||
109 | $options[CURLOPT_POSTFIELDS] = $data; |
||
110 | } |
||
111 | else |
||
112 | // Otherwise we need to encode the value first. |
||
113 | { |
||
114 | $options[CURLOPT_POSTFIELDS] = http_build_query($data); |
||
115 | } |
||
116 | |||
117 | if (!isset($headers['Content-Type'])) |
||
118 | { |
||
119 | $headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8'; |
||
120 | } |
||
121 | |||
122 | // Add the relevant headers. |
||
123 | if (is_scalar($options[CURLOPT_POSTFIELDS])) |
||
124 | { |
||
125 | $headers['Content-Length'] = strlen($options[CURLOPT_POSTFIELDS]); |
||
126 | } |
||
127 | } |
||
128 | |||
129 | // Build the headers string for the request. |
||
130 | $headerArray = array(); |
||
131 | |||
132 | if (isset($headers)) |
||
133 | { |
||
134 | foreach ($headers as $key => $value) |
||
135 | { |
||
136 | $headerArray[] = $key . ': ' . $value; |
||
137 | } |
||
138 | |||
139 | // Add the headers string into the stream context options array. |
||
140 | $options[CURLOPT_HTTPHEADER] = $headerArray; |
||
141 | } |
||
142 | |||
143 | // Curl needs the accepted encoding header as option |
||
144 | if (isset($headers['Accept-Encoding'])) |
||
145 | { |
||
146 | $options[CURLOPT_ENCODING] = $headers['Accept-Encoding']; |
||
147 | } |
||
148 | |||
149 | // If an explicit timeout is given user it. |
||
150 | if (isset($timeout)) |
||
151 | { |
||
152 | $options[CURLOPT_TIMEOUT] = (int) $timeout; |
||
153 | $options[CURLOPT_CONNECTTIMEOUT] = (int) $timeout; |
||
154 | } |
||
155 | |||
156 | // If an explicit user agent is given use it. |
||
157 | if (isset($userAgent)) |
||
158 | { |
||
159 | $options[CURLOPT_USERAGENT] = $userAgent; |
||
160 | } |
||
161 | |||
162 | // Set the request URL. |
||
163 | $options[CURLOPT_URL] = (string) $uri; |
||
164 | |||
165 | // We want our headers. :-) |
||
166 | $options[CURLOPT_HEADER] = true; |
||
167 | |||
168 | // Return it... echoing it would be tacky. |
||
169 | $options[CURLOPT_RETURNTRANSFER] = true; |
||
170 | |||
171 | // Override the Expect header to prevent cURL from confusing itself in its own stupidity. |
||
172 | // Link: http://the-stickman.com/web-development/php-and-curl-disabling-100-continue-header/ |
||
173 | $options[CURLOPT_HTTPHEADER][] = 'Expect:'; |
||
174 | |||
175 | // Follow redirects if server config allows |
||
176 | if ($this->redirectsAllowed()) |
||
177 | { |
||
178 | $options[CURLOPT_FOLLOWLOCATION] = (bool) isset($this->options['follow_location']) ? $this->options['follow_location'] : true; |
||
179 | } |
||
180 | |||
181 | // Authentication, if needed |
||
182 | if (isset($this->options['userauth']) && isset($this->options['passwordauth'])) |
||
183 | { |
||
184 | $options[CURLOPT_USERPWD] = $this->options['userauth'] . ':' . $this->options['passwordauth']; |
||
185 | $options[CURLOPT_HTTPAUTH] = CURLAUTH_BASIC; |
||
186 | } |
||
187 | |||
188 | // Set any custom transport options |
||
189 | if (isset($this->options['transport.curl'])) |
||
190 | { |
||
191 | foreach ($this->options['transport.curl'] as $key => $value) |
||
192 | { |
||
193 | $options[$key] = $value; |
||
194 | } |
||
195 | } |
||
196 | |||
197 | // Set the cURL options. |
||
198 | curl_setopt_array($ch, $options); |
||
199 | |||
200 | // Execute the request and close the connection. |
||
201 | $content = curl_exec($ch); |
||
202 | |||
203 | // Check if the content is a string. If it is not, it must be an error. |
||
204 | if (!is_string($content)) |
||
205 | { |
||
206 | $message = curl_error($ch); |
||
207 | |||
208 | if (empty($message)) |
||
209 | { |
||
210 | // Error but nothing from cURL? Create our own |
||
211 | $message = 'No HTTP response received'; |
||
212 | } |
||
213 | |||
214 | throw new \RuntimeException($message); |
||
215 | } |
||
216 | |||
217 | // Get the request information. |
||
218 | $info = curl_getinfo($ch); |
||
219 | |||
220 | // Close the connection. |
||
221 | curl_close($ch); |
||
222 | |||
223 | return $this->getResponse($content, $info); |
||
224 | } |
||
225 | |||
325 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.