Conditions | 14 |
Paths | 735 |
Total Lines | 82 |
Code Lines | 39 |
Lines | 0 |
Ratio | 0 % |
Changes | 9 | ||
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 |
||
116 | public function handle() |
||
117 | { |
||
118 | try { |
||
119 | |||
120 | $query = $this->di->request->getRoute(); |
||
121 | $parts = $this->di->request->getRouteParts(); |
||
122 | |||
123 | // Match predefined routes |
||
124 | foreach ($this->routes as $route) { |
||
125 | if ($route->match($query)) { |
||
126 | return $route->handle(); |
||
127 | } |
||
128 | } |
||
129 | |||
130 | // Default handling route as :controller/:action/:params using the dispatcher |
||
131 | $dispatcher = $this->di->dispatcher; |
||
132 | |||
133 | $dispatcher->setControllerName(); |
||
134 | |||
135 | // Checks if the first part is a valid controller. |
||
136 | if (isset($parts[0])) { |
||
137 | $dispatcher->setControllerName($parts[0]); |
||
138 | |||
139 | // If not valid then ignore the first param and sets the default. |
||
140 | if ($dispatcher->isValidController()) { |
||
141 | array_shift($parts); |
||
142 | } else { |
||
143 | $dispatcher->setControllerName(); |
||
144 | } |
||
145 | } |
||
146 | |||
147 | if ($dispatcher->isValidController()) { |
||
148 | |||
149 | // Checks if a action is set. If not then will use index action. |
||
150 | $dispatcher->setActionName(); |
||
151 | |||
152 | if (isset($parts[0])) { |
||
153 | $dispatcher->setActionName($parts[0]); |
||
154 | |||
155 | if ($dispatcher->isCallable()) { |
||
156 | array_shift($parts); |
||
157 | } else { |
||
158 | $dispatcher->setActionName(); |
||
159 | } |
||
160 | } |
||
161 | |||
162 | if ($dispatcher->isCallable()) { |
||
163 | |||
164 | $parts = !empty($parts) ? $parts : []; |
||
165 | $dispatcher->setParams($parts); |
||
166 | |||
167 | // Checks if the are correct number of params that the action accepts |
||
168 | if ($dispatcher->isParamsValid()) { |
||
169 | return $dispatcher->dispatch(); |
||
170 | } |
||
171 | } |
||
172 | |||
173 | } |
||
174 | |||
175 | // Use the "catch-all" route |
||
176 | if ($this->defaultRoute) { |
||
177 | return $this->defaultRoute->handle(); |
||
178 | } |
||
179 | |||
180 | // No route was matched |
||
181 | $this->handleInternal('404'); |
||
182 | |||
183 | } catch (\Exception $e) { |
||
184 | |||
185 | // Exception codes can match a route for a http status code |
||
186 | $code = $e->getCode(); |
||
187 | $statusCodes = [403, 404, 500]; |
||
188 | if (in_array($code, $statusCodes)) { |
||
189 | |||
190 | $this->di->flash->setMessage($e->getMessage()); |
||
191 | $this->handleInternal($code); |
||
192 | |||
193 | } else { |
||
194 | throw $e; |
||
195 | } |
||
196 | } |
||
197 | } |
||
198 | } |
||
199 |