| Conditions | 10 |
| Paths | 136 |
| Total Lines | 74 |
| Code Lines | 41 |
| 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 |
||
| 71 | public function run(Request $request) |
||
| 72 | { |
||
| 73 | try { |
||
| 74 | // before hooks |
||
| 75 | $hookData = []; |
||
| 76 | foreach ($this->beforeHooks as $k => $v) { |
||
| 77 | $hookResponse = $v->executeBefore($request, $hookData); |
||
| 78 | // if we get back a Response object, return it immediately |
||
| 79 | if ($hookResponse instanceof Response) { |
||
| 80 | // run afterHooks |
||
| 81 | return $this->runAfterHooks($request, $hookResponse); |
||
| 82 | } |
||
| 83 | |||
| 84 | $hookData[$k] = $hookResponse; |
||
| 85 | } |
||
| 86 | |||
| 87 | $requestMethod = $request->getRequestMethod(); |
||
| 88 | $pathInfo = $request->getPathInfo(); |
||
| 89 | |||
| 90 | if (!array_key_exists($requestMethod, $this->routes)) { |
||
| 91 | throw new HttpException( |
||
| 92 | sprintf('method "%s" not allowed', $requestMethod), |
||
| 93 | 405, |
||
| 94 | ['Allow' => implode(',', array_keys($this->routes))] |
||
| 95 | ); |
||
| 96 | } |
||
| 97 | if (!array_key_exists($pathInfo, $this->routes[$requestMethod])) { |
||
| 98 | throw new HttpException( |
||
| 99 | sprintf('"%s" not found', $pathInfo), |
||
| 100 | 404 |
||
| 101 | ); |
||
| 102 | } |
||
| 103 | |||
| 104 | $response = $this->routes[$requestMethod][$pathInfo]($request, $hookData); |
||
| 105 | |||
| 106 | // after hooks |
||
| 107 | return $this->runAfterHooks($request, $response); |
||
| 108 | } catch (InputValidationException $e) { |
||
| 109 | // in case an InputValidationException is encountered, convert it |
||
| 110 | // into a Bad Request HTTP response |
||
| 111 | throw new HttpException($e->getMessage(), 400); |
||
| 112 | } catch (HttpException $e) { |
||
| 113 | if ($request->isBrowser()) { |
||
| 114 | if (is_null($this->tpl)) { |
||
| 115 | // template not available |
||
| 116 | $response = new Response($e->getCode(), 'text/plain'); |
||
| 117 | $response->setBody(sprintf('%d: %s', $e->getCode(), $e->getMessage())); |
||
| 118 | } else { |
||
| 119 | // template available |
||
| 120 | $response = new Response($e->getCode(), 'text/html'); |
||
| 121 | $response->setBody( |
||
| 122 | $this->tpl->render( |
||
| 123 | 'errorPage', |
||
| 124 | [ |
||
| 125 | 'code' => $e->getCode(), |
||
| 126 | 'message' => $e->getMessage(), |
||
| 127 | ] |
||
| 128 | ) |
||
| 129 | ); |
||
| 130 | } |
||
| 131 | } else { |
||
| 132 | // not a browser |
||
| 133 | $response = new Response($e->getCode(), 'application/json'); |
||
| 134 | $response->setBody(json_encode(['error' => $e->getMessage()])); |
||
| 135 | } |
||
| 136 | |||
| 137 | foreach ($e->getResponseHeaders() as $key => $value) { |
||
| 138 | $response->addHeader($key, $value); |
||
| 139 | } |
||
| 140 | |||
| 141 | // after hooks |
||
| 142 | return $this->runAfterHooks($request, $response); |
||
| 143 | } |
||
| 144 | } |
||
| 145 | |||
| 155 |