Conditions | 16 |
Paths | 440 |
Total Lines | 132 |
Code Lines | 73 |
Lines | 0 |
Ratio | 0 % |
Changes | 8 | ||
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 |
||
185 | public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $out = null) |
||
186 | { |
||
187 | try { |
||
188 | $this->purgeExpiredRoutes(); |
||
189 | |||
190 | $urlNodesCacheItem = $this->cachePool->getItem('splashUrlNodes'); |
||
191 | if (!$urlNodesCacheItem->isHit()) { |
||
192 | // No value in cache, let's get the URL nodes |
||
193 | $urlsList = $this->getSplashActionsList(); |
||
194 | $urlNodes = $this->generateUrlNode($urlsList); |
||
195 | $urlNodesCacheItem->set($urlNodes); |
||
196 | $this->cachePool->save($urlNodesCacheItem); |
||
197 | } |
||
198 | |||
199 | $urlNodes = $urlNodesCacheItem->get(); |
||
200 | /* @var $urlNodes SplashUrlNode */ |
||
201 | |||
202 | $request_path = $request->getUri()->getPath(); |
||
203 | |||
204 | $pos = strpos($request_path, $this->rootUrl); |
||
205 | if ($pos === false) { |
||
206 | 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.'"'); |
||
207 | } |
||
208 | |||
209 | $tailing_url = substr($request_path, $pos + strlen($this->rootUrl)); |
||
210 | |||
211 | $splashRoute = $urlNodes->walk($tailing_url, $request); |
||
212 | |||
213 | if ($splashRoute === null) { |
||
214 | // No route found. Let's try variants with or without trailing / if we are in a GET. |
||
215 | if ($request->getMethod() === 'GET') { |
||
216 | // If there is a trailing /, let's remove it and retry |
||
217 | if (strrpos($tailing_url, '/') === strlen($tailing_url) - 1) { |
||
218 | $url = substr($tailing_url, 0, -1); |
||
219 | $splashRoute = $urlNodes->walk($url, $request); |
||
220 | } else { |
||
221 | $url = $tailing_url.'/'; |
||
222 | $splashRoute = $urlNodes->walk($url, $request); |
||
223 | } |
||
224 | |||
225 | if ($splashRoute !== null) { |
||
226 | // If a route does match, let's make a redirect. |
||
227 | return new RedirectResponse($this->rootUrl.$url); |
||
228 | } |
||
229 | } |
||
230 | |||
231 | $this->log->debug('Found no route for URL {url}.', [ |
||
232 | 'url' => $request_path, |
||
233 | ]); |
||
234 | |||
235 | // No route found, let's pass control to the next middleware. |
||
236 | if ($out !== null) { |
||
237 | return $out($request, $response); |
||
238 | } else { |
||
239 | throw PageNotFoundException::create($tailing_url); |
||
240 | } |
||
241 | } |
||
242 | |||
243 | // Is the route still valid according to the cache? |
||
244 | if (!$splashRoute->isCacheValid()) { |
||
245 | // The route is invalid! Let's purge the cache and retry! |
||
246 | $this->purgeUrlsCache(); |
||
247 | return $this($request, $response, $out); |
||
248 | } |
||
249 | |||
250 | |||
251 | $controller = $this->container->get($splashRoute->getControllerInstanceName()); |
||
252 | $action = $splashRoute->getMethodName(); |
||
253 | |||
254 | $this->log->debug('Routing URL {url} to controller instance {controller} and action {action}', [ |
||
255 | 'url' => $request_path, |
||
256 | 'controller' => $splashRoute->getControllerInstanceName(), |
||
257 | 'action' => $action, |
||
258 | ]); |
||
259 | |||
260 | |||
261 | $filters = $splashRoute->getFilters(); |
||
262 | |||
263 | |||
264 | |||
265 | $middlewareCaller = function(ServerRequestInterface $request, ResponseInterface $response) use ($controller, $action, $splashRoute, $splashRoute) { |
||
266 | // Let's recreate a new context object (because request can be modified by the filters) |
||
267 | $context = new SplashRequestContext($request); |
||
268 | $context->setUrlParameters($splashRoute->getFilledParameters()); |
||
269 | // Let's pass everything to the controller: |
||
270 | $args = $this->parameterFetcherRegistry->toArguments($context, $splashRoute->getParameters()); |
||
271 | |||
272 | |||
273 | |||
274 | $response = SplashUtils::buildControllerResponse( |
||
275 | function () use ($controller, $action, $args) { |
||
276 | return $controller->$action(...$args); |
||
277 | }, |
||
278 | $this->mode, |
||
279 | $this->debug |
||
280 | ); |
||
281 | |||
282 | return $response; |
||
283 | }; |
||
284 | |||
285 | // Apply filters |
||
286 | for ($i = count($filters) - 1; $i >= 0; --$i) { |
||
287 | $filter = $filters[$i]; |
||
288 | $middlewareCaller = function(ServerRequestInterface $request, ResponseInterface $response) use ($middlewareCaller, $filter) { |
||
289 | return $filter($request, $response, $middlewareCaller, $this->container); |
||
290 | }; |
||
291 | } |
||
292 | |||
293 | |||
294 | $response = $middlewareCaller($request, $response); |
||
295 | |||
296 | return $response; |
||
297 | } catch (BadRequestException $e) { |
||
298 | if ($this->http400Handler !== null) { |
||
299 | return $this->http400Handler->badRequest($e, $request); |
||
300 | } else { |
||
301 | throw $e; |
||
302 | } |
||
303 | } catch (PageNotFoundException $e) { |
||
304 | if ($this->http404Handler !== null) { |
||
305 | return $this->http404Handler->pageNotFound($request); |
||
306 | } else { |
||
307 | throw $e; |
||
308 | } |
||
309 | } catch (\Throwable $t) { |
||
310 | if ($this->http500Handler !== null) { |
||
311 | return $this->http500Handler->serverError($t, $request); |
||
312 | } else { |
||
313 | throw $t; |
||
314 | } |
||
315 | } |
||
316 | } |
||
317 | |||
389 |
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..