| Conditions | 24 |
| Paths | > 20000 |
| Total Lines | 114 |
| Code Lines | 75 |
| Lines | 49 |
| Ratio | 42.98 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 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 | public static function makeRequst($options) { |
||
| 93 | $method = isset($options['method']) ? $options['method'] : 'GET'; |
||
| 94 | $query = isset($options['query']) ? |
||
| 95 | array_filter($options['query']) : array(); |
||
| 96 | //echo "query=".$query['member_id'];die(""); |
||
| 97 | $body = isset($options['body']) ? |
||
| 98 | array_filter($options['body']) : null; |
||
| 99 | $file = isset($options['FILE']) ? $options['FILE'] : null; |
||
| 100 | $headers = array( |
||
| 101 | "User-Agent: Route4Me php-sdk" |
||
| 102 | ); |
||
| 103 | |||
| 104 | if (isset($options['HTTPHEADER'])) { |
||
| 105 | $headers[]=$options['HTTPHEADER']; |
||
| 106 | } |
||
| 107 | |||
| 108 | if (isset($options['HTTPHEADERS'])) { |
||
| 109 | foreach ($options['HTTPHEADERS'] As $header) $headers[]=$header; |
||
| 110 | } |
||
| 111 | //self::simplePrint($headers); die(""); |
||
| 112 | $ch = curl_init(); |
||
| 113 | $url = $options['url'] . '?' . http_build_query(array_merge( |
||
| 114 | $query, array( 'api_key' => self::getApiKey()) |
||
| 115 | )); |
||
| 116 | |||
| 117 | //$jfile=json_encode($body); echo $jfile; die(""); |
||
| 118 | |||
| 119 | //self::simplePrint($headers); die(""); |
||
| 120 | $baseUrl=self::getBaseUrl(); |
||
| 121 | |||
| 122 | if (strpos($url,'move_route_destination')>0) $baseUrl='https://www.route4me.com'; |
||
| 123 | $curlOpts = arraY( |
||
| 124 | CURLOPT_URL => $baseUrl. $url, |
||
| 125 | CURLOPT_RETURNTRANSFER => true, |
||
| 126 | CURLOPT_TIMEOUT => 60, |
||
| 127 | CURLOPT_FOLLOWLOCATION => true, |
||
| 128 | CURLOPT_SSL_VERIFYHOST => FALSE, |
||
| 129 | CURLOPT_SSL_VERIFYPEER => FALSE, |
||
| 130 | CURLOPT_HTTPHEADER => $headers |
||
| 131 | ); |
||
| 132 | |||
| 133 | //echo "url=".$baseUrl.$url."<br>";die(""); |
||
| 134 | curl_setopt_array($ch, $curlOpts); |
||
| 135 | |||
| 136 | if ($file !=null) { |
||
| 137 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); |
||
| 138 | $fp=fopen($file,'r'); |
||
| 139 | curl_setopt($ch, CURLOPT_INFILE , $fp); |
||
| 140 | curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file)); |
||
| 141 | } |
||
| 142 | |||
| 143 | View Code Duplication | switch($method) { |
|
| 144 | case 'DELETE': |
||
| 145 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); |
||
| 146 | |||
| 147 | if (isset($body)) { |
||
| 148 | curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body)); |
||
| 149 | } |
||
| 150 | break; |
||
| 151 | case 'DELETEARRAY': |
||
| 152 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); |
||
| 153 | curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($query)); |
||
| 154 | break; |
||
| 155 | case 'PUT': |
||
| 156 | //$jfile=json_encode($body); echo $jfile; die(""); |
||
| 157 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); |
||
| 158 | if (isset($query)) { |
||
| 159 | curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($query)); |
||
| 160 | } |
||
| 161 | |||
| 162 | if (isset($body)) { |
||
| 163 | curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body)); |
||
| 164 | } |
||
| 165 | break; |
||
| 166 | case 'POST': |
||
| 167 | if (isset($query)) { |
||
| 168 | curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($query)); |
||
| 169 | } |
||
| 170 | |||
| 171 | //echo json_encode($body); die(""); |
||
| 172 | if (isset($body)) { |
||
| 173 | curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body)); |
||
| 174 | } |
||
| 175 | break; |
||
| 176 | case 'ADD': |
||
| 177 | curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($query)); break; |
||
| 178 | } |
||
| 179 | |||
| 180 | $result = curl_exec($ch); |
||
| 181 | //var_dump($result); die(""); |
||
| 182 | $isxml=FALSE; |
||
| 183 | $jxml=""; |
||
| 184 | View Code Duplication | if (strpos($result, '<?xml')>-1) |
|
| 185 | { |
||
| 186 | $xml = simplexml_load_string($result); |
||
| 187 | $jxml = json_encode($xml); |
||
| 188 | $isxml = TRUE; |
||
| 189 | } |
||
| 190 | |||
| 191 | $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
||
| 192 | curl_close($ch); |
||
| 193 | |||
| 194 | if ($isxml) { |
||
| 195 | $json = $jxml; |
||
| 196 | } else $json = json_decode($result, true); |
||
| 197 | //var_dump($json); die(""); |
||
| 198 | View Code Duplication | if (200 == $code) { |
|
| 199 | return $json; |
||
| 200 | } elseif (isset($json['errors'])) { |
||
| 201 | throw new ApiError(implode(', ', $json['errors'])); |
||
| 202 | } else { |
||
| 203 | throw new ApiError('Something wrong'); |
||
| 204 | } |
||
| 205 | } |
||
| 206 | |||
| 344 |
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.