@@ -183,7 +183,7 @@ discard block |
||
| 183 | 183 | } |
| 184 | 184 | |
| 185 | 185 | /** |
| 186 | - * @return mixed |
|
| 186 | + * @return callable |
|
| 187 | 187 | */ |
| 188 | 188 | public function endCallback() |
| 189 | 189 | { |
@@ -211,7 +211,7 @@ discard block |
||
| 211 | 211 | |
| 212 | 212 | /** |
| 213 | 213 | * @param $key |
| 214 | - * @return mixed |
|
| 214 | + * @return string |
|
| 215 | 215 | */ |
| 216 | 216 | protected static function fullOption($key) |
| 217 | 217 | { |
@@ -230,7 +230,9 @@ discard block |
||
| 230 | 230 | } |
| 231 | 231 | public function expects($mime) |
| 232 | 232 | { |
| 233 | - if (empty($mime)) return $this; |
|
| 233 | + if (empty($mime)) { |
|
| 234 | + return $this; |
|
| 235 | + } |
|
| 234 | 236 | $this->expected_type = Mime::getFullMime($mime); |
| 235 | 237 | return $this; |
| 236 | 238 | } |
@@ -257,7 +259,9 @@ discard block |
||
| 257 | 259 | */ |
| 258 | 260 | public function getIni($field = null) |
| 259 | 261 | { |
| 260 | - if(!$field) return $this->options; |
|
| 262 | + if(!$field) { |
|
| 263 | + return $this->options; |
|
| 264 | + } |
|
| 261 | 265 | $full = self::fullOption($field); |
| 262 | 266 | return isset($this->options[$full]) ? $this->options[$full] : false; |
| 263 | 267 | } |
@@ -437,12 +441,15 @@ discard block |
||
| 437 | 441 | public function send($isMultiCurl = false) |
| 438 | 442 | { |
| 439 | 443 | try { |
| 440 | - if (!$this->hasInitialized) |
|
| 441 | - $this->applyOptions(); |
|
| 444 | + if (!$this->hasInitialized) { |
|
| 445 | + $this->applyOptions(); |
|
| 446 | + } |
|
| 442 | 447 | $response = $this->makeResponse($isMultiCurl); |
| 443 | 448 | $response->parse(); |
| 444 | 449 | } catch (\Exception $e) { |
| 445 | - if(!isset($response)) $response = Response::create($this, null, null, null, null); |
|
| 450 | + if(!isset($response)) { |
|
| 451 | + $response = Response::create($this, null, null, null, null); |
|
| 452 | + } |
|
| 446 | 453 | $response->error = $e->getMessage(); |
| 447 | 454 | $response->errorCode = 999; |
| 448 | 455 | } |
@@ -506,12 +513,19 @@ discard block |
||
| 506 | 513 | $this->serializeBody(); |
| 507 | 514 | |
| 508 | 515 | //try fix url |
| 509 | - if (strpos($this->options['url'], '://') === FALSE) $this->options['url'] = 'http://' . $this->options['url']; |
|
| 516 | + if (strpos($this->options['url'], '://') === FALSE) { |
|
| 517 | + $this->options['url'] = 'http://' . $this->options['url']; |
|
| 518 | + } |
|
| 510 | 519 | $components = parse_url($this->options['url']); |
| 511 | - if(FALSE === $components) throw new InvalidArgumentException('formatting url occurs error: '. $this->options['url']); |
|
| 520 | + if(FALSE === $components) { |
|
| 521 | + throw new InvalidArgumentException('formatting url occurs error: '. $this->options['url']); |
|
| 522 | + } |
|
| 512 | 523 | if($this->withURIQuery){ |
| 513 | - if(isset($components['query'])) $components['query'] .= '&'. trim($this->withURIQuery); |
|
| 514 | - else $components['query'] = trim($this->withURIQuery); |
|
| 524 | + if(isset($components['query'])) { |
|
| 525 | + $components['query'] .= '&'. trim($this->withURIQuery); |
|
| 526 | + } else { |
|
| 527 | + $components['query'] = trim($this->withURIQuery); |
|
| 528 | + } |
|
| 515 | 529 | } |
| 516 | 530 | $this->options['url'] = self::combineUrl($components); |
| 517 | 531 | |
@@ -553,7 +567,10 @@ discard block |
||
| 553 | 567 | } |
| 554 | 568 | |
| 555 | 569 | $cURLOptions = self::filterAndRaw($this->options); |
| 556 | - if(isset($this->body))$cURLOptions[CURLOPT_POSTFIELDS] = $this->body;//use serialized body not raw data |
|
| 570 | + if(isset($this->body)) { |
|
| 571 | + $cURLOptions[CURLOPT_POSTFIELDS] = $this->body; |
|
| 572 | + } |
|
| 573 | + //use serialized body not raw data |
|
| 557 | 574 | curl_setopt_array($this->curlHandle, $cURLOptions); |
| 558 | 575 | |
| 559 | 576 | return $this; |
@@ -564,7 +581,9 @@ discard block |
||
| 564 | 581 | if (isset($this->options['data'])) { |
| 565 | 582 | if (isset($this->sendMime)) { |
| 566 | 583 | $method = $this->sendMime; |
| 567 | - if (!method_exists($this, $method)) throw new InvalidOperationException($method . ' is not exists in ' . __CLASS__); |
|
| 584 | + if (!method_exists($this, $method)) { |
|
| 585 | + throw new InvalidOperationException($method . ' is not exists in ' . __CLASS__); |
|
| 586 | + } |
|
| 568 | 587 | $this->body = $this->$method($this->options['data']); |
| 569 | 588 | } else { |
| 570 | 589 | $this->body = $this->options['data']; |
@@ -51,7 +51,9 @@ discard block |
||
| 51 | 51 | public function unJson($body) |
| 52 | 52 | { |
| 53 | 53 | $parsed = json_decode($body, true); |
| 54 | - if(json_last_error() !== JSON_ERROR_NONE)throw new UnexpectedResponseException('parsing json occurs error: '. self::jsonLastErrorMsg() . ', raw body: ' .$body ); |
|
| 54 | + if(json_last_error() !== JSON_ERROR_NONE) { |
|
| 55 | + throw new UnexpectedResponseException('parsing json occurs error: '. self::jsonLastErrorMsg() . ', raw body: ' .$body ); |
|
| 56 | + } |
|
| 55 | 57 | return $parsed; |
| 56 | 58 | } |
| 57 | 59 | |
@@ -59,7 +61,9 @@ discard block |
||
| 59 | 61 | * @return string |
| 60 | 62 | */ |
| 61 | 63 | private static function jsonLastErrorMsg(){ |
| 62 | - if(function_exists('json_last_error_msg')) return json_last_error_msg(); |
|
| 64 | + if(function_exists('json_last_error_msg')) { |
|
| 65 | + return json_last_error_msg(); |
|
| 66 | + } |
|
| 63 | 67 | switch (json_last_error()) { |
| 64 | 68 | case JSON_ERROR_NONE: |
| 65 | 69 | return ' - No errors'; |
@@ -72,10 +72,11 @@ discard block |
||
| 72 | 72 | $headers = explode(PHP_EOL, $headers); |
| 73 | 73 | array_shift($headers); // HTTP HEADER |
| 74 | 74 | foreach ($headers as $h) { |
| 75 | - if (false !== strpos($h, ':')) |
|
| 76 | - list($k, $v) = explode(':', $h, 2); |
|
| 77 | - else |
|
| 78 | - list($k, $v) = array($h, ''); |
|
| 75 | + if (false !== strpos($h, ':')) { |
|
| 76 | + list($k, $v) = explode(':', $h, 2); |
|
| 77 | + } else { |
|
| 78 | + list($k, $v) = array($h, ''); |
|
| 79 | + } |
|
| 79 | 80 | |
| 80 | 81 | $this->header[trim($k)] = trim($v); |
| 81 | 82 | } |
@@ -96,9 +97,13 @@ discard block |
||
| 96 | 97 | public function unserializeBody() |
| 97 | 98 | { |
| 98 | 99 | if (isset($this->request->expectedMime)) { |
| 99 | - if (Mime::getFullMime($this->request->expectedMime) !== $this->contentType) throw new UnexpectedResponseException('expected mime can not be matched, real mime:'. $this->contentType. ', expected mime:'. Mime::getFullMime($this->request->expectedMime)); |
|
| 100 | + if (Mime::getFullMime($this->request->expectedMime) !== $this->contentType) { |
|
| 101 | + throw new UnexpectedResponseException('expected mime can not be matched, real mime:'. $this->contentType. ', expected mime:'. Mime::getFullMime($this->request->expectedMime)); |
|
| 102 | + } |
|
| 100 | 103 | $method = 'un'.ucfirst($this->request->expectedMime); |
| 101 | - if (!method_exists($this->request, $method)) throw new InvalidOperationException($method . ' is not exists in ' . __CLASS__); |
|
| 104 | + if (!method_exists($this->request, $method)) { |
|
| 105 | + throw new InvalidOperationException($method . ' is not exists in ' . __CLASS__); |
|
| 106 | + } |
|
| 102 | 107 | $this->body = $this->request->{$method}($this->body); |
| 103 | 108 | } |
| 104 | 109 | } |
@@ -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 | } |