Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Route4Me often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Route4Me, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class Route4Me |
||
| 9 | { |
||
| 10 | static public $apiKey; |
||
| 11 | static public $baseUrl = Endpoint::BASE_URL; |
||
| 12 | |||
| 13 | public static function setApiKey($apiKey) |
||
| 17 | |||
| 18 | public static function getApiKey() |
||
| 22 | |||
| 23 | public static function setBaseUrl($baseUrl) |
||
| 27 | |||
| 28 | public static function getBaseUrl() |
||
| 32 | |||
| 33 | public static function fileUploadRequest($options) { |
||
| 34 | $query = isset($options['query']) ? array_filter($options['query']) : array(); |
||
| 35 | |||
| 36 | if (sizeof($query)==0) { |
||
| 37 | return null; |
||
| 38 | } |
||
| 39 | |||
| 40 | $body = isset($options['body']) ? array_filter($options['body']) : null; |
||
| 41 | |||
| 42 | $fname = isset($body['strFilename']) ? $body['strFilename'] : ''; |
||
| 43 | |||
| 44 | if ($fname=='') { |
||
| 45 | return null; |
||
| 46 | } |
||
| 47 | |||
| 48 | $rpath = function_exists('curl_file_create') ? curl_file_create(realpath($fname)) : '@'.realpath($fname); |
||
| 49 | |||
| 50 | $url = self::$baseUrl.$options['url'].'?'.http_build_query(array_merge(array('api_key' => self::getApiKey()), $query)); |
||
| 51 | |||
| 52 | $ch = curl_init($url); |
||
| 53 | |||
| 54 | $curlOpts = array( |
||
| 55 | CURLOPT_POST => true, |
||
| 56 | CURLOPT_RETURNTRANSFER => false, |
||
| 57 | CURLOPT_TIMEOUT => 60, |
||
| 58 | CURLOPT_FOLLOWLOCATION => true, |
||
| 59 | CURLOPT_SSL_VERIFYHOST => FALSE, |
||
| 60 | CURLOPT_SSL_VERIFYPEER => FALSE |
||
| 61 | ); |
||
| 62 | |||
| 63 | curl_setopt_array($ch, $curlOpts); |
||
| 64 | |||
| 65 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); |
||
| 66 | |||
| 67 | curl_setopt($ch, CURLOPT_HTTPHEADER, array( |
||
| 68 | "Content-Type: multipart/form-data", |
||
| 69 | 'Content-Disposition: form-data; name="strFilename"' |
||
| 70 | )); |
||
| 71 | |||
| 72 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); |
||
| 73 | curl_setopt($ch, CURLOPT_POSTFIELDS, array('strFilename' => $rpath)); |
||
| 74 | |||
| 75 | $result = curl_exec($ch); |
||
| 76 | |||
| 77 | $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
||
| 78 | curl_close($ch); |
||
| 79 | |||
| 80 | $json = json_decode($result, true); |
||
| 81 | |||
| 82 | if (200==$code) { |
||
| 83 | return $json; |
||
| 84 | } elseif (isset($json['errors'])) { |
||
| 85 | throw new ApiError(implode(', ', $json['errors'])); |
||
| 86 | } else { |
||
| 87 | throw new ApiError('Something wrong'); |
||
| 88 | } |
||
| 89 | } |
||
| 90 | |||
| 91 | public static function makeRequst($options) { |
||
| 92 | $errorHandler = new myErrorHandler(); |
||
| 93 | |||
| 94 | $old_error_handler = set_error_handler(array($errorHandler, "proc_error")); |
||
|
|
|||
| 95 | |||
| 96 | $method = isset($options['method']) ? $options['method'] : 'GET'; |
||
| 97 | $query = isset($options['query']) ? array_filter($options['query'], function($x) { return !is_null($x); } ) : array(); |
||
| 98 | |||
| 99 | $body = isset($options['body']) ? $options['body'] : null; |
||
| 100 | $file = isset($options['FILE']) ? $options['FILE'] : null; |
||
| 101 | $headers = array( |
||
| 102 | "User-Agent: Route4Me php-sdk" |
||
| 103 | ); |
||
| 104 | |||
| 105 | if (isset($options['HTTPHEADER'])) { |
||
| 106 | $headers[] = $options['HTTPHEADER']; |
||
| 107 | } |
||
| 108 | |||
| 109 | if (isset($options['HTTPHEADERS'])) { |
||
| 110 | foreach ($options['HTTPHEADERS'] As $header) { |
||
| 111 | $headers[] = $header; |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | $ch = curl_init(); |
||
| 116 | |||
| 117 | $url = isset($options['url']) ? $options['url'].'?'.http_build_query(array_merge( |
||
| 118 | $query, array('api_key' => self::getApiKey()) |
||
| 119 | )) : ""; |
||
| 120 | |||
| 121 | $baseUrl = self::getBaseUrl(); |
||
| 122 | |||
| 123 | $curlOpts = array( |
||
| 124 | CURLOPT_URL => $baseUrl.$url, |
||
| 125 | CURLOPT_RETURNTRANSFER => true, |
||
| 126 | CURLOPT_TIMEOUT => 80, |
||
| 127 | CURLOPT_FOLLOWLOCATION => true, |
||
| 128 | CURLOPT_SSL_VERIFYHOST => FALSE, |
||
| 129 | CURLOPT_SSL_VERIFYPEER => FALSE, |
||
| 130 | CURLOPT_HTTPHEADER => $headers |
||
| 131 | ); |
||
| 132 | |||
| 133 | curl_setopt_array($ch, $curlOpts); |
||
| 134 | |||
| 135 | if ($file!=null) { |
||
| 136 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); |
||
| 137 | $fp=fopen($file, 'r'); |
||
| 138 | curl_setopt($ch, CURLOPT_INFILE, $fp); |
||
| 139 | curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file)); |
||
| 140 | } |
||
| 141 | |||
| 142 | switch ($method) { |
||
| 143 | case 'DELETE': |
||
| 144 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); |
||
| 145 | break; |
||
| 146 | case 'DELETEARRAY': |
||
| 147 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); |
||
| 148 | curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($query)); |
||
| 149 | break; |
||
| 150 | case 'PUT': |
||
| 151 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); |
||
| 152 | break; |
||
| 153 | case 'POST': |
||
| 154 | if (isset($body)) { |
||
| 155 | $bodyData = json_encode($body); |
||
| 156 | if (isset($options['HTTPHEADER'])) { |
||
| 157 | if (strpos($options['HTTPHEADER'], "multipart/form-data")>0) { |
||
| 158 | $bodyData = $body; |
||
| 159 | } |
||
| 160 | } |
||
| 161 | |||
| 162 | curl_setopt($ch, CURLOPT_POSTFIELDS, $bodyData); |
||
| 163 | } |
||
| 164 | break; |
||
| 165 | case 'ADD': |
||
| 166 | curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($query)); break; |
||
| 167 | } |
||
| 168 | |||
| 169 | if (is_numeric(array_search($method, array('DELETE', 'PUT')))) { |
||
| 170 | if (isset($body)) { |
||
| 171 | curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body)); |
||
| 172 | } |
||
| 173 | } |
||
| 174 | |||
| 175 | $result = curl_exec($ch); |
||
| 176 | |||
| 177 | $isxml = FALSE; |
||
| 178 | $jxml = ""; |
||
| 179 | if (strpos($result, '<?xml')>-1) { |
||
| 180 | $xml = simplexml_load_string($result); |
||
| 181 | //$jxml = json_encode($xml); |
||
| 182 | $jxml = self::object2array($xml); |
||
| 183 | $isxml = TRUE; |
||
| 184 | } |
||
| 185 | |||
| 186 | $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
||
| 187 | curl_close($ch); |
||
| 188 | |||
| 189 | if (200==$code) { |
||
| 190 | if ($isxml) { |
||
| 191 | $json = $jxml; |
||
| 192 | } else { |
||
| 193 | $json = json_decode($result, true); |
||
| 194 | } |
||
| 195 | |||
| 196 | if (isset($json['errors'])) { |
||
| 197 | throw new ApiError(implode(', ', $json['errors'])); |
||
| 198 | } else { |
||
| 199 | return $json; |
||
| 200 | } |
||
| 201 | } elseif (409==$code) { |
||
| 202 | throw new ApiError('Wrong API key'); |
||
| 203 | } else { |
||
| 204 | throw new ApiError('Something wrong'); |
||
| 205 | } |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @param $object: JSON object |
||
| 210 | */ |
||
| 211 | public static function object2array($object) |
||
| 215 | |||
| 216 | |||
| 217 | /** |
||
| 218 | * Prints on the screen main keys and values of the array |
||
| 219 | * @param $results: object to be printed on the screen. |
||
| 220 | * @param $deepPrinting: if true, object will be printed recursively. |
||
| 221 | */ |
||
| 222 | public static function simplePrint($results, $deepPrinting = null) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Generates query or body parameters. |
||
| 279 | * @param $allFields: all known fields could be used for parameters generation. |
||
| 280 | * @param $params: input parameters (array or object) |
||
| 281 | */ |
||
| 282 | public static function generateRequestParameters($allFields, $params) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Returns an array of the object properties |
||
| 305 | * @param $object: An object. |
||
| 306 | * @param $exclude: array of the object parameters to be excluded from the returned array. |
||
| 307 | */ |
||
| 308 | public static function getObjectProperties($object, $exclude) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Returns url path generated from the array of the fields and parameters. |
||
| 325 | * @param $allFields; array of the paossible fields (parameter names). |
||
| 326 | * @param $params: input parameters (array or object). |
||
| 327 | */ |
||
| 328 | public static function generateUrlPath($allFields, $params) |
||
| 348 | } |
||
| 349 |
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.