Conditions | 7 |
Paths | 10 |
Total Lines | 65 |
Code Lines | 30 |
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 |
||
70 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
||
71 | { |
||
72 | // Turn the ugly URL into a pretty one, so the router can parse it. |
||
73 | $pretty = $request; |
||
74 | |||
75 | if ($request->getAttribute('rewrite_urls') !== '1') { |
||
76 | // Ugly URLs store the path in a query parameter. |
||
77 | $url_route = $request->getQueryParams()['route'] ?? ''; |
||
78 | $uri = $request->getUri()->withPath($url_route); |
||
79 | $pretty = $request->withUri($uri); |
||
80 | } |
||
81 | |||
82 | // Match the request to a route. |
||
83 | $route = $this->router_container->getMatcher()->match($pretty); |
||
84 | |||
85 | // No route matched? Let the default handler take care of it |
||
86 | if ($route === false) { |
||
87 | return $handler->handle($request); |
||
88 | } |
||
89 | |||
90 | // Add the route as attribute of the request |
||
91 | $request = $request->withAttribute('route', $route); |
||
92 | |||
93 | // This middleware needs cannot run until after the routing, as it needs to know the route. |
||
94 | $post_routing_middleware = [CheckCsrf::class]; |
||
95 | $post_routing_middleware = array_map('app', $post_routing_middleware); |
||
96 | |||
97 | // Firstly, apply the route middleware |
||
98 | $route_middleware = $route->extras['middleware'] ?? []; |
||
99 | $route_middleware = array_map('app', $route_middleware); |
||
100 | |||
101 | // Secondly, apply any module middleware |
||
102 | $module_middleware = $this->module_service->findByInterface(MiddlewareInterface::class)->all(); |
||
103 | |||
104 | // Finally, run the handler using middleware |
||
105 | $handler_middleware = [new WrapHandler($route->handler)]; |
||
106 | |||
107 | $middleware = array_merge( |
||
108 | $post_routing_middleware, |
||
109 | $route_middleware, |
||
110 | $module_middleware, |
||
111 | $handler_middleware |
||
112 | ); |
||
113 | |||
114 | // Add the matched attributes to the request. |
||
115 | foreach ($route->attributes as $key => $value) { |
||
116 | if ($key === 'tree') { |
||
117 | $value = $this->tree_service->all()->get($value); |
||
118 | app()->instance(Tree::class, $value); |
||
119 | |||
120 | // Missing mandatory parameter? Let the default handler take care of it. |
||
121 | if ($value === null && strpos($route->path, '{tree}') !== false) { |
||
122 | return $handler->handle($request); |
||
123 | } |
||
124 | } |
||
125 | |||
126 | $request = $request->withAttribute((string) $key, $value); |
||
127 | } |
||
128 | |||
129 | // Bind the request into the container |
||
130 | app()->instance(ServerRequestInterface::class, $request); |
||
131 | |||
132 | $dispatcher = new Dispatcher($middleware, app()); |
||
133 | |||
134 | return $dispatcher->dispatch($request); |
||
135 | } |
||
137 |