| Conditions | 7 |
| Paths | 8 |
| Total Lines | 55 |
| Code Lines | 28 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 22 | public function route(RequestInterface $request) |
||
| 23 | { |
||
| 24 | if (!is_null($this->cachePath)) { |
||
| 25 | $dispatcher = \FastRoute\cachedDispatcher( |
||
| 26 | function(RouteCollector $collector) { |
||
| 27 | foreach ($this->routes as $route) { |
||
| 28 | $collector->addRoute( |
||
| 29 | $route->getRequestType(), |
||
| 30 | $route->getUri(), |
||
| 31 | serialize($route) |
||
| 32 | ); |
||
| 33 | } |
||
| 34 | }, [ |
||
| 35 | 'cacheFile' => $this->cachePath |
||
| 36 | ] |
||
| 37 | ); |
||
| 38 | } else { |
||
| 39 | // Add routes to the route collector. |
||
| 40 | $dispatcher = \FastRoute\simpleDispatcher( |
||
| 41 | function (RouteCollector $collector) { |
||
| 42 | foreach ($this->routes as $route) { |
||
| 43 | $collector->addRoute( |
||
| 44 | $route->getRequestType(), |
||
| 45 | $route->getUri(), |
||
| 46 | serialize($route) |
||
| 47 | ); |
||
| 48 | } |
||
| 49 | } |
||
| 50 | ); |
||
| 51 | } |
||
| 52 | |||
| 53 | // Dispatch the route. |
||
| 54 | $route_info = $dispatcher->dispatch($request->getMethod(), $request->getUriPath()); |
||
| 55 | |||
| 56 | // Handle the route depending on the response type. |
||
| 57 | switch ($route_info[0]) { |
||
| 58 | // Route not found. |
||
| 59 | case Dispatcher::NOT_FOUND: |
||
| 60 | |||
| 61 | throw new OutOfRangeException("No route matched the given URI."); |
||
| 62 | |||
| 63 | // Method not allowed for the specified route. |
||
| 64 | case Dispatcher::METHOD_NOT_ALLOWED: |
||
| 65 | |||
| 66 | throw new Exception("Method not allowed."); |
||
| 67 | |||
| 68 | // Route found. |
||
| 69 | case Dispatcher::FOUND: |
||
| 70 | |||
| 71 | $route = $route_info[1]; |
||
| 72 | $params = $route_info[2]; |
||
| 73 | |||
| 74 | return new ResolvedRequest($request, unserialize($route), $params); |
||
| 75 | } |
||
| 76 | } |
||
| 77 | } |
||
| 78 |