Conditions | 49 |
Paths | > 20000 |
Total Lines | 235 |
Code Lines | 137 |
Lines | 7 |
Ratio | 2.98 % |
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 |
||
58 | public function request($url, $headers = array(), $data = array(), $options = array()) { |
||
59 | $options['hooks']->dispatch('fsockopen.before_request'); |
||
60 | |||
61 | $url_parts = parse_url($url); |
||
62 | if (empty($url_parts)) { |
||
63 | throw new Requests_Exception('Invalid URL.', 'invalidurl', $url); |
||
64 | } |
||
65 | $host = $url_parts['host']; |
||
66 | $context = stream_context_create(); |
||
67 | $verifyname = false; |
||
68 | $case_insensitive_headers = new Requests_Utility_CaseInsensitiveDictionary($headers); |
||
69 | |||
70 | // HTTPS support |
||
71 | if (isset($url_parts['scheme']) && strtolower($url_parts['scheme']) === 'https') { |
||
72 | $remote_socket = 'ssl://' . $host; |
||
73 | if (!isset($url_parts['port'])) { |
||
74 | $url_parts['port'] = 443; |
||
75 | } |
||
76 | |||
77 | $context_options = array( |
||
78 | 'verify_peer' => true, |
||
79 | // 'CN_match' => $host, |
||
80 | 'capture_peer_cert' => true |
||
81 | ); |
||
82 | $verifyname = true; |
||
83 | |||
84 | // SNI, if enabled (OpenSSL >=0.9.8j) |
||
85 | if (defined('OPENSSL_TLSEXT_SERVER_NAME') && OPENSSL_TLSEXT_SERVER_NAME) { |
||
86 | $context_options['SNI_enabled'] = true; |
||
87 | View Code Duplication | if (isset($options['verifyname']) && $options['verifyname'] === false) { |
|
88 | $context_options['SNI_enabled'] = false; |
||
89 | } |
||
90 | } |
||
91 | |||
92 | if (isset($options['verify'])) { |
||
93 | if ($options['verify'] === false) { |
||
94 | $context_options['verify_peer'] = false; |
||
95 | } |
||
96 | elseif (is_string($options['verify'])) { |
||
97 | $context_options['cafile'] = $options['verify']; |
||
98 | } |
||
99 | } |
||
100 | |||
101 | View Code Duplication | if (isset($options['verifyname']) && $options['verifyname'] === false) { |
|
102 | $context_options['verify_peer_name'] = false; |
||
103 | $verifyname = false; |
||
104 | } |
||
105 | |||
106 | stream_context_set_option($context, array('ssl' => $context_options)); |
||
107 | } |
||
108 | else { |
||
109 | $remote_socket = 'tcp://' . $host; |
||
110 | } |
||
111 | |||
112 | $this->max_bytes = $options['max_bytes']; |
||
113 | |||
114 | if (!isset($url_parts['port'])) { |
||
115 | $url_parts['port'] = 80; |
||
116 | } |
||
117 | $remote_socket .= ':' . $url_parts['port']; |
||
118 | |||
119 | set_error_handler(array($this, 'connect_error_handler'), E_WARNING | E_NOTICE); |
||
120 | |||
121 | $options['hooks']->dispatch('fsockopen.remote_socket', array(&$remote_socket)); |
||
122 | |||
123 | $socket = stream_socket_client($remote_socket, $errno, $errstr, ceil($options['connect_timeout']), STREAM_CLIENT_CONNECT, $context); |
||
124 | |||
125 | restore_error_handler(); |
||
126 | |||
127 | if ($verifyname && !$this->verify_certificate_from_context($host, $context)) { |
||
128 | throw new Requests_Exception('SSL certificate did not match the requested domain name', 'ssl.no_match'); |
||
129 | } |
||
130 | |||
131 | if (!$socket) { |
||
132 | if ($errno === 0) { |
||
133 | // Connection issue |
||
134 | throw new Requests_Exception(rtrim($this->connect_error), 'fsockopen.connect_error'); |
||
135 | } |
||
136 | |||
137 | throw new Requests_Exception($errstr, 'fsockopenerror', null, $errno); |
||
138 | } |
||
139 | |||
140 | $data_format = $options['data_format']; |
||
141 | |||
142 | if ($data_format === 'query') { |
||
143 | $path = self::format_get($url_parts, $data); |
||
|
|||
144 | $data = ''; |
||
145 | } |
||
146 | else { |
||
147 | $path = self::format_get($url_parts, array()); |
||
148 | } |
||
149 | |||
150 | $options['hooks']->dispatch('fsockopen.remote_host_path', array(&$path, $url)); |
||
151 | |||
152 | $request_body = ''; |
||
153 | $out = sprintf("%s %s HTTP/%.1f\r\n", $options['type'], $path, $options['protocol_version']); |
||
154 | |||
155 | if ($options['type'] !== Requests::TRACE) { |
||
156 | if (is_array($data)) { |
||
157 | $request_body = http_build_query($data, null, '&'); |
||
158 | } |
||
159 | else { |
||
160 | $request_body = $data; |
||
161 | } |
||
162 | |||
163 | if (!empty($data)) { |
||
164 | if (!isset($case_insensitive_headers['Content-Length'])) { |
||
165 | $headers['Content-Length'] = strlen($request_body); |
||
166 | } |
||
167 | |||
168 | if (!isset($case_insensitive_headers['Content-Type'])) { |
||
169 | $headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8'; |
||
170 | } |
||
171 | } |
||
172 | } |
||
173 | |||
174 | if (!isset($case_insensitive_headers['Host'])) { |
||
175 | $out .= sprintf('Host: %s', $url_parts['host']); |
||
176 | |||
177 | if (( 'http' === strtolower($url_parts['scheme']) && $url_parts['port'] !== 80 ) || ( 'https' === strtolower($url_parts['scheme']) && $url_parts['port'] !== 443 )) { |
||
178 | $out .= ':' . $url_parts['port']; |
||
179 | } |
||
180 | $out .= "\r\n"; |
||
181 | } |
||
182 | |||
183 | if (!isset($case_insensitive_headers['User-Agent'])) { |
||
184 | $out .= sprintf("User-Agent: %s\r\n", $options['useragent']); |
||
185 | } |
||
186 | |||
187 | $accept_encoding = $this->accept_encoding(); |
||
188 | if (!isset($case_insensitive_headers['Accept-Encoding']) && !empty($accept_encoding)) { |
||
189 | $out .= sprintf("Accept-Encoding: %s\r\n", $accept_encoding); |
||
190 | } |
||
191 | |||
192 | $headers = Requests::flatten($headers); |
||
193 | |||
194 | if (!empty($headers)) { |
||
195 | $out .= implode($headers, "\r\n") . "\r\n"; |
||
196 | } |
||
197 | |||
198 | $options['hooks']->dispatch('fsockopen.after_headers', array(&$out)); |
||
199 | |||
200 | if (substr($out, -2) !== "\r\n") { |
||
201 | $out .= "\r\n"; |
||
202 | } |
||
203 | |||
204 | if (!isset($case_insensitive_headers['Connection'])) { |
||
205 | $out .= "Connection: Close\r\n"; |
||
206 | } |
||
207 | |||
208 | $out .= "\r\n" . $request_body; |
||
209 | |||
210 | $options['hooks']->dispatch('fsockopen.before_send', array(&$out)); |
||
211 | |||
212 | fwrite($socket, $out); |
||
213 | $options['hooks']->dispatch('fsockopen.after_send', array($out)); |
||
214 | |||
215 | if (!$options['blocking']) { |
||
216 | fclose($socket); |
||
217 | $fake_headers = ''; |
||
218 | $options['hooks']->dispatch('fsockopen.after_request', array(&$fake_headers)); |
||
219 | return ''; |
||
220 | } |
||
221 | |||
222 | $timeout_sec = (int) floor($options['timeout']); |
||
223 | if ($timeout_sec == $options['timeout']) { |
||
224 | $timeout_msec = 0; |
||
225 | } |
||
226 | else { |
||
227 | $timeout_msec = self::SECOND_IN_MICROSECONDS * $options['timeout'] % self::SECOND_IN_MICROSECONDS; |
||
228 | } |
||
229 | stream_set_timeout($socket, $timeout_sec, $timeout_msec); |
||
230 | |||
231 | $response = $body = $headers = ''; |
||
232 | $this->info = stream_get_meta_data($socket); |
||
233 | $size = 0; |
||
234 | $doingbody = false; |
||
235 | $download = false; |
||
236 | if ($options['filename']) { |
||
237 | $download = fopen($options['filename'], 'wb'); |
||
238 | } |
||
239 | |||
240 | while (!feof($socket)) { |
||
241 | $this->info = stream_get_meta_data($socket); |
||
242 | if ($this->info['timed_out']) { |
||
243 | throw new Requests_Exception('fsocket timed out', 'timeout'); |
||
244 | } |
||
245 | |||
246 | $block = fread($socket, Requests::BUFFER_SIZE); |
||
247 | if (!$doingbody) { |
||
248 | $response .= $block; |
||
249 | if (strpos($response, "\r\n\r\n")) { |
||
250 | list($headers, $block) = explode("\r\n\r\n", $response, 2); |
||
251 | $doingbody = true; |
||
252 | } |
||
253 | } |
||
254 | |||
255 | // Are we in body mode now? |
||
256 | if ($doingbody) { |
||
257 | $options['hooks']->dispatch('request.progress', array($block, $size, $this->max_bytes)); |
||
258 | $data_length = strlen($block); |
||
259 | if ($this->max_bytes) { |
||
260 | // Have we already hit a limit? |
||
261 | if ($size === $this->max_bytes) { |
||
262 | continue; |
||
263 | } |
||
264 | if (($size + $data_length) > $this->max_bytes) { |
||
265 | // Limit the length |
||
266 | $limited_length = ($this->max_bytes - $size); |
||
267 | $block = substr($block, 0, $limited_length); |
||
268 | } |
||
269 | } |
||
270 | |||
271 | $size += strlen($block); |
||
272 | if ($download) { |
||
273 | fwrite($download, $block); |
||
274 | } |
||
275 | else { |
||
276 | $body .= $block; |
||
277 | } |
||
278 | } |
||
279 | } |
||
280 | $this->headers = $headers; |
||
281 | |||
282 | if ($download) { |
||
283 | fclose($download); |
||
284 | } |
||
285 | else { |
||
286 | $this->headers .= "\r\n\r\n" . $body; |
||
287 | } |
||
288 | fclose($socket); |
||
289 | |||
290 | $options['hooks']->dispatch('fsockopen.after_request', array(&$this->headers, &$this->info)); |
||
291 | return $this->headers; |
||
292 | } |
||
293 | |||
445 |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.