| Conditions | 16 |
| Paths | 36 |
| Total Lines | 65 |
| Code Lines | 35 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 6 | ||
| Bugs | 1 | 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 |
||
| 87 | public function match(string $method, UriInterface $uri): ?Route |
||
| 88 | { |
||
| 89 | $requestPath = $uri->getPath(); |
||
| 90 | |||
| 91 | if ('/' !== $requestPath && isset(Route::URL_PREFIX_SLASHES[$requestPath[-1]])) { |
||
| 92 | $uri = $uri->withPath($requestPath = \substr($requestPath, 0, -1)); |
||
| 93 | } |
||
| 94 | |||
| 95 | if (isset($this->staticRouteMap[$requestPath])) { |
||
| 96 | [$routeId, $methods, $hostsRegex] = $this->staticRouteMap[$requestPath]; |
||
| 97 | $variables = $this->variableRouteData[$routeId]; |
||
| 98 | |||
| 99 | if (!\in_array($method, $methods, true)) { |
||
| 100 | throw new MethodNotAllowedException($methods, $uri->getPath(), $method); |
||
| 101 | } |
||
| 102 | |||
| 103 | if (!empty($hostsRegex)) { |
||
| 104 | $hostAndPost = $uri->getHost() . (null !== $uri->getPort() ? ':' . $uri->getPort() : ''); |
||
| 105 | |||
| 106 | if (!\preg_match($hostsRegex, $hostAndPost, $hostsVar)) { |
||
| 107 | if (!empty($this->regexToRoutesMap)) { |
||
| 108 | goto retry_routing; |
||
| 109 | } |
||
| 110 | |||
| 111 | throw new UriHandlerException(\sprintf('Unfortunately current host "%s" is not allowed on requested static path [%s].', $uri->getHost(), $uri->getPath()), 400); |
||
| 112 | } |
||
| 113 | |||
| 114 | foreach ($variables as $key => $var) { |
||
| 115 | if (isset($hostsVar[$key])) { |
||
| 116 | $variables[$key] = $hostsRegex[$key] ?? $var; |
||
| 117 | } |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | return $this->matchRoute($this->routes[$routeId]->arguments($variables), $uri); |
||
| 122 | } |
||
| 123 | |||
| 124 | if (isset($this->regexToRoutesMap)) { |
||
| 125 | retry_routing: |
||
| 126 | $requestPath = $method . \strpbrk((string) $uri, '/'); |
||
| 127 | |||
| 128 | foreach ($this->regexToRoutesMap as $regexRoute) { |
||
| 129 | if (\preg_match($regexRoute, $requestPath, $matches)) { |
||
| 130 | $route = $this->routes[$routeId = $matches['MARK']]; |
||
| 131 | $variables = $this->variableRouteData[$routeId]; |
||
| 132 | |||
| 133 | if (!empty($matches[1])) { |
||
| 134 | throw new MethodNotAllowedException($route->get('methods'), $uri->getPath(), $method); |
||
| 135 | } |
||
| 136 | |||
| 137 | unset($matches[0], $matches[1], $matches['MARK']); |
||
| 138 | $matchVar = 2; // Indexing shifted due to method and host combined in regex |
||
| 139 | |||
| 140 | foreach ($variables as $key => $value) { |
||
| 141 | $route->argument($key, $matches[$matchVar] ?? $value); |
||
| 142 | |||
| 143 | ++$matchVar; |
||
| 144 | } |
||
| 145 | |||
| 146 | return $this->matchRoute($route, $uri); |
||
| 147 | } |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 151 | return null; |
||
| 152 | } |
||
| 199 |