@@ -17,8 +17,11 @@ |
||
| 17 | 17 | public static function retry($maxTimes = 2, callable $task, $sleep = 0){ |
| 18 | 18 | $tryTimes = 0; |
| 19 | 19 | while(++$tryTimes <= $maxTimes){ |
| 20 | - if($task()) break; |
|
| 21 | - else usleep(abs($sleep) * 1000000); |
|
| 20 | + if($task()) { |
|
| 21 | + break; |
|
| 22 | + } else { |
|
| 23 | + usleep(abs($sleep) * 1000000); |
|
| 24 | + } |
|
| 22 | 25 | } |
| 23 | 26 | return $tryTimes; |
| 24 | 27 | } |
@@ -225,7 +225,7 @@ discard block |
||
| 225 | 225 | return $this; |
| 226 | 226 | } |
| 227 | 227 | /** |
| 228 | - * @return mixed |
|
| 228 | + * @return callable |
|
| 229 | 229 | */ |
| 230 | 230 | public function endCallback() |
| 231 | 231 | { |
@@ -253,7 +253,7 @@ discard block |
||
| 253 | 253 | |
| 254 | 254 | /** |
| 255 | 255 | * @param $key |
| 256 | - * @return mixed |
|
| 256 | + * @return string |
|
| 257 | 257 | */ |
| 258 | 258 | protected static function fullOption($key) |
| 259 | 259 | { |
@@ -246,7 +246,9 @@ discard block |
||
| 246 | 246 | */ |
| 247 | 247 | public function getIni($field = null) |
| 248 | 248 | { |
| 249 | - if(!$field) return $this->options; |
|
| 249 | + if(!$field) { |
|
| 250 | + return $this->options; |
|
| 251 | + } |
|
| 250 | 252 | $full = self::fullOption($field); |
| 251 | 253 | return isset($this->options[$full]) ? $this->options[$full] : false; |
| 252 | 254 | } |
@@ -437,12 +439,15 @@ discard block |
||
| 437 | 439 | public function send($isMultiCurl = false) |
| 438 | 440 | { |
| 439 | 441 | try { |
| 440 | - if (!$this->hasInitialized) |
|
| 441 | - $this->applyOptions(); |
|
| 442 | + if (!$this->hasInitialized) { |
|
| 443 | + $this->applyOptions(); |
|
| 444 | + } |
|
| 442 | 445 | $response = $this->makeResponse($isMultiCurl); |
| 443 | 446 | $response->parse(); |
| 444 | 447 | } catch (\Exception $e) { |
| 445 | - if(!isset($response)) $response = Response::create($this, null, null, null, null); |
|
| 448 | + if(!isset($response)) { |
|
| 449 | + $response = Response::create($this, null, null, null, null); |
|
| 450 | + } |
|
| 446 | 451 | $response->error = $e->getMessage(); |
| 447 | 452 | $response->errorCode = 999; |
| 448 | 453 | } |
@@ -506,12 +511,19 @@ discard block |
||
| 506 | 511 | $this->serializeBody(); |
| 507 | 512 | |
| 508 | 513 | //try fix url |
| 509 | - if (strpos($this->options['url'], '://') === FALSE) $this->options['url'] = 'http://' . $this->options['url']; |
|
| 514 | + if (strpos($this->options['url'], '://') === FALSE) { |
|
| 515 | + $this->options['url'] = 'http://' . $this->options['url']; |
|
| 516 | + } |
|
| 510 | 517 | $components = parse_url($this->options['url']); |
| 511 | - if(FALSE === $components) throw new InvalidArgumentException('formatting url occurs error: '. $this->options['url']); |
|
| 518 | + if(FALSE === $components) { |
|
| 519 | + throw new InvalidArgumentException('formatting url occurs error: '. $this->options['url']); |
|
| 520 | + } |
|
| 512 | 521 | if($this->withURIQuery){ |
| 513 | - if(isset($components['query'])) $components['query'] .= '&'. trim($this->withURIQuery); |
|
| 514 | - else $components['query'] = trim($this->withURIQuery); |
|
| 522 | + if(isset($components['query'])) { |
|
| 523 | + $components['query'] .= '&'. trim($this->withURIQuery); |
|
| 524 | + } else { |
|
| 525 | + $components['query'] = trim($this->withURIQuery); |
|
| 526 | + } |
|
| 515 | 527 | } |
| 516 | 528 | $this->options['url'] = self::combineUrl($components); |
| 517 | 529 | |
@@ -553,7 +565,10 @@ discard block |
||
| 553 | 565 | } |
| 554 | 566 | |
| 555 | 567 | $cURLOptions = self::filterAndRaw($this->options); |
| 556 | - if(isset($this->body))$cURLOptions[CURLOPT_POSTFIELDS] = $this->body;//use serialized body not raw data |
|
| 568 | + if(isset($this->body)) { |
|
| 569 | + $cURLOptions[CURLOPT_POSTFIELDS] = $this->body; |
|
| 570 | + } |
|
| 571 | + //use serialized body not raw data |
|
| 557 | 572 | curl_setopt_array($this->curlHandle, $cURLOptions); |
| 558 | 573 | |
| 559 | 574 | return $this; |
@@ -566,7 +581,9 @@ discard block |
||
| 566 | 581 | $this->options[CURLOPT_POST] = true; |
| 567 | 582 | $clz = '\\MultiHttp\\Handler\\'.ucfirst($this->sendMime); |
| 568 | 583 | $inst = new $clz; |
| 569 | - if (!($inst instanceof Handler\IHandler)) throw new InvalidOperationException($clz . ' is not implement of IHandler'); |
|
| 584 | + if (!($inst instanceof Handler\IHandler)) { |
|
| 585 | + throw new InvalidOperationException($clz . ' is not implement of IHandler'); |
|
| 586 | + } |
|
| 570 | 587 | $this->body = $inst->encode($this->options['data']); |
| 571 | 588 | } |
| 572 | 589 | } |
@@ -73,10 +73,11 @@ discard block |
||
| 73 | 73 | $headers = explode(PHP_EOL, $headers); |
| 74 | 74 | array_shift($headers); // HTTP HEADER |
| 75 | 75 | foreach ($headers as $h) { |
| 76 | - if (false !== strpos($h, ':')) |
|
| 77 | - list($k, $v) = explode(':', $h, 2); |
|
| 78 | - else |
|
| 79 | - list($k, $v) = array($h, ''); |
|
| 76 | + if (false !== strpos($h, ':')) { |
|
| 77 | + list($k, $v) = explode(':', $h, 2); |
|
| 78 | + } else { |
|
| 79 | + list($k, $v) = array($h, ''); |
|
| 80 | + } |
|
| 80 | 81 | |
| 81 | 82 | $this->header[trim($k)] = trim($v); |
| 82 | 83 | } |
@@ -97,10 +98,14 @@ discard block |
||
| 97 | 98 | public function unserializeBody() |
| 98 | 99 | { |
| 99 | 100 | if (isset($this->request->expectsMime)) { |
| 100 | - if (Mime::getFullMime($this->request->expectsMime) !== $this->contentType) throw new UnexpectedResponseException('expected mime can not be matched, real mime:'. $this->contentType. ', expected mime:'. Mime::getFullMime($this->request->expectsMime)); |
|
| 101 | + if (Mime::getFullMime($this->request->expectsMime) !== $this->contentType) { |
|
| 102 | + throw new UnexpectedResponseException('expected mime can not be matched, real mime:'. $this->contentType. ', expected mime:'. Mime::getFullMime($this->request->expectsMime)); |
|
| 103 | + } |
|
| 101 | 104 | $clz = "\MultiHttp\\Handler\\".ucfirst($this->request->expectsMime); |
| 102 | 105 | $inst = new $clz; |
| 103 | - if (!($inst instanceof Handler\IHandler)) throw new InvalidOperationException($clz . ' is not implement of IHandler'); |
|
| 106 | + if (!($inst instanceof Handler\IHandler)) { |
|
| 107 | + throw new InvalidOperationException($clz . ' is not implement of IHandler'); |
|
| 108 | + } |
|
| 104 | 109 | $this->body = $inst->decode($this->body); |
| 105 | 110 | } |
| 106 | 111 | } |
@@ -34,7 +34,9 @@ discard block |
||
| 34 | 34 | public function decode($body) |
| 35 | 35 | { |
| 36 | 36 | $parsed = json_decode($body, true); |
| 37 | - if(json_last_error() !== JSON_ERROR_NONE)throw new UnexpectedResponseException('parsing json occurs error: '. self::jsonLastErrorMsg() . ', raw body: ' .$body ); |
|
| 37 | + if(json_last_error() !== JSON_ERROR_NONE) { |
|
| 38 | + throw new UnexpectedResponseException('parsing json occurs error: '. self::jsonLastErrorMsg() . ', raw body: ' .$body ); |
|
| 39 | + } |
|
| 38 | 40 | return $parsed; |
| 39 | 41 | } |
| 40 | 42 | |
@@ -42,7 +44,9 @@ discard block |
||
| 42 | 44 | * @return string |
| 43 | 45 | */ |
| 44 | 46 | private static function jsonLastErrorMsg(){ |
| 45 | - if(function_exists('json_last_error_msg')) return json_last_error_msg(); |
|
| 47 | + if(function_exists('json_last_error_msg')) { |
|
| 48 | + return json_last_error_msg(); |
|
| 49 | + } |
|
| 46 | 50 | switch (json_last_error()) { |
| 47 | 51 | case JSON_ERROR_NONE: |
| 48 | 52 | return ' - No errors'; |