| Conditions | 7 |
| Paths | 10 |
| Total Lines | 69 |
| Code Lines | 43 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 8 | ||
| Bugs | 0 | Features | 4 |
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 |
||
| 26 | public function renderDefault() |
||
|
|
|||
| 27 | { |
||
| 28 | $start = microtime(true); |
||
| 29 | |||
| 30 | $this->getHttpResponse()->addHeader('Access-Control-Allow-Origin', '*'); |
||
| 31 | |||
| 32 | $logger = null; |
||
| 33 | if ($this->context->hasService('apiLogger')) { |
||
| 34 | $logger = $this->context->getService('apiLogger'); |
||
| 35 | } |
||
| 36 | |||
| 37 | // get handler |
||
| 38 | $hand = $this->apiDecider->getApiHandler( |
||
| 39 | $this->request->getMethod(), |
||
| 40 | $this->params['version'], |
||
| 41 | $this->params['package'], |
||
| 42 | $this->params['apiAction'] |
||
| 43 | ); |
||
| 44 | $handler = $hand['handler']; |
||
| 45 | $authorization = $hand['authorization']; |
||
| 46 | |||
| 47 | // check authorization |
||
| 48 | if (!$authorization->authorized()) { |
||
| 49 | $this->getHttpResponse()->setCode(Response::S500_INTERNAL_SERVER_ERROR); |
||
| 50 | $this->sendResponse(new JsonResponse(['status' => 'error', 'message' => $authorization->getErrorMessage()])); |
||
| 51 | return; |
||
| 52 | } |
||
| 53 | |||
| 54 | // process params |
||
| 55 | $paramsProcessor = new ParamsProcessor($handler->params()); |
||
| 56 | if ($paramsProcessor->isError()) { |
||
| 57 | $this->getHttpResponse()->setCode(Response::S500_INTERNAL_SERVER_ERROR); |
||
| 58 | $this->sendResponse(new JsonResponse(['status' => 'error', 'message' => 'wrong input'])); |
||
| 59 | return; |
||
| 60 | } |
||
| 61 | $params = $paramsProcessor->getValues(); |
||
| 62 | |||
| 63 | // process handler |
||
| 64 | $response = $handler->handle($params); |
||
| 65 | $code = $response->getCode(); |
||
| 66 | |||
| 67 | $end = microtime(true); |
||
| 68 | |||
| 69 | if ($logger) { |
||
| 70 | $headers = []; |
||
| 71 | if (function_exists('getallheaders')) { |
||
| 72 | $headers = getallheaders(); |
||
| 73 | } |
||
| 74 | |||
| 75 | $requestHeaders = ''; |
||
| 76 | foreach ($headers as $key => $value) { |
||
| 77 | $requestHeaders .= "$key: $value\n"; |
||
| 78 | } |
||
| 79 | |||
| 80 | $logger->log( |
||
| 81 | $code, |
||
| 82 | $this->request->getMethod(), |
||
| 83 | $requestHeaders, |
||
| 84 | $_SERVER['REQUEST_URI'], |
||
| 85 | $this->ipDetector->getRequestIp(), |
||
| 86 | isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null, |
||
| 87 | ($end-$start) * 1000 |
||
| 88 | ); |
||
| 89 | } |
||
| 90 | |||
| 91 | // output to nette |
||
| 92 | $this->getHttpResponse()->setCode($code); |
||
| 93 | $this->sendResponse($response); |
||
| 94 | } |
||
| 95 | } |
||
| 96 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: