| Conditions | 10 |
| Paths | 24 |
| Total Lines | 45 |
| Code Lines | 32 |
| Lines | 13 |
| Ratio | 28.89 % |
| Changes | 3 | ||
| 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 |
||
| 29 | public function __invoke(HandlerContext $context) |
||
| 30 | { |
||
| 31 | $callback = $context->annotations()->getCallback(); |
||
| 32 | |||
| 33 | $context->body()->add('$request = new \GuzzleHttp\Psr7\Request("%s", $requestUrl, $headers, $body);', strtoupper($context->annotations()->getRequestMethod())); |
||
| 34 | $context->body()->add('$beforeSendEvent = new \Tebru\Retrofit\Event\BeforeSendEvent($request);'); |
||
| 35 | $context->body()->add('$this->eventDispatcher->dispatch("retrofit.beforeSend", $beforeSendEvent);'); |
||
| 36 | $context->body()->add('$request = $beforeSendEvent->getRequest();'); |
||
| 37 | $context->body()->add('try {'); |
||
| 38 | |||
| 39 | View Code Duplication | if (null !== $callback) { |
|
| 40 | if ($context->annotations()->isCallbackOptional()) { |
||
| 41 | $context->body()->add('if (%s !== null) {', $callback); |
||
| 42 | $context->body()->add('$response = $this->client->sendAsync($request, %s);', $callback); |
||
| 43 | $context->body()->add('} else {'); |
||
| 44 | $context->body()->add('$response = $this->client->send($request);'); |
||
| 45 | $context->body()->add('}'); |
||
| 46 | } else { |
||
| 47 | $context->body()->add('$response = $this->client->sendAsync($request, %s);', $callback); |
||
| 48 | } |
||
| 49 | } else { |
||
| 50 | $context->body()->add('$response = $this->client->send($request);'); |
||
| 51 | } |
||
| 52 | |||
| 53 | $context->body()->add('} catch (\Exception $exception) {'); |
||
| 54 | $context->body()->add('$apiExceptionEvent = new \Tebru\Retrofit\Event\ApiExceptionEvent($exception, $request);'); |
||
| 55 | $context->body()->add('$this->eventDispatcher->dispatch("retrofit.apiException", $apiExceptionEvent);'); |
||
| 56 | $context->body()->add('$exception = $apiExceptionEvent->getException();'); |
||
| 57 | $context->body()->add('throw new \Tebru\Retrofit\Exception\RetrofitApiException(get_class($this), $exception->getMessage(), $exception->getCode(), $exception);'); |
||
| 58 | $context->body()->add('}'); |
||
| 59 | |||
| 60 | if (null !== $callback && $context->annotations()->isCallbackOptional()) { |
||
| 61 | $context->body()->add('if (%s !== null) {', $callback); |
||
| 62 | } |
||
| 63 | |||
| 64 | if (null === $callback || (null !== $callback && $context->annotations()->isCallbackOptional())) { |
||
| 65 | $context->body()->add('$afterSendEvent = new \Tebru\Retrofit\Event\AfterSendEvent($request, $response);'); |
||
| 66 | $context->body()->add('$this->eventDispatcher->dispatch("retrofit.afterSend", $afterSendEvent);'); |
||
| 67 | $context->body()->add('$response = $afterSendEvent->getResponse();'); |
||
| 68 | } |
||
| 69 | |||
| 70 | if (null !== $callback && $context->annotations()->isCallbackOptional()) { |
||
| 71 | $context->body()->add('}'); |
||
| 72 | } |
||
| 73 | } |
||
| 74 | } |
||
| 75 |