| Conditions | 5 |
| Paths | 8 |
| Total Lines | 51 |
| Code Lines | 23 |
| 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 |
||
| 68 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
||
| 69 | { |
||
| 70 | // Load the routing table. |
||
| 71 | require __DIR__ . '/../../../routes/web.php'; |
||
| 72 | |||
| 73 | if ($request->getAttribute('rewrite_urls') !== '1') { |
||
| 74 | // Turn the URL into a pretty one. |
||
| 75 | $route = $request->getQueryParams()['route'] ?? ''; |
||
| 76 | $path = parse_url($request->getAttribute('base_url') . $route, PHP_URL_PATH) ?? ''; |
||
| 77 | $uri = $request->getUri()->withPath($path); |
||
| 78 | $request = $request->withUri($uri); |
||
| 79 | } |
||
| 80 | |||
| 81 | // Bind the request into the container and the layout |
||
| 82 | app()->instance(ServerRequestInterface::class, $request); |
||
| 83 | View::share('request', $request); |
||
| 84 | |||
| 85 | // Match the request to a route. |
||
| 86 | $route = $this->router_container->getMatcher()->match($request); |
||
| 87 | |||
| 88 | // No route matched? |
||
| 89 | if ($route === false) { |
||
| 90 | return $handler->handle($request); |
||
| 91 | } |
||
| 92 | |||
| 93 | // Firstly, apply the route middleware |
||
| 94 | $route_middleware = $route->extras['middleware'] ?? []; |
||
| 95 | $route_middleware = array_map('app', $route_middleware); |
||
| 96 | |||
| 97 | // Secondly, apply any module middleware |
||
| 98 | $module_middleware = $this->module_service->findByInterface(MiddlewareInterface::class)->all(); |
||
| 99 | |||
| 100 | // Add the route as attribute of the request |
||
| 101 | $request = $request->withAttribute('route', $route->name); |
||
| 102 | |||
| 103 | // Finally, run the handler using middleware |
||
| 104 | $handler_middleware = [new WrapHandler($route->handler)]; |
||
| 105 | |||
| 106 | $middleware = array_merge($route_middleware, $module_middleware, $handler_middleware); |
||
| 107 | |||
| 108 | // Add the matched attributes to the request. |
||
| 109 | foreach ($route->attributes as $key => $value) { |
||
| 110 | if ($key === 'tree') { |
||
| 111 | $value = Tree::findByName($value); |
||
| 112 | } |
||
| 113 | $request = $request->withAttribute($key, $value); |
||
| 114 | } |
||
| 115 | |||
| 116 | $dispatcher = new Dispatcher($middleware, app()); |
||
| 117 | |||
| 118 | return $dispatcher->dispatch($request); |
||
| 119 | } |
||
| 121 |