Conditions | 11 |
Paths | 152 |
Total Lines | 94 |
Code Lines | 60 |
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 |
||
125 | public static function get($url, $toFile = false, $header = false) |
||
126 | { |
||
127 | $output = false; |
||
128 | $debug = false; |
||
129 | |||
130 | if ($debug) { |
||
131 | Ajde_Log::_('cURL URL', Ajde_Log::CHANNEL_INFO, Ajde_Log::LEVEL_INFORMATIONAL, $url); |
||
132 | } |
||
133 | |||
134 | try { |
||
135 | $ch = curl_init(); |
||
136 | |||
137 | curl_setopt($ch, CURLOPT_URL, |
||
138 | $url); // The URL to fetch. This can also be set when initializing a session with curl_init(). |
||
139 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, |
||
140 | true); // TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly. |
||
141 | curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, |
||
142 | 5); // The number of seconds to wait while trying to connect. Use 0 to wait indefinitely. |
||
143 | curl_setopt($ch, CURLOPT_TIMEOUT, |
||
144 | 5); // The maximum number of seconds to allow cURL functions to execute. |
||
145 | curl_setopt($ch, CURLOPT_USERAGENT, |
||
146 | 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36'); // The contents of the "User-Agent: " header to be used in a HTTP request. |
||
147 | curl_setopt($ch, CURLOPT_ENCODING, |
||
148 | ''); // The contents of the "Accept-Encoding: " header. This enables decoding of the response. Supported encodings are "identity", "deflate", and "gzip". If an empty string, "", is set, a header containing all supported encoding types is sent. |
||
149 | curl_setopt($ch, CURLOPT_AUTOREFERER, |
||
150 | true); // TRUE to automatically set the Referer: field in requests where it follows a Location: redirect. |
||
151 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, |
||
152 | false); // FALSE to stop cURL from verifying the peer's certificate. Alternate certificates to verify against can be specified with the CURLOPT_CAINFO option or a certificate directory can be specified with the CURLOPT_CAPATH option. CURLOPT_SSL_VERIFYHOST may also need to be TRUE or FALSE if CURLOPT_SSL_VERIFYPEER is disabled (it defaults to 2). |
||
153 | curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); |
||
154 | curl_setopt($ch, CURLOPT_COOKIEFILE, ''); |
||
155 | |||
156 | if ($toFile !== false) { |
||
157 | |||
158 | // @TODO We need SAFE_MODE to be off |
||
159 | if (ini_get('safe_mode')) { |
||
160 | throw new Ajde_Exception('SAFE_MODE must be off when downloading files'); |
||
161 | } |
||
162 | |||
163 | $fp = fopen($toFile, 'w+'); //This is the file where we save the information |
||
164 | |||
165 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
||
166 | curl_setopt($ch, CURLOPT_MAXREDIRS, |
||
167 | 20); // The maximum amount of HTTP redirections to follow. Use this option alongside CURLOPT_FOLLOWLOCATION. |
||
168 | curl_setopt($ch, CURLOPT_TIMEOUT, 300); |
||
169 | curl_setopt($ch, CURLOPT_FILE, $fp); // write curl response to file |
||
170 | curl_setopt($ch, CURLINFO_HEADER_OUT, true); |
||
171 | |||
172 | if ($header) { |
||
173 | curl_setopt($ch, CURLOPT_HTTPHEADER, $header); |
||
174 | } |
||
175 | |||
176 | curl_exec($ch); |
||
177 | |||
178 | fclose($fp); |
||
179 | $output = true; |
||
180 | |||
181 | $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
||
182 | |||
183 | if ($debug) { |
||
184 | $verbose = curl_getinfo($ch); |
||
185 | } |
||
186 | if ($debug) { |
||
187 | Ajde_Log::_('cURL result', Ajde_Log::CHANNEL_INFO, Ajde_Log::LEVEL_INFORMATIONAL, |
||
188 | var_export($verbose, true)); |
||
189 | } |
||
190 | |||
191 | curl_close($ch); |
||
192 | |||
193 | if (substr($http_status, 0, 1 == '4')) { |
||
194 | return false; |
||
195 | } |
||
196 | } else { |
||
197 | // Not possible in SAFE_MODE |
||
198 | // curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // TRUE to follow any "Location: " header that the server sends as part of the HTTP header (note this is recursive, PHP will follow as many "Location: " headers that it is sent, unless CURLOPT_MAXREDIRS is set). |
||
199 | // curl_setopt($ch, CURLOPT_HEADER, false); // TRUE to include the header in the output. |
||
200 | // curl_setopt($ch, CURLOPT_MAXREDIRS, 10); // The maximum amount of HTTP redirections to follow. Use this option alongside CURLOPT_FOLLOWLOCATION. |
||
201 | $output = self::_curl_exec_follow($ch, 10, false); |
||
202 | |||
203 | if ($debug) { |
||
204 | $verbose = curl_getinfo($ch); |
||
205 | } |
||
206 | if ($debug) { |
||
207 | Ajde_Log::_('cURL result', Ajde_Log::CHANNEL_INFO, Ajde_Log::LEVEL_INFORMATIONAL, |
||
208 | var_export($verbose, true)); |
||
209 | } |
||
210 | |||
211 | curl_close($ch); |
||
212 | } |
||
213 | } catch (Exception $e) { |
||
214 | throw $e; |
||
215 | } |
||
216 | |||
217 | return $output; |
||
218 | } |
||
219 | |||
280 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.