Total Complexity | 56 |
Total Lines | 371 |
Duplicated Lines | 0 % |
Changes | 4 | ||
Bugs | 1 | Features | 0 |
Complex classes like Router often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Router, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class Router extends RouteController |
||
28 | { |
||
29 | |||
30 | /** |
||
31 | * Parameter types |
||
32 | */ |
||
33 | const PARAM_TYPES = [ |
||
34 | ':alpha' => '[a-zA-Z]', |
||
35 | ':num' => '[0-9]', |
||
36 | ':any' => '[^\/]' |
||
37 | ]; |
||
38 | |||
39 | /** |
||
40 | * Request instance |
||
41 | * @var \Quantum\Http\Request; |
||
42 | */ |
||
43 | private $request; |
||
44 | |||
45 | /** |
||
46 | * Response instance |
||
47 | * @var \Quantum\Http\Response; |
||
48 | */ |
||
49 | private $response; |
||
50 | |||
51 | /** |
||
52 | * List of routes |
||
53 | * @var array |
||
54 | */ |
||
55 | private $routes = []; |
||
56 | |||
57 | /** |
||
58 | * matched routes |
||
59 | * @var array |
||
60 | */ |
||
61 | private $matchedRoutes = []; |
||
62 | |||
63 | /** |
||
64 | * Matched URI |
||
65 | * @var string |
||
66 | */ |
||
67 | private $matchedUri = null; |
||
68 | |||
69 | /** |
||
70 | * Router constructor. |
||
71 | * @param \Quantum\Http\Request $request |
||
72 | * @param \Quantum\Http\Response $response |
||
73 | */ |
||
74 | public function __construct(Request $request, Response $response) |
||
75 | { |
||
76 | $this->request = $request; |
||
77 | $this->response = $response; |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * Finds the current route |
||
82 | * @throws \Quantum\Exceptions\DiException |
||
83 | * @throws \Quantum\Exceptions\RouteException |
||
84 | * @throws \Quantum\Exceptions\StopExecutionException |
||
85 | * @throws \ReflectionException |
||
86 | */ |
||
87 | public function findRoute() |
||
127 | } |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Set Routes |
||
132 | * @param array $routes |
||
133 | */ |
||
134 | public function setRoutes(array $routes) |
||
135 | { |
||
136 | $this->routes = $routes; |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * Get Routes |
||
141 | * @return array |
||
142 | */ |
||
143 | public function getRoutes(): array |
||
144 | { |
||
145 | return $this->routes; |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * Resets the routes |
||
150 | */ |
||
151 | private function resetRoutes() |
||
152 | { |
||
153 | parent::$currentRoute = null; |
||
154 | $this->matchedUri = null; |
||
155 | $this->matchedRoutes = []; |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Finds matches by pattern |
||
160 | * @param string $uri |
||
161 | */ |
||
162 | private function findPatternMatches(string $uri) |
||
163 | { |
||
164 | $requestUri = urldecode(parse_url($uri)['path']); |
||
165 | |||
166 | foreach ($this->routes as $route) { |
||
167 | |||
168 | list($pattern, $params) = $this->handleRoutePattern($route); |
||
169 | |||
170 | preg_match("/^" . $this->escape($pattern) . "$/u", $requestUri, $matches); |
||
171 | |||
172 | if (count($matches)) { |
||
173 | $this->matchedUri = array_shift($matches) ?: '/'; |
||
174 | |||
175 | $route['params'] = $this->routeParams($params, $matches); |
||
176 | $route['pattern'] = $pattern; |
||
177 | $this->matchedRoutes[] = $route; |
||
178 | } |
||
179 | } |
||
180 | } |
||
181 | |||
182 | private function handleRoutePattern(array $route): array |
||
183 | { |
||
184 | $routeSegments = explode('/', trim($route['route'], '/')); |
||
185 | |||
186 | $routePattern = '(\/)?'; |
||
187 | $routeParams = []; |
||
188 | |||
189 | $lastIndex = (int) array_key_last($routeSegments); |
||
190 | |||
191 | foreach ($routeSegments as $index => $segment) { |
||
192 | $segmentParam = $this->checkSegment($segment, $index, $lastIndex); |
||
193 | |||
194 | if (!empty($segmentParam)) { |
||
195 | $this->checkParamName($routeParams, $segmentParam['name']); |
||
196 | |||
197 | $routeParams[] = [ |
||
198 | 'route_pattern' => $segment, |
||
199 | 'pattern' => $segmentParam['pattern'], |
||
200 | 'name' => $segmentParam['name'] |
||
201 | ]; |
||
202 | |||
203 | $routePattern = $this->normilizePattern($routePattern, $segmentParam, $index, $lastIndex); |
||
204 | } else { |
||
205 | $routePattern .= $segment; |
||
206 | |||
207 | if ($index != $lastIndex) { |
||
208 | $routePattern .= '(\/)'; |
||
209 | } |
||
210 | } |
||
211 | } |
||
212 | |||
213 | return [ |
||
214 | $routePattern, |
||
215 | $routeParams |
||
216 | ]; |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * Normalize the pattern |
||
221 | * @param string $routePattern |
||
222 | * @param array $segmentParam |
||
223 | * @param int $index |
||
224 | * @param int $lastIndex |
||
225 | * @return string |
||
226 | */ |
||
227 | private function normilizePattern(string $routePattern, array $segmentParam, int $index, int $lastIndex): string |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * Gets the route parameters |
||
242 | * @param array $params |
||
243 | * @param array $arguments |
||
244 | * @return array |
||
245 | */ |
||
246 | private function routeParams(array $params, array $arguments): array |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * Checks the segment for parameter |
||
262 | * @param string $segment |
||
263 | * @return array |
||
264 | */ |
||
265 | private function checkSegment(string $segment, int $index, int $lastIndex): array |
||
274 | } |
||
275 | |||
276 | /** |
||
277 | * Checks the parameter name availability |
||
278 | * @param array $routeParams |
||
279 | * @param string $name |
||
280 | * @throws \Quantum\Exceptions\RouteException |
||
281 | */ |
||
282 | private function checkParamName(array $routeParams, string $name) |
||
287 | } |
||
288 | } |
||
289 | } |
||
290 | |||
291 | /** |
||
292 | * Finds pattern for parameter |
||
293 | * @param array $match |
||
294 | * @param string $expr |
||
295 | * @param int $index |
||
296 | * @param int $lastIndex |
||
297 | * @return array |
||
298 | */ |
||
299 | private function getParamPattern(array $match, string $expr, int $index, int $lastIndex): array |
||
300 | { |
||
301 | $pattern = ''; |
||
302 | |||
303 | $name = $this->getParamName($match, $index); |
||
304 | |||
305 | $pattern .= '(?<' . $name . '>' . $expr; |
||
306 | |||
307 | if (isset($match[4]) && is_numeric($match[4])) { |
||
308 | if (isset($match[5]) && $match[5] == '?') { |
||
309 | $pattern .= '{0,' . $match[4] . '})'; |
||
310 | } else { |
||
311 | $pattern .= '{' . $match[4] . '})'; |
||
312 | } |
||
313 | } else { |
||
314 | if (isset($match[5]) && $match[5] == '?') { |
||
315 | $pattern .= '*)'; |
||
316 | } else { |
||
317 | $pattern .= '+)'; |
||
318 | } |
||
319 | } |
||
320 | |||
321 | if (isset($match[5]) && $match[5] == '?') { |
||
322 | $pattern = ($index == $lastIndex ? '(\/)?' . $pattern : $pattern . '(\/)?'); |
||
323 | } else { |
||
324 | $pattern = ($index == $lastIndex ? '(\/)' . $pattern : $pattern . '(\/)'); |
||
325 | } |
||
326 | |||
327 | return [ |
||
328 | 'name' => $name, |
||
329 | 'pattern' => $pattern |
||
330 | ]; |
||
331 | } |
||
332 | |||
333 | /** |
||
334 | * Gets the parameter name |
||
335 | * @param array $match |
||
336 | * @return string |
||
337 | * @throws \Quantum\Exceptions\RouteException |
||
338 | */ |
||
339 | private function getParamName(array $match, int $index): string |
||
352 | } |
||
353 | |||
354 | /** |
||
355 | * Checks the route collisions |
||
356 | * @throws \Quantum\Exceptions\RouteException |
||
357 | */ |
||
358 | private function checkCollision() |
||
369 | } |
||
370 | } |
||
371 | } |
||
372 | } |
||
373 | |||
374 | /** |
||
375 | * Checks the request method against defined route method |
||
376 | * @param array $matchedRoute |
||
377 | * @throws \Quantum\Exceptions\RouteException |
||
378 | */ |
||
379 | private function checkMethod(array $matchedRoute) |
||
380 | { |
||
381 | if (strpos($matchedRoute['method'], '|') !== false) { |
||
382 | if (!in_array($this->request->getMethod(), explode('|', $matchedRoute['method']))) { |
||
383 | throw RouteException::incorrectMethod($this->request->getMethod()); |
||
384 | } |
||
385 | } else if ($this->request->getMethod() != $matchedRoute['method']) { |
||
386 | throw RouteException::incorrectMethod($this->request->getMethod()); |
||
387 | } |
||
388 | } |
||
389 | |||
390 | /** |
||
391 | * Escapes the slashes |
||
392 | * @param string $str |
||
393 | * @return string |
||
394 | */ |
||
395 | private function escape($str) |
||
398 | } |
||
399 | |||
400 | } |
||
401 |