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