| Conditions | 47 |
| Paths | > 20000 |
| Total Lines | 161 |
| Code Lines | 84 |
| 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 |
||
| 76 | public function build($url = '', $vars = '', $suffix = true, $domain = false) |
||
| 77 | { |
||
| 78 | // 解析URL |
||
| 79 | if (0 === strpos($url, '[') && $pos = strpos($url, ']')) { |
||
| 80 | // [name] 表示使用路由命名标识生成URL |
||
| 81 | $name = substr($url, 1, $pos - 1); |
||
| 82 | $url = 'name' . substr($url, $pos + 1); |
||
| 83 | } |
||
| 84 | |||
| 85 | if (false === strpos($url, '://') && 0 !== strpos($url, '/')) { |
||
| 86 | $info = parse_url($url); |
||
| 87 | $url = !empty($info['path']) ? $info['path'] : ''; |
||
| 88 | |||
| 89 | if (isset($info['fragment'])) { |
||
| 90 | // 解析锚点 |
||
| 91 | $anchor = $info['fragment']; |
||
| 92 | |||
| 93 | if (false !== strpos($anchor, '?')) { |
||
| 94 | // 解析参数 |
||
| 95 | list($anchor, $info['query']) = explode('?', $anchor, 2); |
||
| 96 | } |
||
| 97 | |||
| 98 | if (false !== strpos($anchor, '@')) { |
||
| 99 | // 解析域名 |
||
| 100 | list($anchor, $domain) = explode('@', $anchor, 2); |
||
| 101 | } |
||
| 102 | } elseif (strpos($url, '@') && false === strpos($url, '\\')) { |
||
| 103 | // 解析域名 |
||
| 104 | list($url, $domain) = explode('@', $url, 2); |
||
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | // 解析参数 |
||
| 109 | if (is_string($vars)) { |
||
| 110 | // aaa=1&bbb=2 转换成数组 |
||
| 111 | parse_str($vars, $vars); |
||
| 112 | } |
||
| 113 | |||
| 114 | if ($url) { |
||
| 115 | $checkName = isset($name) ? $name : $url . (isset($info['query']) ? '?' . $info['query'] : ''); |
||
| 116 | $checkDomain = $domain && is_string($domain) ? $domain : null; |
||
| 117 | |||
| 118 | $rule = $this->app['route']->getName($checkName, $checkDomain); |
||
| 119 | |||
| 120 | if (is_null($rule) && isset($info['query'])) { |
||
| 121 | $rule = $this->app['route']->getName($url); |
||
| 122 | // 解析地址里面参数 合并到vars |
||
| 123 | parse_str($info['query'], $params); |
||
| 124 | $vars = array_merge($params, $vars); |
||
| 125 | unset($info['query']); |
||
| 126 | } |
||
| 127 | } |
||
| 128 | |||
| 129 | if (!empty($rule) && $match = $this->getRuleUrl($rule, $vars, $domain)) { |
||
| 130 | // 匹配路由命名标识 |
||
| 131 | $url = $match[0]; |
||
| 132 | |||
| 133 | $domain = $match[1]; |
||
| 134 | |||
| 135 | if (!is_null($match[2])) { |
||
| 136 | $suffix = $match[2]; |
||
| 137 | } |
||
| 138 | } elseif (!empty($rule) && isset($name)) { |
||
| 139 | throw new \InvalidArgumentException('route name not exists:' . $name); |
||
| 140 | } else { |
||
| 141 | // 检查别名路由 |
||
| 142 | $alias = $this->app['route']->getAlias(); |
||
| 143 | $matchAlias = false; |
||
| 144 | |||
| 145 | if ($alias) { |
||
| 146 | // 别名路由解析 |
||
| 147 | foreach ($alias as $key => $item) { |
||
| 148 | $val = $item->getRoute(); |
||
| 149 | |||
| 150 | if (0 === strpos($url, $val)) { |
||
| 151 | $url = $key . substr($url, strlen($val)); |
||
| 152 | $matchAlias = true; |
||
| 153 | break; |
||
| 154 | } |
||
| 155 | } |
||
| 156 | } |
||
| 157 | |||
| 158 | if (!$matchAlias) { |
||
| 159 | // 路由标识不存在 直接解析 |
||
| 160 | $url = $this->parseUrl($url); |
||
| 161 | } |
||
| 162 | |||
| 163 | // 检测URL绑定 |
||
| 164 | if (!$this->bindCheck) { |
||
| 165 | $bind = $this->app['route']->getBind($domain && is_string($domain) ? $domain : null); |
||
| 166 | |||
| 167 | if ($bind && 0 === strpos($url, $bind)) { |
||
| 168 | $url = substr($url, strlen($bind) + 1); |
||
| 169 | } else { |
||
| 170 | $binds = $this->app['route']->getBind(true); |
||
| 171 | |||
| 172 | foreach ($binds as $key => $val) { |
||
| 173 | if (is_string($val) && 0 === strpos($url, $val) && substr_count($val, '/') > 1) { |
||
| 174 | $url = substr($url, strlen($val) + 1); |
||
| 175 | $domain = $key; |
||
| 176 | break; |
||
| 177 | } |
||
| 178 | } |
||
| 179 | } |
||
| 180 | } |
||
| 181 | |||
| 182 | if (isset($info['query'])) { |
||
| 183 | // 解析地址里面参数 合并到vars |
||
| 184 | parse_str($info['query'], $params); |
||
| 185 | $vars = array_merge($params, $vars); |
||
| 186 | } |
||
| 187 | } |
||
| 188 | |||
| 189 | // 还原URL分隔符 |
||
| 190 | $depr = $this->config['pathinfo_depr']; |
||
| 191 | $url = str_replace('/', $depr, $url); |
||
| 192 | |||
| 193 | // URL后缀 |
||
| 194 | if ('/' == substr($url, -1) || '' == $url) { |
||
| 195 | $suffix = ''; |
||
| 196 | } else { |
||
| 197 | $suffix = $this->parseSuffix($suffix); |
||
| 198 | } |
||
| 199 | |||
| 200 | // 锚点 |
||
| 201 | $anchor = !empty($anchor) ? '#' . $anchor : ''; |
||
| 202 | |||
| 203 | // 参数组装 |
||
| 204 | if (!empty($vars)) { |
||
| 205 | // 添加参数 |
||
| 206 | if ($this->config['url_common_param']) { |
||
| 207 | $vars = http_build_query($vars); |
||
| 208 | $url .= $suffix . '?' . $vars . $anchor; |
||
| 209 | } else { |
||
| 210 | $paramType = $this->config['url_param_type']; |
||
| 211 | |||
| 212 | foreach ($vars as $var => $val) { |
||
| 213 | if ('' !== trim($val)) { |
||
| 214 | if ($paramType) { |
||
| 215 | $url .= $depr . urlencode($val); |
||
| 216 | } else { |
||
| 217 | $url .= $depr . $var . $depr . urlencode($val); |
||
| 218 | } |
||
| 219 | } |
||
| 220 | } |
||
| 221 | |||
| 222 | $url .= $suffix . $anchor; |
||
| 223 | } |
||
| 224 | } else { |
||
| 225 | $url .= $suffix . $anchor; |
||
| 226 | } |
||
| 227 | |||
| 228 | // 检测域名 |
||
| 229 | $domain = $this->parseDomain($url, $domain); |
||
| 230 | |||
| 231 | // URL组装 |
||
| 232 | $url = $domain . rtrim($this->root ?: $this->app['request']->root(), '/') . '/' . ltrim($url, '/'); |
||
| 233 | |||
| 234 | $this->bindCheck = false; |
||
| 235 | |||
| 236 | return $url; |
||
| 237 | } |
||
| 405 |