| Conditions | 10 |
| Paths | 50 |
| Total Lines | 50 |
| Code Lines | 34 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 38 | public function run(Request $request) |
||
| 39 | { |
||
| 40 | try { |
||
| 41 | switch ($request->getRequestMethod()) { |
||
| 42 | case 'GET': |
||
| 43 | return $this->handleGet($request); |
||
| 44 | case 'POST': |
||
| 45 | return $this->handlePost($request); |
||
| 46 | case 'PUT': |
||
| 47 | $tokenInfo = $this->bearerAuth->requireAuth($request); |
||
| 48 | |||
| 49 | return $this->apiModule->put($request, $tokenInfo); |
||
| 50 | case 'DELETE': |
||
| 51 | $tokenInfo = $this->bearerAuth->requireAuth($request); |
||
| 52 | |||
| 53 | return $this->apiModule->delete($request, $tokenInfo); |
||
| 54 | case 'OPTIONS': |
||
| 55 | return $this->apiModule->options($request); |
||
| 56 | case 'HEAD': |
||
| 57 | $tokenInfo = $this->bearerAuth->optionalAuth($request); |
||
| 58 | |||
| 59 | return $this->apiModule->head($request, $tokenInfo); |
||
| 60 | default: |
||
| 61 | throw new HttpException('method not allowed', 405); |
||
| 62 | } |
||
| 63 | } catch (HttpException $e) { |
||
| 64 | if ($request->isBrowser()) { |
||
| 65 | $response = new Response($e->getCode(), 'text/html'); |
||
| 66 | $response->setBody( |
||
| 67 | $this->tpl->render( |
||
| 68 | 'errorPage', |
||
| 69 | [ |
||
| 70 | 'code' => $e->getCode(), |
||
| 71 | 'message' => $e->getMessage(), |
||
| 72 | ] |
||
| 73 | ) |
||
| 74 | ); |
||
| 75 | } else { |
||
| 76 | // not a browser |
||
| 77 | $response = new Response($e->getCode(), 'application/json'); |
||
| 78 | $response->setBody(json_encode(['error' => $e->getMessage()])); |
||
| 79 | } |
||
| 80 | |||
| 81 | foreach ($e->getResponseHeaders() as $key => $value) { |
||
| 82 | $response->addHeader($key, $value); |
||
| 83 | } |
||
| 84 | |||
| 85 | return $response; |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 140 |
According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.
}
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.