| Conditions | 49 |
| Paths | > 20000 |
| Total Lines | 210 |
| Code Lines | 149 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 147 | private function invoke($method, $args = array(), $holder, $location = NULL) |
||
| 148 | { |
||
| 149 | $api = API::$API[$method]; |
||
| 150 | if (!$api) { |
||
| 151 | throw new Ks3ClientException($method . " Not Found API"); |
||
| 152 | } |
||
| 153 | if (count($args) !== 0) { |
||
| 154 | if (count($args) > 1 || !is_array($args[0])) { |
||
| 155 | throw new Ks3ClientException("this method only needs one array argument"); |
||
| 156 | } |
||
| 157 | $args = $args[0]; |
||
| 158 | } |
||
| 159 | if (isset($api["redirect"])) { |
||
| 160 | $api = API::$API[$api["redirect"]]; |
||
| 161 | } |
||
| 162 | $request = new Ks3Request(); |
||
| 163 | if (empty($args["Bucket"])) { |
||
| 164 | if ($api["needBucket"]) { |
||
| 165 | throw new Ks3ClientException($method . " this api need bucket"); |
||
| 166 | } |
||
| 167 | } else { |
||
| 168 | $request->bucket = $args["Bucket"]; |
||
| 169 | } |
||
| 170 | $position = $api["objectPostion"] ?? "Key"; |
||
| 171 | if (empty($args[$position])) { |
||
| 172 | if ($api["needObject"]) { |
||
| 173 | throw new Ks3ClientException($method . " this api need " . $position); |
||
| 174 | } |
||
| 175 | } else { |
||
| 176 | $key = $args[$position]; |
||
| 177 | $preEncoding = mb_detect_encoding($key, array("ASCII", "UTF-8", "GB2312", "GBK", "BIG5")); |
||
| 178 | $holder->msg .= "key encoding " . $preEncoding . "\r\n"; |
||
| 179 | if (strtolower($preEncoding) !== "utf-8") { |
||
| 180 | $key = iconv($preEncoding, "UTF-8", $key); |
||
| 181 | } |
||
| 182 | $request->key = $key; |
||
| 183 | } |
||
| 184 | $method = $api["method"]; |
||
| 185 | if ($method === "Method") { |
||
| 186 | if (empty($args["Method"])) { |
||
| 187 | $request->method = "GET"; |
||
| 188 | } else { |
||
| 189 | $request->method = $args["Method"]; |
||
| 190 | } |
||
| 191 | } else { |
||
| 192 | $request->method = $api["method"]; |
||
| 193 | } |
||
| 194 | if (KS3_API_USE_HTTPS) { |
||
| 195 | $request->scheme = "https://"; |
||
| 196 | } |
||
| 197 | else { |
||
| 198 | $request->scheme = "http://"; |
||
| 199 | } |
||
| 200 | $request->endpoint = $this->endpoint; |
||
| 201 | //add subresource |
||
| 202 | if (!empty($api["subResource"])) { |
||
| 203 | $request->subResource = $api["subResource"]; |
||
| 204 | } |
||
| 205 | //add query params |
||
| 206 | if (isset($api["queryParams"])) { |
||
| 207 | foreach ($api["queryParams"] as $key => $value) { |
||
| 208 | $required = FALSE; |
||
| 209 | if (strpos($value, "!") === 0) { |
||
| 210 | $required = TRUE; |
||
| 211 | $value = substr($value, 1); |
||
| 212 | } |
||
| 213 | $index = explode("->", $value); |
||
| 214 | $curIndexArg = $args; |
||
| 215 | $add = TRUE; |
||
| 216 | $curkey = ""; |
||
| 217 | foreach ($index as $key1 => $value1) { |
||
| 218 | if (!isset($curIndexArg[$value1]) && $value1 !== "*") { |
||
| 219 | $add = FALSE; |
||
| 220 | } else { |
||
| 221 | $curkey = $value1; |
||
| 222 | //星号表示所有,按照暂时的业务,默认星号后面就没了 |
||
| 223 | if ($curkey === "*") { |
||
| 224 | foreach ($curIndexArg as $queryK => $queryV) { |
||
| 225 | if (!is_array($queryV)) { |
||
| 226 | $request->addQueryParams($queryK, $queryV); |
||
| 227 | } |
||
| 228 | } |
||
| 229 | $add = FALSE; |
||
| 230 | $required = FALSE; |
||
| 231 | break; |
||
| 232 | } |
||
| 233 | |||
| 234 | $curIndexArg = $curIndexArg[$value1]; |
||
| 235 | } |
||
| 236 | } |
||
| 237 | if (!empty($curIndexArg) && $add) { |
||
| 238 | $request->addQueryParams($curkey, $curIndexArg); |
||
| 239 | continue; |
||
| 240 | } |
||
| 241 | if ($required) { |
||
| 242 | throw new Ks3ClientException($method . " param " . $value . " is required"); |
||
| 243 | } |
||
| 244 | } |
||
| 245 | } |
||
| 246 | if (isset($api["body"])) { |
||
| 247 | if (isset($api["body"]["builder"])) { |
||
| 248 | $builderName = $api["body"]["builder"]; |
||
| 249 | $builder = new $builderName(); |
||
| 250 | $request->body = $builder->build($args); |
||
| 251 | } else if (isset($api["body"]["position"])) { |
||
| 252 | $position = $api["body"]["position"]; |
||
| 253 | $index = explode("->", $position); |
||
| 254 | $curIndexArg = $args; |
||
| 255 | $add = TRUE; |
||
| 256 | $curkey = ""; |
||
| 257 | foreach ($index as $key1 => $value1) { |
||
| 258 | if (!isset($curIndexArg[$value1])) { |
||
| 259 | $add = FALSE; |
||
| 260 | } else { |
||
| 261 | $curIndexArg = $curIndexArg[$value1]; |
||
| 262 | $curkey = $value1; |
||
| 263 | } |
||
| 264 | } |
||
| 265 | if (!empty($curIndexArg) && $add) { |
||
| 266 | $request->body = $curIndexArg; |
||
| 267 | } |
||
| 268 | } |
||
| 269 | } |
||
| 270 | |||
| 271 | //add ext headers |
||
| 272 | //TODO |
||
| 273 | //sign request |
||
| 274 | $signer = NULL; |
||
| 275 | if (isset($api["signer"])) { |
||
| 276 | $signers = explode("->", $api["signer"]); |
||
| 277 | foreach ($signers as $key => $value) { |
||
| 278 | $signer = new $value(); |
||
| 279 | $log = $signer->sign($request, array("accessKey" => $this->accessKey, "secretKey" => $this->secretKey, "args" => $args)); |
||
| 280 | if (!empty($log)) { |
||
| 281 | $holder->msg .= $log . "\r\n"; |
||
| 282 | } |
||
| 283 | } |
||
| 284 | } |
||
| 285 | |||
| 286 | if ($signer === NULL || !($signer instanceof QueryAuthSigner)) { |
||
| 287 | $url = $request->toUrl($this->endpoint); |
||
| 288 | if ($location != NULL) { |
||
| 289 | $url = $location; |
||
| 290 | } |
||
| 291 | $httpRequest = new RequestCore($url); |
||
| 292 | if (KS3_API_DEBUG_MODE === TRUE) { |
||
| 293 | $httpRequest->debug_mode = TRUE; |
||
| 294 | } |
||
| 295 | $httpRequest->set_method($request->method); |
||
| 296 | foreach ($request->headers as $key => $value) { |
||
| 297 | $httpRequest->add_header($key, $value); |
||
| 298 | } |
||
| 299 | $httpRequest->request_body = $request->body; |
||
| 300 | |||
| 301 | if (isset($args["writeCallBack"])) { |
||
| 302 | $httpRequest->register_streaming_write_callback($args["writeCallBack"]); |
||
| 303 | } |
||
| 304 | if (isset($args["readCallBack"])) { |
||
| 305 | $httpRequest->register_streaming_read_callback($args["readCallBack"]); |
||
| 306 | } |
||
| 307 | |||
| 308 | $read_stream = $request->read_stream; |
||
| 309 | $read_length = $request->getHeader(Headers::$ContentLength); |
||
| 310 | $seek_position = $request->seek_position; |
||
| 311 | if (isset($read_stream)) { |
||
| 312 | $httpRequest->set_read_stream($read_stream, $read_length); |
||
| 313 | $httpRequest->set_seek_position($seek_position); |
||
| 314 | $httpRequest->remove_header(Headers::$ContentLength); |
||
| 315 | } |
||
| 316 | $write_stream = $request->write_stream; |
||
| 317 | if (isset($write_stream)) { |
||
| 318 | $httpRequest->set_write_stream($write_stream); |
||
| 319 | } |
||
| 320 | |||
| 321 | $holder->msg .= "request url->" . serialize($httpRequest->request_url) . "\r\n"; |
||
| 322 | $holder->msg .= "request headers->" . serialize($httpRequest->request_headers) . "\r\n"; |
||
| 323 | $holder->msg .= "request body->" . $httpRequest->request_body . "\r\n"; |
||
| 324 | $holder->msg .= "request read stream length->" . $read_length . "\r\n"; |
||
| 325 | $holder->msg .= "request read stream seek position->" . $seek_position . "\r\n"; |
||
| 326 | $httpRequest->send_request(); |
||
| 327 | //print_r($httpRequest); |
||
| 328 | $body = $httpRequest->get_response_body(); |
||
| 329 | $data = new ResponseCore ($httpRequest->get_response_header(), Utils::replaceNS2($body), $httpRequest->get_response_code()); |
||
| 330 | |||
| 331 | if ($data->status == 307) { |
||
| 332 | $respHeaders = $httpRequest->get_response_header(); |
||
| 333 | $location = $respHeaders["location"]; |
||
| 334 | if (strpos($location, "http") === 0) { |
||
| 335 | $holder->msg .= "response code->" . $httpRequest->get_response_code() . "\r\n"; |
||
| 336 | $holder->msg .= "response headers->" . serialize($httpRequest->get_response_header()) . "\r\n"; |
||
| 337 | $holder->msg .= "response body->" . $body . "\r\n"; |
||
| 338 | $holder->msg .= "retry request to " . $location . "\r\n"; |
||
| 339 | //array($args)详见invoke开头 |
||
| 340 | return $this->invoke($method, array($args), $holder, $location); |
||
| 341 | } |
||
| 342 | } |
||
| 343 | $holder->msg .= "response code->" . $httpRequest->get_response_code() . "\r\n"; |
||
| 344 | $holder->msg .= "response headers->" . serialize($httpRequest->get_response_header()) . "\r\n"; |
||
| 345 | $holder->msg .= "response body->" . $body . "\r\n"; |
||
| 346 | $handlers = explode("->", $api["handler"]); |
||
| 347 | foreach ($handlers as $key => $value) { |
||
| 348 | $handler = new $value(); |
||
| 349 | $data = $handler->handle($data); |
||
| 350 | } |
||
| 351 | return $data; |
||
| 352 | } |
||
| 353 | |||
| 354 | $url = $request->toUrl($this->endpoint); |
||
| 355 | $holder->msg .= $url . "\r\n"; |
||
| 356 | return $url; |
||
| 357 | } |
||
| 392 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.