| Conditions | 11 |
| Paths | 176 |
| Total Lines | 24 |
| Code Lines | 19 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 21 | public function dispatch() |
||
| 22 | { |
||
| 23 | $requestUri = $_SERVER['REQUEST_URI']; |
||
| 24 | $verb = isset($_SERVER['REQUEST_METHOD']) ? strtolower($_SERVER['REQUEST_METHOD']) : 'get'; |
||
| 25 | $appendUri = strpos($requestUri, '?'); |
||
| 26 | $query = substr($requestUri, 0, $appendUri === false ? strlen($requestUri) : $appendUri); |
||
| 27 | $parts = explode('/', preg_replace('~^' . Basepath::get() . '~', '', $query)); |
||
| 28 | $action = count($parts) >= 2 ? array_pop($parts) : 'index'; |
||
| 29 | $controllerName = isset($parts[0]) && $parts[0] ? implode($parts, '\\') : 'index'; |
||
| 30 | $controller = $this->projectNamespace . $this->controllerPackage . '\\' . ucfirst($controllerName); |
||
| 31 | if (!class_exists($controller)) { |
||
| 32 | throw new \Exception('controller ' . $controllerName . ' not found'); |
||
| 33 | }; |
||
| 34 | $controller = new $controller; |
||
| 35 | $finalAction = $verb . ($action ?: 'index') . $this->controllerActionSuffix; |
||
| 36 | if (is_callable(array($controller, $finalAction))) { |
||
| 37 | return $controller->$finalAction(); |
||
| 38 | } |
||
| 39 | $finalAction = ($action ?: 'index') . $this->controllerActionSuffix; |
||
| 40 | if (!is_callable(array($controller, $finalAction))) { |
||
| 41 | throw new \Exception('action ' . $action . ' not found in controller ' . $controllerName); |
||
| 42 | } |
||
| 43 | return $controller->$finalAction(); |
||
| 44 | } |
||
| 45 | |||
| 81 |