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