| Conditions | 25 |
| Paths | > 20000 |
| Total Lines | 116 |
| 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 |
||
| 33 | public static function makeRequst($options) { |
||
| 34 | $errorHandler = new myErrorHandler(); |
||
| 35 | |||
| 36 | $old_error_handler = set_error_handler(array($errorHandler, "proc_error")); |
||
|
|
|||
| 37 | |||
| 38 | $method = isset($options['method']) ? $options['method'] : 'GET'; |
||
| 39 | $query = isset($options['query']) ? array_filter($options['query'], function($x) { return !is_null($x); } ) : array(); |
||
| 40 | |||
| 41 | $body = isset($options['body']) ? $options['body'] : null; |
||
| 42 | $file = isset($options['FILE']) ? $options['FILE'] : null; |
||
| 43 | $headers = array( |
||
| 44 | "User-Agent: Route4Me php-sdk" |
||
| 45 | ); |
||
| 46 | |||
| 47 | if (isset($options['HTTPHEADER'])) { |
||
| 48 | $headers[] = $options['HTTPHEADER']; |
||
| 49 | } |
||
| 50 | |||
| 51 | if (isset($options['HTTPHEADERS'])) { |
||
| 52 | foreach ($options['HTTPHEADERS'] As $header) { |
||
| 53 | $headers[] = $header; |
||
| 54 | } |
||
| 55 | } |
||
| 56 | |||
| 57 | $ch = curl_init(); |
||
| 58 | |||
| 59 | $url = isset($options['url']) ? $options['url'].'?'.http_build_query(array_merge( |
||
| 60 | $query, array('api_key' => self::getApiKey()) |
||
| 61 | )) : ""; |
||
| 62 | |||
| 63 | $baseUrl = self::getBaseUrl(); |
||
| 64 | |||
| 65 | $curlOpts = array( |
||
| 66 | CURLOPT_URL => $baseUrl.$url, |
||
| 67 | CURLOPT_RETURNTRANSFER => true, |
||
| 68 | CURLOPT_TIMEOUT => 80, |
||
| 69 | CURLOPT_FOLLOWLOCATION => true, |
||
| 70 | CURLOPT_SSL_VERIFYHOST => FALSE, |
||
| 71 | CURLOPT_SSL_VERIFYPEER => FALSE, |
||
| 72 | CURLOPT_HTTPHEADER => $headers |
||
| 73 | ); |
||
| 74 | |||
| 75 | curl_setopt_array($ch, $curlOpts); |
||
| 76 | |||
| 77 | if ($file!=null) { |
||
| 78 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); |
||
| 79 | $fp=fopen($file, 'r'); |
||
| 80 | curl_setopt($ch, CURLOPT_INFILE, $fp); |
||
| 81 | curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file)); |
||
| 82 | } |
||
| 83 | |||
| 84 | switch ($method) { |
||
| 85 | case 'DELETE': |
||
| 86 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); |
||
| 87 | break; |
||
| 88 | case 'DELETEARRAY': |
||
| 89 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); |
||
| 90 | curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($query)); |
||
| 91 | break; |
||
| 92 | case 'PUT': |
||
| 93 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); |
||
| 94 | break; |
||
| 95 | case 'POST': |
||
| 96 | if (isset($body)) { |
||
| 97 | $bodyData = json_encode($body); |
||
| 98 | if (isset($options['HTTPHEADER'])) { |
||
| 99 | if (strpos($options['HTTPHEADER'], "multipart/form-data")>0) { |
||
| 100 | $bodyData = $body; |
||
| 101 | } |
||
| 102 | } |
||
| 103 | |||
| 104 | curl_setopt($ch, CURLOPT_POSTFIELDS, $bodyData); |
||
| 105 | } |
||
| 106 | break; |
||
| 107 | case 'ADD': |
||
| 108 | curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($query)); break; |
||
| 109 | } |
||
| 110 | |||
| 111 | if (is_numeric(array_search($method, array('DELETE', 'PUT')))) { |
||
| 112 | if (isset($body)) { |
||
| 113 | curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body)); |
||
| 114 | } |
||
| 115 | } |
||
| 116 | |||
| 117 | $result = curl_exec($ch); |
||
| 118 | |||
| 119 | $isxml = FALSE; |
||
| 120 | $jxml = ""; |
||
| 121 | if (strpos($result, '<?xml')>-1) { |
||
| 122 | $xml = simplexml_load_string($result); |
||
| 123 | //$jxml = json_encode($xml); |
||
| 124 | $jxml = self::object2array($xml); |
||
| 125 | $isxml = TRUE; |
||
| 126 | } |
||
| 127 | |||
| 128 | $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
||
| 129 | curl_close($ch); |
||
| 130 | |||
| 131 | if (200==$code) { |
||
| 132 | if ($isxml) { |
||
| 133 | $json = $jxml; |
||
| 134 | } else { |
||
| 135 | $json = json_decode($result, true); |
||
| 136 | } |
||
| 137 | |||
| 138 | if (isset($json['errors'])) { |
||
| 139 | throw new ApiError(implode(', ', $json['errors'])); |
||
| 140 | } else { |
||
| 141 | return $json; |
||
| 142 | } |
||
| 143 | } elseif (409==$code) { |
||
| 144 | throw new ApiError('Wrong API key'); |
||
| 145 | } else { |
||
| 146 | throw new ApiError('Something wrong'); |
||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 298 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.