| Conditions | 13 |
| Paths | 105 |
| Total Lines | 50 |
| Code Lines | 24 |
| 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 |
||
| 39 | protected function parseUrl(string $url): array |
||
| 40 | { |
||
| 41 | $depr = $this->rule->config('pathinfo_depr'); |
||
| 42 | $bind = $this->rule->getRouter()->getDomainBind(); |
||
| 43 | |||
| 44 | if ($bind && preg_match('/^[a-z]/is', $bind)) { |
||
| 45 | $bind = str_replace('/', $depr, $bind); |
||
| 46 | // 如果有模块/控制器绑定 |
||
| 47 | $url = $bind . ('.' != substr($bind, -1) ? $depr : '') . ltrim($url, $depr); |
||
| 48 | } |
||
| 49 | |||
| 50 | list($path, $var) = $this->rule->parseUrlPath($url); |
||
| 51 | if (empty($path)) { |
||
| 52 | return [null, null]; |
||
| 53 | } |
||
| 54 | |||
| 55 | // 解析控制器 |
||
| 56 | $controller = !empty($path) ? array_shift($path) : null; |
||
| 57 | |||
| 58 | if ($controller && !preg_match('/^[A-Za-z][\w|\.]*$/', $controller)) { |
||
| 59 | throw new HttpException(404, 'controller not exists:' . $controller); |
||
| 60 | } |
||
| 61 | |||
| 62 | // 解析操作 |
||
| 63 | $action = !empty($path) ? array_shift($path) : null; |
||
| 64 | |||
| 65 | // 解析额外参数 |
||
| 66 | if ($path) { |
||
| 67 | preg_replace_callback('/(\w+)\|([^\|]+)/', function ($match) use (&$var) { |
||
| 68 | $var[$match[1]] = strip_tags($match[2]); |
||
| 69 | }, implode('|', $path)); |
||
| 70 | } |
||
| 71 | |||
| 72 | $panDomain = $this->request->panDomain(); |
||
| 73 | if ($panDomain && $key = array_search('*', $var)) { |
||
| 74 | // 泛域名赋值 |
||
| 75 | $var[$key] = $panDomain; |
||
| 76 | } |
||
| 77 | |||
| 78 | // 设置当前请求的参数 |
||
| 79 | $this->request->setRoute($var); |
||
| 80 | |||
| 81 | // 封装路由 |
||
| 82 | $route = [$controller, $action]; |
||
| 83 | |||
| 84 | if ($this->hasDefinedRoute($route)) { |
||
| 85 | throw new HttpException(404, 'invalid request:' . str_replace('|', $depr, $url)); |
||
| 86 | } |
||
| 87 | |||
| 88 | return $route; |
||
| 89 | } |
||
| 115 |