Conditions | 13 |
Paths | 28 |
Total Lines | 56 |
Code Lines | 30 |
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 |
||
73 | public function match(string $method, UriInterface $uri): ?Route |
||
74 | { |
||
75 | if ('/' !== ($requestPath = $uri->getPath()) && isset(Route::URL_PREFIX_SLASHES[$requestPath[-1]])) { |
||
76 | $requestPath = \substr($requestPath, 0, -1); |
||
77 | } |
||
78 | |||
79 | if (isset($this->routeMap[0][$requestPath])) { |
||
80 | [$routeId, $methods, $hostsRegex] = $this->routeMap[0][$requestPath]; |
||
81 | $variables = $this->routeMap[2][$routeId]; |
||
82 | |||
83 | if (!\array_key_exists($method, $methods)) { |
||
84 | throw new MethodNotAllowedException($methods, $uri->getPath(), $method); |
||
85 | } |
||
86 | |||
87 | if (!empty($hostsRegex)) { |
||
88 | $variables = $this->matchStaticRouteHost($uri, $hostsRegex, $variables); |
||
89 | |||
90 | if (null === $variables) { |
||
91 | if (!empty($this->routeMap[1])) { |
||
92 | goto retry_routing; |
||
93 | } |
||
94 | |||
95 | throw new UriHandlerException(\sprintf('Unfortunately current host "%s" is not allowed on requested static path [%s].', $uri->getHost(), $uri->getPath()), 400); |
||
96 | } |
||
97 | } |
||
98 | |||
99 | return $this->matchRoute($this->routes[$routeId]->arguments($variables), $uri); |
||
100 | } |
||
101 | |||
102 | retry_routing: |
||
103 | if (!empty($dynamicRouteMap = $this->routeMap[1])) { |
||
104 | $requestPath = $method . \strpbrk((string) $uri->withPath($requestPath), '/'); |
||
105 | |||
106 | foreach ($dynamicRouteMap as $regexRoute) { |
||
107 | if (\preg_match($regexRoute, $requestPath, $matches)) { |
||
108 | $route = $this->routes[$routeId = $matches['MARK']]; |
||
109 | |||
110 | if (!empty($matches[1])) { |
||
111 | throw new MethodNotAllowedException($route->get('methods'), $uri->getPath(), $method); |
||
112 | } |
||
113 | |||
114 | unset($matches[0], $matches[1], $matches['MARK']); |
||
115 | $matchVar = 2; // Indexing shifted due to method and host combined in regex |
||
116 | |||
117 | foreach ($this->routeMap[2][$routeId] as $key => $value) { |
||
118 | $route->argument($key, $matches[$matchVar] ?? $value); |
||
119 | |||
120 | ++$matchVar; |
||
121 | } |
||
122 | |||
123 | return $this->matchRoute($route, $uri); |
||
124 | } |
||
125 | } |
||
126 | } |
||
127 | |||
128 | return null; |
||
129 | } |
||
198 |