| Conditions | 11 |
| Paths | 97 |
| Total Lines | 58 |
| Code Lines | 30 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 8 | ||
| 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 | $dispatcher->setControllerName(isset($parts[0]) ? $parts[0] : 'index'); |
||
| 133 | |||
| 134 | if ($dispatcher->isValidController()) { |
||
| 135 | |||
| 136 | $dispatcher->setActionName(isset($parts[1]) ? $parts[1] : 'index'); |
||
| 137 | |||
| 138 | $params = []; |
||
| 139 | if (isset($parts[2])) { |
||
| 140 | $params = $parts; |
||
| 141 | array_shift($params); |
||
| 142 | array_shift($params); |
||
| 143 | } |
||
| 144 | $dispatcher->setParams($params); |
||
| 145 | |||
| 146 | if ($dispatcher->isCallable()) { |
||
| 147 | return $dispatcher->dispatch(); |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 151 | // Use the "catch-all" route |
||
| 152 | if ($this->defaultRoute) { |
||
| 153 | return $this->defaultRoute->handle(); |
||
| 154 | } |
||
| 155 | |||
| 156 | // No route was matched |
||
| 157 | $this->handleInternal('404'); |
||
| 158 | |||
| 159 | } catch (\Exception $e) { |
||
| 160 | |||
| 161 | // Exception codes can match a route for a http status code |
||
| 162 | $code = $e->getCode(); |
||
| 163 | $statusCodes = [403, 404, 500]; |
||
| 164 | if (in_array($code, $statusCodes)) { |
||
| 165 | |||
| 166 | $this->di->flash->setMessage($e->getMessage()); |
||
| 167 | $this->handleInternal($code); |
||
| 168 | |||
| 169 | } else { |
||
| 170 | throw $e; |
||
| 171 | } |
||
| 172 | } |
||
| 173 | } |
||
| 174 | } |
||
| 175 |