Conditions | 19 |
Paths | 737 |
Total Lines | 105 |
Code Lines | 63 |
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 |
||
92 | function http_request($request, $timeout = 5) { |
||
93 | |||
94 | $url = $request['url']; |
||
95 | // Extract the hostname from url |
||
96 | $parts = parse_url($url); |
||
97 | if (array_key_exists('host', $parts)) { |
||
98 | $remote = $parts['host']; |
||
99 | } else { |
||
100 | return myErrorHandler("url ($url) has no host. Is it relative?"); |
||
101 | } |
||
102 | if (array_key_exists('port', $parts)) { |
||
103 | $port = $parts['port']; |
||
104 | } else { |
||
105 | $port = 0; |
||
106 | } |
||
107 | |||
108 | // Beware that RFC2616 (HTTP/1.1) defines header fields as case-insensitive entities. |
||
109 | $request_headers = ""; |
||
110 | foreach ($request['headers'] as $name => $value) { |
||
111 | switch (strtolower($name)) { |
||
112 | //omit some headers |
||
113 | case "keep-alive": |
||
114 | case "connection": |
||
115 | case "cookie": |
||
116 | //TODO: we don't handle any compression encodings. compression |
||
117 | //can cause a problem if client communication is already being |
||
118 | //compressed by the server/app that integrates this script |
||
119 | //(which would double compress the content, once from the remote |
||
120 | //server to us, and once from us to the client, but the client |
||
121 | //would de-compress only once). |
||
122 | case "accept-encoding": |
||
123 | break; |
||
124 | // correct the host parameter |
||
125 | case "host": |
||
126 | $host_info = $remote; |
||
127 | if ($port) { |
||
128 | $host_info .= ':' . $port; |
||
129 | } |
||
130 | $request_headers .= "$name: $host_info\r\n"; |
||
131 | break; |
||
132 | // forward all other headers |
||
133 | default: |
||
134 | $request_headers .= "$name: $value\r\n"; |
||
135 | break; |
||
136 | } |
||
137 | } |
||
138 | |||
139 | //set fsockopen transport scheme, and the default port |
||
140 | switch (strtolower($parts['scheme'])) { |
||
141 | case 'https': |
||
142 | $scheme = 'ssl://'; |
||
143 | if ( ! $port ) $port = 443; |
||
144 | break; |
||
145 | case 'http': |
||
146 | $scheme = ''; |
||
147 | if ( ! $port ) $port = 80; |
||
148 | break; |
||
149 | default: |
||
150 | //some other transports are available but not really supported |
||
151 | //by this script: http://php.net/manual/en/transports.inet.php |
||
152 | $scheme = $parts['scheme'] . '://'; |
||
153 | if ( ! $port ) { |
||
154 | return myErrorHandler("Unknown scheme ($scheme) and no port."); |
||
155 | } |
||
156 | break; |
||
157 | } |
||
158 | |||
159 | //we make the request with socket operations since we don't want to |
||
160 | //depend on the curl extension, and the higher level wrappers don't |
||
161 | //give us usable error information. |
||
162 | $sock = @fsockopen("$scheme$remote", $port, $errno, $errstr, $timeout); |
||
163 | if ( ! $sock ) { |
||
164 | return myErrorHandler("Unable to open URL ($url): $errstr"); |
||
165 | } |
||
166 | |||
167 | //the timeout in fsockopen is only for the connection, the following |
||
168 | //is for reading the content |
||
169 | stream_set_timeout($sock, $timeout); |
||
170 | |||
171 | //an absolute url should only be specified for proxy requests |
||
172 | if (array_key_exists('path', $parts)) { |
||
173 | $path_info = $parts['path']; |
||
174 | } else { |
||
175 | $path_info = '/'; |
||
176 | } |
||
177 | |||
178 | if (array_key_exists('query', $parts)) $path_info .= '?' . $parts['query']; |
||
179 | if (array_key_exists('fragment', $parts)) $path_info .= '#' . $parts['fragment']; |
||
180 | |||
181 | $out = $request["method"]." ".$path_info." ".$request["protocol"]."\r\n" |
||
182 | . $request_headers |
||
183 | . "Connection: close\r\n\r\n"; |
||
184 | fwrite($sock, $out); |
||
185 | fwrite($sock, $request['payload']); |
||
186 | |||
187 | $header_str = stream_get_line($sock, 1024*16, "\r\n\r\n"); |
||
188 | $headers = http_parse_headers($header_str); |
||
189 | $status_line = array_shift($headers); |
||
190 | |||
191 | // get http status |
||
192 | preg_match('|HTTP/\d+\.\d+\s+(\d+)\s+.*|i',$status_line,$match); |
||
193 | $status = $match[1]; |
||
194 | |||
195 | return array('headers' => $headers, 'socket' => $sock, 'status' => $status); |
||
196 | } |
||
197 | |||
249 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.