Complex classes like Rule 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Rule, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
32 | abstract class Rule |
||
33 | { |
||
34 | /** |
||
35 | * 路由标识 |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $name; |
||
39 | |||
40 | /** |
||
41 | * 所在域名 |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $domain; |
||
45 | |||
46 | /** |
||
47 | * 路由对象 |
||
48 | * @var Route |
||
49 | */ |
||
50 | protected $router; |
||
51 | |||
52 | /** |
||
53 | * 路由所属分组 |
||
54 | * @var RuleGroup |
||
55 | */ |
||
56 | protected $parent; |
||
57 | |||
58 | /** |
||
59 | * 路由规则 |
||
60 | * @var mixed |
||
61 | */ |
||
62 | protected $rule; |
||
63 | |||
64 | /** |
||
65 | * 路由地址 |
||
66 | * @var string|Closure |
||
67 | */ |
||
68 | protected $route; |
||
69 | |||
70 | /** |
||
71 | * 请求类型 |
||
72 | * @var string |
||
73 | */ |
||
74 | protected $method; |
||
75 | |||
76 | /** |
||
77 | * 路由变量 |
||
78 | * @var array |
||
79 | */ |
||
80 | protected $vars = []; |
||
81 | |||
82 | /** |
||
83 | * 路由参数 |
||
84 | * @var array |
||
85 | */ |
||
86 | protected $option = []; |
||
87 | |||
88 | /** |
||
89 | * 路由变量规则 |
||
90 | * @var array |
||
91 | */ |
||
92 | protected $pattern = []; |
||
93 | |||
94 | /** |
||
95 | * 需要和分组合并的路由参数 |
||
96 | * @var array |
||
97 | */ |
||
98 | protected $mergeOptions = ['model', 'append', 'middleware']; |
||
99 | |||
100 | abstract public function check(Request $request, string $url, bool $completeMatch = false); |
||
101 | |||
102 | /** |
||
103 | * 设置路由参数 |
||
104 | * @access public |
||
105 | * @param array $option 参数 |
||
106 | * @return $this |
||
107 | */ |
||
108 | public function option(array $option) |
||
114 | |||
115 | /** |
||
116 | * 设置单个路由参数 |
||
117 | * @access public |
||
118 | * @param string $name 参数名 |
||
119 | * @param mixed $value 值 |
||
120 | * @return $this |
||
121 | */ |
||
122 | 18 | public function setOption(string $name, $value) |
|
128 | |||
129 | /** |
||
130 | * 注册变量规则 |
||
131 | * @access public |
||
132 | * @param array $pattern 变量规则 |
||
133 | * @return $this |
||
134 | */ |
||
135 | public function pattern(array $pattern) |
||
141 | |||
142 | /** |
||
143 | * 设置标识 |
||
144 | * @access public |
||
145 | * @param string $name 标识名 |
||
146 | * @return $this |
||
147 | */ |
||
148 | public function name(string $name) |
||
154 | |||
155 | /** |
||
156 | * 获取路由对象 |
||
157 | * @access public |
||
158 | * @return Route |
||
159 | */ |
||
160 | 6 | public function getRouter(): Route |
|
164 | |||
165 | /** |
||
166 | * 获取Name |
||
167 | * @access public |
||
168 | * @return string |
||
169 | */ |
||
170 | public function getName(): string |
||
174 | |||
175 | /** |
||
176 | * 获取当前路由规则 |
||
177 | * @access public |
||
178 | * @return mixed |
||
179 | */ |
||
180 | 6 | public function getRule() |
|
184 | |||
185 | /** |
||
186 | * 获取当前路由地址 |
||
187 | * @access public |
||
188 | * @return mixed |
||
189 | */ |
||
190 | 18 | public function getRoute() |
|
194 | |||
195 | /** |
||
196 | * 获取当前路由的变量 |
||
197 | * @access public |
||
198 | * @return array |
||
199 | */ |
||
200 | public function getVars(): array |
||
204 | |||
205 | /** |
||
206 | * 获取Parent对象 |
||
207 | * @access public |
||
208 | * @return $this|null |
||
209 | */ |
||
210 | public function getParent() |
||
214 | |||
215 | /** |
||
216 | * 获取路由所在域名 |
||
217 | * @access public |
||
218 | * @return string |
||
219 | */ |
||
220 | public function getDomain(): string |
||
224 | |||
225 | /** |
||
226 | * 获取路由参数 |
||
227 | * @access public |
||
228 | * @param string $name 变量名 |
||
229 | * @return mixed |
||
230 | */ |
||
231 | 9 | public function config(string $name = '') |
|
235 | |||
236 | /** |
||
237 | * 获取变量规则定义 |
||
238 | * @access public |
||
239 | * @param string $name 变量名 |
||
240 | * @return mixed |
||
241 | */ |
||
242 | 18 | public function getPattern(string $name = '') |
|
250 | |||
251 | /** |
||
252 | * 获取路由参数定义 |
||
253 | * @access public |
||
254 | * @param string $name 参数名 |
||
255 | * @param mixed $default 默认值 |
||
256 | * @return mixed |
||
257 | */ |
||
258 | 18 | public function getOption(string $name = '', $default = null) |
|
266 | |||
267 | /** |
||
268 | * 获取当前路由的请求类型 |
||
269 | * @access public |
||
270 | * @return string |
||
271 | */ |
||
272 | 6 | public function getMethod(): string |
|
276 | |||
277 | /** |
||
278 | * 设置路由请求类型 |
||
279 | * @access public |
||
280 | * @param string $method 请求类型 |
||
281 | * @return $this |
||
282 | */ |
||
283 | public function method(string $method) |
||
287 | |||
288 | /** |
||
289 | * 检查后缀 |
||
290 | * @access public |
||
291 | * @param string $ext URL后缀 |
||
292 | * @return $this |
||
293 | */ |
||
294 | public function ext(string $ext = '') |
||
298 | |||
299 | /** |
||
300 | * 检查禁止后缀 |
||
301 | * @access public |
||
302 | * @param string $ext URL后缀 |
||
303 | * @return $this |
||
304 | */ |
||
305 | public function denyExt(string $ext = '') |
||
309 | |||
310 | /** |
||
311 | * 检查域名 |
||
312 | * @access public |
||
313 | * @param string $domain 域名 |
||
314 | * @return $this |
||
315 | */ |
||
316 | public function domain(string $domain) |
||
321 | |||
322 | /** |
||
323 | * 设置参数过滤检查 |
||
324 | * @access public |
||
325 | * @param array $filter 参数过滤 |
||
326 | * @return $this |
||
327 | */ |
||
328 | public function filter(array $filter) |
||
334 | |||
335 | /** |
||
336 | * 绑定模型 |
||
337 | * @access public |
||
338 | * @param array|string|Closure $var 路由变量名 多个使用 & 分割 |
||
339 | * @param string|Closure $model 绑定模型类 |
||
340 | * @param bool $exception 是否抛出异常 |
||
341 | * @return $this |
||
342 | */ |
||
343 | public function model($var, $model = null, bool $exception = true) |
||
357 | |||
358 | /** |
||
359 | * 附加路由隐式参数 |
||
360 | * @access public |
||
361 | * @param array $append 追加参数 |
||
362 | * @return $this |
||
363 | */ |
||
364 | public function append(array $append = []) |
||
370 | |||
371 | /** |
||
372 | * 绑定验证 |
||
373 | * @access public |
||
374 | * @param mixed $validate 验证器类 |
||
375 | * @param string $scene 验证场景 |
||
376 | * @param array $message 验证提示 |
||
377 | * @param bool $batch 批量验证 |
||
378 | * @return $this |
||
379 | */ |
||
380 | public function validate($validate, string $scene = null, array $message = [], bool $batch = false) |
||
386 | |||
387 | /** |
||
388 | * 指定路由中间件 |
||
389 | * @access public |
||
390 | * @param string|array|Closure $middleware 中间件 |
||
391 | * @param mixed $params 参数 |
||
392 | * @return $this |
||
393 | */ |
||
394 | 3 | public function middleware($middleware, ...$params) |
|
406 | |||
407 | /** |
||
408 | * 允许跨域 |
||
409 | * @access public |
||
410 | * @param array $header 自定义Header |
||
411 | * @return $this |
||
412 | */ |
||
413 | 3 | public function allowCrossDomain(array $header = []) |
|
417 | |||
418 | /** |
||
419 | * 表单令牌验证 |
||
420 | * @access public |
||
421 | * @param string $token 表单令牌token名称 |
||
422 | * @return $this |
||
423 | */ |
||
424 | public function token(string $token = '__token__') |
||
428 | |||
429 | /** |
||
430 | * 设置路由缓存 |
||
431 | * @access public |
||
432 | * @param array|string $cache 缓存 |
||
433 | * @return $this |
||
434 | */ |
||
435 | public function cache($cache) |
||
439 | |||
440 | /** |
||
441 | * 检查URL分隔符 |
||
442 | * @access public |
||
443 | * @param string $depr URL分隔符 |
||
444 | * @return $this |
||
445 | */ |
||
446 | public function depr(string $depr) |
||
450 | |||
451 | /** |
||
452 | * 设置需要合并的路由参数 |
||
453 | * @access public |
||
454 | * @param array $option 路由参数 |
||
455 | * @return $this |
||
456 | */ |
||
457 | public function mergeOptions(array $option = []) |
||
462 | |||
463 | /** |
||
464 | * 检查是否为HTTPS请求 |
||
465 | * @access public |
||
466 | * @param bool $https 是否为HTTPS |
||
467 | * @return $this |
||
468 | */ |
||
469 | public function https(bool $https = true) |
||
473 | |||
474 | /** |
||
475 | * 检查是否为JSON请求 |
||
476 | * @access public |
||
477 | * @param bool $json 是否为JSON |
||
478 | * @return $this |
||
479 | */ |
||
480 | public function json(bool $json = true) |
||
484 | |||
485 | /** |
||
486 | * 检查是否为AJAX请求 |
||
487 | * @access public |
||
488 | * @param bool $ajax 是否为AJAX |
||
489 | * @return $this |
||
490 | */ |
||
491 | public function ajax(bool $ajax = true) |
||
495 | |||
496 | /** |
||
497 | * 检查是否为PJAX请求 |
||
498 | * @access public |
||
499 | * @param bool $pjax 是否为PJAX |
||
500 | * @return $this |
||
501 | */ |
||
502 | public function pjax(bool $pjax = true) |
||
506 | |||
507 | /** |
||
508 | * 路由到一个模板地址 需要额外传入的模板变量 |
||
509 | * @access public |
||
510 | * @param array $view 视图 |
||
511 | * @return $this |
||
512 | */ |
||
513 | public function view(array $view = []) |
||
517 | |||
518 | /** |
||
519 | * 当前路由为重定向 |
||
520 | * @access public |
||
521 | * @param bool $redirect 是否为重定向 |
||
522 | * @return $this |
||
523 | */ |
||
524 | public function redirect(bool $redirect = true) |
||
528 | |||
529 | /** |
||
530 | * 设置status |
||
531 | * @access public |
||
532 | * @param int $status 状态码 |
||
533 | * @return $this |
||
534 | */ |
||
535 | public function status(int $status) |
||
539 | |||
540 | /** |
||
541 | * 设置路由完整匹配 |
||
542 | * @access public |
||
543 | * @param bool $match 是否完整匹配 |
||
544 | * @return $this |
||
545 | */ |
||
546 | public function completeMatch(bool $match = true) |
||
550 | |||
551 | /** |
||
552 | * 是否去除URL最后的斜线 |
||
553 | * @access public |
||
554 | * @param bool $remove 是否去除最后斜线 |
||
555 | * @return $this |
||
556 | */ |
||
557 | 18 | public function removeSlash(bool $remove = true) |
|
561 | |||
562 | /** |
||
563 | * 设置路由规则全局有效 |
||
564 | * @access public |
||
565 | * @return $this |
||
566 | */ |
||
567 | public function crossDomainRule() |
||
579 | |||
580 | /** |
||
581 | * 合并分组参数 |
||
582 | * @access public |
||
583 | * @return array |
||
584 | */ |
||
585 | 18 | public function mergeGroupOptions(): array |
|
599 | |||
600 | /** |
||
601 | * 解析匹配到的规则路由 |
||
602 | * @access public |
||
603 | * @param Request $request 请求对象 |
||
604 | * @param string $rule 路由规则 |
||
605 | * @param mixed $route 路由地址 |
||
606 | * @param string $url URL地址 |
||
607 | * @param array $option 路由参数 |
||
608 | * @param array $matches 匹配的变量 |
||
609 | * @return Dispatch |
||
610 | */ |
||
611 | 18 | public function parseRule(Request $request, string $rule, $route, string $url, array $option = [], array $matches = []): Dispatch |
|
643 | |||
644 | /** |
||
645 | * 发起路由调度 |
||
646 | * @access protected |
||
647 | * @param Request $request Request对象 |
||
648 | * @param mixed $route 路由地址 |
||
649 | * @param array $option 路由参数 |
||
650 | * @return Dispatch |
||
651 | */ |
||
652 | 18 | protected function dispatch(Request $request, $route, array $option): Dispatch |
|
678 | |||
679 | /** |
||
680 | * 解析URL地址为 模块/控制器/操作 |
||
681 | * @access protected |
||
682 | * @param Request $request Request对象 |
||
683 | * @param string $route 路由地址 |
||
684 | * @return CallbackDispatch |
||
685 | */ |
||
686 | protected function dispatchMethod(Request $request, string $route): CallbackDispatch |
||
695 | |||
696 | /** |
||
697 | * 解析URL地址为 模块/控制器/操作 |
||
698 | * @access protected |
||
699 | * @param Request $request Request对象 |
||
700 | * @param string $route 路由地址 |
||
701 | * @return ControllerDispatch |
||
702 | */ |
||
703 | 12 | protected function dispatchController(Request $request, string $route): ControllerDispatch |
|
713 | |||
714 | /** |
||
715 | * 路由检查 |
||
716 | * @access protected |
||
717 | * @param array $option 路由参数 |
||
718 | * @param Request $request Request对象 |
||
719 | * @return bool |
||
720 | */ |
||
721 | 18 | protected function checkOption(array $option, Request $request): bool |
|
768 | |||
769 | /** |
||
770 | * 解析URL地址中的参数Request对象 |
||
771 | * @access protected |
||
772 | * @param string $rule 路由规则 |
||
773 | * @param array $var 变量 |
||
774 | * @return void |
||
775 | */ |
||
776 | 18 | protected function parseUrlParams(string $url, array &$var = []): void |
|
784 | |||
785 | /** |
||
786 | * 解析URL的pathinfo参数 |
||
787 | * @access public |
||
788 | * @param string $url URL地址 |
||
789 | * @return array |
||
790 | */ |
||
791 | 12 | public function parseUrlPath(string $url): array |
|
806 | |||
807 | /** |
||
808 | * 生成路由的正则规则 |
||
809 | * @access protected |
||
810 | * @param string $rule 路由规则 |
||
811 | * @param array $match 匹配的变量 |
||
812 | * @param array $pattern 路由变量规则 |
||
813 | * @param array $option 路由参数 |
||
814 | * @param bool $completeMatch 路由是否完全匹配 |
||
815 | * @param string $suffix 路由正则变量后缀 |
||
816 | * @return string |
||
817 | */ |
||
818 | 3 | protected function buildRuleRegex(string $rule, array $match, array $pattern = [], array $option = [], bool $completeMatch = false, string $suffix = ''): string |
|
843 | |||
844 | /** |
||
845 | * 生成路由变量的正则规则 |
||
846 | * @access protected |
||
847 | * @param string $name 路由变量 |
||
848 | * @param array $pattern 变量规则 |
||
849 | * @param string $suffix 路由正则变量后缀 |
||
850 | * @return string |
||
851 | */ |
||
852 | 3 | protected function buildNameRegex(string $name, array $pattern, string $suffix): string |
|
887 | |||
888 | /** |
||
889 | * 设置路由参数 |
||
890 | * @access public |
||
891 | * @param string $method 方法名 |
||
892 | * @param array $args 调用参数 |
||
893 | * @return $this |
||
894 | */ |
||
895 | public function __call($method, $args) |
||
904 | |||
905 | public function __sleep() |
||
909 | |||
910 | public function __wakeup() |
||
914 | |||
915 | public function __debugInfo() |
||
927 | } |
||
928 |