| Conditions | 13 |
| Paths | 32 |
| Total Lines | 43 |
| Code Lines | 25 |
| 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 |
||
| 97 | private function parseController(array $paths, bool $multi = false) : array |
||
| 98 | { |
||
| 99 | $tempPaths = \array_merge([], $paths); |
||
| 100 | $controller = !empty($tempPaths) ? array_shift($tempPaths) : null; |
||
| 101 | if ($controller && !\preg_match('/^[A-Za-z][\w|\.]*$/', $controller)) { |
||
| 102 | throw new HttpException(404, 'controller not exists:' . $controller); |
||
| 103 | } |
||
| 104 | |||
| 105 | if ($multi) { |
||
| 106 | // 处理多级控制器 |
||
| 107 | $controllers = []; |
||
| 108 | \array_push($controllers, $controller); |
||
| 109 | // 控制器是否存在 |
||
| 110 | $exists = $this->exists($controller); |
||
| 111 | do { |
||
| 112 | if (!$exists) { |
||
| 113 | // 控制器不存在,取下一级路径 |
||
| 114 | $nextPath = !empty($tempPaths) ? \array_shift($tempPaths) : null; |
||
| 115 | if ($nextPath && \preg_match('/^[A-Za-z][\w|\.]*$/', $nextPath)) { |
||
| 116 | \array_push($controllers, $nextPath); |
||
| 117 | $controller = join('.', $controllers); |
||
| 118 | } else { |
||
| 119 | // 下一级路径命名不正确, 压回数组, 退出循环 |
||
| 120 | if (!empty($nextPath)) { |
||
| 121 | \array_unshift($tempPaths, $nextPath); |
||
| 122 | } |
||
| 123 | break; |
||
| 124 | } |
||
| 125 | } |
||
| 126 | // 控制器是否存在 |
||
| 127 | $exists = $this->exists($controller); |
||
| 128 | } while (!$exists); |
||
| 129 | |||
| 130 | // 多级控制器不存在,还原为单级控制器 |
||
| 131 | if (!$exists) { |
||
| 132 | while (count($controllers) > 1) { |
||
| 133 | \array_unshift($tempPaths, \array_pop($controllers)); |
||
| 134 | } |
||
| 135 | $controller = join('.', $controllers); |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | return [$controller, $tempPaths]; |
||
| 140 | } |
||
| 166 |