Conditions | 13 |
Paths | 30 |
Total Lines | 117 |
Code Lines | 65 |
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 |
||
217 | private function route(ServerRequestInterface $request, ResponseInterface $response, callable $out = null, $retry = false) : ResponseInterface |
||
218 | { |
||
219 | $this->purgeExpiredRoutes(); |
||
220 | |||
221 | $urlNodesCacheItem = $this->cachePool->getItem('splashUrlNodes'); |
||
222 | if (!$urlNodesCacheItem->isHit()) { |
||
223 | // No value in cache, let's get the URL nodes |
||
224 | $urlsList = $this->getSplashActionsList(); |
||
225 | $urlNodes = $this->generateUrlNode($urlsList); |
||
226 | $urlNodesCacheItem->set($urlNodes); |
||
227 | $this->cachePool->save($urlNodesCacheItem); |
||
228 | } |
||
229 | |||
230 | $urlNodes = $urlNodesCacheItem->get(); |
||
231 | /* @var $urlNodes SplashUrlNode */ |
||
232 | |||
233 | $request_path = $request->getUri()->getPath(); |
||
234 | |||
235 | $pos = strpos($request_path, $this->rootUrl); |
||
236 | if ($pos === false) { |
||
237 | throw new SplashException('Error: the prefix of the web application "'.$this->rootUrl.'" was not found in the URL. The application must be misconfigured. Check the ROOT_URL parameter in your config.php file at the root of your project. It should have the same value as the RewriteBase parameter in your .htaccess file. Requested URL : "'.$request_path.'"'); |
||
238 | } |
||
239 | |||
240 | $tailing_url = substr($request_path, $pos + strlen($this->rootUrl)); |
||
241 | $tailing_url = urldecode($tailing_url); |
||
242 | $splashRoute = $urlNodes->walk($tailing_url, $request); |
||
243 | |||
244 | if ($splashRoute === null) { |
||
245 | // No route found. Let's try variants with or without trailing / if we are in a GET. |
||
246 | if ($request->getMethod() === 'GET') { |
||
247 | // If there is a trailing /, let's remove it and retry |
||
248 | if (strrpos($tailing_url, '/') === strlen($tailing_url) - 1) { |
||
249 | $url = substr($tailing_url, 0, -1); |
||
250 | $splashRoute = $urlNodes->walk($url, $request); |
||
251 | } else { |
||
252 | $url = $tailing_url.'/'; |
||
253 | $splashRoute = $urlNodes->walk($url, $request); |
||
254 | } |
||
255 | |||
256 | if ($splashRoute !== null) { |
||
257 | // If a route does match, let's make a redirect. |
||
258 | return new RedirectResponse($this->rootUrl.$url); |
||
259 | } |
||
260 | } |
||
261 | |||
262 | $this->log->debug('Found no route for URL {url}.', [ |
||
263 | 'url' => $request_path, |
||
264 | ]); |
||
265 | |||
266 | if ($this->debug === false || $retry === true) { |
||
267 | // No route found, let's pass control to the next middleware. |
||
268 | if ($out !== null) { |
||
269 | return $out($request, $response); |
||
270 | } else { |
||
271 | throw PageNotFoundException::create($tailing_url); |
||
272 | } |
||
273 | } else { |
||
274 | // We have a 404, but we are in debug mode and have not retried yet... |
||
275 | // Let's purge the cache and retry! |
||
276 | $this->purgeUrlsCache(); |
||
277 | |||
278 | return $this->route($request, $response, $out, true); |
||
279 | } |
||
280 | } |
||
281 | |||
282 | // Is the route still valid according to the cache? |
||
283 | if (!$splashRoute->isCacheValid()) { |
||
284 | // The route is invalid! Let's purge the cache and retry! |
||
285 | $this->purgeUrlsCache(); |
||
286 | |||
287 | return $this($request, $response, $out); |
||
288 | } |
||
289 | |||
290 | $controller = $this->container->get($splashRoute->getControllerInstanceName()); |
||
291 | $action = $splashRoute->getMethodName(); |
||
292 | |||
293 | $this->log->debug('Routing URL {url} to controller instance {controller} and action {action}', [ |
||
294 | 'url' => $request_path, |
||
295 | 'controller' => $splashRoute->getControllerInstanceName(), |
||
296 | 'action' => $action, |
||
297 | ]); |
||
298 | |||
299 | $filters = $splashRoute->getFilters(); |
||
300 | |||
301 | $middlewareCaller = function (ServerRequestInterface $request, ResponseInterface $response) use ($controller, $action, $splashRoute) { |
||
302 | // Let's recreate a new context object (because request can be modified by the filters) |
||
303 | $context = new SplashRequestContext($request); |
||
304 | $context->setUrlParameters($splashRoute->getFilledParameters()); |
||
305 | // Let's pass everything to the controller: |
||
306 | $args = $this->parameterFetcherRegistry->toArguments($context, $splashRoute->getParameters()); |
||
307 | |||
308 | try { |
||
309 | $response = SplashUtils::buildControllerResponse( |
||
310 | function () use ($controller, $action, $args) { |
||
311 | return $controller->$action(...$args); |
||
312 | }, |
||
313 | $this->mode, |
||
314 | $this->debug |
||
315 | ); |
||
316 | } catch (SplashException $e) { |
||
317 | throw new SplashException($e->getMessage(). ' (in '.$splashRoute->getControllerInstanceName().'->'.$splashRoute->getMethodName().')', $e->getCode(), $e); |
||
318 | } |
||
319 | return $response; |
||
320 | }; |
||
321 | |||
322 | // Apply filters |
||
323 | for ($i = count($filters) - 1; $i >= 0; --$i) { |
||
324 | $filter = $filters[$i]; |
||
325 | $middlewareCaller = function (ServerRequestInterface $request, ResponseInterface $response) use ($middlewareCaller, $filter) { |
||
326 | return $filter($request, $response, $middlewareCaller, $this->container); |
||
327 | }; |
||
328 | } |
||
329 | |||
330 | $response = $middlewareCaller($request, $response); |
||
331 | |||
332 | return $response; |
||
333 | } |
||
334 | |||
405 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..